Class: Asciidoctor::Html::Converter

Inherits:
Object
  • Object
show all
Includes:
Figure
Defined in:
lib/asciidoctor/html/converter.rb

Overview

A custom HTML5 converter that plays nicely with Bootstrap CSS

Instance Method Summary collapse

Methods included from Figure

#convert_figlist, #display_figure, #display_image, #image_attrs

Instance Method Details

#convert_admonition(node) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/asciidoctor/html/converter.rb', line 71

def convert_admonition(node)
  name = node.attr "name"
  icon_class = case name
               when "note" then "info-lg"
               when "tip" then "lightbulb"
               else "exclamation-lg"
               end
  icon = %(<div class="icon"><i class="bi bi-#{icon_class}"></i></div>)
  content = node.blocks? ? node.content : "<p>#{node.content}</p>"
  content = %(#{icon}\n#{Utils.display_title node}#{content})
  classes = %(admonition admonition-#{name}#{" #{node.role}" if node.role})
  Utils.wrap_id_classes content, node.id, classes
end

#convert_colist(node) ⇒ Object



214
215
216
# File 'lib/asciidoctor/html/converter.rb', line 214

def convert_colist(node)
  List.convert node
end

#convert_dlist(node) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/asciidoctor/html/converter.rb', line 224

def convert_dlist(node)
  return Tabs.convert_tabs(node) if node.option?("tabs")

  classes = ["dlist", node.style, node.role].compact.join(" ")
  result = [%(<dl#{Utils.dyn_id_class_attr_str node, classes}>)]
  live = node.attr? "live"
  line_number = 1
  node.items.each do |terms, dd|
    return %(<p class="text-danger">Definition is required.</p>) unless dd

    terms.each do |dt|
      result << %(<dt class="dterm"#{Utils.line_number_attr line_number, live:}>#{dt.text}</dt>)
      line_number += 1
    end

    result << "<dd#{Utils.line_number_attr line_number, live:}>"
    line_number += 1
    result << %(<p>#{dd.text}</p>) if dd.text?
    result << dd.content if dd.blocks?
    result << "</dd>"
  end
  result << "</dl>\n"
  Utils.wrap_live(Utils.wrap_id_classes_with_title(result.join("\n"), node, node.id, "dlist-wrapper"),
                  node.attr("live"))
end

#convert_embedded(node) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/asciidoctor/html/converter.rb', line 19

def convert_embedded(node)
  result = [node.content]
  if node.footnotes?
    result << %(<div class="fn-wrapper">)
    result << %(<div class="footnote-separator"></div><div class="footnotes">)
    node.footnotes.each do |fn|
      result << %(<div class="fn-row"><div class="fn-mark">#{fn.index}</div>)
      result << %(<div class="footnote" id="_footnotedef_#{fn.index}">#{fn.text}</div></div>)
    end
    result << %(</div></div>)
  end
  result.join("\n")
end

#convert_example(node) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'lib/asciidoctor/html/converter.rb', line 171

def convert_example(node)
  content = Utils.display_title(node) + node.content
  if node.option?("unstyled")
    classes = %(block#{" #{node.role}" if node.role})
    return Utils.wrap_id_classes(content, node.id, classes)
  end

  Utils.wrap_node content, node
end

#convert_floating_title(node) ⇒ Object



48
49
50
# File 'lib/asciidoctor/html/converter.rb', line 48

def convert_floating_title(node)
  Utils.heading node, node.title, discrete: true
end

#convert_image(node) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/asciidoctor/html/converter.rb', line 181

def convert_image(node)
  content = display_figure node
  classes = ["figbox", node.role]
  Utils.wrap_live(
    Utils.wrap_id_classes(content, node.id, classes.compact.join(" ")),
    node.attr("live")
  )
end

#convert_inline_anchor(node) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/asciidoctor/html/converter.rb', line 250

def convert_inline_anchor(node)
  node_text = node.text
  if node.type == :xref
    target = node.document.catalog[:refs][node.attr("refid")]
    if target&.inline? && target.parent&.parent&.style == "bibliography"
      reftext = target.reftext
      if node_text
        /\A\[(?<numeral>\d+)\]\z/ =~ reftext
        reftext = if numeral
                    "[#{numeral}, #{node_text}]"
                  else
                    "#{reftext}, #{node_text}"
                  end
      end
      return Utils.popover_button(reftext, target.id, "bibref")
    end

    if !node_text && (text = target&.inline? ? target&.text : target&.attr("reftext"))
      subs = %i[specialcharacters quotes replacements macros]
      return %(<a href="#{node.target}">#{node.apply_subs text, subs}</a>)
    end
  end
  super
end

#convert_inline_callout(node) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/asciidoctor/html/converter.rb', line 106

def convert_inline_callout(node)
  i = node.text.to_i
  case i
  when 1..20
    (i + 9311).chr(Encoding::UTF_8)
  when 21..50
    (i + 3230).chr(Encoding::UTF_8)
  else
    "[#{node.text}]"
  end
end

#convert_inline_footnote(node) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/asciidoctor/html/converter.rb', line 118

def convert_inline_footnote(node)
  if (index = node.attr "index")
    %(<sup>#{Utils.popover_button index, "_footnotedef_#{index}", "fnref"}</sup>)
  else
    %(<sup class="text-danger">[??]</sup>)
  end
end

#convert_inline_image(node) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/asciidoctor/html/converter.rb', line 190

def convert_inline_image(node)
  target = node.target
  return read_svg_contents(node, target) if node.option?("inline")

  parent = node.parent
  mark = parent.attr "mark"
  title_attr = node.attr? "title"
  if mark # The image is part of a figlist
    title = node.attr("title") if title_attr
    caption = if title_attr || parent.context == :olist
                %(\n    <figcaption><span class="li-mark">#{mark}</span>#{title}</figcaption>)
              end
    %(    #{display_image node, target}#{caption})
  else
    display_image node, target, title_attr:
  end
end

#convert_listing(node) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/asciidoctor/html/converter.rb', line 126

def convert_listing(node)
  if node.option? "jupyter"
    return Utils.wrap_id_classes(
      "#{Utils.display_title node}#{Jupyterlite.html node}",
      node.id,
      "jupyter-cell-wrapper"
    )
  end

  nowrap = (node.option? "nowrap") || !(node.document.attr? "prewrap")
  if node.style == "source"
    classes = []
    classes << "language-#{node.attr "language"}" if node.attr?("language")
    classes << "linenums" if (has_linenums = node.option? "linenums")
    classes << "ln-plugin" if has_linenums || node.attr?("live")
    code_open = %(<code#{%( class="#{classes.join " "}") unless classes.empty?}>)
    pre_open = %(<pre#{%( class="nowrap") if nowrap}>#{code_open})
    pre_close = "</code></pre>"
  else
    pre_open = %(<pre#{%( class="nowrap") if nowrap}>)
    pre_close = "</pre>"
  end
  title = Utils.display_title node
  content = title + pre_open + node.content + pre_close
  Utils.wrap_live Utils.wrap_node(content, node), node.attr("live")
end

#convert_literal(node) ⇒ Object



153
154
155
156
157
158
# File 'lib/asciidoctor/html/converter.rb', line 153

def convert_literal(node)
  nowrap = !(node.document.attr? "prewrap") || (node.option? "nowrap")
  pre = %(<pre#{%( class="nowrap") if nowrap}>#{node.content}</pre>)
  title = Utils.display_title(node)
  Utils.wrap_node "#{title}#{pre}", node
end

#convert_olist(node) ⇒ Object



208
209
210
211
212
# File 'lib/asciidoctor/html/converter.rb', line 208

def convert_olist(node)
  return convert_figlist(node) if node.style == "figlist"

  List.convert node
end

#convert_open(node) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/asciidoctor/html/converter.rb', line 160

def convert_open(node)
  collapsible = node.option? "collapsible"
  title = if collapsible
            %(<summary>#{node.title || "Details"}</summary>\n)
          else
            Utils.display_title(node)
          end
  tag_name = collapsible ? :details : :div
  Utils.wrap_node(title + node.content, node, tag_name)
end

#convert_paragraph(node) ⇒ Object



52
53
54
55
# File 'lib/asciidoctor/html/converter.rb', line 52

def convert_paragraph(node)
  content = %(<p#{Utils.dyn_id_class_attr_str node, node.role}>#{node.content}</p>\n)
  Utils.wrap_node_with_title content, node
end

#convert_preamble(node) ⇒ Object



33
34
35
# File 'lib/asciidoctor/html/converter.rb', line 33

def convert_preamble(node)
  %(<div class="preamble flip d-block">\n#{node.content}</div> <!-- .preamble.flip -->\n)
end

#convert_quote(node) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/asciidoctor/html/converter.rb', line 57

def convert_quote(node)
  attribution = node.attr?("attribution") ? node.attr("attribution") : nil
  citetitle = node.attr?("citetitle") ? node.attr("citetitle") : nil
  classes = ["blockquote", node.role].compact.join(" ")
  cite_element = citetitle ? %(<cite>#{citetitle}</cite>) : ""
  attr_element = attribution ? %(<span class="attribution">#{attribution}</span>) : ""
  content = %(<blockquote#{Utils.dyn_id_class_attr_str node, classes}>\n#{node.content}\n</blockquote>)
  if attribution || citetitle
    caption = %(<figcaption class="blockquote-footer">\n#{attr_element}#{cite_element}\n</figcaption>)
    content = %(<figure>\n#{content}\n#{caption}\n</figure>\n)
  end
  Utils.wrap_node_with_title content, node
end

#convert_section(node) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/asciidoctor/html/converter.rb', line 37

def convert_section(node)
  document = node.document
  level = node.level
  show_sectnum = node.numbered && level <= (document.attr("sectnumlevels") || 1).to_i
  classes = "section#{" flip" if level == 1}#{" #{node.role}" if node.role}"
  display_sectnum = Utils.display_sectnum(node, level) if show_sectnum
  title = "#{display_sectnum}#{node.title}"
  content = "#{Utils.heading node, title}#{node.content}"
  Utils.wrap_id_classes content, node.id, classes, :section
end

#convert_sidebar(node) ⇒ Object



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

def convert_sidebar(node)
  classes = ["aside", node.role].compact.join(" ")
  title = node.title? ? %(<div class="aside-title">#{node.title}</div>\n) : ""
  content = "#{title}#{node.content}"
  %(<aside#{Utils.id_class_attr_str node.id, classes}>\n#{content}\n</aside>\n)
end

#convert_stem(node) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/asciidoctor/html/converter.rb', line 92

def convert_stem(node)
  open, close = BLOCK_MATH_DELIMITERS[node.style.to_sym]
  equation = node.content || ""
  equation = "#{open}#{equation}#{close}" unless (equation.start_with? open) && (equation.end_with? close)
  classes = ["stem", node.role].compact
  if node.option? "numbered"
    equation = %(<div class="equation">\n#{equation}\n</div> <!-- .equation -->)
    equation = %(#{equation}\n<div class="equation-number">#{node.reftext}</div>)
    classes << "stem-equation"
  end
  content = %(<div#{Utils.dyn_id_class_attr_str node, classes.join(" ")}>\n#{equation}\n</div>\n)
  Utils.wrap_id_classes_with_title content, node, node.id, "stem-wrapper"
end

#convert_table(node) ⇒ Object



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
304
305
306
# File 'lib/asciidoctor/html/converter.rb', line 275

def convert_table(node)
  classes = ["table"]
  classes << "table-striped" if node.option? "striped"
  classes << "table-bordered" if node.option? "bordered"
  classes << "table-sm" if node.option? "compact"
  classes << "align-#{node.attr "valign"}" if node.attr?("valign")
  classes << "table-h#{node.attr "halign"}" if node.attr?("halign")
  width_attribute = ""
  if (autowidth = node.option? "autowidth") && !(node.attr? "width")
    classes << "table-fit"
  elsif (tablewidth = node.attr "width")
    width_attribute = %( width="#{tablewidth}%")
  end
  result = [%(<table#{Utils.id_class_attr_str node.id, classes.join(" ")}#{width_attribute}>)]
  if Utils.show_title? node
    result << %(<caption class="table-title">#{Utils.display_title_prefix node}#{node.title}</caption>)
  end
  if node.attr("rowcount").positive? && node.attr?("cols")
    result << "<colgroup>"
    if autowidth
      result += (Array.new node.columns.size, %(<col>))
    else
      node.columns.each do |col|
        result << (col.option?("autowidth") ? %(<col>) : %(<col style="width:#{col.attr "colpcwidth"}%;">))
      end
    end
    result << "</colgroup>"
  end
  result << "#{Table.display_rows(node)}</table>"
  wrap_classes = "table-responsive#{" #{node.role}" if node.role}"
  Utils.wrap_live Utils.wrap_id_classes(result.join("\n"), nil, wrap_classes), node.attr("live")
end

#convert_ulist(node) ⇒ Object



218
219
220
221
222
# File 'lib/asciidoctor/html/converter.rb', line 218

def convert_ulist(node)
  return convert_figlist(node) if node.style == "figlist"

  List.convert node, :ul
end