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.
Constant Summary collapse
- NUMBERED_CONTEXTS =
{ example: "thm-number", table: "tbl-number", image: "fig-number" }.freeze
Instance Method Summary collapse
- #assign_numeral!(node, document, counter_name) ⇒ Object
- #li_mark(depth, idx) ⇒ Object
- #number_within(document) ⇒ Object
- #offset(list) ⇒ Object
- #process(document) ⇒ Object
- #process_flat_item!(item, idx) ⇒ Object
- #process_numbered_block!(block, document, sectnum) ⇒ Object
- #process_numbered_block?(block) ⇒ Boolean
- #process_olist!(block, depth, flat_style: false) ⇒ Object
- #ref_li_mark(mark, depth) ⇒ 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, document, sectnum) ⇒ Object
- #reset_counters!(document) ⇒ Object
Instance Method Details
#assign_numeral!(node, document, counter_name) ⇒ Object
24 25 26 27 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 24 def assign_numeral!(node, document, counter_name) document.counters[counter_name] ||= 0 node.numeral = (document.counters[counter_name] += 1) end |
#li_mark(depth, idx) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 52 def li_mark(depth, idx) case depth when 0 idx + 1 when 1 ("a".."z").to_a[idx] when 2 RomanNumerals.to_roman(idx + 1).downcase when 3 ("a".."z").to_a[idx].upcase end end |
#number_within(document) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 17 def number_within(document) return :chapter if document.attr? "chapnum" return :section if document.attr? "sectnums" :document end |
#offset(list) ⇒ Object
71 72 73 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 71 def offset(list) list.attr?("start") ? (list.attr("start").to_i - 1) : 0 end |
#process(document) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 119 def process(document) sectnum = 0 listdepth = 0 flat_style = false flat_idx = 0 # flat index for (pseudocode) list tw = TreeWalker.new(document) while (block = tw.next_block) context = block.context unless block.attr? "refprocessed" process_numbered_block!(block, document, sectnum) if process_numbered_block?(block) if context == :section && block.level == 1 && number_within(document) == :section sectnum += 1 reset_counters! document elsif context == :olist if listdepth.zero? flat_style = (block.style == "pseudocode") flat_idx = offset block end process_olist!(block, listdepth, flat_style:) elsif context == :list_item && flat_style process_flat_item!(block, flat_idx) flat_idx += 1 end block.set_attr "refprocessed", true end tw.walk do |move| listdepth += 1 if context == :olist && move == :explore listdepth -= 1 if context == :list_item && move == :retreat end end end |
#process_flat_item!(item, idx) ⇒ Object
106 107 108 109 110 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 106 def process_flat_item!(item, idx) mark = li_mark(0, idx) item.set_attr "mark", mark register_reftext! item, ref_li_mark(mark, 0) end |
#process_numbered_block!(block, document, sectnum) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 38 def process_numbered_block!(block, document, sectnum) context = block.context env = (block.style || context).to_s env = "figure" if context == :image || env == "figlist" block.set_attr "showcaption", true assign_numeral! block, document, NUMBERED_CONTEXTS[context] title_prefix = "#{env.capitalize} #{relative_numeral block, document, sectnum}" block.set_attr "reftext", title_prefix end |
#process_numbered_block?(block) ⇒ Boolean
48 49 50 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 48 def process_numbered_block?(block) NUMBERED_CONTEXTS.key?(block.context) || block.style == "figlist" end |
#process_olist!(block, depth, flat_style: false) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 84 def process_olist!(block, depth, flat_style: false) parent_reftext = "" if depth.positive? parent = block.parent parent = parent.parent until parent.context == :list_item parent_reftext = parent.reftext? ? parent.reftext : "" end block.set_attr "list-depth", depth if flat_style block.set_attr("flat-style", true) else offset = offset block block.items.each_with_index do |item, idx| d = block.style == "figlist" ? 1 : depth mark = li_mark(d, idx + offset) item.set_attr "mark", mark item_reftext = "#{parent_reftext}#{ref_li_mark mark, d}" register_reftext! item, item_reftext end end end |
#ref_li_mark(mark, depth) ⇒ Object
65 66 67 68 69 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 65 def ref_li_mark(mark, depth) return mark.to_s if depth.even? "(#{mark})" 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.
77 78 79 80 81 82 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 77 def register_reftext!(item, reftext) item.set_attr "reftext", reftext /^<a id="(?<anchor_id>.+?)"/ =~ item.text node = item.document.catalog[:refs][anchor_id] node&.text ||= reftext end |
#relative_numeral(node, document, sectnum) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 29 def relative_numeral(node, document, sectnum) if node.numeral prefix_number = (document.attr("chapnum") || sectnum).to_i prefix_number.positive? ? "#{prefix_number}.#{node.numeral}" : node.numeral.to_s else "" end end |
#reset_counters!(document) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 112 def reset_counters!(document) counters = document.counters NUMBERED_CONTEXTS.each_value do |counter_name| counters[counter_name] = 0 end end |