Usage
has_class(x, cls)
add_class(x, cls)
remove_class(x, cls)
get_id(x)
set_id(x, id)
get_attr(x, key)
set_attr(x, ...)Arguments
- x
A
pandoc_node(typically one with an@attrslot).- cls
A character vector of class names.
- id
A single string.
- key
A single string naming an attribute (for
get_attr()).- ...
For
set_attr(), namedkey = valuepairs; eachvalueis a single string, orNULLto remove that key. Quote names that are not syntactic R identifiers (e.g."data-level" = "2"), and use rlang's!!!/:=to supply dynamic keys.
Value
Predicates return a logical scalar (or a string, for getters).
Setters return a node of the same class as x.
Details
Concise getters and setters for the pandoc_attr slot found on
many pandoc node types (pandoc_header, pandoc_div,
pandoc_code, pandoc_link, pandoc_image, pandoc_span,
etc.). Modelled on Pandoc Lua filters' direct field access
(el.classes, el.identifier, el.attributes) but using R's
immutable value semantics: every setter returns a new node, the
input is never modified.
Nodes without a standard @attr slot are handled defensively: the
getters return FALSE / "" / NA (including nodes such as ordered
and bullet lists, whose @attr is a pandoc_list_attributes rather
than a pandoc_attr), while setters error with a clear message.
Available helpers
has_class(x, cls)TRUEif any ofclsis in@attr@classes. When used inside aselect_nodes()/map_nodes()predicate, the data-mask version is bound to the current node automatically; the exported version here takes the node as its first argument.add_class(x, cls)add one or more classes (idempotent: classes already present are not duplicated).remove_class(x, cls)remove one or more classes; no-op on classes that are not present.get_id(x)/set_id(x, id)read or replace@attr@id.get_attr(x, key)reads a single attribute, returningNA_character_for missing keys.set_attr(x, key = value, ...)sets one or more attributes from namedkey = valuepairs. AvalueofNULLremoves that key (the named map's idiomatic R removal, mirroring Lua'sel.attributes[key] = nil), so a single call can both set and drop:set_attr(x, target = "_blank", lang = NULL).