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



237
238
239
240
241
242
243
244
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 237

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

#render_admonition(admonition, **_opts) ⇒ Object



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

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



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

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



321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 321

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



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

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



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

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



268
269
270
271
272
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 268

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



219
220
221
222
223
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 219

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

#render_formula(formula, **_opts) ⇒ Object



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

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



342
343
344
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 342

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

#render_image(image) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 225

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



197
198
199
200
201
202
203
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 197

def render_list_item_content(li)
  li_id = safe_attr(li, :id)
  attrs = li_id ? %( id="#{escape_html(li_id)}") : ""
  inner = coordinator.render_mixed_content_in_order(li)
  render_liquid("_list_item.html.liquid", "attrs" => attrs,
                                          "content" => inner)
end

#render_note(note, **_opts) ⇒ Object



262
263
264
265
266
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 262

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



334
335
336
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 334

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

#render_ordered_list(ol, **_opts) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 184

def render_ordered_list(ol, **_opts)
  attrs = element_attrs(id: safe_attr(ol, :id),
                        start: safe_attr(ol, :start), type: safe_attr(ol, :type_attr))
  items = ol.listitem&.filter_map do |li|
    render_list_item_content(li)
  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



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 286

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



338
339
340
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 338

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

#render_sourcecode(sc, **_opts) ⇒ Object



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

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



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

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
# File 'lib/metanorma/html/renderers/block_renderer.rb', line 156

def render_unordered_list(ul, **_opts)
  attrs = element_attrs(id: safe_attr(ul, :id))
  items = ul.listitem&.filter_map do |li|
    render_list_item_content(li)
  end || []
  render_liquid("_list.html.liquid", {
                  "list_tag" => "ul",
                  "attrs" => attrs,
                  "items" => items,
                })
end

#render_video(video) ⇒ Object



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

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