yarn | R Documentation |
Wrapper around an XML representation of a Markdown document. It contains four publicly accessible slots: path, yaml, body, and ns.
This class is a fancy wrapper around the results of to_xml()
and
has methods that make it easier to add, analyze, remove, or write elements
of your markdown document.
path
[character
] path to file on disk
yaml
[character
] text block at head of file
body
[xml_document
] an xml document of the (R)Markdown file.
ns
[xml_document
] an xml namespace object defining "md" to
commonmark.
new()
Create a new yarn document
yarn$new(path = NULL, encoding = "UTF-8", sourcepos = FALSE, ...)
path
[character
] path to a markdown episode file on disk
encoding
[character
] encoding passed to readLines()
sourcepos
passed to commonmark::markdown_xml()
. If TRUE
, the
source position of the file will be included as a "sourcepos" attribute.
Defaults to FALSE
.
...
arguments passed on to to_xml()
.
A new yarn object containing an XML representation of a (R)Markdown file.
path <- system.file("extdata", "example1.md", package = "tinkr") ex1 <- tinkr::yarn$new(path) ex1 path2 <- system.file("extdata", "example2.Rmd", package = "tinkr") ex2 <- tinkr::yarn$new(path2) ex2
reset()
reset a yarn document from the original file
yarn$reset()
path <- system.file("extdata", "example1.md", package = "tinkr") ex1 <- tinkr::yarn$new(path) # OH NO ex1$body ex1$body <- xml2::xml_missing() ex1$reset() ex1$body
write()
Write a yarn document to Markdown/R Markdown
yarn$write(path = NULL, stylesheet_path = stylesheet())
path
path to the file you want to write
stylesheet_path
path to the xsl stylesheet to convert XML to markdown.
path <- system.file("extdata", "example1.md", package = "tinkr") ex1 <- tinkr::yarn$new(path) ex1 tmp <- tempfile() try(readLines(tmp)) # nothing in the file ex1$write(tmp) head(readLines(tmp)) # now a markdown file unlink(tmp)
show()
show the markdown contents on the screen
yarn$show(stylesheet_path = stylesheet())
stylesheet_path
path to the xsl stylesheet to convert XML to markdown.
a character vector with one line for each line in the output
path <- system.file("extdata", "example2.Rmd", package = "tinkr") ex2 <- tinkr::yarn$new(path) ex2$head(5) ex2$tail(5) ex2$show()
head()
show the head of the markdown contents on the screen
yarn$head(n = 6L, stylesheet_path = stylesheet())
n
the number of elements to show from the top. Negative numbers
stylesheet_path
path to the xsl stylesheet to convert XML to markdown. exclude lines from the bottom
a character vector with n
elements
tail()
show the tail of the markdown contents on the screen
yarn$tail(n = 6L, stylesheet_path = stylesheet())
n
the number of elements to show from the bottom. Negative numbers
stylesheet_path
path to the xsl stylesheet to convert XML to markdown. exclude lines from the top
a character vector with n
elements
add_md()
add an arbitrary Markdown element to the document
yarn$add_md(md, where = 0L)
md
a string of markdown formatted text.
where
the location in the document to add your markdown text.
This is passed on to xml2::xml_add_child()
. Defaults to 0, which
indicates the very top of the document.
path <- system.file("extdata", "example2.Rmd", package = "tinkr") ex <- tinkr::yarn$new(path) # two headings, no lists xml2::xml_find_all(ex$body, "md:heading", ex$ns) xml2::xml_find_all(ex$body, "md:list", ex$ns) ex$add_md( "# Hello\n\nThis is *new* formatted text from `{tinkr}`!", where = 1L )$add_md( " - This\n - is\n - a new list", where = 2L ) # three headings xml2::xml_find_all(ex$body, "md:heading", ex$ns) xml2::xml_find_all(ex$body, "md:list", ex$ns) tmp <- tempfile() ex$write(tmp) readLines(tmp, n = 20)
protect_math()
Protect math blocks from being escaped
yarn$protect_math()
path <- system.file("extdata", "math-example.md", package = "tinkr") ex <- tinkr::yarn$new(path) ex$tail() # math blocks are escaped :( ex$protect_math()$tail() # math blocks are no longer escaped :)
protect_curly()
Protect curly phrases {likethat}
from being escaped
yarn$protect_curly()
path <- system.file("extdata", "basic-curly.md", package = "tinkr") ex <- tinkr::yarn$new(path) ex$protect_curly()$head()
protect_unescaped()
Protect unescaped square braces from being escaped.
This is applied by default when you use yarn$new(sourcepos = TRUE)
.
yarn$protect_unescaped()
path <- system.file("extdata", "basic-curly.md", package = "tinkr") ex <- tinkr::yarn$new(path, sourcepos = TRUE, unescaped = FALSE) ex$tail() ex$protect_unescaped()$tail()
clone()
The objects of this class are cloneable with this method.
yarn$clone(deep = FALSE)
deep
Whether to make a deep clone.
this requires the sourcepos
attribute to be recorded when the
object is initialised. See protect_unescaped()
for details.
## ------------------------------------------------
## Method `yarn$new`
## ------------------------------------------------
path <- system.file("extdata", "example1.md", package = "tinkr")
ex1 <- tinkr::yarn$new(path)
ex1
path2 <- system.file("extdata", "example2.Rmd", package = "tinkr")
ex2 <- tinkr::yarn$new(path2)
ex2
## ------------------------------------------------
## Method `yarn$reset`
## ------------------------------------------------
path <- system.file("extdata", "example1.md", package = "tinkr")
ex1 <- tinkr::yarn$new(path)
# OH NO
ex1$body
ex1$body <- xml2::xml_missing()
ex1$reset()
ex1$body
## ------------------------------------------------
## Method `yarn$write`
## ------------------------------------------------
path <- system.file("extdata", "example1.md", package = "tinkr")
ex1 <- tinkr::yarn$new(path)
ex1
tmp <- tempfile()
try(readLines(tmp)) # nothing in the file
ex1$write(tmp)
head(readLines(tmp)) # now a markdown file
unlink(tmp)
## ------------------------------------------------
## Method `yarn$show`
## ------------------------------------------------
path <- system.file("extdata", "example2.Rmd", package = "tinkr")
ex2 <- tinkr::yarn$new(path)
ex2$head(5)
ex2$tail(5)
ex2$show()
## ------------------------------------------------
## Method `yarn$add_md`
## ------------------------------------------------
path <- system.file("extdata", "example2.Rmd", package = "tinkr")
ex <- tinkr::yarn$new(path)
# two headings, no lists
xml2::xml_find_all(ex$body, "md:heading", ex$ns)
xml2::xml_find_all(ex$body, "md:list", ex$ns)
ex$add_md(
"# Hello\n\nThis is *new* formatted text from `{tinkr}`!",
where = 1L
)$add_md(
" - This\n - is\n - a new list",
where = 2L
)
# three headings
xml2::xml_find_all(ex$body, "md:heading", ex$ns)
xml2::xml_find_all(ex$body, "md:list", ex$ns)
tmp <- tempfile()
ex$write(tmp)
readLines(tmp, n = 20)
## ------------------------------------------------
## Method `yarn$protect_math`
## ------------------------------------------------
path <- system.file("extdata", "math-example.md", package = "tinkr")
ex <- tinkr::yarn$new(path)
ex$tail() # math blocks are escaped :(
ex$protect_math()$tail() # math blocks are no longer escaped :)
## ------------------------------------------------
## Method `yarn$protect_curly`
## ------------------------------------------------
path <- system.file("extdata", "basic-curly.md", package = "tinkr")
ex <- tinkr::yarn$new(path)
ex$protect_curly()$head()
## ------------------------------------------------
## Method `yarn$protect_unescaped`
## ------------------------------------------------
path <- system.file("extdata", "basic-curly.md", package = "tinkr")
ex <- tinkr::yarn$new(path, sourcepos = TRUE, unescaped = FALSE)
ex$tail()
ex$protect_unescaped()$tail()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.