Class: Asciidoctor::Html::RefTreeProcessor
- Inherits:
-
Extensions::TreeProcessor
- Object
- Extensions::TreeProcessor
- Asciidoctor::Html::RefTreeProcessor
- Defined in:
- lib/asciidoctor/html/ref_tree_processor.rb
Overview
Traverses the document tree and:
- attaches a correct reftext to numbered nodes;
- populates the text (= reftext for inline nodes) of anchors at the beginning of a list item for an ordered list;
- registers every encountered source code language not included in the default highlightjs build.
Constant Summary collapse
- NUMBERED_CONTEXTS =
{ example: "thm-number", table: "tbl-number", image: "fig-number", stem: "eqn-number", listing: "ltg-number" }.freeze
Instance Method Summary collapse
- #assign_numeral!(node, counter_name) ⇒ Object
- #bullet(depth) ⇒ Object
- #colist?(node) ⇒ Boolean
- #convert_mark(numeral, idx) ⇒ Object
- #env(context, style) ⇒ Object
-
#initialize(*args) ⇒ RefTreeProcessor
constructor
A new instance of RefTreeProcessor.
- #li_default_format(depth, style = nil) ⇒ Object
- #li_mark(node, idx, depth, format_str) ⇒ Object
- #li_ref_mark(mark) ⇒ Object
- #offset(list) ⇒ Object
- #olist?(node) ⇒ Boolean
- #olist_item?(node) ⇒ Boolean
- #process(document) ⇒ Object
- #process_block!(block) ⇒ Object
- #process_colist!(block) ⇒ Object
- #process_flat_item!(item, idx) ⇒ Object
- #process_numbered_block!(block) ⇒ Object
- #process_numbered_block?(block) ⇒ Boolean
- #process_olist!(block, depth, flat_style: false) ⇒ Object
- #process_source_code!(document, lang, linenums: false) ⇒ Object
- #process_ulist!(block, depth) ⇒ Object
- #process_unnumbered_block!(block) ⇒ Object
-
#register_reftext!(item, reftext) ⇒ Object
Finds an anchor at the start of item.text and updates its reftext to that of item's if necessary.
- #relative_numeral(node) ⇒ Object
- #source_code?(node) ⇒ Boolean
- #ulist?(node) ⇒ Boolean
- #ulist_item?(node) ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ RefTreeProcessor
Returns a new instance of RefTreeProcessor.
25 26 27 28 29 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 25 def initialize(*args) @jupyter_cell_id = 0 @tabs_id = 0 super end |
Instance Method Details
#assign_numeral!(node, counter_name) ⇒ Object
31 32 33 34 35 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 31 def assign_numeral!(node, counter_name) document = node.document document.counters[counter_name] ||= 0 node.numeral = (document.counters[counter_name] += 1) end |
#bullet(depth) ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 144 def bullet(depth) case depth when 1 then "‐" when 2 then "⭑" when 3 then "◦" else "•" end end |
#colist?(node) ⇒ Boolean
250 251 252 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 250 def colist?(node) node.context == :colist end |
#convert_mark(numeral, idx) ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 107 def convert_mark(numeral, idx) case numeral when "a" then ("a".."z").to_a[idx] when "A" then ("a".."z").to_a[idx].upcase when "I" then RomanNumerals.to_roman(idx + 1) when "i" then RomanNumerals.to_roman(idx + 1).downcase else idx + 1 end end |
#env(context, style) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 84 def env(context, style) case context when :image then "figure" when :stem then "equation" when :listing then "listing" else style || context.to_s end end |
#li_default_format(depth, style = nil) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 117 def li_default_format(depth, style = nil) return "[1]" if style == "bibliography" return "(a)" if style == "figlist" case depth when 1 then "(a)" when 2 then "i." when 3 then "(A)" else "1." end end |
#li_mark(node, idx, depth, format_str) ⇒ Object
135 136 137 138 139 140 141 142 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 135 def li_mark(node, idx, depth, format_str) rgx = /(?<left>.?)(?<numeral>[1iIaA])(?<right>.?)/ match = rgx.match(format_str) || rgx.match(li_default_format(depth)) delim_left = node.sub_specialchars match[:left] delim_right = node.sub_specialchars match[:right] mark = convert_mark match[:numeral], idx ->(prefix) { "#{delim_left}#{prefix}#{mark}#{delim_right}" } end |
#li_ref_mark(mark) ⇒ Object
129 130 131 132 133 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 129 def li_ref_mark(mark) return mark[0..-2] if mark.end_with?(".") mark end |
#offset(list) ⇒ Object
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 153 def offset(list) return (list.attr("start").to_i - 1) if list.attr?("start") return 0 unless list.attr?("continue") id = list.attr "continue" node = list.document.catalog[:refs][id] return node.attr("nflatitems") || node.items.size if node&.context == :olist 0 end |
#olist?(node) ⇒ Boolean
246 247 248 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 246 def olist?(node) node.context == :olist end |
#olist_item?(node) ⇒ Boolean
234 235 236 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 234 def olist_item?(node) node.context == :list_item && node.parent.context == :olist end |
#process(document) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 258 def process(document) listdepth = bulletdepth = flat_idx = 0 flat_style = live = false line_number = 1 tw = TreeWalker.new document while (block = tw.next_block) unless block.attr? "refprocessed" process_block! block if colist? block process_colist! block elsif (is_olist = olist? block) || (is_ulist = ulist? block) if is_olist && listdepth.zero? flat_style = (block.style == "pseudocode") flat_idx = offset block end process_olist!(block, listdepth, flat_style:) if is_olist process_ulist!(block, bulletdepth) if is_ulist if (listdepth + bulletdepth).zero? live = block.attr?("live") document.set_attr "live", true if live && !document.attr?("live") line_number = offset(block) + 1 end elsif (is_olist_item = olist_item? block) || ulist_item?(block) if is_olist_item && flat_style process_flat_item! block, flat_idx flat_idx += 1 end if live block.set_attr "line-number", line_number line_number += 1 end elsif source_code? block linenums = block.option?("linenums") || block.attr?("live") process_source_code! document, block.attr("language"), linenums: end block.set_attr "refprocessed", true end tw.walk do |move| listdepth += 1 if olist?(block) && move == :explore listdepth -= 1 if olist_item?(block) && move == :retreat bulletdepth += 1 if ulist?(block) && move == :explore bulletdepth -= 1 if ulist_item?(block) && move == :retreat block.set_attr("nflatitems", flat_idx) if olist?(block) && flat_style && move == :retreat end end end |
#process_block!(block) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 68 def process_block!(block) if block.option? "unnumbered" process_unnumbered_block! block elsif process_numbered_block? block process_numbered_block! block end if block.option? "jupyter" @jupyter_cell_id += 1 block.set_attr "jupyter-cell-id", @jupyter_cell_id end return unless block.option? "tabs" @tabs_id += 1 block.set_attr "tabs-id", @tabs_id end |
#process_colist!(block) ⇒ Object
199 200 201 202 203 204 205 206 207 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 199 def process_colist!(block) block.set_attr "list-depth", 0 block.items.each_with_index do |item, idx| icon_type = "#{idx + 1}-circle" icon = %(<i class="bi bi-#{icon_type}"></i>) item.set_attr "mark", icon register_reftext! item, "bi:#{icon_type}[]" end end |
#process_flat_item!(item, idx) ⇒ Object
221 222 223 224 225 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 221 def process_flat_item!(item, idx) mark = (idx + 1).to_s item.set_attr "mark", mark register_reftext! item, mark end |
#process_numbered_block!(block) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 45 def process_numbered_block!(block) context = block.context style = block.style context = :image if style == "figlist" env = env context, style block.set_attr("showcaption", true) unless context == :stem assign_numeral! block, NUMBERED_CONTEXTS[context] unless context == :section relative_numeral = relative_numeral block reftext = if context == :stem "(#{relative_numeral})" else "#{env.capitalize} #{relative_numeral}" end block.set_attr "reftext", reftext unless block.reftext? block.set_attr "title-prefix", reftext end |
#process_numbered_block?(block) ⇒ Boolean
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 93 def process_numbered_block?(block) context = block.context case context when :olist block.style == "figlist" when :stem, :listing block.option? "numbered" when :section block.numbered && block.level == 1 else NUMBERED_CONTEXTS.include? context end end |
#process_olist!(block, depth, flat_style: false) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 173 def process_olist!(block, depth, flat_style: false) relative = block.option? "relative" parent_mark = "#{block.document.attr "chapnum"}." if relative parent_reftext = "" if depth.positive? parent = block.parent parent = parent.parent until parent.context == :list_item parent_reftext = parent.reftext if parent.reftext? parent_mark = parent.attr "mark" if relative end block.set_attr "list-depth", depth if flat_style block.set_attr "flat-style", true else offset = offset block style = block.style marker_format = block.attr("markers") || li_default_format(depth, style) block.items.each_with_index do |item, idx| mark = li_mark(block, idx + offset, depth, marker_format) item.set_attr "mark", mark.call(parent_mark) ref_mark_prefix = parent_mark if depth.zero? register_reftext! item, "#{parent_reftext}#{li_ref_mark mark.call(ref_mark_prefix)}" end end end |
#process_source_code!(document, lang, linenums: false) ⇒ Object
227 228 229 230 231 232 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 227 def process_source_code!(document, lang, linenums: false) document.set_attr("source-langs", {}) unless document.attr?("source-langs") document.set_attr("linenumbering", true) if !document.attr?("linenumbering") && linenums langs = document.attr "source-langs" langs[lang] = true unless Highlightjs::INCLUDED_LANGS.include?(lang) end |
#process_ulist!(block, depth) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 209 def process_ulist!(block, depth) block.set_attr "list-depth", depth block.items.each do |item| is_checkbox = item.attr? "checkbox" icon_class = item.attr?("checked") ? "check-" : "" icon = %(<i class="bi bi-#{icon_class}square"></i>) mark = is_checkbox ? icon : bullet(depth) item.role = "checked" if is_checkbox item.set_attr "mark", mark end end |
#process_unnumbered_block!(block) ⇒ Object
63 64 65 66 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 63 def process_unnumbered_block!(block) reftext = block.title? ? block.title : "[#{block.id}]" block.set_attr "reftext", reftext end |
#register_reftext!(item, reftext) ⇒ Object
Finds an anchor at the start of item.text and updates its reftext to that of item's if necessary.
166 167 168 169 170 171 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 166 def register_reftext!(item, reftext) item.set_attr "reftext", reftext /\A<a id="(?<anchor_id>.+?)"/ =~ item.text node = item.document.catalog[:refs][anchor_id] node&.text ||= reftext end |
#relative_numeral(node) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 37 def relative_numeral(node) return "" unless node.numeral chapnum = node.document.attr "chapnum" has_prefix = chapnum && !chapnum.empty? && chapnum != "0" has_prefix ? "#{chapnum}.#{node.numeral}" : node.numeral.to_s end |
#source_code?(node) ⇒ Boolean
254 255 256 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 254 def source_code?(node) node.context == :listing && node.style == "source" && node.attr?("language") end |
#ulist?(node) ⇒ Boolean
242 243 244 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 242 def ulist?(node) node.context == :ulist end |
#ulist_item?(node) ⇒ Boolean
238 239 240 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 238 def ulist_item?(node) node.context == :list_item && node.parent.context == :ulist end |