metanorma-iso 3.5.31 → 3.5.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -1
  3. data/lib/metanorma/iso/sts/html_renderer/ruby.rb +9 -1079
  4. data/lib/metanorma/iso/sts/parity_validator/normaliser.rb +4 -84
  5. data/lib/metanorma/iso/sts/parity_validator/report.rb +3 -125
  6. data/lib/metanorma/iso/sts/profile.rb +68 -0
  7. data/lib/metanorma/iso/sts/transformer/back_transformer.rb +1 -1
  8. data/lib/metanorma/iso/sts/transformer/base.rb +5 -76
  9. data/lib/metanorma/iso/sts/transformer/block_dispatcher.rb +1 -1
  10. data/lib/metanorma/iso/sts/transformer/body_transformer.rb +1 -1
  11. data/lib/metanorma/iso/sts/transformer/context.rb +13 -37
  12. data/lib/metanorma/iso/sts/transformer/dl_transformer.rb +5 -122
  13. data/lib/metanorma/iso/sts/transformer/document_transformer.rb +1 -1
  14. data/lib/metanorma/iso/sts/transformer/figure_transformer.rb +5 -118
  15. data/lib/metanorma/iso/sts/transformer/footnote_collector.rb +1 -1
  16. data/lib/metanorma/iso/sts/transformer/formula_transformer.rb +6 -35
  17. data/lib/metanorma/iso/sts/transformer/front_transformer.rb +1 -1
  18. data/lib/metanorma/iso/sts/transformer/id_generator.rb +1 -1
  19. data/lib/metanorma/iso/sts/transformer/inline_transformer.rb +2 -1
  20. data/lib/metanorma/iso/sts/transformer/list_transformer.rb +1 -1
  21. data/lib/metanorma/iso/sts/transformer/note_transformer.rb +4 -185
  22. data/lib/metanorma/iso/sts/transformer/paragraph_transformer.rb +6 -3
  23. data/lib/metanorma/iso/sts/transformer/provision_index.rb +1 -1
  24. data/lib/metanorma/iso/sts/transformer/quote_transformer.rb +5 -41
  25. data/lib/metanorma/iso/sts/transformer/reference_transformer.rb +1 -1
  26. data/lib/metanorma/iso/sts/transformer/rendered_text_extractor.rb +1 -1
  27. data/lib/metanorma/iso/sts/transformer/requirement_transformer.rb +1 -1
  28. data/lib/metanorma/iso/sts/transformer/section_transformer.rb +5 -239
  29. data/lib/metanorma/iso/sts/transformer/source_document.rb +1 -1
  30. data/lib/metanorma/iso/sts/transformer/standard.rb +1 -1
  31. data/lib/metanorma/iso/sts/transformer/table_transformer.rb +1 -1
  32. data/lib/metanorma/iso/sts/transformer/term_transformer.rb +1 -1
  33. data/lib/metanorma/iso/sts/validation_report.rb +4 -49
  34. data/lib/metanorma/iso/sts/validator.rb +1 -1
  35. data/lib/metanorma/iso/sts.rb +11 -0
  36. data/lib/metanorma/iso/version.rb +1 -1
  37. data/metanorma-iso.gemspec +1 -1
  38. data/tasks/sts_golden.rake +39 -0
  39. metadata +6 -4
@@ -1,1089 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "cgi"
4
- require "liquid"
5
- require "sts"
3
+ # Delegates to the shared engine (metanorma-document 0.5.15, Metanorma::Document::Sts)
4
+ # per metanorma-iso#1760 / metanorma-document#165 — defaulting to the ISO
5
+ # profile so the branding (publisher identity) resolves.
6
+
7
+ require "metanorma/iso/sts/profile"
6
8
 
7
9
  module Metanorma
8
10
  module Iso
9
11
  module Sts
10
12
  module HtmlRenderer
11
- # STS XML → HTML renderer. Parses the input once into the sts-ruby
12
- # typed model and emits HTML through Liquid templates — no DOM
13
- # library, no XSLT engine.
14
- #
15
- # Architecture:
16
- #
17
- # render
18
- # ├── coerce_model (XML string → Sts::NisoSts::Standard)
19
- # ├── render_node (model tree → fragment, via DISPATCH)
20
- # └── assemble_document (fragment → full page via
21
- # templates/document.html.liquid with
22
- # assets/sts.css and the OIML logo)
23
- #
24
- # Element markup lives in templates/ and is overridable per
25
- # organization with Ruby.new(templates_dir:, assets_dir:) — no
26
- # code changes needed.
27
- class Ruby
28
- TEMPLATES_DIR = File.expand_path("templates", __dir__)
29
- ASSETS_DIR = File.expand_path("assets", __dir__)
30
-
31
- NISO = ::Sts::NisoSts
32
- TBX = ::Sts::TbxIsoTml
33
-
34
- # demodulized model class name → handler method. Anything not
35
- # listed renders its children transparently (see #render_children).
36
- # NisoSts is the canonical NISO STS namespace; TbxIsoTml covers
37
- # the terminology model and table cells.
38
- DISPATCH = {
39
- "MetadataIso" => :meta, "MetadataStd" => :meta,
40
- "RegMeta" => :meta, "NatMeta" => :meta,
41
- "Body" => :children,
42
- "Section" => :section, "App" => :section, "TermSection" => :section,
43
- "Label" => :label,
44
- "Title" => :title,
45
- "Standard" => :standard, "Adoption" => :standard,
46
- "Paragraph" => :paragraph,
47
- "List" => :list, "ListItem" => :list_item,
48
- "DefList" => :def_list, "Term" => :term, "Def" => :def_item,
49
- "TableWrap" => :table_wrap, "Table" => :table,
50
- "Thead" => :thead, "Tbody" => :tbody, "Tr" => :tr,
51
- "Td" => :td, "Th" => :th,
52
- "Figure" => :figure, "Caption" => :caption, "Graphic" => :graphic,
53
- "DisplayFormula" => :disp_formula, "InlineFormula" => :inline_formula,
54
- "Preformat" => :preformat,
55
- "NonNormativeNote" => :note, "NonNormativeExample" => :example,
56
- "DispQuote" => :quote,
57
- "ReferenceList" => :ref_list, "Reference" => :ref,
58
- "MixedCitation" => :citation, "ElementCitation" => :citation,
59
- "ReferenceStandard" => :std, "StandardIdentification" => :std_ident,
60
- "Originator" => :originator,
61
- "CopyrightStatement" => :copyright,
62
- "Fn" => :fn, "Xref" => :xref,
63
- "ExtLink" => :ext_link, "Uri" => :ext_link,
64
- "Break" => :brk, "ProcessingMeta" => :skip,
65
- }.freeze
66
-
67
- # Inline phrase-level elements → HTML tag (constant-keyed).
68
- # NisoSts for Monospace/Sub/Sup/Sc/Strike/Underline;
69
- # TbxIsoTml for Bold/Italic (terminology namespace).
70
- INLINE_TAGS = {
71
- TBX::Bold => "strong",
72
- TBX::Italic => "em",
73
- NISO::Monospace => "code",
74
- NISO::Sub => "sub",
75
- NISO::Sup => "sup",
76
- NISO::Sc => "span",
77
- NISO::Strike => "s",
78
- NISO::Underline => "u",
79
- }.freeze
80
-
81
- META_ID_NAMES = %w[StandardIdentification
82
- StandardRef
83
- DocumentIdentification].freeze
84
-
85
- PUBLISHER_NAME = "International Organization for Standardization (ISO)"
86
- PUBLISHER_ADDRESS = "ISO Central Secretariat · Chemin de Blandonnet 4, CP 401, 1214 Vernier, Geneva, Switzerland"
87
-
88
- def initialize(templates_dir: nil, assets_dir: nil, publisher_name: nil, publisher_address: nil, license_text: nil)
89
- @templates_dir = templates_dir || TEMPLATES_DIR
90
- @assets_dir = assets_dir || ASSETS_DIR
91
- @publisher_name = publisher_name
92
- @publisher_address = publisher_address
93
- @license_text = license_text || "All rights reserved"
94
- @liquid_env = Liquid::Environment.new
95
- @liquid_env.file_system = Liquid::LocalFileSystem.new(@templates_dir)
96
- @template_cache = {}
97
- @mapping_cache = Hash.new { |h, klass| h[klass] = build_mapping(klass) }
98
- @toc = []
99
- @deferred_footnotes = []
100
- @depth = 0
101
- end
102
-
103
- # Render +model_or_xml+. With +full_document: true+ (default)
104
- # the fragment is assembled into a complete branded page;
105
- # with +full_document: false+ the bare fragment is returned.
106
- def render(model_or_xml, full_document: true)
107
- model = coerce_model(model_or_xml)
108
- @assemble = full_document
109
- @toc = []
110
- @depth = 0
111
- body = render_node(model)
112
- return body unless full_document
113
-
114
- assemble_document(body, model)
115
- end
116
-
117
- private
118
-
119
- attr_reader :templates_dir, :assets_dir
120
-
121
- # Accept either a parsed model or a raw XML string; the
122
- # latter is parsed via the sts-ruby typed model.
123
- def coerce_model(input)
124
- return input if input.is_a?(Lutaml::Model::Serializable)
125
-
126
- ::Sts::NisoSts::Standard.from_xml(input.to_s)
127
- end
128
-
129
- # --------------------------------------------------------------
130
- # Dispatch
131
- # --------------------------------------------------------------
132
-
133
- def render_node(node)
134
- case node
135
- when String then escape(node)
136
- when Mml::V3::Math then mathml_to_html(node)
137
- when *INLINE_TAGS.keys then render_inline_tag(node)
138
- else
139
- handler = DISPATCH[node.class.name.split("::").last]
140
- handler ? send(handler, node) : render_children(node)
141
- end
142
- end
143
-
144
- # Mml::V3::Math serializes with mml: prefix and compact
145
- # element spacing; MN HTML uses bare element names with
146
- # newlines between them. Strip the prefix and add newlines
147
- # so Nokogiri's text extraction produces the same whitespace
148
- # between MathML leaf elements.
149
- def mathml_to_html(node)
150
- node.to_xml.to_s
151
- .gsub(/<(\/?)mml:/, '<\1')
152
- .gsub(/\s+xmlns:mml="[^"]*"/, "")
153
- .gsub(/\s+xmlns:xlink="[^"]*"/, "")
154
- .gsub(/></, ">\n<")
155
- end
156
-
157
- # --------------------------------------------------------------
158
- # Handlers
159
- # --------------------------------------------------------------
160
-
161
- def children(node) = render_children(node)
162
-
163
- # Renders the document: front matter first, then ONE <main>
164
- # wrapping body AND back matter (annexes, bibliography), so
165
- # back content stays in the main flow instead of becoming
166
- # grid items behind the TOC.
167
- def standard(node)
168
- front = document_child(node, "Front")
169
- body = document_child(node, "Body")
170
- back = document_child(node, "Back")
171
-
172
- parts = []
173
- parts << render_children(front) if front
174
- main_content = []
175
- main_content << render_children(body) if body
176
- main_content << render_children(back) if back
177
- parts << render_element("main", main_content.join)
178
- parts.join
179
- end
180
-
181
- # First child model of +node+ whose demodulized class name
182
- # equals +demodulized+, in document order.
183
- def document_child(node, demodulized)
184
- mapping = @mapping_cache[node.class][:elements]
185
- node.element_order.each do |entry|
186
- next unless entry.node_type == :element
187
-
188
- attr = mapping[entry.name.to_s]
189
- next unless attr
190
-
191
- item = node.public_send(attr)
192
- item = item.first if item.is_a?(Array)
193
- return item if item && item.class.name.split("::").last == demodulized
194
- end
195
- nil
196
- end
197
-
198
- def section(node)
199
- sec_id = section_id(node)
200
- title_text = extract_title(node)
201
- register_toc(node, sec_id, title_text)
202
- previous_section_id = @current_section_id
203
- previous_label = @current_label
204
- label_node = own_label(node)
205
- suppress_label(label_node) if label_node
206
- raw_label = label_node ? plain_text(label_node).gsub(/\s+/, " ").strip : nil
207
- @current_label = display_label(node, raw_label)
208
- # Boilerplate front-matter (Copyright, Feedback) renders as
209
- # a muted card; every other section renders uniformly with
210
- # its own heading — including unnumbered prose sections like
211
- # Foreword and Introduction.
212
- frontmatter = frontmatter_section?(sec_id, title_text)
213
- @depth += 1
214
- @current_section_id = sec_id
215
- inner = render_children(node)
216
- render_element("section", inner, id: sec_id, css: frontmatter ? "frontmatter" : nil)
217
- ensure
218
- @depth -= 1
219
- @current_section_id = previous_section_id
220
- @current_label = previous_label
221
- end
222
-
223
- # Only the boilerplate front-matter sections (copyright,
224
- # feedback) get the muted-card treatment. Other unnumbered
225
- # sections (Foreword, Introduction) render as normal sections.
226
- def frontmatter_section?(sec_id, title_text)
227
- return false unless @depth.zero? && @current_label.nil?
228
- return true if sec_id && %w[copyright feedback].include?(sec_id.downcase)
229
-
230
- title_match = TITLE_DOWNCASE_MAP[title_text.to_s.downcase]
231
- title_match == :frontmatter
232
- end
233
-
234
- TITLE_DOWNCASE_MAP = {
235
- "copyright" => :frontmatter,
236
- "feedback" => :frontmatter,
237
- }.freeze
238
-
239
- # The section's/list item's own <label> child model (nil when
240
- # absent or a bare string, e.g. TableWrap#label).
241
- def own_label(node)
242
- return nil unless node.class.attributes.key?(:label)
243
-
244
- label = node.label
245
- label if label.is_a?(Lutaml::Model::Serializable)
246
- end
247
-
248
- # Marks a label model as consumed by its container (section
249
- # heading prefix / list-item marker) so the generic label
250
- # handler does not render it a second time.
251
- def suppress_label(node)
252
- suppressed_labels[node] = true
253
- end
254
-
255
- def label_suppressed?(node)
256
- suppressed_labels.key?(node)
257
- end
258
-
259
- def suppressed_labels
260
- @suppressed_labels ||= {}.compare_by_identity
261
- end
262
-
263
- # Section's <title> child text, nil when absent.
264
- def extract_title(node)
265
- title_node = find_child(node, "Title")
266
- return nil unless title_node
267
-
268
- text = plain_text(title_node).gsub(/\s+/, " ").strip
269
- text.empty? ? nil : text
270
- end
271
-
272
- # Term sections (id starts with "term-") carry the term
273
- # number in <title> and the preferred name as the bold run
274
- # of the first <p>. For the TOC entry, append the preferred
275
- # name so the entry reads "3.1 NISO STS".
276
- def append_term_name(node, sec_id, title_text)
277
- return title_text unless sec_id&.start_with?("term-")
278
- return title_text if title_text.to_s.match?(/\s/)
279
-
280
- name = preferred_term_name(node)
281
- return title_text if name.nil? || name.empty?
282
-
283
- "#{title_text} #{name}"
284
- end
285
-
286
- # The preferred name is the bold run of the section's first
287
- # <p> child (the term heading paragraph). Returns nil when
288
- # the section is not a term section or the heading paragraph
289
- # is absent / has no bold run.
290
- def preferred_term_name(node)
291
- paragraph = find_child(node, "Paragraph")
292
- return nil unless paragraph
293
-
294
- bold = find_child(paragraph, "Bold")
295
- return nil unless bold
296
-
297
- text = plain_text(bold).gsub(/\s+/, " ").strip
298
- text.empty? ? nil : text
299
- end
300
-
301
- # Sections built without an id (e.g. front-matter sections)
302
- # get a slug from their title so they are linkable and can
303
- # appear in the TOC.
304
- def section_id(node)
305
- return node.id if node.id
306
-
307
- title_node = find_child(node, "Title")
308
- return nil unless title_node
309
-
310
- plain_text(title_node).gsub(/\s+/, " ").strip
311
- .downcase.gsub(/[^a-z0-9]+/, "-")
312
- .gsub(/\A-|-\z/, "")
313
- end
314
-
315
- # The display form of a section label: annex letters gain the
316
- # "Annex" prefix ("Annex A"), everything else renders bare.
317
- def display_label(node, label_text)
318
- return nil if label_text.nil? || label_text.empty?
319
- return "Annex #{label_text}" if node.class.name.split("::").last == "App"
320
-
321
- label_text
322
- end
323
-
324
- # Records the section in the interactive table of contents.
325
- # Apps are sections too; their titles come from the same walk.
326
- # The entry text carries the section number ("1 Scope").
327
- # Term sections (id starts with "term-") append the preferred
328
- # name (the bold text of the first <p>) so the entry reads
329
- # "3.1 NISO STS" instead of just "3.1".
330
- def register_toc(node, sec_id, title_text = nil)
331
- title_node = find_child(node, "Title")
332
- return unless sec_id && title_node
333
-
334
- label_node = own_label(node)
335
- label_text = display_label(node, label_node ? plain_text(label_node).gsub(/\s+/, " ").strip : nil)
336
- title_text ||= plain_text(title_node).gsub(/\s+/, " ").strip
337
- title_text = "#{label_text} #{title_text}" if label_text
338
- title_text = append_term_name(node, sec_id, title_text)
339
- register_toc_entry(id: sec_id, title: title_text, depth: @depth)
340
- end
341
-
342
- def register_toc_entry(id:, title:, depth:)
343
- @toc << { id: id, title: title, depth: depth }
344
- end
345
-
346
- # First direct child model whose demodulized class name matches.
347
- def find_child(node, demodulized_name)
348
- return nil unless node.is_a?(Lutaml::Model::Serializable)
349
-
350
- mapping = @mapping_cache[node.class][:elements]
351
- node.element_order.each do |entry|
352
- next unless entry.node_type == :element
353
-
354
- attr = mapping[entry.name.to_s]
355
- next unless attr
356
-
357
- value = node.public_send(attr)
358
- value = value.first if value.is_a?(Array)
359
- return value if value && value.class.name.split("::").last == demodulized_name
360
- end
361
- nil
362
- end
363
-
364
- def label(node)
365
- return "" if label_suppressed?(node)
366
-
367
- render_element("span", render_inline(node), css: "label")
368
- end
369
-
370
- def title(node)
371
- return render_element("em", render_inline(node)) if @in_std
372
-
373
- level = (1 + @depth).clamp(2, 4)
374
- anchor = if @current_section_id
375
- %(<a class="h-anchor" href="##{@current_section_id}" aria-label="Link to this section">§</a>)
376
- else
377
- ""
378
- end
379
- # The section number captured from its <label> prefixes the
380
- # heading ("1 Scope"); consumed so nested content never
381
- # inherits it.
382
- prefix = ""
383
- if @current_label && !@current_label.empty?
384
- prefix = %(<span class="sec-label">#{escape(@current_label)}</span> )
385
- @current_label = nil
386
- end
387
- render_element("h#{level}", prefix + render_inline(node) + anchor)
388
- end
389
-
390
- def paragraph(node) = render_element("p", render_inline(node), id: node.id)
391
-
392
- # A list whose items carry explicit <label> markers from the
393
- # source renders without HTML bullets — the label IS the
394
- # marker (mn-labeled-list); otherwise the browser's bullets
395
- # would duplicate the source's markers.
396
- def list(node)
397
- tag = node.list_type == "order" ? "ol" : "ul"
398
- css = labeled_list?(node) ? "mn-labeled-list" : nil
399
- render_element(tag, render_children(node), css: css)
400
- end
401
-
402
- def labeled_list?(node)
403
- return false unless node.class.attributes.key?(:list_item)
404
-
405
- Array(node.list_item).any? { |item| own_label(item) }
406
- end
407
-
408
- def list_item(node)
409
- label_node = own_label(node)
410
- suppress_label(label_node) if label_node
411
- inner = render_children(node)
412
- if label_node
413
- marker = render_element("span", escape(plain_text(label_node).strip), css: "li-label")
414
- inner = marker + inner
415
- end
416
- render_element("li", inner)
417
- end
418
-
419
- def def_list(node) = render_element("dl", render_children(node))
420
-
421
- def term(node) = render_element("dt", render_element("p", render_children(node)))
422
-
423
- def def_item(node) = render_element("dd", render_children(node))
424
-
425
- # Table number + caption render as a caption band above the
426
- # table; the raw label/caption children are skipped so they
427
- # don't repeat inside the box.
428
- def table_wrap(node)
429
- inner = render_children(node, skip: %w[label caption])
430
- render_element("div", table_caption_line(node) + inner, css: "table-wrap")
431
- end
432
-
433
- def table_caption_line(node)
434
- label_text = node.class.attributes.key?(:label) ? node.label.to_s.strip : ""
435
- caption_node = node.caption if node.class.attributes.key?(:caption)
436
- title_node = caption_node.title if caption_node && caption_node.class.attributes.key?(:title)
437
- title_html = title_node ? render_inline(title_node) : ""
438
- return "" if label_text.empty? && title_html.empty?
439
-
440
- parts = []
441
- parts << %(<span class="tc-label">#{escape(label_text)}</span>) unless label_text.empty?
442
- parts << title_html unless title_html.empty?
443
- # NB: class is tbl-caption — "table-caption" collides with
444
- # Tailwind's own display utility (display: table-caption).
445
- %(<p class="tbl-caption">#{parts.join('<span class="tc-delim"> — </span>')}</p>)
446
- end
447
-
448
- # Render the table, then append a <tfoot> with one row per
449
- # footnote encountered in any cell. MN renders table-cell
450
- # footnotes this way (one <td> per footnote in <tfoot>), so
451
- # the body text appears as a matchable cell instead of being
452
- # deferred to end-of-document like paragraph footnotes.
453
- def table(node)
454
- @table_fns ||= []
455
- @table_fns.push([])
456
- body = render_children(node)
457
- collected = @table_fns.pop
458
- parts = [body]
459
- unless collected.empty?
460
- tfoot_cells = collected.map do |fn_body|
461
- cell = render_element("p", escape(fn_body))
462
- inner_div = %(<div class="TableFootnote">#{cell}</div>)
463
- %(<td colspan="4">#{inner_div}</td>)
464
- end.join
465
- parts << "<tfoot><tr>#{tfoot_cells}</tr></tfoot>"
466
- end
467
- render_element("table", parts.join)
468
- end
469
-
470
- def thead(node) = render_element("thead", render_children(node))
471
-
472
- def tbody(node) = render_element("tbody", render_children(node))
473
-
474
- def tr(node) = render_element("tr", render_children(node))
475
-
476
- def td(node) = render_element("td", render_children(node))
477
-
478
- def th(node) = render_element("th", render_children(node))
479
-
480
- def figure(node) = render_element("figure", render_children(node))
481
-
482
- def caption(node) = render_element("figcaption", render_children(node))
483
-
484
- def graphic(node)
485
- # NISO STS carries a graphic's accessibility text as the
486
- # <alt-text> child element (access.class); the element has
487
- # no @alttext attribute (metanorma-iso#1793).
488
- alt = Array(node.alt_text)
489
- .flat_map { |a| Array(a.content) }
490
- .compact.join(" ")
491
- render_liquid("_img.html.liquid", {
492
- "src" => node.href.to_s,
493
- "alt" => alt,
494
- })
495
- end
496
-
497
- def disp_formula(node) = render_element("div", render_inline(node), css: "formula")
498
-
499
- def inline_formula(node) = render_element("span", render_inline(node), css: "formula")
500
-
501
- def preformat(node) = render_element("pre", render_inline(node))
502
-
503
- # Notes carrying their own label (term notes, "Note 1 to
504
- # entry: …") render the label inside the first paragraph, the
505
- # way Metanorma marks up term notes; plain notes get the kind
506
- # label ("NOTE") — also injected inside the first <p> so it
507
- # appears in the same text node MN produces for parity.
508
- def note(node)
509
- label_node = own_label(node)
510
- suppress_label(label_node) if label_node
511
- inner = render_children(node)
512
- label_text = label_node ? plain_text(label_node).strip : "NOTE"
513
- label_html = %(<span class="note-label">#{escape(label_text)}</span> )
514
- inner = inner.sub(/<p(?:\s[^>]*)?>/, "\\0#{label_html}")
515
- render_element("div", inner, css: "note")
516
- end
517
-
518
- def example(node)
519
- label_node = own_label(node)
520
- suppress_label(label_node) if label_node
521
- inner = render_children(node)
522
- if label_node
523
- label_html = %(<span class="example-label">#{escape(plain_text(label_node).strip)}</span> )
524
- inner = inner.sub(/<p(?:\s[^>]*)?>/, "\\0#{label_html}")
525
- else
526
- inner = kind_label("EXAMPLE") + inner
527
- end
528
- render_element("div", inner, css: "example")
529
- end
530
-
531
- def kind_label(text)
532
- %(<span class="note-label">#{text}</span> )
533
- end
534
-
535
- def quote(node) = render_element("blockquote", render_children(node))
536
-
537
- # A ref-list with a title (the back-matter bibliography) gets a
538
- # TOC entry and an anchor; an untitled one (e.g. the normative
539
- # references list nested in its numbered section) stays
540
- # anonymous — the enclosing section carries the TOC entry.
541
- def ref_list(node)
542
- title_node = find_child(node, "Title")
543
- title_text = title_node ? plain_text(title_node).gsub(/\s+/, " ").strip : nil
544
- sec_id = title_text ? title_text.downcase.gsub(/[^a-z0-9]+/, "-").gsub(/\A-|-\z/, "") : nil
545
- sec_id = nil if sec_id && sec_id.empty?
546
- register_toc_entry(id: sec_id, title: title_text, depth: @depth) if sec_id
547
- previous_section_id = @current_section_id
548
- @current_section_id = sec_id
549
- render_element("div", render_children(node), id: sec_id, css: "ref-list")
550
- ensure
551
- @current_section_id = previous_section_id
552
- end
553
-
554
- # A ref with a structured <std> renders label + std (std-ref
555
- # and title); the plain mixed-citation is skipped to avoid
556
- # repeating the same reference three times. The label sits
557
- # inside the body <p> — a reference reads "[1] Citation…".
558
- def ref(node)
559
- # Prefer the structured <std> child (std-ref + title joined
560
- # by ", " — the NISO STS citation idiom) when present; it
561
- # carries the same text as mixed-citation without duplication.
562
- std_node = find_model(node, "ReferenceStandard")
563
- content = std_node ? render_node(std_node) : render_children(node, skip: %w[label])
564
- body = render_element("p", ref_label(node) + content, css: "ref-body")
565
- render_element("div", body, css: "ref")
566
- end
567
-
568
- def ref_label(node)
569
- label_node = find_model(node, "Label")
570
- return "" unless label_node
571
-
572
- render_element("span", render_inline(label_node), css: "label") + " "
573
- end
574
-
575
- # First typed model in the tree whose demodulized class name
576
- # equals +demodulized+, depth-first.
577
- def find_model(node, demodulized)
578
- found = nil
579
- walk_models(node) do |model|
580
- found ||= model if model.class.name.split("::").last == demodulized
581
- end
582
- found
583
- end
584
-
585
- def citation(node) = render_element("span", render_inline(node), css: "citation")
586
-
587
- # <std> inside a bibliography ref: keep everything inline —
588
- # titles become <em>, never headings. std-ref and title join
589
- # with the NISO STS citation idiom ", ". NisoSts stores
590
- # title as a bare String on ReferenceStandard (not as a
591
- # separate Title model), so we read it via #title and escape.
592
- def std(node)
593
- @in_std = true
594
- parts = []
595
- ref_node = find_model(node, "StandardRef")
596
- parts << render_element("span", render_inline(ref_node), css: "std-ref") if ref_node
597
- title_str = node.title if node.class.attributes.key?(:title)
598
- parts << render_element("em", escape(title_str.to_s)) if title_str && !title_str.to_s.empty?
599
- render_element("span", parts.join(", "), css: "std")
600
- ensure
601
- @in_std = false
602
- end
603
-
604
- def std_ident(node) = render_element("span", render_inline(node), css: "std-ident")
605
-
606
- # <originator> inside std-ref: separated, quieter.
607
- def originator(node) = render_element("span", render_inline(node), css: "originator")
608
-
609
- def doc_id(node) = render_element("span", render_inline(node), css: "doc-id")
610
-
611
- def copyright(node) = render_element("span", render_inline(node), css: "copyright")
612
-
613
- # Fn inside a paragraph renders as an inline <sup> marker only
614
- # (the body paragraphs are already rendered as the fn's
615
- # children — but block-level <div> inside <p> breaks the
616
- # paragraph in HTML parsers, so we keep just the marker when
617
- # Fn inside a paragraph: render inline <sup> marker only.
618
- # The fn body paragraphs are collected in @deferred_footnotes
619
- # and rendered at the end of the document so they become
620
- # matchable <p> elements (block <p> inside inline context
621
- # breaks HTML parsers). Fn inside a table cell routes the
622
- # body to the enclosing table's <tfoot> instead (see #table),
623
- # matching MN's table-footnote layout and avoiding duplicate
624
- # rendering at end of document.
625
- def fn(node)
626
- label_node = own_label(node)
627
- suppress_label(label_node) if label_node
628
- label_text = label_node ? plain_text(label_node).strip : ""
629
- display_label = label_text.match?(/\A\d+\z/) ? "#{label_text})" : label_text
630
- # NisoSts::Fn stores paragraphs in #paragraph; TbxIsoTml::Fn
631
- # stores them in #p. Accept either so the renderer handles
632
- # footnotes from paragraphs and table cells uniformly.
633
- fn_paragraphs = if node.class.attributes.key?(:paragraph)
634
- Array(node.paragraph)
635
- elsif node.class.attributes.key?(:p)
636
- Array(node.p)
637
- else
638
- []
639
- end
640
- body_parts = fn_paragraphs.map { |para| plain_text(para) }
641
- body_text = body_parts.join(" ")
642
- unless body_text.empty?
643
- if @table_fns&.last
644
- @table_fns.last.push(body_text)
645
- else
646
- @deferred_footnotes << { "label" => display_label, "body" => body_text }
647
- end
648
- end
649
- render_element("sup", escape(display_label), css: "fn-label")
650
- end
651
-
652
- def ext_link(node)
653
- render_link(href: node.href, text: inline_or_content(node))
654
- end
655
-
656
- def xref(node)
657
- rid = node.rid.to_s
658
- text = render_inline(node).strip
659
- text = "[#{rid}]" if text.empty?
660
-
661
- render_link(href: "##{rid}", text: text)
662
- end
663
-
664
- def brk(_node) = "<br/>"
665
-
666
- def skip(_node) = ""
667
-
668
- def meta(node)
669
- return "" if @assemble
670
-
671
- title = find_text(node, ["TitleWrap", "Title"])
672
- docid = find_text(node, META_ID_NAMES)
673
- return "" if title.empty? && docid.empty?
674
-
675
- render_liquid("_meta_header.html.liquid", {
676
- "docid" => docid, "title" => title
677
- })
678
- end
679
-
680
- def render_inline_tag(node)
681
- tag = INLINE_TAGS[node.class]
682
- inner = render_inline(node)
683
- return render_element("span", inner, css: "small-caps") if tag == "span"
684
-
685
- render_element(tag, inner)
686
- end
687
-
688
- # --------------------------------------------------------------
689
- # Walking (document-order traversal of the typed model)
690
- # --------------------------------------------------------------
691
-
692
- # Children of a non-mixed container in document order.
693
- # Repeated elements of a collection appear as one element_order
694
- # entry PER ITEM, so collection items are consumed one entry at
695
- # a time (per-attribute index counters). +skip+ lists element
696
- # names the caller renders itself (e.g. table-wrap's label and
697
- # caption, which become the caption band).
698
- def render_children(node, skip: [])
699
- return render_inline(node) if mixed_model?(node)
700
-
701
- mapping = @mapping_cache[node.class][:elements]
702
- indices = Hash.new(0)
703
- # Parsed models of non-mixed elements carry no element_order
704
- # (lutaml-model fills it only under mixed_content); fall back
705
- # to the mapped elements in declaration order.
706
- entries = node.element_order || declared_element_order(node, mapping)
707
- entries.filter_map do |entry|
708
- case entry.node_type
709
- when :text
710
- escape(entry.text_content.to_s)
711
- when :element
712
- next if skip.include?(entry.name.to_s)
713
-
714
- attr = mapping[entry.name.to_s]
715
- next unless attr
716
-
717
- item = collection_item_at(node, attr, indices)
718
- next unless item
719
-
720
- rendered = render_node(item)
721
- rendered unless rendered.empty?
722
- end
723
- end.join
724
- end
725
-
726
- # Mixed-content (inline) walk; non-mixed models route back to
727
- # the ordered walk.
728
- def render_inline(node)
729
- return render_children(node) unless mixed_model?(node)
730
-
731
- node.each_mixed_content.filter_map do |child|
732
- rendered = child.is_a?(String) ? escape(child) : render_node(child)
733
- rendered unless rendered.empty?
734
- end.join
735
- end
736
-
737
- def mixed_model?(node)
738
- @mapping_cache[node.class][:mixed]
739
- end
740
-
741
- # element name → attribute name + mixed-content flag, per model
742
- # class (cached).
743
- def build_mapping(klass)
744
- return { elements: {}, mixed: false } unless klass.respond_to?(:mappings_for)
745
-
746
- instance = klass.allocate
747
- xml_mapping = klass.mappings_for(:xml, instance.lutaml_register)
748
- elements = {}
749
- xml_mapping.mapping_elements_hash.each_value do |rule_or_array|
750
- Array(rule_or_array).each { |rule| elements[rule.name.to_s] = rule.to }
751
- end
752
- { elements: elements, mixed: xml_mapping.mixed_content? }
753
- rescue Lutaml::Model::Error
754
- { elements: {}, mixed: false }
755
- end
756
-
757
- def collection_item_at(node, attr, indices)
758
- value = node.public_send(attr)
759
- return value unless value.is_a?(Array)
760
-
761
- item = value[indices[attr]]
762
- indices[attr] += 1
763
- item
764
- end
765
-
766
- # --------------------------------------------------------------
767
- # Document assembly (full page)
768
- # --------------------------------------------------------------
769
-
770
- def assemble_document(body, model)
771
- meta = meta_info(model)
772
- footnotes = deferred_footnotes_html
773
- title_p = body_title_paragraph(meta)
774
- render_liquid("document.html.liquid", {
775
- "lang" => "en",
776
- "title" => meta[:title],
777
- "css" => stylesheet,
778
- "logo_light" => logo_svg_light,
779
- "logo_dark" => logo_svg_dark,
780
- "docid" => meta[:docid],
781
- "toc" => toc_html,
782
- "hero" => hero_html(meta),
783
- "content" => title_p + body + footnotes,
784
- "footer" => footer_html(meta),
785
- "js" => javascript,
786
- })
787
- end
788
-
789
- # MN renders the document title as a <p class="zzSTDTitle1">
790
- # at the start of the body. Emitting a matching paragraph
791
- # keeps the title in the parity check's paragraph set.
792
- def body_title_paragraph(meta)
793
- return "" if meta[:title].to_s.empty?
794
-
795
- render_element("p", escape(meta[:title]), css: "doc-title")
796
- end
797
-
798
- # Deferred footnotes collected from fn() calls during rendering.
799
- # Each fn body becomes a matchable <p> at the end of the document.
800
- def deferred_footnotes_html
801
- return "" if @deferred_footnotes.empty?
802
-
803
- parts = @deferred_footnotes.map do |fn|
804
- render_element("p", escape(fn["body"]), css: "footnote")
805
- end
806
- parts.join("\n")
807
- end
808
-
809
- def hero_html(meta)
810
- return "" if meta[:title].empty?
811
-
812
- chips = []
813
- chips << meta[:docid] unless meta[:docid].empty?
814
- chips << "Series #{meta[:series]}" unless meta[:series].empty?
815
- chips << meta[:year] unless meta[:year].empty?
816
- chips << "EN"
817
- render_liquid("_hero.html.liquid", {
818
- "eyebrow" => @publisher_name || PUBLISHER_NAME,
819
- "title" => meta[:title],
820
- "chips" => chips,
821
- })
822
- end
823
-
824
- def footer_html(meta)
825
- render_liquid("_footer.html.liquid", {
826
- "copyright" => meta[:copyright],
827
- "holder" => meta[:holder],
828
- "year" => meta[:year],
829
- "docid" => meta[:docid],
830
- "series" => meta[:series],
831
- "publisher_name" => @publisher_name || PUBLISHER_NAME,
832
- "publisher_address" => @publisher_address || PUBLISHER_ADDRESS,
833
- "license" => @license_text,
834
- "version" => ::Metanorma::Iso::VERSION,
835
- "mn_icon_light" => metanorma_icon_light,
836
- "mn_icon_dark" => metanorma_icon_dark,
837
- })
838
- end
839
-
840
- # Metanorma icon (aequitate verum) in light/dark variants,
841
- # from metanorma.org.
842
- def metanorma_icon_light
843
- @mn_icon_light ||= File.read(File.join(assets_dir, "metanorma-icon-light.svg"))
844
- .sub(/\A<\?xml[^?]*\?>\s*/, "")
845
- end
846
-
847
- def metanorma_icon_dark
848
- @mn_icon_dark ||= File.read(File.join(assets_dir, "metanorma-icon-dark.svg"))
849
- .sub(/\A<\?xml[^?]*\?>\s*/, "")
850
- end
851
-
852
- # Flat TOC list with depth classes (indented via CSS); consumed
853
- # by the scroll-spy script in the page.
854
- def toc_html
855
- return "" if @toc.empty?
856
-
857
- items = @toc.map do |entry|
858
- %(<li><a class="toc-link toc-link-d#{entry[:depth]}" href="##{entry[:id]}">#{escape(entry[:title])}</a></li>)
859
- end.join
860
- %(<nav id="toc" class="toc-panel sticky top-[68px] self-start max-h-[calc(100vh-5rem)] overflow-y-auto py-4 pr-3 text-sm [scrollbar-width:thin]" aria-label="Contents"><h2 class="toc-heading m-0 mb-2 ml-4 text-xs font-bold tracking-[0.14em] uppercase text-ink-faint">Contents</h2><ol class="toc-list list-none m-0 p-0">#{items}</ol></nav>)
861
- end
862
-
863
- def meta_info(model)
864
- meta_node = find_meta_node(model)
865
- return { title: "", docid: "" } unless meta_node
866
-
867
- { title: title_text(meta_node),
868
- docid: doc_id(meta_node),
869
- year: find_text(meta_node, ["CopyrightYear", "PubDate", "Year"]),
870
- series: doc_series(meta_node),
871
- copyright: find_text(meta_node, ["CopyrightStatement"]),
872
- holder: find_text(meta_node, ["CopyrightHolder"]) }
873
- end
874
-
875
- # NisoSts TitleWrap stores the title parts as bare Strings
876
- # (main:, intro:, compl:, full:) rather than as typed Title
877
- # model children, so plain_text on the wrap returns "".
878
- # Prefer main, then fall back to compl / full / intro, then
879
- # fall back to the generic find_text walk for older layouts.
880
- def title_text(meta_node)
881
- wrap = find_model(meta_node, "TitleWrap")
882
- return find_text(meta_node, ["Title"]) unless wrap
883
-
884
- %i[main compl full intro].each do |attr|
885
- next unless wrap.class.attributes.key?(attr)
886
- val = wrap.public_send(attr)
887
- val = val.text if val.respond_to?(:text) && !val.text.nil?
888
- return val.to_s.strip unless val.to_s.strip.empty?
889
- end
890
- find_text(meta_node, ["Title"])
891
- end
892
-
893
- # Display form of the document identifier, rebuilt from the
894
- # decomposed <std-ident> parts ("OIML" + "x" + "999" →
895
- # "OIML X 999"), dated with the copyright year
896
- # ("OIML X 999:2026"). Falls back to raw text extraction when
897
- # the std-ident carries no originator (legacy/other layouts).
898
- def doc_id(meta_node)
899
- std_ident = find_model(meta_node, "StandardIdentification")
900
- return find_text(meta_node, META_ID_NAMES) unless std_ident
901
-
902
- originator = find_text(std_ident, %w[Originator])
903
- return find_text(meta_node, META_ID_NAMES) if originator.empty?
904
-
905
- parts = [originator,
906
- find_text(std_ident, %w[DocType]).upcase,
907
- find_text(std_ident, %w[DocNumber])].reject(&:empty?)
908
- id = parts.join(" ")
909
- part_number = find_text(std_ident, %w[PartNumber])
910
- id += "-#{part_number}" unless part_number.empty?
911
- year = find_text(meta_node, %w[CopyrightYear])
912
- id += ":#{year}" if !id.empty? && year.match?(/\A\d{4}\z/)
913
- id
914
- end
915
-
916
- # The oiml-doc-series letter recorded in
917
- # <custom-meta name="oiml-doc-series"> (X 999 §4).
918
- def doc_series(meta_node)
919
- series = ""
920
- walk_models(meta_node) do |model|
921
- next unless model.instance_of?(::Sts::NisoSts::CustomMeta)
922
-
923
- name = model.class.attributes.key?(:meta_name) ? model.meta_name.to_s : ""
924
- value = model.class.attributes.key?(:meta_value) ? model.meta_value.to_s : ""
925
- series = value if name == "oiml-doc-series" && !value.empty?
926
- end
927
- series
928
- end
929
-
930
- # Yields every typed model in the tree, depth-first.
931
- def walk_models(node, &block)
932
- return unless node.is_a?(Lutaml::Model::Serializable)
933
-
934
- yield node
935
- node.class.attributes.each_value do |attr_def|
936
- Array(node.public_send(attr_def.name)).compact.each do |child|
937
- walk_models(child, &block) if child.is_a?(Lutaml::Model::Serializable)
938
- end
939
- end
940
- end
941
-
942
- # First metadata block found under the document front matter.
943
- # Recognises any subclass of the NisoSts metadata containers
944
- # (MetadataIso → <iso-meta>, MetadataStd → <std-meta>) via
945
- # is_a?, so OIML (or any other future subclass) is picked up
946
- # without extending a hard-coded class-name list.
947
- def find_meta_node(node)
948
- return node if meta_node?(node)
949
- return nil unless node.is_a?(Lutaml::Model::Serializable)
950
-
951
- node.class.attributes.each_value do |attr_def|
952
- value = node.public_send(attr_def.name)
953
- Array(value).compact.each do |child|
954
- next unless child.is_a?(Lutaml::Model::Serializable)
955
-
956
- found = find_meta_node(child)
957
- return found if found
958
- end
959
- end
960
- nil
961
- end
962
-
963
- def meta_node?(node)
964
- node.is_a?(::Sts::NisoSts::MetadataIso) ||
965
- node.is_a?(::Sts::NisoSts::MetadataStd)
966
- end
967
-
968
- def stylesheet
969
- @stylesheet ||= File.read(File.join(assets_dir, "theme.css"))
970
- end
971
-
972
- # Light- and dark-mode logo variants (theme picks visibility).
973
- # Uses the Metanorma brand icons for the ISO flavor.
974
- def logo_svg_light
975
- @logo_svg_light ||= read_asset("metanorma-icon-light.svg")
976
- end
977
-
978
- def logo_svg_dark
979
- @logo_svg_dark ||= read_asset("metanorma-icon-dark.svg")
980
- end
981
-
982
- def read_asset(name)
983
- File.read(File.join(assets_dir, name))
984
- .sub(/\A<\?xml[^?]*\?>\s*/, "")
985
- end
986
-
987
- # Page behaviour: scroll-spy TOC, mobile TOC drawer, back-to-top,
988
- # heading-anchor copy. Vanilla JS, no dependencies.
989
- def javascript
990
- @javascript ||= File.read(File.join(assets_dir, "page.js"))
991
- end
992
-
993
- # --------------------------------------------------------------
994
- # Text extraction
995
- # --------------------------------------------------------------
996
-
997
- # First non-empty plain text under the node whose demodulized
998
- # class name is in +names+.
999
- def find_text(node, names)
1000
- if names.include?(node.class.name.split("::").last)
1001
- text = plain_text(node).gsub(/\s+/, " ").strip
1002
- return text unless text.empty?
1003
- end
1004
-
1005
- node.class.attributes.each_value do |attr_def|
1006
- value = node.public_send(attr_def.name)
1007
- Array(value).compact.each do |child|
1008
- next unless child.is_a?(Lutaml::Model::Serializable)
1009
-
1010
- found = find_text(child, names)
1011
- return found unless found.empty?
1012
- end
1013
- end
1014
- ""
1015
- end
1016
-
1017
- def plain_text(node)
1018
- return node.to_s unless node.is_a?(Lutaml::Model::Serializable)
1019
-
1020
- children = mixed_model?(node) ? node.each_mixed_content.to_a : ordered_content_parts(node)
1021
- children.map do |child|
1022
- child.is_a?(String) ? child : plain_text(child)
1023
- end.join
1024
- end
1025
-
1026
- def ordered_content_parts(node)
1027
- mapping = @mapping_cache[node.class][:elements]
1028
- indices = Hash.new(0)
1029
- entries = node.element_order || declared_element_order(node, mapping)
1030
- entries.filter_map do |entry|
1031
- if entry.node_type == :text
1032
- entry.text_content.to_s
1033
- else
1034
- attr = mapping[entry.name.to_s]
1035
- next unless attr
1036
-
1037
- collection_item_at(node, attr, indices)
1038
- end
1039
- end
1040
- end
1041
-
1042
- # Declaration-order entries for models whose parse produced no
1043
- # element_order: one entry per mapped element name.
1044
- DeclaredEntry = Struct.new(:node_type, :name, :text_content)
1045
-
1046
- def declared_element_order(node, mapping)
1047
- return [] if node.nil?
1048
-
1049
- mapping.keys.map { |name| DeclaredEntry.new(:element, name, nil) }
1050
- end
1051
-
1052
- def inline_or_content(node)
1053
- rendered = render_inline(node)
1054
- rendered.empty? ? node.content.to_s : rendered
1055
- end
1056
-
1057
- # --------------------------------------------------------------
1058
- # Liquid emitters
1059
- # --------------------------------------------------------------
1060
-
1061
- def render_element(tag, content, id: nil, css: nil)
1062
- render_liquid("_element.html.liquid", {
1063
- "tag" => tag, "id" => id,
1064
- "css" => css, "content" => content
1065
- })
1066
- end
1067
-
1068
- def render_link(href:, text:)
1069
- render_liquid("_link.html.liquid", {
1070
- "href" => href.to_s, "text" => text
1071
- })
1072
- end
1073
-
1074
- def render_liquid(template_name, assigns)
1075
- template = @template_cache[template_name] ||= begin
1076
- path = File.join(templates_dir, template_name)
1077
- # chomp: template files must not contribute their own
1078
- # trailing newline into the output (it surfaces as a
1079
- # visible space between inline elements)
1080
- Liquid::Template.parse(File.read(path).chomp, environment: @liquid_env)
1081
- end
1082
- template.render(assigns)
1083
- end
1084
-
1085
- def escape(text)
1086
- CGI.escapeHTML(text.to_s)
13
+ class Ruby < Metanorma::Document::Sts::HtmlRenderer::Ruby
14
+ def initialize(**opts)
15
+ opts[:profile] ||= Metanorma::Iso::Sts::Profile.instance
16
+ super
1087
17
  end
1088
18
  end
1089
19
  end