Title: | Pandoc Filters for R |
---|---|
Description: | The document converter 'pandoc' <https://pandoc.org/> is widely used in the R community. One feature of 'pandoc' is that it can produce and consume JSON-formatted abstract syntax trees (AST). This allows to transform a given source document into JSON-formatted AST, alter it by so called filters and pass the altered JSON-formatted AST back to 'pandoc'. This package provides functions which allow to write such filters in native R code. Although this package is inspired by the Python package 'pandocfilters' <https://github.com/jgm/pandocfilters/>, it provides additional convenience functions which make it simple to use the 'pandocfilters' package as a report generator. Since 'pandocfilters' inherits most of it's functionality from 'pandoc' it can create documents in many formats (for more information see <https://pandoc.org/>) but is also bound to the same limitations as 'pandoc'. |
Authors: | Florian Schwendinger [aut, cre], Kurt Hornik [aut], Andrie de Vries [ctb] |
Maintainer: | Florian Schwendinger <[email protected]> |
License: | GPL-3 |
Version: | 0.1-6 |
Built: | 2024-11-05 06:18:45 UTC |
Source: | CRAN |
In pandoc "block"
objects are used as container for
"inline"
objects and to give them specific roles.
Objects of the classes "NULL"
and "character"
can be coerced to "block"
.
as.block(x)
as.block(x)
x |
an object of type |
an object of class "block"
.
as.block("some text") as.block(NULL)
as.block("some text") as.block(NULL)
Objects of the classes "NULL"
and "character"
can be coerced to "inline"
.
as.inline(x)
as.inline(x)
x |
an object of type |
an object of class "inline"
.
as.inline("some text") as.inline(NULL)
as.inline("some text") as.inline(NULL)
Apply the function FUN
on the abstract syntax tree (AST) obtained from pandoc.
astrapply(x, FUN, ...)
astrapply(x, FUN, ...)
x |
a list representing the AST obtained from pandoc. |
FUN |
the function to be applied to the AST. |
... |
optional arguments to |
A list containing the modified AST.
A constructor for pandoc attributes.
Attr(identifier = "", classes = character(), key_val_pairs = list())
Attr(identifier = "", classes = character(), key_val_pairs = list())
identifier |
a character string |
classes |
a character giving the classes |
key_val_pairs |
a list of tuple of type |
Attr("A", c("B", "C"), list(c("D", "E")))
Attr("A", c("B", "C"), list(c("D", "E")))
Constructs a block object of type "BlockQuote"
.
BlockQuote(blocks)
BlockQuote(blocks)
blocks |
a block object or list of block objects |
BlockQuote(Plain("Hello R!"))
BlockQuote(Plain("Hello R!"))
Constructs a block object of type "BulletList"
.
BulletList(llblocks)
BulletList(llblocks)
llblocks |
a list of lists of blocks |
bullet_1 <- Plain("A") bullet_2 <- Plain(Str("B")) bullet_3 <- list(Plain(list(Str("C")))) BulletList(list(bullet_1, bullet_2, bullet_3))
bullet_1 <- Plain("A") bullet_2 <- Plain(Str("B")) bullet_3 <- list(Plain(list(Str("C")))) BulletList(list(bullet_1, bullet_2, bullet_3))
Objects of class "block"
can be combined by using the generic
default method "c"
(combine).
## S3 method for class 'block' c(...)
## S3 method for class 'block' c(...)
... |
objects to be concatenated. |
an list of "block"
objects.
c(Header( "R Basics" ), Header("What is R?", level=2), Plain(c(Emph("R"), Space(), "is a system for ", Strong("statistical computation"))))
c(Header( "R Basics" ), Header("What is R?", level=2), Plain(c(Emph("R"), Space(), "is a system for ", Strong("statistical computation"))))
Objects of class "inline"
can be combined by using the generic
default method "c"
(combine).
## S3 method for class 'inline' c(...)
## S3 method for class 'inline' c(...)
... |
objects to be concatenated. |
an list of "inline"
objects.
c(Str("some"), Strong("text"))
c(Str("some"), Strong("text"))
Constructs an object of type "Citation"
.
Citation( suffix, id, note_num = 0L, mode = "AuthorInText", prefix = list(), hash = 0L )
Citation( suffix, id, note_num = 0L, mode = "AuthorInText", prefix = list(), hash = 0L )
suffix |
a inline object or list of inline objects |
id |
a character string (not visible in the text) |
note_num |
an integer |
mode |
a character string giving the citation mode, possible values are
|
prefix |
a inline object or list of inline objects |
hash |
an integer |
Constructs an inline object of type "Cite"
.
Cite(citation, x)
Cite(citation, x)
citation |
an object of type |
x |
a inline object or a list of inline objects |
ci <- Citation(suffix=list(Str("Suffix_1")), id="Citation_ID_1", prefix=list(Str("Prefix_1"))) Cite(ci, Str("some text"))
ci <- Citation(suffix=list(Str("Suffix_1")), id="Citation_ID_1", prefix=list(Str("Prefix_1"))) Cite(ci, Str("some text"))
Constructs an inline object of type "Code"
.
Code(code, name = "", language = NULL, line_numbers = FALSE, start_from = 1)
Code(code, name = "", language = NULL, line_numbers = FALSE, start_from = 1)
code |
a character string giving the inline code |
name |
an optional character string giving the name of the inline code chunk |
language |
an optional character string giving the programming language |
line_numbers |
a logical which controls if line numbers should be used |
start_from |
an integer giving the first line number |
Code("lm(hello ~ world)", "my_r_inline_code", "R", TRUE, 0) Code("lm(hello ~ world)")
Code("lm(hello ~ world)", "my_r_inline_code", "R", TRUE, 0) Code("lm(hello ~ world)")
Constructs a block object of type "CodeBlock"
.
CodeBlock(attr, code)
CodeBlock(attr, code)
attr |
an object of type |
code |
a character string containing the source code. |
attr <- Attr("id", "Programming Language", list(c("key", "value"))) code <- "x <- 3\nprint('Hello R!')" CodeBlock(attr, code)
attr <- Attr("id", "Programming Language", list(c("key", "value"))) code <- "x <- 3\nprint('Hello R!')" CodeBlock(attr, code)
Constructs a Definition
which can be used as
an element of a "DefinitionList"
.
Definition(key, value)
Definition(key, value)
key |
a inline object or list of inline objects |
value |
a block object or list of block objects |
Definition("some key", Plain("some value"))
Definition("some key", Plain("some value"))
Constructs a block object of type "DefinitionList"
.
DefinitionList(x)
DefinitionList(x)
x |
a list of key value pairs, the key is a list of |
In the pandoc API https://johnmacfarlane.net/BayHac2014/doc/pandoc-types/Text-Pandoc-Definition.html
the DefinitionList
is described as follows, each list item is a pair consisting of a term
(a list of "inline"
objects) and one or more definitions (each a list of blocks).
key <- list(Str("key")) value <- list(list(Plain(list(Str("value"))))) DefinitionList(list(list(key, value), Definition("some key", Plain("some value"))))
key <- list(Str("key")) value <- list(list(Plain(list(Str("value"))))) DefinitionList(list(list(key, value), Definition("some key", Plain("some value"))))
Constructs a block object of type "Div"
.
Div(blocks, attr = Attr())
Div(blocks, attr = Attr())
blocks |
a block object or list of block objects |
attr |
an object of type |
blocks <- Plain("Hello R!") Div(blocks)
blocks <- Plain("Hello R!") Div(blocks)
Constructs an object of type "document"
.
document()
document()
Each document has the following methods:
to_json()
Description
Returns the JSON
representation of the document.
write(con, format = "markdown", writer = write.pandoc)
Description
Write the JSON-formatted AST to a connection.
Arguments
con |
a connection object or a character string to which the document is written. |
format |
a character string giving the format (e.g. "latex" , "html" ). |
writer |
an optional writer function, see write.pandoc. |
Note
Any function with the three arguments x
, con
and format
can be used as writer function.
append(x)
Description
Append a new block to the document.
Arguments
x |
block object or list of block objects. |
append_plain(x)
Description
For more information about the arguments see Plain.
append_para(x)
Description
For more information about the arguments see Para.
append_code_block(attr, code)
Description
For more information about the arguments see CodeBlock.
append_block_quote(blocks)
Description
For more information about the arguments see BlockQuote.
append_ordered_list(lattr, lblocks)
Description
For more information about the arguments see OrderedList.
append_bullet_list(lblocks)
Description
For more information about the arguments see BulletList.
append_definition_list(x)
Description
For more information about the arguments see DefinitionList.
append_header(x, level=1L, attr=Attr())
Description
For more information about the arguments see Header.
append_horizontal_rule()
Description
For more information about the arguments see HorizontalRule.
append_table(rows, col_names=NULL, aligns=NULL, col_width=NULL, caption=list())
Description
For more information about the arguments see Table.
append_div(blocks, attr)
Description
For more information about the arguments see Div.
append_null()
Description
For more information about the arguments see Null.
Constructs an inline object of type "Emph"
.
Emph(x)
Emph(x)
x |
a inline object or a list of inline objects |
Emph("emphasize")
Emph("emphasize")
Apply a filter on the JSON-formatted abstract syntax tree (AST).
filter(FUN, ..., input = stdin(), output = stdout())
filter(FUN, ..., input = stdin(), output = stdout())
FUN |
the function to be applied on the AST. |
... |
optional arguments to |
input |
a connection object or a character string from which the JSON-formatted AST is read. |
output |
a connection object or a character string to which the JSON-formatted AST is written. |
Get the version of pandoc-types.
get_pandoc_types_version(type = c("numeric", "character"))
get_pandoc_types_version(type = c("numeric", "character"))
type |
a character giving the type of the return value. |
get_pandoc_types_version()
get_pandoc_types_version()
Get the version of pandoc.
get_pandoc_version(type = c("numeric", "character"))
get_pandoc_version(type = c("numeric", "character"))
type |
a character giving the type of the return value. |
get_pandoc_version()
get_pandoc_version()
Constructs a block object of type "Header"
.
Header(x, level = 1L, attr = Attr())
Header(x, level = 1L, attr = Attr())
x |
a inline object or a list of inline objects |
level |
an integer giving the level |
attr |
an object of type |
Header("My Header")
Header("My Header")
Constructs a block object of type "HorizontalRule"
.
HorizontalRule()
HorizontalRule()
HorizontalRule()
HorizontalRule()
Constructs an inline object of type "Image"
.
Image(target, text, caption = "", attr = Attr())
Image(target, text, caption = "", attr = Attr())
target |
a character string giving the target (hyper reference) |
text |
a inline object or a list of inline objects giving the visible part |
caption |
a character string describing the picture |
attr |
an optional object of type |
Further Usage examples can be found in the README.
Image("https:://Rlogo.jpg", "some_text", "fig:some_caption")
Image("https:://Rlogo.jpg", "some_text", "fig:some_caption")
Tests if an object has the class attribute "block"
.
is.block(x)
is.block(x)
x |
an object to be tested. |
a logical indicating if the provided object is of type "block"
.
is.block(as.block(NULL))
is.block(as.block(NULL))
Tests if an object has the class attribute "inline"
.
is.inline(x)
is.inline(x)
x |
an object to be tested. |
a logical indicating if the provided object is of type "inline"
.
is.inline(as.inline(NULL))
is.inline(as.inline(NULL))
Constructs an inline object of type "LineBreak"
.
LineBreak()
LineBreak()
LineBreak()
LineBreak()
Constructs an inline object of type "Link"
.
Link(target, text, title = "", attr = Attr())
Link(target, text, title = "", attr = Attr())
target |
a character string giving the target (hyper reference) |
text |
a inline object or a list of inline objects giving the visible part |
title |
an optional character string giving the title |
attr |
an optional object of type |
Further Usage examples can be found in the README.
Link("https://cran.r-project.org/", "Text_Shown", "some title")
Link("https://cran.r-project.org/", "Text_Shown", "some title")
A constructor for pandoc list attributes.
ListAttributes( first_number = 1L, style = "DefaultStyle", delim = "DefaultDelim" )
ListAttributes( first_number = 1L, style = "DefaultStyle", delim = "DefaultDelim" )
first_number |
an integer giving the first number of the list |
style |
a character string giving the style, possible values are |
delim |
a character string giving the delimiter, possible values are |
Constructs an inline object of type "Math"
.
Math(x)
Math(x)
x |
a character string |
Math("3*x^2")
Math("3*x^2")
Constructs an inline object of type "Note"
.
Note(x)
Note(x)
x |
a pandoc block object or a list of pandoc block objects |
block <- Plain("x") Note(block)
block <- Plain("x") Note(block)
Constructs a block object of type "Null"
.
Null()
Null()
Null()
Null()
Constructs a block object of type "OrderedList"
.
OrderedList(lattr, llblocks)
OrderedList(lattr, llblocks)
lattr |
a list of attributes |
llblocks |
a list of lists of blocks |
ordered_1 <- Plain("A") ordered_2 <- list(Plain(Str("B"))) ordered_3 <- list(Plain(list(Str("C")))) OrderedList(ListAttributes(), ordered_1) OrderedList(ListAttributes(), list(ordered_1, ordered_2, ordered_3))
ordered_1 <- Plain("A") ordered_2 <- list(Plain(Str("B"))) ordered_3 <- list(Plain(list(Str("C")))) OrderedList(ListAttributes(), ordered_1) OrderedList(ListAttributes(), list(ordered_1, ordered_2, ordered_3))
Utility functions for testing filters
pandoc_to_json(file, from = "markdown") pandoc_from_json(json, to = "markdown", exchange = c("file", "arg"))
pandoc_to_json(file, from = "markdown") pandoc_from_json(json, to = "markdown", exchange = c("file", "arg"))
file |
file name |
from |
markdown, html, latex or native |
json |
a |
to |
markdown, html, latex or native |
exchange |
a character string |
Constructs a block object of type "Para"
.
Para(x)
Para(x)
x |
a inline object or list of inline objects |
Para("x")
Para("x")
Constructs a block object of type "Plain"
, a plain paragraph.
Plain(x)
Plain(x)
x |
a inline object or list of inline objects |
Plain("x")
Plain("x")
Constructs an inline object of type "Quoted"
.
Quoted(x, quote_type = "DoubleQuote")
Quoted(x, quote_type = "DoubleQuote")
x |
a inline object or a list of inline objects |
quote_type |
a character giving the quote type,
valid types are |
Quoted("some text", quote_type="SingleQuote") Quoted("some text", quote_type="DoubleQuote")
Quoted("some text", quote_type="SingleQuote") Quoted("some text", quote_type="DoubleQuote")
Constructs an inline object of type "RawInline"
.
RawInline(format, x)
RawInline(format, x)
format |
a character string giving the format (e.g. |
x |
a character string giving the inline |
RawInline("latex", "some RawInline")
RawInline("latex", "some RawInline")
Set the path to pandoc.
set_pandoc_path(path = "pandoc")
set_pandoc_path(path = "pandoc")
path |
a character giving the location of pandoc
(default is |
Constructs an inline object of type "SmallCaps"
.
SmallCaps(x)
SmallCaps(x)
x |
a inline object or a list of inline objects |
SmallCaps("The latex command for 'small caps' is 'textsc'!")
SmallCaps("The latex command for 'small caps' is 'textsc'!")
Constructs an inline object of type "SoftBreak"
.
SoftBreak()
SoftBreak()
SoftBreak()
SoftBreak()
Constructs an inline object of type "Space"
.
Space()
Space()
Space()
Space()
Constructs an inline object of type "Span"
.
Span(attr, inline)
Span(attr, inline)
attr |
an object of type |
inline |
a inline object or a list of inline objects which will be shown |
attr <- Attr("A", "B", list(c("C", "D"))) Span(attr, "some inline string")
attr <- Attr("A", "B", list(c("C", "D"))) Span(attr, "some inline string")
Constructs an inline object of type "Str"
.
Str(x)
Str(x)
x |
a character string |
To minimize the amount of unnecessary typing, pandoc filters automatically
converts character strings to pandoc objects of type "Str"
if needed.
Furthermore, if a single inline object is provided where a list of inline
objects is needed pandocfilters automatically converts this inline
object into a list of inline objects. For example, the canonical way to emphasize
the character string "some text"
would be Emph(list(Str("some text")))
since single inline objects are automatically transformed to lists of inline objects,
this is equivalent to Emph(Str("some text"))
. Since a character
string is automatically transformed to an inline object, this is is equivalent
to Emph("some string")
. In short, whenever a list of inline objects
is needed one can also use a single inline object or a character string.
Str("SomeString")
Str("SomeString")
Constructs an inline object of type "Strikeout"
.
Strikeout(x)
Strikeout(x)
x |
a inline object or a list of inline objects |
Strikeout("strikeout")
Strikeout("strikeout")
Constructs an inline object of type "Strong"
.
Strong(x)
Strong(x)
x |
a inline object or a list of inline objects |
Strong("strong")
Strong("strong")
Constructs an inline object of type "Subscript"
.
Subscript(x)
Subscript(x)
x |
a inline object or a list of inline objects |
Subscript("some text written in superscript")
Subscript("some text written in superscript")
Constructs an inline object of type "Superscript"
.
Superscript(x)
Superscript(x)
x |
a inline object or a list of inline objects |
Superscript("some text written in superscript")
Superscript("some text written in superscript")
Constructs a block object of type "Table"
.
Table( rows, col_names = NULL, aligns = NULL, col_width = NULL, caption = list() )
Table( rows, col_names = NULL, aligns = NULL, col_width = NULL, caption = list() )
rows |
an object of class |
col_names |
a list of objects of type |
aligns |
a character vector of alignments, possible values are “l” for left, “r” for right, “c” for center and “d” for default. |
col_width |
a numeric vector |
caption |
a inline object or a list of inline objects giving the caption |
Table, with caption, column alignments (required), relative column widths (0 = default), column headers (each a list of blocks), and rows (each a list of lists of blocks)
Table cells is a constructor for plain table cells.
TableCell(x)
TableCell(x)
x |
a character string giving the content of the table cell |
In general table cells are a list of block elements, the
constructor TableCell
creates a plain table cell.
TableCell("Cell 1")
TableCell("Cell 1")
Write the JSON-formatted AST to a connection.
write.pandoc(json, file, format, exchange = c("arg", "file"))
write.pandoc(json, file, format, exchange = c("arg", "file"))
json |
a JSON representation of the AST to be written out |
file |
a connection object or a character string to which the JSON-formatted AST is written |
format |
a character string giving the format (e.g. |
exchange |
a character string |
If you want to apply a filter to the document before it get's written out, or your
pandoc installation is not registered in the PATH
it can be favorable to provide your
own writer function to the document class.