Class: Asciidoctor::Html::RefTreeProcessor

Inherits:
Extensions::TreeProcessor
  • Object
show all
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

Instance Method Details

#assign_numeral!(node, document, counter_name) ⇒ Object



25
26
27
28
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 25

def assign_numeral!(node, document, counter_name)
  document.counters[counter_name] ||= 0
  node.numeral = (document.counters[counter_name] += 1)
end

#bullet(depth) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 84

def bullet(depth)
  case depth
  when 1 then "‐"
  when 2 then "⭑"
  when 3 then "◦"
  else "•"
  end
end

#colist?(node) ⇒ Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 205

def colist?(node)
  node.context == :colist
end

#env(context, style) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 54

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_mark(depth, idx) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 75

def li_mark(depth, idx)
  case depth
  when 1 then ("a".."z").to_a[idx]
  when 2 then RomanNumerals.to_roman(idx + 1).downcase
  when 3 then ("a".."z").to_a[idx].upcase
  else idx + 1
  end
end

#li_style(depth, list_style) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 103

def li_style(depth, list_style)
  return "mark-square-brackets" if list_style == "bibliography"
  return "mark-round-brackets" if list_style == "figlist"

  case depth
  when 1, 3 then "mark-round-brackets"
  else "mark-dot"
  end
end

#offset(list) ⇒ Object



113
114
115
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 113

def offset(list)
  list.attr?("start") ? (list.attr("start").to_i - 1) : 0
end

#olist?(node) ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 201

def olist?(node)
  node.context == :olist
end

#olist_item?(node) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 189

def olist_item?(node)
  node.context == :list_item && node.parent.context == :olist
end

#process(document) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 213

def process(document)
  listdepth = 0
  bulletdepth = 0
  flat_style = false
  flat_idx = 0 # flat index for (pseudocode) list
  tw = TreeWalker.new document
  while (block = tw.next_block)
    unless block.attr? "refprocessed"
      process_numbered_block!(block, document) if process_numbered_block?(block)
      if colist? block
        process_colist! block
      elsif olist? block
        if listdepth.zero?
          flat_style = (block.style == "pseudocode")
          flat_idx = offset block # rubocop:disable Lint/UselessAssignment
        end
        process_olist! block, listdepth, flat_style:
      elsif olist_item?(block) && flat_style
        process_flat_item! block, flat_idx
        flat_idx += 1
      elsif source_code? block
        process_source_code! document, block.attr("language")
      elsif ulist? block
        process_ulist! block, bulletdepth
      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
    end
  end
end

#process_colist!(block) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 156

def process_colist!(block)
  block.set_attr "list-depth", 0
  block.items.each_with_index do |item, idx|
    icon = %(<i class="bi bi-#{idx + 1}-circle"></i>)
    item.set_attr "mark", icon
    register_reftext! item, icon
  end
end

#process_flat_item!(item, idx) ⇒ Object



177
178
179
180
181
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 177

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) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 38

def process_numbered_block!(block, document)
  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, document, NUMBERED_CONTEXTS[context]
  relative_numeral = relative_numeral block, document
  reftext = if context == :stem
              "(#{relative_numeral})"
            else
              "#{env.capitalize} #{relative_numeral}"
            end
  block.set_attr "reftext", reftext
end

#process_numbered_block?(block) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 63

def process_numbered_block?(block)
  context = block.context
  case context
  when :olist
    block.style == "figlist"
  when :stem, :listing
    block.option? "numbered"
  else
    NUMBERED_CONTEXTS.include? context
  end
end

#process_olist!(block, depth, flat_style: false) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 130

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
    shift = shift block
    style = block.style
    d = (style == "figlist" ? 1 : depth) + shift
    role = li_style d, style
    block.items.each_with_index do |item, idx|
      mark = li_mark(d, idx + offset)
      item.set_attr "mark", mark
      item.role = role
      item_reftext = "#{parent_reftext}#{ref_li_mark mark, d, style}"
      register_reftext! item, item_reftext
    end
  end
end

#process_source_code!(document, lang) ⇒ Object



183
184
185
186
187
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 183

def process_source_code!(document, lang)
  document.set_attr("source-langs", {}) unless document.attr?("source-langs")
  langs = document.attr "source-langs"
  langs[lang] = true unless Highlightjs::INCLUDED_LANGS.include?(lang)
end

#process_ulist!(block, depth) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 165

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

#ref_li_mark(mark, depth, style = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 93

def ref_li_mark(mark, depth, style = nil)
  return mark.to_s unless style

  case li_style depth, style
  when "mark-square-brackets" then "[#{mark}]"
  when "mark-round-brackets" then "(#{mark})"
  else mark.to_s
  end
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.



123
124
125
126
127
128
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 123

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, document) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 30

def relative_numeral(node, document)
  return "" unless node.numeral

  chapnum = document.attr "chapnum"
  has_prefix = chapnum && !chapnum.empty? && chapnum != "0"
  has_prefix ? "#{chapnum}.#{node.numeral}" : node.numeral.to_s
end

#shift(list) ⇒ Object



117
118
119
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 117

def shift(list)
  list.attr?("shift") ? list.attr("shift").to_i : 0
end

#source_code?(node) ⇒ Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 209

def source_code?(node)
  node.context == :listing && node.style == "source" && node.attr?("language")
end

#ulist?(node) ⇒ Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 197

def ulist?(node)
  node.context == :ulist
end

#ulist_item?(node) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/asciidoctor/html/ref_tree_processor.rb', line 193

def ulist_item?(node)
  node.context == :list_item && node.parent.context == :ulist
end