Learn R Programming

redoc (version 0.1.0.9000)

make_wrapper: Make a wrapper function

Description

Wrapper functions are passed to the wrapper= argument of redoc() to specify what text in R Markdown files should be captured and restored when de-rendering (in addition to code chunks, inline code, and YAML blocks). make_wrapper() simplifies creation of these wrapper functions and returns a function that can be passed to redoc().

Wrappers included in the package, all of which are used by default, are:

Type of markup Function Label Type
HTML Comments htmlcommentwrap() "htmlcomment" inline
Single-line LaTeX latexwrap() "latex" block
Pandoc citations citationwrap() "citation" inline
Raw Blocks rawblockwrap() "rawblock" block

Usage

make_wrapper(label, regex, type = c("block", "inline"))

Arguments

label

the label to use for chunks of this type, e.g., "citation", "table". These should be unique for each type of wrapper used. redoc() will throw an error otherwise.

regex

A regular expression that identifies the text to be wrapped and restored, including all delimiters. It will search in a single string with line breaks. ICU regular expressions are used. The (?s) flag is recommended for multi-line expressions.

type

whether the text should be treated as inline or block text. Inlines are wrapped in span elements and blocks are wrapped in divs. These are treated differently in several ways, especially when restoring text in dedoc().

Value

A function of class redoc_wrapper

Details

Some captured text can not be selected by regular expressions, in which case custom functions can be provided to parse out the relevant text. See the vignette "Developing with redoc" for more detail.

Examples

Run this code
# NOT RUN {
rmarkdown::render(
    redoc_example_rmd(),
    output_format = redoc(
                      wrappers = list(
                        htmlcommentwrap,
                        latexwrap)))

# This is how each of these functions are defined in the redoc package
htmlcommentwrap <- make_wrapper(
    label = "htmlcomment",
    regex = "(?s)<!--.*?-->",
    type = "inline")

latexwrap <- make_wrapper(
    label = "latex",
    regex = "(?<=[\n\r])\\\\\\w+.*?(?=[\n\r])",
    type = "block")

rawblockwrap <- make_wrapper(
    label = "rawblock",
    regex = "(?s)```\\{=\\w+\\}.*?```\\h*",
    type = "block")

rawspanwrap <- make_wrapper(
    label = "rawspan",
    regex = "(?s)`[^`]`\\{=\\w+?\\}",
    type = "inline")

citationwrap <- make_wrapper(
    label = "citation",
    regex = "(?:@\\w+|\\[.*?-?@\\w+.*?\\](?!\\[\\(\\{))",
    type = "inline")
# }

Run the code above in your browser using DataLab