Skip to contents

Isolate nodes in a document

Usage

isolate_nodes(nodelist, type = "context")

Arguments

nodelist

an object of class xml_nodeset OR xml_node OR a list of either.

type

a string of either "context" (default), "censor", or "list"

Value

a list of two elements:

  • doc: a copy of the document with the nodes isolated depending on the context

  • key: a string used to tag nodes that are isolated via the tnk-key attribute

Details

isolate_nodes()is the workhorse for the show family of functions. These functions will create a copy of the document with the nodes present in nodelist isolated. It has the following switches for "type":

  • "context" include the nodes within the block context of the document. For example, if the nodelist contains links in headings, paragraphs, and lists, those links will appear within these blocks. When mark = TRUE, ellipses [...] will be added to indicate hidden content.

  • "censor" by default will replace all non-whitespace characters with a censor character. This is controlled by tinkr.censor.regex and tinkr.censor.mark

  • "list" creates a new document and copies over the nodes so they appear as a list of paragraphs.

See also

Other nodeset isolation functions: provision_isolation()

Examples

path <- system.file("extdata", "show-example.md", package = "tinkr")
y <- tinkr::yarn$new(path, sourcepos = TRUE)
y$protect_math()$protect_curly()
items <- xml2::xml_find_all(y$body, ".//md:item", tinkr::md_ns())
tnk <- asNamespace("tinkr")
tnk$isolate_nodes(items, type = "context")
#> $doc
#> {xml_document}
#> <document sourcepos="1:1-47:42" xmlns="http://commonmark.org/xml/1.0">
#> [1] <list sourcepos="14:1-24:0" type="bullet" tight="true">\n  <item sourcepo ...
#> 
#> $key
#> [1] "1738682594"
#> 
tnk$isolate_nodes(items, type = "censor")
#> $doc
#> {xml_document}
#> <document sourcepos="1:1-47:42" xmlns="http://commonmark.org/xml/1.0">
#>  [1] <heading sourcepos="2:1-2:8" level="2">\n  <text sourcepos="2:4-2:8" xml ...
#>  [2] <heading sourcepos="4:1-4:12" level="3">\n  <text sourcepos="4:5-4:12" x ...
#>  [3] <paragraph sourcepos="6:1-6:59">\n  <text sourcepos="6:1-6:14" xml:space ...
#>  [4] <heading sourcepos="8:1-8:10" level="3">\n  <text sourcepos="8:5-8:10" x ...
#>  [5] <paragraph sourcepos="10:1-10:88">\n  <image sourcepos="10:1-10:52" dest ...
#>  [6] <heading sourcepos="12:1-12:8" level="2">\n  <text sourcepos="12:4-12:8" ...
#>  [7] <list sourcepos="14:1-24:0" type="bullet" tight="true">\n  <item sourcep ...
#>  [8] <heading sourcepos="25:1-25:7" level="2">\n  <text sourcepos="25:4-25:7" ...
#>  [9] <paragraph sourcepos="27:1-27:56">\n  <text sourcepos="27:1-27:26" xml:s ...
#> [10] <code_block sourcepos="29:1-37:3" info="r" xml:space="preserve" name=""> ...
#> [11] <heading sourcepos="39:1-39:7" level="2">\n  <text sourcepos="39:4-39:7" ...
#> [12] <paragraph sourcepos="41:1-41:70">\n  <text sourcepos="41:1-41:70" xml:s ...
#> [13] <paragraph sourcepos="43:1-45:2">\n  <text sourcepos="43:1-43:2" xml:spa ...
#> [14] <paragraph>\n  <link destination="▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇" title="" a ...
#> 
#> $key
#> [1] "1738682594"
#> 
tnk$isolate_nodes(items, type = "list")
#> $doc
#> {xml_document}
#> <document xmlns="http://commonmark.org/xml/1.0">
#>  [1] <paragraph>\n  <item xmlns="http://commonmark.org/xml/1.0" sourcepos="14 ...
#>  [2] <paragraph>\n  <item xmlns="http://commonmark.org/xml/1.0" sourcepos="15 ...
#>  [3] <paragraph>\n  <item xmlns="http://commonmark.org/xml/1.0" sourcepos="16 ...
#>  [4] <paragraph>\n  <item xmlns="http://commonmark.org/xml/1.0" sourcepos="17 ...
#>  [5] <paragraph>\n  <item xmlns="http://commonmark.org/xml/1.0" sourcepos="18 ...
#>  [6] <paragraph>\n  <item xmlns="http://commonmark.org/xml/1.0" sourcepos="19 ...
#>  [7] <paragraph>\n  <item xmlns="http://commonmark.org/xml/1.0" sourcepos="20 ...
#>  [8] <paragraph>\n  <item xmlns="http://commonmark.org/xml/1.0" sourcepos="21 ...
#>  [9] <paragraph>\n  <item xmlns="http://commonmark.org/xml/1.0" sourcepos="22 ...
#> [10] <paragraph>\n  <item xmlns="http://commonmark.org/xml/1.0" sourcepos="23 ...
#> 
#> $key
#> NULL
#>