Class: Metanorma::Html::Renderers::BlockRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/html/renderers/block_renderer.rb

Constant Summary collapse

BLOCK_CHILDREN =
{
  paragraphs: :render_paragraph,
  ul: :render_unordered_list,
  ol: :render_ordered_list,
  dl: :render_definition_list,
  sourcecode: :render_sourcecode,
  table: :render_table,
  figure: :render_figure,
  quote: :render_quote,
  formula: :render_formula,
}.freeze
SIMPLE_CHILDREN =
{
  paragraphs: :render_paragraph,
}.freeze
NOTE_CHILDREN =
{
  paragraphs: :render_paragraph,
  ul: :render_unordered_list,
  ol: :render_ordered_list,
  dl: :render_definition_list,
  quote: :render_quote,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(coordinator) ⇒ BlockRenderer

Returns a new instance of BlockRenderer.



33
34
35
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 33

def initialize(coordinator)
  @coordinator = coordinator
end

Instance Method Details

#image_source(image) ⇒ Object



263
264
265
266
267
268
269
270
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 263

def image_source(image)
  svg_xml = safe_attr(image, :inline_svg)
  if svg_xml && !svg_xml.empty?
    "data:image/svg+xml;base64,#{Base64.strict_encode64(svg_xml)}"
  else
    safe_attr(image, :source)
  end
end

#list_label(li) ⇒ Object

Label glyph for a list item, taken from the presentation XML (the item's fmt-name/autonum). The presentation XML is authoritative for the bullet or number; when present it is rendered as the item marker and the browser's own list-style marker is suppressed (see components/lists.css).



223
224
225
226
227
228
229
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 223

def list_label(li)
  fmt_name = coordinator.safe_attr(li, :fmt_name)
  return nil unless fmt_name

  label = coordinator.extract_plain_text(fmt_name).strip
  label.empty? ? nil : label
end

#render_admonition(admonition, **_opts) ⇒ Object



345
346
347
348
349
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 345

def render_admonition(admonition, **_opts)
  drop = Drops::AdmonitionDrop.from_model(admonition,
                                          renderer: coordinator.renderer_context)
  render_liquid("_admonition.html.liquid", { "block" => drop })
end

#render_audio(audio) ⇒ Object



280
281
282
283
284
285
286
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 280

def render_audio(audio)
  attrs = element_attrs(
    id: safe_attr(audio, :id),
    src: safe_attr(audio, :src),
  )
  render_liquid("_audio.html.liquid", { "attrs" => attrs })
end

#render_block_children(model, children:) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 357

def render_block_children(model, children:)
  parts = []
  children.each do |attr, render_method|
    values = safe_attr(model, attr)
    next if values.nil?

    Array(values).each do |v|
      parts << (public_send(render_method, v) || "")
    end
  end
  parts.join
end

#render_bookmark(bookmark, **_opts) ⇒ Object



351
352
353
354
355
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 351

def render_bookmark(bookmark, **_opts)
  render_liquid("_bookmark.html.liquid", {
                  "id" => escape_html(safe_attr(bookmark, :id).to_s),
                })
end

#render_definition_list(dl, **_opts) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 231

def render_definition_list(dl, **_opts)
  attrs = element_attrs(id: safe_attr(dl, :id))
  terms = dl.dt&.each_with_index&.map do |dt, i|
    dt_html = coordinator.render_mixed_inline(dt)
    dd = dl.dd&.[](i)
    dd_html = dd ? coordinator.render_mixed_inline(dd) : nil
    { "dt" => dt_html, "dd" => dd_html }
  end || []
  render_liquid("_definition_list.html.liquid", {
                  "attrs" => attrs,
                  "terms" => terms,
                })
end

#render_example(example, **_opts) ⇒ Object



294
295
296
297
298
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 294

def render_example(example, **_opts)
  drop = Drops::ExampleDrop.from_model(example,
                                       renderer: coordinator.renderer_context)
  render_liquid("_example.html.liquid", { "block" => drop })
end

#render_figure(figure, **_opts) ⇒ Object



245
246
247
248
249
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 245

def render_figure(figure, **_opts)
  drop = Drops::FigureDrop.from_model(figure,
                                      renderer: coordinator.renderer_context)
  render_liquid("_figure.html.liquid", { "block" => drop })
end

#render_form(form, **_opts) ⇒ Object



300
301
302
303
304
305
306
307
308
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 300

def render_form(form, **_opts)
  attrs = element_attrs(id: safe_attr(form, :id), class: "form")
  content = coordinator.render_ordered_content(form)
  render_liquid("_element.html.liquid", {
                  "tag" => "div",
                  "extra_attrs" => attrs,
                  "content" => content,
                })
end

#render_formula(formula, **_opts) ⇒ Object



316
317
318
319
320
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 316

def render_formula(formula, **_opts)
  drop = Drops::FormulaDrop.from_model(formula,
                                       renderer: coordinator.renderer_context)
  render_liquid("_formula.html.liquid", { "block" => drop })
end

#render_full_block_children(model) ⇒ Object



378
379
380
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 378

def render_full_block_children(model)
  render_block_children(model, children: BLOCK_CHILDREN)
end

#render_image(image) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 251

def render_image(image)
  src_val = image_source(image)
  attrs = element_attrs(
    id: safe_attr(image, :id),
    src: src_val,
    alt: safe_attr(image, :alt),
    height: safe_attr(image, :height),
    width: safe_attr(image, :width),
  )
  render_liquid("_image.html.liquid", { "attrs" => attrs })
end

#render_list_item_content(li, labeled: false) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 201

def render_list_item_content(li, labeled: false)
  li_id = safe_attr(li, :id)
  attrs = li_id ? %( id="#{escape_html(li_id)}") : ""
  label = labeled ? list_label(li) : nil
  inner = if label
            coordinator.render_mixed_content_in_order(
              li,
              skip_classes: [Metanorma::Document::Components::Inline::FmtNameElement],
            )
          else
            coordinator.render_mixed_content_in_order(li)
          end
  render_liquid("_list_item.html.liquid", "attrs" => attrs,
                                          "label" => label && escape_html(label),
                                          "content" => inner)
end

#render_note(note, **_opts) ⇒ Object



288
289
290
291
292
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 288

def render_note(note, **_opts)
  drop = Drops::NoteDrop.from_model(note,
                                    renderer: coordinator.renderer_context)
  render_liquid("_note.html.liquid", { "block" => drop })
end

#render_note_children(model) ⇒ Object



370
371
372
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 370

def render_note_children(model)
  render_block_children(model, children: NOTE_CHILDREN)
end

#render_ordered_list(ol, **_opts) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 186

def render_ordered_list(ol, **_opts)
  labeled = ol.listitem&.any? { |li| list_label(li) } || false
  attrs = element_attrs(id: safe_attr(ol, :id),
                        class: labeled ? "mn-labeled-list" : nil,
                        start: safe_attr(ol, :start), type: safe_attr(ol, :type_attr))
  items = ol.listitem&.filter_map do |li|
    render_list_item_content(li, labeled: labeled)
  end || []
  render_liquid("_list.html.liquid", {
                  "list_tag" => "ol",
                  "attrs" => attrs,
                  "items" => items,
                })
end

#render_paragraph(p, **_opts) ⇒ Object



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

def render_paragraph(p, **_opts)
  attrs = element_attrs(id: safe_attr(p, :id),
                        style: coordinator.alignment_style(safe_attr(p,
                                                                     :alignment)))
  content = coordinator.render_mixed_inline(p)
  render_liquid("_paragraph.html.liquid", {
                  "attrs" => attrs,
                  "content" => content,
                })
end

#render_quote(quote, **_opts) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 322

def render_quote(quote, **_opts)
  attrs = element_attrs(id: safe_attr(quote, :id), class: "quote")
  content_parts = []
  quote.paragraphs&.each do |para|
    content_parts << (render_paragraph(para) || "")
  end
  quote.ul&.each do |ul|
    content_parts << (render_unordered_list(ul) || "")
  end
  quote.ol&.each do |ol|
    content_parts << (render_ordered_list(ol) || "")
  end
  content = content_parts.join
  attribution_html = if quote.attribution
                       coordinator.render_mixed_inline(quote.attribution)
                     end
  render_liquid("_quote.html.liquid", {
                  "attrs" => attrs,
                  "content" => content,
                  "attribution" => attribution_html,
                })
end

#render_simple_children(model) ⇒ Object



374
375
376
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 374

def render_simple_children(model)
  render_block_children(model, children: SIMPLE_CHILDREN)
end

#render_sourcecode(sc, **_opts) ⇒ Object



310
311
312
313
314
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 310

def render_sourcecode(sc, **_opts)
  drop = Drops::SourcecodeDrop.from_model(sc,
                                          renderer: coordinator.renderer_context)
  render_liquid("_sourcecode.html.liquid", { "block" => drop })
end

#render_table(table, **_opts) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 48

def render_table(table, **_opts)
  attrs = element_attrs(id: safe_attr(table, :id), class: "table-block")
  table_id = safe_attr(table, :id)
  name_el = safe_attr(table, :fmt_name) || safe_attr(table, :name)
  if table_id && name_el
    register_table_entry(id: table_id,
                         text: coordinator.extract_plain_text(name_el))
  end
  col_count = table_column_count(table)

  caption_html = if name_el
                   coordinator.render_inline_element(name_el)
                 end

  colgroup_html = if table.colgroup
                    render_table_colgroup(table.colgroup)
                  end

  thead_html = if table.thead
                 render_table_section(table.thead, "thead")
               end

  tbody_html = if table.tbody
                 render_table_section(table.tbody, "tbody")
               end

  tfoot_html = nil
  if table.tfoot || (table.note && !table.note.empty?)
    tfoot_inner = render_table_section_rows(table.tfoot) if table.tfoot
    notes_html = nil
    if table.note && !table.note.empty?
      notes_inner = table.note.filter_map { |n| render_note(n) }.join
      notes_html = render_liquid("_element.html.liquid", "tag" => "tr", "extra_attrs" => "",
                                                         "content" => render_liquid("_element.html.liquid", "tag" => "td", "extra_attrs" => %( colspan="#{col_count}" class="table-notes"), "content" => notes_inner))
    end
    tfoot_html = render_liquid("_element.html.liquid", "tag" => "tfoot", "extra_attrs" => "",
                                                       "content" => "#{tfoot_inner}#{notes_html}")
  end

  render_liquid("_table.html.liquid", {
                  "attrs" => attrs,
                  "caption" => caption_html,
                  "colgroup_html" => colgroup_html,
                  "thead_html" => thead_html,
                  "tbody_html" => tbody_html,
                  "tfoot_html" => tfoot_html,
                })
end

#render_table_cell(cell, force_tag = nil) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 170

def render_table_cell(cell, force_tag = nil)
  tag_name = force_tag || (cell.is_a?(Metanorma::Document::Components::Tables::HeaderTableCell) ? "th" : "td")
  attrs = element_attrs(
    colspan: safe_attr(cell, :colspan),
    rowspan: safe_attr(cell, :rowspan),
    align: safe_attr(cell, :alignment),
    valign: safe_attr(cell, :vertical_alignment),
  )
  content = coordinator.render_cell_content(cell)
  render_liquid("_element.html.liquid", {
                  "tag" => tag_name,
                  "extra_attrs" => attrs,
                  "content" => content,
                })
end

#render_table_colgroup(colgroup) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 121

def render_table_colgroup(colgroup)
  colgroup.col&.filter_map do |col|
    attrs = element_attrs(style: col.width ? "width: #{col.width}" : nil)
    render_liquid("_element.html.liquid", "tag" => "col",
                                          "extra_attrs" => attrs, "content" => "")
  end&.join
end

#render_table_section(section, tag_name) ⇒ Object



129
130
131
132
133
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 129

def render_table_section(section, tag_name)
  inner = render_table_section_rows(section)
  render_liquid("_element.html.liquid", "tag" => tag_name,
                                        "extra_attrs" => "", "content" => inner)
end

#render_table_section_rows(section) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 135

def render_table_section_rows(section)
  section.tr&.filter_map do |tr|
    inner_parts = []
    walked = coordinator.walk_ordered(tr) do |type, obj|
      next unless type == :element

      inner_parts << (render_table_cell(obj) || "")
    end
    unless walked
      Array(tr.th).each do |th|
        inner_parts << (render_table_cell(th, "th") || "")
      end
      Array(tr.td).each do |td|
        inner_parts << (render_table_cell(td, "td") || "")
      end
    end
    render_liquid("_element.html.liquid", "tag" => "tr",
                                          "extra_attrs" => "", "content" => inner_parts.join)
  end&.join
end

#render_unordered_list(ul, **_opts) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 156

def render_unordered_list(ul, **_opts)
  labeled = ul.listitem&.any? { |li| list_label(li) } || false
  attrs = element_attrs(id: safe_attr(ul, :id),
                        class: labeled ? "mn-labeled-list" : nil)
  items = ul.listitem&.filter_map do |li|
    render_list_item_content(li, labeled: labeled)
  end || []
  render_liquid("_list.html.liquid", {
                  "list_tag" => "ul",
                  "attrs" => attrs,
                  "items" => items,
                })
end

#render_video(video) ⇒ Object



272
273
274
275
276
277
278
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 272

def render_video(video)
  attrs = element_attrs(
    id: safe_attr(video, :id),
    src: safe_attr(video, :src),
  )
  render_liquid("_video.html.liquid", { "attrs" => attrs })
end

#table_column_count(table) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 97

def table_column_count(table)
  if table.colgroup&.col && !table.colgroup.col.empty?
    return table.colgroup.col.size
  end

  max_cols = 0
  %i[thead tbody tfoot].each do |section|
    sec = table.public_send(section)
    next unless sec&.tr

    sec.tr.each do |tr|
      cols = 0
      Array(tr.th).each do |th|
        cols += th.colspan && th.colspan > 1 ? th.colspan : 1
      end
      Array(tr.td).each do |td|
        cols += td.colspan && td.colspan > 1 ? td.colspan : 1
      end
      max_cols = cols if cols > max_cols
    end
  end
  max_cols.positive? ? max_cols : 1
end