A top-down recursive descent parser for qmd files.
Given this input .qmd
file:
---
title: "my document"
format: html
editor: visual
---
# Overview
In this document, I will talk about very little of meaning or merit.
# My First Real Section
This is where I tell you my grand idea!
## A Mere Subsection
While I merely am a subsection, I am mighty!
I contain code and everything!
`` ```{r}
#| label: my_code
#| eval: TRUE
mean(mtcars$mpg)
`` ```
We can parse it like so:
library(qmdparse)
out <- parse_qmd("/home/nic/qmdparse/tests/testthat/qmds/simple_doc.qmd")
# Refer to sections of the document as nested lists
out[["My First Real Section"]][["A Mere Subsection"]]
#> ## A Mere Subsection
#> While I merely am a subsection, I am mighty!
#> I contain code and everything!
#>
#> ```{r}
#> #| label: my_code
#> #| eval: TRUE
#> mean(mtcars$mpg)
#> ```
# Extract all code chunks
extract_code(out)
#> [[1]]
#> ```{r}
#> #| label: my_code
#> #| eval: TRUE
#> mean(mtcars$mpg)
#> ```
# Extract a section based on its name
extract_named(out, "A Mere Subsection")
#> $`A Mere Subsection`
#> ## A Mere Subsection
#> While I merely am a subsection, I am mighty!
#> I contain code and everything!
#>
#> ```{r}
#> #| label: my_code
#> #| eval: TRUE
#> mean(mtcars$mpg)
#> ```
# Extract all headings
extract_headings(out)
#> [[1]]
#> # Overview
#>
#> [[2]]
#> # My First Real Section
#>
#> [[3]]
#> ## A Mere Subsection
# Print the AST of the document
print_tree(out)
#> simple_doc.qmd
#> ├──yaml
#> ├──markdown
#> ├──h1: Overview
#> └──markdown
#> └──h1: My First Real Section
#> ├──markdown
#> └──h2: A Mere Subsection
#> ├──markdown
#> └──code
You can install the latest version of qmdparse from github with:
library(devtools)
devtools::install_github("thisisnic/qmdparse")
Check out parsermd for a .rmd
parser based on C++ libraries or lightparser for a .rmd
and .qmd
parser based on knitr.