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!(document, anchor_id, reftext) ⇒ Object
- #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
118 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 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 118 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
105 106 107 108 109 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 105 def process_flat_item!(item, idx) mark = li_mark(0, idx) item.set_attr "mark", mark item.set_attr "reftext", 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
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 80 def process_olist!(block, depth, flat_style: false) parent_reftext = "" document = block.document 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}" item.set_attr "reftext", item_reftext /^<a id="(?<id>.+?)"/ =~ item.text register_reftext! document, id, item_reftext if id 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!(document, anchor_id, reftext) ⇒ Object
75 76 77 78 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 75 def register_reftext!(document, anchor_id, reftext) node = 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
111 112 113 114 115 116 |
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 111 def reset_counters!(document) counters = document.counters NUMBERED_CONTEXTS.each_value do |counter_name| counters[counter_name] = 0 end end |