Skip to contents

Get paired div blocks

Usage

get_divs(body, type = NULL, include = FALSE)

Arguments

body

an xml document

type

the type of div to return

Value

a list of nodesets

Details

The strategy behind this is to :

  1. find all dtag elements, which will necessarily be <html_block> elements containing div tags

  2. grab the first tag of each pair

  3. filter on div tag class (type)

  4. grab all elements between the tags

Examples

loop <- Episode$new(file.path(lesson_fragment(), "_episodes", "14-looping-data-sets.md"))
loop$body # a full document with block quotes and code blocks, etc
#> {xml_document}
#> <document sourcepos="1:1-190:22" xmlns="http://commonmark.org/xml/1.0">
#>  [1] <heading sourcepos="2:1-2:65" level="2">\n  <text sourcepos="2:4-2:9" xm ...
#>  [2] <list sourcepos="4:1-6:0" type="bullet" tight="true">\n  <item sourcepos ...
#>  [3] <code_block sourcepos="7:1-12:3" xml:space="preserve" name="" ktag="{: . ...
#>  [4] <code_block sourcepos="14:1-33:3" xml:space="preserve" name="" ktag="{:  ...
#>  [5] <heading sourcepos="36:1-36:126" level="2">\n  <text sourcepos="36:4-36: ...
#>  [6] <list sourcepos="38:1-47:0" type="bullet" tight="true">\n  <item sourcep ...
#>  [7] <code_block sourcepos="48:1-51:3" xml:space="preserve" name="" ktag="{:  ...
#>  [8] <code_block sourcepos="53:1-57:3" xml:space="preserve" name="" ktag="{:  ...
#>  [9] <code_block sourcepos="60:1-62:3" xml:space="preserve" name="" ktag="{:  ...
#> [10] <code_block sourcepos="64:1-66:3" xml:space="preserve" name="" ktag="{:  ...
#> [11] <heading sourcepos="69:1-69:52" level="2">\n  <text sourcepos="69:4-69:7 ...
#> [12] <list sourcepos="71:1-73:0" type="bullet" tight="true">\n  <item sourcep ...
#> [13] <code_block sourcepos="74:1-78:3" xml:space="preserve" name="" ktag="{:  ...
#> [14] <code_block sourcepos="80:1-87:3" xml:space="preserve" name="" ktag="{:  ...
#> [15] <list sourcepos="90:1-94:0" type="bullet" tight="true">\n  <item sourcep ...
#> [16] <block_quote sourcepos="95:1-108:14" ktag="{: .challenge}">\n  <heading  ...
#> [17] <block_quote sourcepos="110:1-140:14" ktag="{: .challenge}">\n  <heading ...
#> [18] <block_quote sourcepos="142:1-170:14" ktag="{: .challenge}">\n  <heading ...
#> [19] <heading sourcepos="172:1-172:29" level="3">\n  <text sourcepos="172:5-1 ...
#> [20] <html_block sourcepos="174:1-174:86" xml:space="preserve">&lt;img src="h ...
#> ...
loop$unblock() # removing blockquotes and replacing with div tags
pegboard:::get_divs(loop$body, 'challenge') # all challenge blocks
#> $`div-1-challenge`
#> {xml_nodeset (9)}
#> [1] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pre ...
#> [2] <heading sourcepos="95:3-95:24" level="2">\n  <text sourcepos="95:6-95:24 ...
#> [3] <paragraph sourcepos="97:3-97:87">\n  <text sourcepos="97:3-97:26" xml:sp ...
#> [4] <list sourcepos="99:3-103:1" type="ordered" start="1" delim="period" tigh ...
#> [5] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pre ...
#> [6] <heading sourcepos="104:5-104:15" level="2">\n  <text sourcepos="104:8-10 ...
#> [7] <paragraph sourcepos="106:5-108:14">\n  <text sourcepos="106:5-106:33" xm ...
#> [8] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pre ...
#> [9] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pre ...
#> 
#> $`div-3-challenge`
#> {xml_nodeset (10)}
#>  [1] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pr ...
#>  [2] <heading sourcepos="110:3-110:22" level="2">\n  <text sourcepos="110:6-1 ...
#>  [3] <paragraph sourcepos="112:3-113:39">\n  <text sourcepos="112:3-112:64" x ...
#>  [4] <code_block sourcepos="115:3-123:5" xml:space="preserve" name="" ktag="{ ...
#>  [5] <paragraph sourcepos="124:3-126:72">\n  <text sourcepos="125:3-125:16" x ...
#>  [6] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pr ...
#>  [7] <heading sourcepos="128:5-128:15" level="2">\n  <text sourcepos="128:8-1 ...
#>  [8] <code_block sourcepos="129:5-137:7" xml:space="preserve" name="" ktag="{ ...
#>  [9] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pr ...
#> [10] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pr ...
#> 
#> $`div-5-challenge`
#> {xml_nodeset (9)}
#> [1] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pre ...
#> [2] <heading sourcepos="142:3-142:19" level="2">\n  <text sourcepos="142:6-14 ...
#> [3] <paragraph sourcepos="144:3-146:20">\n  <text sourcepos="144:3-144:54" xm ...
#> [4] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pre ...
#> [5] <heading sourcepos="147:5-147:15" level="2">\n  <text sourcepos="147:8-14 ...
#> [6] <paragraph sourcepos="148:5-151:31">\n  <text sourcepos="148:5-148:61" xm ...
#> [7] <code_block sourcepos="152:5-167:7" xml:space="preserve" name="" ktag="{: ...
#> [8] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pre ...
#> [9] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pre ...
#> 
pegboard:::get_divs(loop$body, 'solution') # all solution blocks
#> $`div-2-solution`
#> {xml_nodeset (4)}
#> [1] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pre ...
#> [2] <heading sourcepos="104:5-104:15" level="2">\n  <text sourcepos="104:8-10 ...
#> [3] <paragraph sourcepos="106:5-108:14">\n  <text sourcepos="106:5-106:33" xm ...
#> [4] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pre ...
#> 
#> $`div-4-solution`
#> {xml_nodeset (4)}
#> [1] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pre ...
#> [2] <heading sourcepos="128:5-128:15" level="2">\n  <text sourcepos="128:8-12 ...
#> [3] <code_block sourcepos="129:5-137:7" xml:space="preserve" name="" ktag="{: ...
#> [4] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pre ...
#> 
#> $`div-6-solution`
#> {xml_nodeset (5)}
#> [1] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pre ...
#> [2] <heading sourcepos="147:5-147:15" level="2">\n  <text sourcepos="147:8-14 ...
#> [3] <paragraph sourcepos="148:5-151:31">\n  <text sourcepos="148:5-148:61" xm ...
#> [4] <code_block sourcepos="152:5-167:7" xml:space="preserve" name="" ktag="{: ...
#> [5] <paragraph xmlns="http://commonmark.org/xml/1.0">\n  <text xml:space="pre ...
#>