Class: Metanorma::Html::BaseRenderer
- Inherits:
-
Object
- Object
- Metanorma::Html::BaseRenderer
- Defined in:
- lib/metanorma/html/base_renderer.rb
Overview
Renders BasicDocument components to HTML. Subclassed by StandardRenderer and flavor-specific renderers. Owns the full HTML document generation pipeline: body content, header, footer, ToC sidebar, CSS (via Theme), and JavaScript.
Direct Known Subclasses
Defined Under Namespace
Classes: RendererContext
Constant Summary collapse
- LOGO_DIR =
File.("../../../data/logos", __dir__)
- SPAN_ROLE_CLASSES =
HTML-specific class names for inline spans, keyed by the XML span role. The XML class_attr is INPUT only — we never emit it in HTML.
{ "boldtitle" => "title-text", "nonboldtitle" => "subtitle-text", "citeapp" => "xref-app", "citefig" => "xref-fig", "citesec" => "xref-section", "citetbl" => "xref-table", "fmt-autonum-delim" => "number-delim", "fmt-caption-label" => "caption-label", "fmt-caption-delim" => "caption-delim", "fmt-element-name" => "element-label", "fmt-comma" => "comma", "fmt-conn" => "connector", "fmt-label-delim" => "label-delim", "fmt-obligation" => "obligation-text", "fmt-xref-container" => "xref-container", "fmt-xref-label" => "xref-label", "std_publisher" => "ref-publisher", "stdpublisher" => "ref-publisher-name", "stddocNumber" => "ref-doc-number", "stddocTitle" => "ref-title", "stddocPartNumber" => "ref-part-number", "stdyear" => "ref-year", "date" => "date", "smallcap" => "small-caps", }.freeze
- METANORMA_LOGO =
"metanorma-logo.svg"- TEMPLATE_CACHE =
— Document Assembly —
{}
- TEMPLATE_CACHE_MUTEX =
Mutex.new
- BLOCK_CHILDREN =
Standard child set for container blocks (example, note, etc.)
{ 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 =
Minimal child set for simple blocks (admonition, note with content only)
{ paragraphs: :render_paragraph, }.freeze
- NOTE_CHILDREN =
Note-style child set (paragraphs + lists + dl, no nested blocks)
{ paragraphs: :render_paragraph, ul: :render_unordered_list, ol: :render_ordered_list, dl: :render_definition_list, quote: :render_quote, }.freeze
- BLOCK_TYPES =
{ Metanorma::Document::Components::Paragraphs::ParagraphBlock => true, Metanorma::Document::Components::Tables::TableBlock => true, Metanorma::Document::Components::Lists::UnorderedList => true, Metanorma::Document::Components::Lists::OrderedList => true, Metanorma::Document::Components::Lists::DefinitionList => true, Metanorma::Document::Components::AncillaryBlocks::FigureBlock => true, Metanorma::Document::Components::Blocks::NoteBlock => true, Metanorma::Document::Components::AncillaryBlocks::ExampleBlock => true, Metanorma::Document::Components::AncillaryBlocks::SourcecodeBlock => true, Metanorma::Document::Components::AncillaryBlocks::FormulaBlock => true, Metanorma::Document::Components::MultiParagraph::QuoteBlock => true, Metanorma::Document::Components::MultiParagraph::AdmonitionBlock => true, Metanorma::Document::Components::Sections::HierarchicalSection => true, Metanorma::Document::Components::Sections::BasicSection => true, Metanorma::Document::Components::Sections::ContentSection => true, }.freeze
Instance Attribute Summary collapse
-
#footnote_collector ⇒ Object
readonly
Returns the value of attribute footnote_collector.
-
#index_term_collector ⇒ Object
readonly
Returns the value of attribute index_term_collector.
Class Method Summary collapse
- .inline_registry ⇒ Object
- .register_inline_render(type_class, method_name) ⇒ Object
- .register_render(type_class, method_name) ⇒ Object
- .render_registry ⇒ Object
Instance Method Summary collapse
- #alignment_style(alignment) ⇒ Object
- #assemble_document(body) ⇒ Object
- #block_element?(obj) ⇒ Boolean
- #build_footer ⇒ Object
-
#build_header ⇒ Object
— Header and Footer —.
- #build_publisher_logos ⇒ Object
-
#build_scripts ⇒ Object
— Scripts —.
-
#build_semx_skip_set(node) ⇒ Object
In presentation XML, semantic elements are followed by <semx> wrappers or <fmt-*> display elements.
- #build_styles ⇒ Object
-
#build_toc_html(entries) ⇒ Object
— ToC generation —.
- #capture_output ⇒ Object
- #check_presentation_markers(node) ⇒ Object
- #collect_index_term(element) ⇒ Object
-
#collect_ordered_children(section) ⇒ Object
Collect all renderable children from a node in document order, sorted by displayorder when available.
- #deduplicate_semx_label(source_node, semx_node) ⇒ Object
- #deduplicate_semx_text(semx_text, output) ⇒ Object
- #element_attrs(**attrs) ⇒ Object
- #escape_html(text) ⇒ Object
- #extract_block_label(block, default) ⇒ Object
- #extract_plain_text(node) ⇒ Object
- #extract_primary_doc_id ⇒ Object
- #extract_text_value(val) ⇒ Object
- #extract_title_text(titles) ⇒ Object
- #flavor_css_module ⇒ Object
- #flavor_font_url ⇒ Object
- #flavor_js_module ⇒ Object
- #flavor_publisher_name ⇒ Object
- #flavor_publishers(_doc_id) ⇒ Object
-
#generate_full_document(document) ⇒ Object
Generate a complete HTML document from a presentation XML document.
- #header_title_text ⇒ Object
- #html_class_for_span(xml_class) ⇒ Object
- #html_title ⇒ Object
-
#initialize ⇒ BaseRenderer
constructor
A new instance of BaseRenderer.
-
#language ⇒ Object
— Metadata extraction —.
- #load_logo_svg(filename, height: 32) ⇒ Object
- #lookup_dispatch(type_class, registry_method) ⇒ Object
-
#publisher_logo_map ⇒ Object
Map of publisher name => logo filename.
- #raw_content_node?(node) ⇒ Boolean
- #register_figure_entry(id:, text:) ⇒ Object
- #register_table_entry(id:, text:) ⇒ Object
-
#register_toc_entry(id:, level:, text:) ⇒ Object
— CSS loader —.
-
#render(node) ⇒ Object
Dispatch to the appropriate render method via type registry.
- #render_admonition(admonition, **_opts) ⇒ Object
- #render_asciimath(el) ⇒ Object
- #render_audio(audio) ⇒ Object
-
#render_basic_section(section, level: 1, **_opts) ⇒ Object
— Section rendering —.
-
#render_block_children(model, children:) ⇒ Object
Renders the typed child collections of a block element (paragraphs, lists, nested blocks) in a standard order.
- #render_bookmark(bookmark, **_opts) ⇒ Object
- #render_br ⇒ Object
-
#render_cell_content(cell) ⇒ Object
Table cells can contain both block-level content (p, ul, ol, dl) and inline content (text, em, strong, etc.) in document order.
- #render_comma ⇒ Object
- #render_concept(concept) ⇒ Object
- #render_content_section(section, level: 1, **_opts) ⇒ Object
- #render_definition_list(dl, **_opts) ⇒ Object
-
#render_em(el) ⇒ Object
Inline adapter methods for registry dispatch.
- #render_eref(eref) ⇒ Object
- #render_example(example, **_opts) ⇒ Object
- #render_figure(figure, **_opts) ⇒ Object
-
#render_fmt_stem(fmt_stem) ⇒ Object
— Stem/math rendering —.
- #render_fmt_xref(el) ⇒ Object
- #render_fn(fn) ⇒ Object
- #render_fn_inline(el) ⇒ Object
- #render_footnotes_section ⇒ Object
- #render_formula(formula, **_opts) ⇒ Object
- #render_hierarchical_section(section, level: 1, **_opts) ⇒ Object
- #render_image(image) ⇒ Object
- #render_index(el) ⇒ Object
- #render_inline_collections(node) ⇒ Object
-
#render_inline_element(element) ⇒ Object
Dispatch to the appropriate inline render method via type registry.
- #render_inline_tag(tag_name, element, **extra_attrs) ⇒ Object
- #render_link(link) ⇒ Object
- #render_liquid(template_name, assigns) ⇒ Object
- #render_list_item(li) ⇒ Object
- #render_math(el) ⇒ Object
-
#render_mixed_content_in_order(node) ⇒ Object
Render all children of a mixed-content node in document order, dispatching block elements to their render methods.
- #render_mixed_inline(node) ⇒ Object
- #render_noop ⇒ Object
- #render_noop_inline ⇒ Object
- #render_note(note, **_opts) ⇒ Object
- #render_note_inline(el) ⇒ Object
-
#render_ordered_content(section, level = 1) ⇒ Object
Render children of a section in displayorder, skipping title elements.
-
#render_ordered_inline(node) ⇒ Object
Iterate element_order directly, preserving whitespace text nodes that each_mixed_content drops (it skips text where text.strip.empty?).
- #render_ordered_list(ol, **_opts) ⇒ Object
-
#render_paragraph(p, **_opts) ⇒ Object
— Block-level rendering —.
- #render_quote(quote, **_opts) ⇒ Object
-
#render_raw_content(raw_xml) ⇒ Object
Process raw XML content from map_all_content models (e.g. RawParagraph).
- #render_section_content(section, _level) ⇒ Object
- #render_section_title(section, level) ⇒ Object
-
#render_semx_content(element, **_opts) ⇒ Object
Render SemxElement display content only, skipping semantic linkage.
- #render_semx_inline(el) ⇒ Object
- #render_small_caps(el) ⇒ Object
- #render_sourcecode(sc, **_opts) ⇒ Object
- #render_span(el) ⇒ Object
- #render_stem(el) ⇒ Object
- #render_stem_content(stem) ⇒ Object
- #render_strike(el) ⇒ Object
- #render_strong(el) ⇒ Object
- #render_sub(el) ⇒ Object
- #render_sup(el) ⇒ Object
- #render_tab ⇒ Object
- #render_table(table, **_opts) ⇒ Object
- #render_table_cell(cell, force_tag = nil) ⇒ Object
- #render_table_colgroup(colgroup) ⇒ Object
- #render_table_section(section, tag_name) ⇒ Object
- #render_table_section_rows(section) ⇒ Object
- #render_tt(el) ⇒ Object
- #render_underline(el) ⇒ Object
- #render_unordered_list(ul, **_opts) ⇒ Object
- #render_video(video) ⇒ Object
- #render_xref(xref) ⇒ Object
- #renderer_context ⇒ Object
- #safe_attr(obj, method_name) ⇒ Object
- #sort_by_displayorder(children) ⇒ Object
- #table_column_count(table) ⇒ Object
-
#tag(name, attrs_str) ⇒ Object
— Helper methods —.
-
#theme ⇒ Object
— Flavor configuration hooks (override in subclasses) —.
- #to_html ⇒ Object
- #toc_entries ⇒ Object
-
#validate_presentation_xml! ⇒ Object
— Validation —.
-
#walk_ordered(node, allow_filter: nil) ⇒ Object
Walk element_order in document order, resolving each element to its Ruby object via the XML mapping, and yielding (element_order_entry, resolved_object) to the given block.
Constructor Details
#initialize ⇒ BaseRenderer
Returns a new instance of BaseRenderer.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/metanorma/html/base_renderer.rb', line 67 def initialize @output = +"" @toc_entries = [] @figure_entries = [] @table_entries = [] @index_term_collector = Component::IndexTermCollector.new @footnote_collector = Component::FootnoteCollector.new @current_section_id = nil @current_section_number = nil end |
Instance Attribute Details
#footnote_collector ⇒ Object (readonly)
Returns the value of attribute footnote_collector.
410 411 412 |
# File 'lib/metanorma/html/base_renderer.rb', line 410 def footnote_collector @footnote_collector end |
#index_term_collector ⇒ Object (readonly)
Returns the value of attribute index_term_collector.
410 411 412 |
# File 'lib/metanorma/html/base_renderer.rb', line 410 def index_term_collector @index_term_collector end |
Class Method Details
.inline_registry ⇒ Object
58 59 60 |
# File 'lib/metanorma/html/base_renderer.rb', line 58 def inline_registry @inline_registry ||= {} end |
.register_inline_render(type_class, method_name) ⇒ Object
62 63 64 |
# File 'lib/metanorma/html/base_renderer.rb', line 62 def register_inline_render(type_class, method_name) inline_registry[type_class] = method_name end |
.register_render(type_class, method_name) ⇒ Object
54 55 56 |
# File 'lib/metanorma/html/base_renderer.rb', line 54 def register_render(type_class, method_name) render_registry[type_class] = method_name end |
.render_registry ⇒ Object
50 51 52 |
# File 'lib/metanorma/html/base_renderer.rb', line 50 def render_registry @render_registry ||= {} end |
Instance Method Details
#alignment_style(alignment) ⇒ Object
1666 1667 1668 1669 1670 |
# File 'lib/metanorma/html/base_renderer.rb', line 1666 def alignment_style(alignment) return nil if alignment.nil? || alignment.to_s.empty? "text-align: #{alignment}" end |
#assemble_document(body) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/metanorma/html/base_renderer.rb', line 176 def assemble_document(body) toc_html = build_toc_html(@toc_entries) header = build_header = render_liquid("document.html.liquid", { "lang" => language, "title" => html_title, "font_url" => flavor_font_url, "styles" => build_styles, "header" => header, "toc" => toc_html, "body" => body, "footer" => , "scripts" => build_scripts, }) end |
#block_element?(obj) ⇒ Boolean
1618 1619 1620 |
# File 'lib/metanorma/html/base_renderer.rb', line 1618 def block_element?(obj) BLOCK_TYPES[obj.class] || BLOCK_TYPES.any? { |type, _| obj.is_a?(type) } end |
#build_footer ⇒ Object
257 258 259 260 261 262 263 |
# File 'lib/metanorma/html/base_renderer.rb', line 257 def mn_logo = load_logo_svg(METANORMA_LOGO, height: 20) render_liquid("_footer.html.liquid", { "mn_logo" => mn_logo, "generated_at" => Time.now.strftime("%Y-%m-%d %H:%M"), }) end |
#build_header ⇒ Object
— Header and Footer —
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/metanorma/html/base_renderer.rb', line 196 def build_header doc_id = extract_primary_doc_id pub_logos = build_publisher_logos pub_name = flavor_publisher_name display_id = if pub_name && doc_id && !doc_id.start_with?(pub_name) "#{pub_name} #{doc_id}" else doc_id end render_liquid("_header.html.liquid", { "publisher_logos" => pub_logos, "doc_id" => display_id, "doc_title" => header_title_text, }) end |
#build_publisher_logos ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/metanorma/html/base_renderer.rb', line 218 def build_publisher_logos publishers = flavor_publishers(extract_primary_doc_id) logo_map = publisher_logo_map return "" if publishers.empty? && logo_map.empty? # Use flavor-declared publishers; fall back to logo map keys display_pubs = publishers.empty? ? logo_map.keys : publishers display_pubs.filter_map do |pub| filename = logo_map[pub] next unless filename svg = load_logo_svg(filename, height: 26) next unless svg "<span class=\"brand-logo\" aria-label=\"#{pub} logo\">#{svg}</span>" end.join("\n") end |
#build_scripts ⇒ Object
— Scripts —
283 284 285 286 287 |
# File 'lib/metanorma/html/base_renderer.rb', line 283 def build_scripts pipeline = AssetPipeline.new compiled = pipeline.compile_js(flavor_js: flavor_js_module) "<script>\n#{compiled}\n</script>" end |
#build_semx_skip_set(node) ⇒ Object
In presentation XML, semantic elements are followed by <semx> wrappers or <fmt-*> display elements. Skip source elements to avoid duplicates.
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 |
# File 'lib/metanorma/html/base_renderer.rb', line 1095 def build_semx_skip_set(node) skip_after = { "link" => "semx", "xref" => "semx", "eref" => "semx", "stem" => nil, "concept" => "fmt-concept", "refterm" => nil, "renderterm" => nil, "origin" => "semx", } skip = {} node.element_order.each_with_index do |el, i| next unless el.element? next_tag = skip_after[el.name] next unless next_tag next_el = node.element_order[i + 1] if next_tag.nil? skip[i] = true elsif next_el&.element? && next_el.name == next_tag skip[i] = true end end skip end |
#build_styles ⇒ Object
297 298 299 300 301 |
# File 'lib/metanorma/html/base_renderer.rb', line 297 def build_styles pipeline = AssetPipeline.new css = pipeline.compile_css(flavor_css: flavor_css_module) "#{theme.to_css_root}\n#{css}\n#{theme.to_css_extras}" end |
#build_toc_html(entries) ⇒ Object
— ToC generation —
267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/metanorma/html/base_renderer.rb', line 267 def build_toc_html(entries) entry_drops = entries.map { |e| Drops::TocEntryDrop.new(e) } figure_drops = @figure_entries.map { |f| Drops::FigureListEntryDrop.new(f) } table_drops = @table_entries.map { |t| Drops::FigureListEntryDrop.new(t) } has_special_lists = !@figure_entries.empty? || !@table_entries.empty? render_liquid("_toc.html.liquid", { "entries" => entry_drops, "figures" => figure_drops, "tables" => table_drops, "has_special_lists" => has_special_lists, }) end |
#capture_output ⇒ Object
1729 1730 1731 1732 1733 1734 1735 1736 |
# File 'lib/metanorma/html/base_renderer.rb', line 1729 def capture_output old_output = @output @output = +"" yield result = @output @output = old_output result end |
#check_presentation_markers(node) ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/metanorma/html/base_renderer.rb', line 315 def check_presentation_markers(node) return false unless node return false if node.is_a?(String) if node.is_a?(Metanorma::Document::Root) && node.type == "presentation" return true end if node.is_a?(Lutaml::Model::Serializable) node_attrs = node.class.attributes if node_attrs.key?(:fmt_title) && node.fmt_title return true end if node_attrs.key?(:displayorder) && node.displayorder return true end %i[preface sections annex bibliography].each do |attr| next unless node_attrs.key?(attr) val = node.public_send(attr) next unless val Array(val).each { |v| return true if check_presentation_markers(v) } end node.each_mixed_content do |child| next if child.is_a?(String) return true if check_presentation_markers(child) end end false end |
#collect_index_term(element) ⇒ Object
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 |
# File 'lib/metanorma/html/base_renderer.rb', line 1632 def collect_index_term(element) primary = safe_attr(element, :primary) return unless primary && !primary.to_s.strip.empty? @index_term_collector.add( primary: primary.to_s.strip, secondary: safe_attr(element, :secondary)&.to_s&.strip, tertiary: safe_attr(element, :tertiary)&.to_s&.strip, target_id: @current_section_id, target_text: @current_section_number, ) rescue StandardError nil end |
#collect_ordered_children(section) ⇒ Object
Collect all renderable children from a node in document order, sorted by displayorder when available. Uses walk_ordered to traverse element_order, and also gathers typed attributes that may not appear in element_order (e.g. terms, definitions on section models).
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 |
# File 'lib/metanorma/html/base_renderer.rb', line 806 def collect_ordered_children(section) children = [] walk_ordered(section) do |type, obj| next if %i[text tab].include?(type) children << obj end # Gather typed attributes that may not appear in element_order supplementary_attrs = %i[terms definitions] supplementary_attrs.each do |attr| val = safe_attr(section, attr) next if val.nil? Array(val).each do |v| children << v unless children.include?(v) end end children.compact! sort_by_displayorder(children) end |
#deduplicate_semx_label(source_node, semx_node) ⇒ Object
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 |
# File 'lib/metanorma/html/base_renderer.rb', line 1433 def deduplicate_semx_label(source_node, semx_node) first_text = semx_node.children.find do |c| c.text? && !c.text.strip.empty? end return unless first_text semx_prefix = first_text.text[/\A(\s*\S+)/, 1] return unless semx_prefix && !semx_prefix.strip.empty? prev = source_node.previous_sibling return unless prev.is_a?(Nokogiri::XML::Text) label = semx_prefix.strip prev_text = prev.text.rstrip return unless prev_text.end_with?(label) prev.content = prev_text.sub(/#{Regexp.escape(label)}\s*\z/, "") first_text.content = first_text.text.sub( /\A\s*#{Regexp.escape(label)}\s*/, "" ) end |
#deduplicate_semx_text(semx_text, output) ⇒ Object
1372 1373 1374 1375 1376 1377 1378 1379 1380 |
# File 'lib/metanorma/html/base_renderer.rb', line 1372 def deduplicate_semx_text(semx_text, output) first_word = semx_text[/\A\s*(\S+)/, 1] return semx_text unless first_word tail = output[-200..] return semx_text unless tail&.rstrip&.end_with?(first_word) semx_text.sub(/\A\s*#{Regexp.escape(first_word)}\s*/, "") end |
#element_attrs(**attrs) ⇒ Object
1586 1587 1588 1589 1590 1591 1592 1593 1594 |
# File 'lib/metanorma/html/base_renderer.rb', line 1586 def element_attrs(**attrs) parts = [] attrs.each do |k, v| next if v.nil? || v == false || (v.is_a?(String) && v.empty?) parts << %( #{k}="#{escape_html(v.to_s)}") end parts.join end |
#escape_html(text) ⇒ Object
1680 1681 1682 |
# File 'lib/metanorma/html/base_renderer.rb', line 1680 def escape_html(text) CGI.escapeHTML(text.to_s) end |
#extract_block_label(block, default) ⇒ Object
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 |
# File 'lib/metanorma/html/base_renderer.rb', line 1647 def extract_block_label(block, default) # Presentation XML puts label in <name> child element names = safe_attr(block, :name) if names && !names.empty? name = names.is_a?(Array) ? names.first : names text = extract_text_value(name) return text unless text.to_s.strip.empty? end # Fallback: autonum XML attribute autonum = safe_attr(block, :autonum) if autonum && !autonum.to_s.empty? number = autonum.to_s return "#{default} #{number}" end default end |
#extract_plain_text(node) ⇒ Object
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
# File 'lib/metanorma/html/base_renderer.rb', line 412 def extract_plain_text(node) return node.to_s if node.is_a?(String) return extract_text_value(node).to_s unless node.is_a?(Lutaml::Model::Serializable) parts = [] xml_mapping = node.class.mappings_for(:xml, node.lutaml_register) if node.element_order.is_a?(Array) && xml_mapping element_to_attr = {} xml_mapping.mapping_elements_hash.each_value do |rule_or_array| Array(rule_or_array).each do |rule| element_to_attr[rule.name.to_s] = rule.to end end indices = Hash.new(0) node.element_order.each do |el| next unless el.is_a?(Lutaml::Xml::Element) if el.text? parts << el.text_content.to_s elsif el.name == "tab" parts << "\u00A0\u00A0" elsif el.name == "br" parts << " " elsif el.element? attr_name = element_to_attr[el.name] next unless attr_name coll = node.public_send(attr_name) obj = if coll.is_a?(Array) idx = indices[attr_name] indices[attr_name] += 1 coll[idx] else coll end parts << extract_plain_text(obj) if obj end end end # Fallback: try .text if parts.join.strip.empty? t = safe_attr(node, :text) parts << (t.is_a?(Array) ? t.join : t.to_s) if t end parts.join.strip.gsub("\u00A0", " ") end |
#extract_primary_doc_id ⇒ Object
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/metanorma/html/base_renderer.rb', line 378 def extract_primary_doc_id bibdata = @document.bibdata return nil unless bibdata identifiers = bibdata.doc_identifier return nil unless identifiers && !identifiers.empty? first_id = identifiers.first text = if first_id.is_a?(String) first_id elsif first_id.is_a?(Lutaml::Model::Serializable) Array(first_id.value).join else first_id.to_s end text.strip.empty? ? nil : text.strip end |
#extract_text_value(val) ⇒ Object
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 |
# File 'lib/metanorma/html/base_renderer.rb', line 1684 def extract_text_value(val) return nil if val.nil? return val if val.is_a?(String) if val.is_a?(Array) val.map { |v| extract_text_value(v) }.join elsif val.is_a?(Lutaml::Model::Serializable) c = safe_attr(val, :content) if c && !c.equal?(val) extract_text_value(c) else t = safe_attr(val, :text) if t extract_text_value(t) else v = safe_attr(val, :value) if v extract_text_value(v) else val.to_s end end end else val.to_s end end |
#extract_title_text(titles) ⇒ Object
1672 1673 1674 1675 1676 1677 1678 |
# File 'lib/metanorma/html/base_renderer.rb', line 1672 def extract_title_text(titles) return "" if titles.nil? || titles.empty? titles = Array(titles) title = titles.first extract_text_value(title).to_s end |
#flavor_css_module ⇒ Object
293 294 295 |
# File 'lib/metanorma/html/base_renderer.rb', line 293 def flavor_css_module nil end |
#flavor_font_url ⇒ Object
158 159 160 |
# File 'lib/metanorma/html/base_renderer.rb', line 158 def flavor_font_url theme.font_url end |
#flavor_js_module ⇒ Object
289 290 291 |
# File 'lib/metanorma/html/base_renderer.rb', line 289 def flavor_js_module nil end |
#flavor_publisher_name ⇒ Object
148 149 150 151 |
# File 'lib/metanorma/html/base_renderer.rb', line 148 def flavor_publisher_name pubs = flavor_publishers(extract_primary_doc_id) pubs.empty? ? nil : pubs.join("/") end |
#flavor_publishers(_doc_id) ⇒ Object
144 145 146 |
# File 'lib/metanorma/html/base_renderer.rb', line 144 def flavor_publishers(_doc_id) [] end |
#generate_full_document(document) ⇒ Object
Generate a complete HTML document from a presentation XML document.
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/metanorma/html/base_renderer.rb', line 127 def generate_full_document(document) @document = document validate_presentation_xml! # First pass: render body content (collects ToC entries as side effect) render(@document) body = @output assemble_document(body) end |
#header_title_text ⇒ Object
213 214 215 216 |
# File 'lib/metanorma/html/base_renderer.rb', line 213 def header_title_text raw = html_title.to_s.split(" — ").first.to_s.gsub(/<[^>]+>/, "") raw.length > 60 ? "#{raw[0, 57]}..." : raw end |
#html_class_for_span(xml_class) ⇒ Object
1614 1615 1616 |
# File 'lib/metanorma/html/base_renderer.rb', line 1614 def html_class_for_span(xml_class) SPAN_ROLE_CLASSES[xml_class] || "span-#{xml_class}" end |
#html_title ⇒ Object
365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/metanorma/html/base_renderer.rb', line 365 def html_title bibdata = @document.bibdata return "Document" unless bibdata titles = bibdata.titles if titles title = bibdata.title_for("en") title.to_s else "Document" end end |
#language ⇒ Object
— Metadata extraction —
352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/metanorma/html/base_renderer.rb', line 352 def language bibdata = @document.bibdata return "en" unless bibdata langs = bibdata.language if langs && !langs.empty? lang = langs.find { |l| l.current == "true" } || langs.first lang.value || lang.to_s else "en" end end |
#load_logo_svg(filename, height: 32) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/metanorma/html/base_renderer.rb', line 237 def load_logo_svg(filename, height: 32) path = File.join(LOGO_DIR, filename) return nil unless File.exist?(path) svg = File.read(path) svg = svg.sub(/\A<\?xml[^?]*\?>\s*/, "") svg = svg.sub(/\A\s*<!--.*?-->\s*/m, "") svg = svg.sub(/<path[^>]*style="fill:#e3000f[^"]*"[^>]*\/>/, "") svg = svg.sub(/<svg\s/, '<svg class="header-logo" ') svg = if svg.match?(/<svg[^>]*\sheight="[^"]*"/) svg.sub(/(<svg[^>]*?)(\sheight="[^"]*")/, "\\1 height=\"#{height}\"") else svg.sub(/(<svg\b)/, "\\1 height=\"#{height}\"") end svg.sub(/(<svg[^>]*?)\swidth="[^"]*"/, '\1') rescue StandardError nil end |
#lookup_dispatch(type_class, registry_method) ⇒ Object
631 632 633 634 635 636 637 638 639 640 |
# File 'lib/metanorma/html/base_renderer.rb', line 631 def lookup_dispatch(type_class, registry_method) self.class.ancestors.each do |ancestor| next unless ancestor.is_a?(Class) && (ancestor == BaseRenderer || ancestor < BaseRenderer) registry = ancestor.public_send(registry_method) method_name = registry[type_class] return method_name if method_name end nil end |
#publisher_logo_map ⇒ Object
Map of publisher name => logo filename. Override in flavor renderers.
154 155 156 |
# File 'lib/metanorma/html/base_renderer.rb', line 154 def publisher_logo_map {} end |
#raw_content_node?(node) ⇒ Boolean
1171 1172 1173 1174 1175 1176 |
# File 'lib/metanorma/html/base_renderer.rb', line 1171 def raw_content_node?(node) node.is_a?(Lutaml::Model::Serializable) && node.content.is_a?(String) rescue NoMethodError false end |
#register_figure_entry(id:, text:) ⇒ Object
402 403 404 |
# File 'lib/metanorma/html/base_renderer.rb', line 402 def register_figure_entry(id:, text:) @figure_entries << { id: id, text: text } end |
#register_table_entry(id:, text:) ⇒ Object
406 407 408 |
# File 'lib/metanorma/html/base_renderer.rb', line 406 def register_table_entry(id:, text:) @table_entries << { id: id, text: text } end |
#register_toc_entry(id:, level:, text:) ⇒ Object
— CSS loader —
398 399 400 |
# File 'lib/metanorma/html/base_renderer.rb', line 398 def register_toc_entry(id:, level:, text:) @toc_entries << { id: id, level: level, text: text } end |
#render(node) ⇒ Object
Dispatch to the appropriate render method via type registry. Lookups traverse the ancestor chain so subclasses inherit parent registrations and can override them independently.
466 467 468 469 470 471 |
# File 'lib/metanorma/html/base_renderer.rb', line 466 def render(node, **) return escape_html(node) if node.is_a?(String) method = lookup_dispatch(node.class, :render_registry) method ? public_send(method, node, **) : "" end |
#render_admonition(admonition, **_opts) ⇒ Object
938 939 940 941 942 |
# File 'lib/metanorma/html/base_renderer.rb', line 938 def render_admonition(admonition, **_opts) drop = Drops::AdmonitionDrop.from_model(admonition, renderer: renderer_context) @output << render_liquid("_admonition.html.liquid", { "block" => drop }) end |
#render_asciimath(el) ⇒ Object
1282 1283 1284 |
# File 'lib/metanorma/html/base_renderer.rb', line 1282 def render_asciimath(el) @output << %(<span class="stem">#{escape_html(Array(el.text).join)}</span>) end |
#render_audio(audio) ⇒ Object
894 895 896 897 898 899 900 |
# File 'lib/metanorma/html/base_renderer.rb', line 894 def render_audio(audio) attrs = element_attrs( id: safe_attr(audio, :id), src: safe_attr(audio, :src), ) @output << "<audio#{attrs} controls></audio>" end |
#render_basic_section(section, level: 1, **_opts) ⇒ Object
— Section rendering —
995 996 997 998 999 1000 1001 1002 |
# File 'lib/metanorma/html/base_renderer.rb', line 995 def render_basic_section(section, level: 1, **_opts) attrs = element_attrs(id: safe_attr(section, :id)) tag("div", attrs) do render_section_title(section, level) section.blocks&.each { |block| render(block) } section.notes&.each { |note| render_note(note) } end end |
#render_block_children(model, children:) ⇒ Object
Renders the typed child collections of a block element (paragraphs, lists, nested blocks) in a standard order. Used by Drops and section rendering to avoid duplicating the enumeration in each consumer.
children is a Hash mapping attr names to render-method symbols:
{ paragraphs: :render_paragraph, ul: :render_unordered_list, ... }
Each key is sent to model via safe_attr; non-nil results are dispatched to the corresponding render method.
957 958 959 960 961 962 963 964 |
# File 'lib/metanorma/html/base_renderer.rb', line 957 def render_block_children(model, children:) children.each do |attr, render_method| values = safe_attr(model, attr) next if values.nil? Array(values).each { |v| public_send(render_method, v) } end end |
#render_bookmark(bookmark, **_opts) ⇒ Object
944 945 946 |
# File 'lib/metanorma/html/base_renderer.rb', line 944 def render_bookmark(bookmark, **_opts) @output << %(<a id="#{escape_html(safe_attr(bookmark, :id).to_s)}"></a>) end |
#render_br ⇒ Object
1237 1238 1239 |
# File 'lib/metanorma/html/base_renderer.rb', line 1237 def render_br(*) @output << "<br />" end |
#render_cell_content(cell) ⇒ Object
Table cells can contain both block-level content (p, ul, ol, dl) and inline content (text, em, strong, etc.) in document order.
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 |
# File 'lib/metanorma/html/base_renderer.rb', line 1125 def render_cell_content(cell) walked = walk_ordered(cell) do |type, obj| case type when :text @output << escape_html(obj) when :tab @output << "\u00a0\u00a0" when :element if block_element?(obj) render(obj) else render_inline_element(obj) end end end unless walked render_mixed_content_in_order(cell) end end |
#render_comma ⇒ Object
1274 1275 1276 |
# File 'lib/metanorma/html/base_renderer.rb', line 1274 def render_comma(*) @output << ", " end |
#render_concept(concept) ⇒ Object
1501 1502 1503 |
# File 'lib/metanorma/html/base_renderer.rb', line 1501 def render_concept(concept) render_mixed_inline(concept) end |
#render_content_section(section, level: 1, **_opts) ⇒ Object
1013 1014 1015 |
# File 'lib/metanorma/html/base_renderer.rb', line 1013 def render_content_section(section, level: 1, **_opts) render_hierarchical_section(section, level: level, **_opts) end |
#render_definition_list(dl, **_opts) ⇒ Object
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 |
# File 'lib/metanorma/html/base_renderer.rb', line 852 def render_definition_list(dl, **_opts) attrs = element_attrs(id: safe_attr(dl, :id)) tag("dl", attrs) do dl.dt&.each_with_index do |dt, i| @output << "<dt>" render_mixed_inline(dt) @output << "</dt>" dd = dl.dd&.[](i) if dd @output << "<dd>" render_mixed_inline(dd) @output << "</dd>" end end end end |
#render_em(el) ⇒ Object
Inline adapter methods for registry dispatch
1205 1206 1207 |
# File 'lib/metanorma/html/base_renderer.rb', line 1205 def render_em(el) render_inline_tag("em", el) end |
#render_eref(eref) ⇒ Object
1477 1478 1479 1480 1481 1482 1483 1484 |
# File 'lib/metanorma/html/base_renderer.rb', line 1477 def render_eref(eref) citeas = safe_attr(eref, :citeas) if citeas @output << escape_html(citeas) else render_mixed_inline(eref) end end |
#render_example(example, **_opts) ⇒ Object
907 908 909 910 911 |
# File 'lib/metanorma/html/base_renderer.rb', line 907 def render_example(example, **_opts) drop = Drops::ExampleDrop.from_model(example, renderer: renderer_context) @output << render_liquid("_example.html.liquid", { "block" => drop }) end |
#render_figure(figure, **_opts) ⇒ Object
869 870 871 872 |
# File 'lib/metanorma/html/base_renderer.rb', line 869 def render_figure(figure, **_opts) drop = Drops::FigureDrop.from_model(figure, renderer: renderer_context) @output << render_liquid("_figure.html.liquid", { "block" => drop }) end |
#render_fmt_stem(fmt_stem) ⇒ Object
— Stem/math rendering —
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 |
# File 'lib/metanorma/html/base_renderer.rb', line 1507 def render_fmt_stem(fmt_stem) semx_items = Array(fmt_stem.semx) return if semx_items.empty? semx = semx_items.first math_items = Array(semx.math) ascii_items = Array(semx.asciimath) # Collect source formats for interactive copy dropdown source_formats = {} if ascii_items.any? ascii_text = ascii_items.map { |a| a.text.to_s.strip }.join source_formats["asciimath"] = ascii_text unless ascii_text.empty? end data_attrs = "" unless source_formats.empty? data_attrs = " data-stem-formats='#{escape_html(source_formats.to_json)}'" end if math_items.any? content = math_items.map { |m| m.content.to_s }.join unless content.empty? @output << "<span class=\"math-container\"#{data_attrs}>" @output << "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">#{content}</math>" @output << "</span>" end elsif ascii_items.any? # No MathML — render asciimath as fallback text = ascii_items.map { |a| a.text.to_s.strip }.join @output << "<span class=\"stem\"#{data_attrs}>#{escape_html(text)}</span>" end end |
#render_fmt_xref(el) ⇒ Object
1264 1265 1266 1267 1268 1269 1270 1271 1272 |
# File 'lib/metanorma/html/base_renderer.rb', line 1264 def render_fmt_xref(el) target = safe_attr(el, :target) || safe_attr(el, :to_attr) if target attrs = element_attrs(href: "##{escape_html(target)}", class: "xref") tag("a", attrs) { render_mixed_inline(el) } else render_mixed_inline(el) end end |
#render_fn(fn) ⇒ Object
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 |
# File 'lib/metanorma/html/base_renderer.rb', line 1486 def render_fn(fn) fn_id = safe_attr(fn, :id) number = @footnote_collector.register(fn) attrs = element_attrs(id: fn_id, class: "fn-marker") tag("span", attrs) do label = safe_attr(fn, :fn_label) || safe_attr(fn, :reference) if label @output << "<sup><a href=\"##{escape_html("footnote-#{number}")}\" " \ "class=\"fn-link\" id=\"#{escape_html("fnref-#{number}")}\">" \ "#{escape_html(label.to_s)}</a></sup>" end end end |
#render_fn_inline(el) ⇒ Object
1252 1253 1254 |
# File 'lib/metanorma/html/base_renderer.rb', line 1252 def render_fn_inline(el) render_fn(el) end |
#render_footnotes_section ⇒ Object
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 |
# File 'lib/metanorma/html/base_renderer.rb', line 1712 def render_footnotes_section return if @footnote_collector.empty? drops = @footnote_collector.to_a.map do |entry| content_html = "" if entry.content && !entry.content.empty? content_html = capture_output do Array(entry.content).each { |p| render_paragraph(p) } end end Drops::FootnoteDrop.new(entry, content_html) end @output << render_liquid("_footnotes.html.liquid", { "footnotes" => drops }) end |
#render_formula(formula, **_opts) ⇒ Object
918 919 920 921 922 |
# File 'lib/metanorma/html/base_renderer.rb', line 918 def render_formula(formula, **_opts) drop = Drops::FormulaDrop.from_model(formula, renderer: renderer_context) @output << render_liquid("_formula.html.liquid", { "block" => drop }) end |
#render_hierarchical_section(section, level: 1, **_opts) ⇒ Object
1004 1005 1006 1007 1008 1009 1010 1011 |
# File 'lib/metanorma/html/base_renderer.rb', line 1004 def render_hierarchical_section(section, level: 1, **_opts) attrs = element_attrs(id: safe_attr(section, :id)) tag("div", attrs) do render_section_title(section, level) render_section_content(section, level) section.subsections&.each { |sub| render(sub, level: level + 1) } end end |
#render_image(image) ⇒ Object
874 875 876 877 878 879 880 881 882 883 884 |
# File 'lib/metanorma/html/base_renderer.rb', line 874 def render_image(image) src_val = safe_attr(image, :src) || safe_attr(image, :source) 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), ) @output << "<img#{attrs} />" end |
#render_index(el) ⇒ Object
1286 1287 1288 1289 |
# File 'lib/metanorma/html/base_renderer.rb', line 1286 def render_index(el) collect_index_term(el) "" end |
#render_inline_collections(node) ⇒ Object
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 |
# File 'lib/metanorma/html/base_renderer.rb', line 1295 def render_inline_collections(node) texts = node.text if texts.is_a?(Array) texts.each do |t| if t.is_a?(String) @output << escape_html(t) else render_inline_element(t) end end elsif texts.is_a?(String) @output << escape_html(texts) end inline_attrs = %i[em strong smallcap sub sup tt underline strike xref eref link span stem concept fn br tab keyword fmt_annotation_start fmt_annotation_end fmt_stem fmt_fn_label fmt_concept bookmark image semx fmt_xref_label] inline_attrs.each do |attr| values = safe_attr(node, attr) next if values.nil? Array(values).each { |v| render_inline_element(v) } end end |
#render_inline_element(element) ⇒ Object
Dispatch to the appropriate inline render method via type registry.
474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/metanorma/html/base_renderer.rb', line 474 def render_inline_element(element, **) return "" if element.nil? return escape_html(element) if element.is_a?(String) method = lookup_dispatch(element.class, :inline_registry) if method public_send(method, element) elsif element.is_a?(Lutaml::Model::Serializable) && element.mixed? render_mixed_inline(element) end end |
#render_inline_tag(tag_name, element, **extra_attrs) ⇒ Object
1382 1383 1384 1385 1386 |
# File 'lib/metanorma/html/base_renderer.rb', line 1382 def render_inline_tag(tag_name, element, **extra_attrs) tag(tag_name, element_attrs(**extra_attrs)) do render_mixed_inline(element) end end |
#render_link(link) ⇒ Object
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 |
# File 'lib/metanorma/html/base_renderer.rb', line 1455 def render_link(link) target = safe_attr(link, :target) || safe_attr(link, :href) attrs = element_attrs(href: target, id: safe_attr(link, :id)) tag("a", attrs) do content = safe_attr(link, :content) if content && !Array(content).join.strip.empty? render_mixed_inline(link) else display_text = target.to_s.delete_prefix("mailto:") @output << escape_html(display_text) end end end |
#render_liquid(template_name, assigns) ⇒ Object
167 168 169 170 171 172 173 174 |
# File 'lib/metanorma/html/base_renderer.rb', line 167 def render_liquid(template_name, assigns) template_path = File.join(TEMPLATES_ROOT, template_name) template = TEMPLATE_CACHE_MUTEX.synchronize do TEMPLATE_CACHE[template_path] ||= Liquid::Template.parse(File.read(template_path)) end assigns = assigns.transform_keys(&:to_s) if assigns.is_a?(Hash) template.render(assigns) end |
#render_list_item(li) ⇒ Object
777 778 779 780 781 782 783 |
# File 'lib/metanorma/html/base_renderer.rb', line 777 def render_list_item(li) li_id = safe_attr(li, :id) attrs = li_id ? %( id="#{escape_html(li_id)}") : "" @output << "<li#{attrs}>" render_mixed_content_in_order(li) @output << "</li>" end |
#render_math(el) ⇒ Object
1278 1279 1280 |
# File 'lib/metanorma/html/base_renderer.rb', line 1278 def render_math(el) @output << el.content.to_s end |
#render_mixed_content_in_order(node) ⇒ Object
Render all children of a mixed-content node in document order, dispatching block elements to their render methods.
787 788 789 790 791 792 793 794 795 796 797 798 799 800 |
# File 'lib/metanorma/html/base_renderer.rb', line 787 def render_mixed_content_in_order(node) node.each_mixed_content do |child| case child when String @output << escape_html(child) else if block_element?(child) render(child) else render_inline_element(child) end end end end |
#render_mixed_inline(node) ⇒ Object
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 |
# File 'lib/metanorma/html/base_renderer.rb', line 1145 def render_mixed_inline(node) # Models using map_all_content (e.g. RawParagraph): raw XML in content if raw_content_node?(node) raw = node.content if raw.is_a?(String) && !raw.strip.empty? @output << render_raw_content(raw) return end end if node.is_a?(Lutaml::Model::Serializable) && node.element_order && !node.element_order.empty? render_ordered_inline(node) elsif node.is_a?(Lutaml::Model::Serializable) node.each_mixed_content do |child| case child when String @output << escape_html(child) else render_inline_element(child) end end else render_inline_collections(node) end end |
#render_noop ⇒ Object
642 643 644 |
# File 'lib/metanorma/html/base_renderer.rb', line 642 def render_noop(*) "" end |
#render_noop_inline ⇒ Object
646 647 648 |
# File 'lib/metanorma/html/base_renderer.rb', line 646 def render_noop_inline(*) nil end |
#render_note(note, **_opts) ⇒ Object
902 903 904 905 |
# File 'lib/metanorma/html/base_renderer.rb', line 902 def render_note(note, **_opts) drop = Drops::NoteDrop.from_model(note, renderer: renderer_context) @output << render_liquid("_note.html.liquid", { "block" => drop }) end |
#render_note_inline(el) ⇒ Object
1291 1292 1293 |
# File 'lib/metanorma/html/base_renderer.rb', line 1291 def render_note_inline(el) render_note(el) end |
#render_ordered_content(section, level = 1) ⇒ Object
Render children of a section in displayorder, skipping title elements.
831 832 833 834 835 836 837 838 839 |
# File 'lib/metanorma/html/base_renderer.rb', line 831 def render_ordered_content(section, level = 1) children = collect_ordered_children(section) children.each do |node| next if node.is_a?(String) next if is_title_element?(node, section) render(node, level: level + 1) end end |
#render_ordered_inline(node) ⇒ Object
Iterate element_order directly, preserving whitespace text nodes that each_mixed_content drops (it skips text where text.strip.empty?)
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 |
# File 'lib/metanorma/html/base_renderer.rb', line 1180 def render_ordered_inline(node) walked = walk_ordered(node) do |type, obj| case type when :text @output << escape_html(obj) when :tab @output << "\u00a0\u00a0" when :element render_inline_element(obj) end end unless walked node.each_mixed_content do |child| case child when String @output << escape_html(child) else render_inline_element(child) end end end end |
#render_ordered_list(ol, **_opts) ⇒ Object
769 770 771 772 773 774 775 |
# File 'lib/metanorma/html/base_renderer.rb', line 769 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)) tag("ol", attrs) do ol.listitem&.each { |li| render_list_item(li) } end end |
#render_paragraph(p, **_opts) ⇒ Object
— Block-level rendering —
652 653 654 655 656 657 658 |
# File 'lib/metanorma/html/base_renderer.rb', line 652 def render_paragraph(p, **_opts) attrs = element_attrs(id: safe_attr(p, :id), style: alignment_style(safe_attr( p, :alignment ))) tag("p", attrs) { render_mixed_inline(p) } end |
#render_quote(quote, **_opts) ⇒ Object
924 925 926 927 928 929 930 931 932 933 934 935 936 |
# File 'lib/metanorma/html/base_renderer.rb', line 924 def render_quote(quote, **_opts) attrs = element_attrs(id: safe_attr(quote, :id), class: "quote") tag("blockquote", attrs) do quote.paragraphs&.each { |para| render_paragraph(para) } quote.ul&.each { |ul| render_unordered_list(ul) } quote.ol&.each { |ol| render_ordered_list(ol) } if quote.attribution @output << "<footer>" render_mixed_inline(quote.attribution) @output << "</footer>" end end end |
#render_raw_content(raw_xml) ⇒ Object
Process raw XML content from map_all_content models (e.g. RawParagraph). Strips source elements (xref, eref, stem) that have a following <semx> wrapper, keeping only the semx display content.
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 |
# File 'lib/metanorma/html/base_renderer.rb', line 1391 def render_raw_content(raw_xml) doc = Nokogiri::XML.fragment(raw_xml) # Convert fmt-link elements to HTML <a> tags before stripping wrappers doc.css("fmt-link").each do |el| target = el["target"] || el["href"] if target display_text = target.delete_prefix("mailto:") a = doc.document.create_element("a", display_text, "href" => target) el.replace(a) else el.replace(el.children) end end # Remove source elements that precede a <semx> sibling, # deduplicating any label text that appears in both the source # paragraph and the semx display content. doc.traverse do |node| next unless node.element? next unless %w[xref eref stem link].include?(node.name) next_sib = node.next_sibling while next_sib.is_a?(Nokogiri::XML::Text) && next_sib.text.strip.empty? next_sib = next_sib.next_sibling end next unless next_sib&.element? && next_sib.name == "semx" deduplicate_semx_label(node, next_sib) node.remove end # Strip presentation wrappers, keeping inner content %w[semx fmt-xref].each do |tag| doc.css(tag).each { |el| el.replace(el.children) } end # Remap XML class names to HTML-specific class names doc.css("[class]").each do |el| el["class"] = el["class"].split(/\s+/).map do |c| html_class_for_span(c) end.join(" ") end doc.inner_html end |
#render_section_content(section, _level) ⇒ Object
1026 1027 1028 1029 |
# File 'lib/metanorma/html/base_renderer.rb', line 1026 def render_section_content(section, _level) section.blocks&.each { |block| render(block) } section.notes&.each { |note| render_note(note) } end |
#render_section_title(section, level) ⇒ Object
1017 1018 1019 1020 1021 1022 1023 1024 |
# File 'lib/metanorma/html/base_renderer.rb', line 1017 def render_section_title(section, level) titles = section.title return unless titles && !titles.empty? h = "h#{[[level, 6].min, 1].max}" title_text = extract_title_text(titles) @output << "<#{h}>#{escape_html(title_text)}</#{h}>" end |
#render_semx_content(element, **_opts) ⇒ Object
Render SemxElement display content only, skipping semantic linkage. semx wraps both semantic data (origin, xref, source, etc.) and display content (fmt-xref, span, strong, etc.). Only render display.
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 |
# File 'lib/metanorma/html/base_renderer.rb', line 1325 def render_semx_content(element, **_opts) display_attrs = %i[text fmt_xref fmt_link fmt_concept span strong em sup p semx asciimath math sub_child tt_child br_child tab_child stem_child figure_child formula_child sourcecode_child] label_stripped = false walked = walk_ordered(element, allow_filter: display_attrs) do |type, obj| case type when :text text = obj if !label_stripped text = deduplicate_semx_text(text, @output) label_stripped = true end @output << escape_html(text) when :element if obj.is_a?(Metanorma::Document::Components::Paragraphs::ParagraphBlock) render_paragraph(obj) else render_inline_element(obj) end end end unless walked display_attrs.each do |attr| val = safe_attr(element, attr) next if val.nil? if val.is_a?(Array) val.each do |v| if v.is_a?(Metanorma::Document::Components::Paragraphs::ParagraphBlock) render_paragraph(v) else render_inline_element(v) end end elsif val.is_a?(String) @output << escape_html(val) else render_inline_element(val) end end end end |
#render_semx_inline(el) ⇒ Object
1260 1261 1262 |
# File 'lib/metanorma/html/base_renderer.rb', line 1260 def render_semx_inline(el) render_semx_content(el) end |
#render_small_caps(el) ⇒ Object
1225 1226 1227 |
# File 'lib/metanorma/html/base_renderer.rb', line 1225 def render_small_caps(el) render_inline_tag("span", el, class: "small-caps") end |
#render_sourcecode(sc, **_opts) ⇒ Object
913 914 915 916 |
# File 'lib/metanorma/html/base_renderer.rb', line 913 def render_sourcecode(sc, **_opts) drop = Drops::SourcecodeDrop.from_model(sc, renderer: renderer_context) @output << render_liquid("_sourcecode.html.liquid", { "block" => drop }) end |
#render_span(el) ⇒ Object
1245 1246 1247 1248 1249 1250 |
# File 'lib/metanorma/html/base_renderer.rb', line 1245 def render_span(el) xml_class = safe_attr(el, :class_attr).to_s html_class = html_class_for_span(xml_class) unless xml_class.empty? attrs = element_attrs(style: safe_attr(el, :style), class: html_class) tag("span", attrs) { render_mixed_inline(el) } end |
#render_stem(el) ⇒ Object
1256 1257 1258 |
# File 'lib/metanorma/html/base_renderer.rb', line 1256 def render_stem(el) @output << render_stem_content(el) end |
#render_stem_content(stem) ⇒ Object
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 |
# File 'lib/metanorma/html/base_renderer.rb', line 1541 def render_stem_content(stem) return "" if stem.nil? # StemInlineElement — source element, skip (rendered via FmtStemElement) if stem.is_a?(Metanorma::Document::Components::Inline::StemInlineElement) return "" end # FmtStemElement — already handled by render_fmt_stem if stem.is_a?(Metanorma::Document::Components::Inline::FmtStemElement) return "" end # TextElements::StemElement — block math (no fmt- counterpart for display formulas) if stem.is_a?(Metanorma::Document::Components::TextElements::StemElement) if stem.math begin return stem.math.to_xml rescue StandardError math_items = Array(stem.math) return math_items.join end end if stem.asciimath text = extract_text_value(stem.asciimath) return %(<span class="stem">#{escape_html(text)}</span>) end if stem.latexmath text = extract_text_value(stem.latexmath) return %(<span class="stem">#{escape_html(text)}</span>) end end text = extract_text_value(stem) text.empty? ? "" : %(<span class="stem">#{escape_html(text)}</span>) end |
#render_strike(el) ⇒ Object
1233 1234 1235 |
# File 'lib/metanorma/html/base_renderer.rb', line 1233 def render_strike(el) render_inline_tag("s", el) end |
#render_strong(el) ⇒ Object
1209 1210 1211 |
# File 'lib/metanorma/html/base_renderer.rb', line 1209 def render_strong(el) render_inline_tag("strong", el) end |
#render_sub(el) ⇒ Object
1217 1218 1219 |
# File 'lib/metanorma/html/base_renderer.rb', line 1217 def render_sub(el) render_inline_tag("sub", el) end |
#render_sup(el) ⇒ Object
1221 1222 1223 |
# File 'lib/metanorma/html/base_renderer.rb', line 1221 def render_sup(el) render_inline_tag("sup", el) end |
#render_tab ⇒ Object
1241 1242 1243 |
# File 'lib/metanorma/html/base_renderer.rb', line 1241 def render_tab(*) @output << "\u00a0\u00a0" end |
#render_table(table, **_opts) ⇒ Object
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 |
# File 'lib/metanorma/html/base_renderer.rb', line 660 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: extract_plain_text(name_el)) end col_count = table_column_count(table) @output << "<div class=\"table-scroll-wrapper\">" tag("table", attrs) do name_el = safe_attr(table, :fmt_name) || safe_attr(table, :name) if name_el @output << "<caption>" render_inline_element(name_el) @output << "</caption>" end @output << "<colgroup>" if table.colgroup render_table_colgroup(table.colgroup) if table.colgroup @output << "</colgroup>" if table.colgroup render_table_section(table.thead, "thead") if table.thead render_table_section(table.tbody, "tbody") if table.tbody if table.tfoot || (table.note && !table.note.empty?) @output << "<tfoot>" render_table_section_rows(table.tfoot) if table.tfoot if table.note && !table.note.empty? @output << "<tr><td colspan=\"#{col_count}\" class=\"table-notes\">" table.note.each { |n| render_note(n) } @output << "</td></tr>" end @output << "</tfoot>" end end @output << "</div>" end |
#render_table_cell(cell, force_tag = nil) ⇒ Object
756 757 758 759 760 761 762 763 764 765 766 767 |
# File 'lib/metanorma/html/base_renderer.rb', line 756 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), ) @output << "<#{tag_name}#{attrs}>" render_cell_content(cell) @output << "</#{tag_name}>" end |
#render_table_colgroup(colgroup) ⇒ Object
720 721 722 723 724 725 |
# File 'lib/metanorma/html/base_renderer.rb', line 720 def render_table_colgroup(colgroup) colgroup.col&.each do |col| attrs = element_attrs(style: col.width ? "width: #{col.width}" : nil) @output << "<col#{attrs}>" end end |
#render_table_section(section, tag_name) ⇒ Object
727 728 729 730 731 |
# File 'lib/metanorma/html/base_renderer.rb', line 727 def render_table_section(section, tag_name) @output << "<#{tag_name}>" render_table_section_rows(section) @output << "</#{tag_name}>" end |
#render_table_section_rows(section) ⇒ Object
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 |
# File 'lib/metanorma/html/base_renderer.rb', line 733 def render_table_section_rows(section) section.tr&.each do |tr| @output << "<tr>" walked = walk_ordered(tr) do |type, obj| next unless type == :element render_table_cell(obj) end unless walked Array(tr.th).each { |th| render_table_cell(th, "th") } Array(tr.td).each { |td| render_table_cell(td, "td") } end @output << "</tr>" end end |
#render_tt(el) ⇒ Object
1213 1214 1215 |
# File 'lib/metanorma/html/base_renderer.rb', line 1213 def render_tt(el) render_inline_tag("tt", el) end |
#render_underline(el) ⇒ Object
1229 1230 1231 |
# File 'lib/metanorma/html/base_renderer.rb', line 1229 def render_underline(el) render_inline_tag("u", el) end |
#render_unordered_list(ul, **_opts) ⇒ Object
749 750 751 752 753 754 |
# File 'lib/metanorma/html/base_renderer.rb', line 749 def render_unordered_list(ul, **_opts) attrs = element_attrs(id: safe_attr(ul, :id)) tag("ul", attrs) do ul.listitem&.each { |li| render_list_item(li) } end end |
#render_video(video) ⇒ Object
886 887 888 889 890 891 892 |
# File 'lib/metanorma/html/base_renderer.rb', line 886 def render_video(video) attrs = element_attrs( id: safe_attr(video, :id), src: safe_attr(video, :src), ) @output << "<video#{attrs} controls></video>" end |
#render_xref(xref) ⇒ Object
1469 1470 1471 1472 1473 1474 1475 |
# File 'lib/metanorma/html/base_renderer.rb', line 1469 def render_xref(xref) target = safe_attr(xref, :target) || safe_attr(xref, :to_attr) attrs = element_attrs(href: "##{escape_html(target)}", id: safe_attr(xref, :id)) tag("a", attrs) { render_mixed_inline(xref) } end |
#renderer_context ⇒ Object
114 115 116 |
# File 'lib/metanorma/html/base_renderer.rb', line 114 def renderer_context @renderer_context ||= RendererContext.new(self) end |
#safe_attr(obj, method_name) ⇒ Object
1622 1623 1624 1625 1626 1627 1628 1629 1630 |
# File 'lib/metanorma/html/base_renderer.rb', line 1622 def safe_attr(obj, method_name) if obj.is_a?(Lutaml::Model::Serializable) && !obj.class.attributes.key?(method_name) return nil end obj.public_send(method_name) rescue NoMethodError nil end |
#sort_by_displayorder(children) ⇒ Object
841 842 843 844 845 846 847 848 849 850 |
# File 'lib/metanorma/html/base_renderer.rb', line 841 def sort_by_displayorder(children) children.sort_by do |node| order = if node.is_a?(Lutaml::Model::Serializable) && node.class.attributes.key?(:displayorder) node.displayorder end order &&= order.to_i order || Float::INFINITY end end |
#table_column_count(table) ⇒ Object
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 |
# File 'lib/metanorma/html/base_renderer.rb', line 695 def table_column_count(table) if table.colgroup&.col && !table.colgroup.col.empty? return table.colgroup.col.size end # Walk all rows to find max column count, accounting for colspan 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 |
#tag(name, attrs_str) ⇒ Object
— Helper methods —
1580 1581 1582 1583 1584 |
# File 'lib/metanorma/html/base_renderer.rb', line 1580 def tag(name, attrs_str) @output << "<#{name}#{attrs_str}>" yield @output << "</#{name}>" end |
#theme ⇒ Object
— Flavor configuration hooks (override in subclasses) —
140 141 142 |
# File 'lib/metanorma/html/base_renderer.rb', line 140 def theme @theme ||= Theme.new end |
#to_html ⇒ Object
118 119 120 |
# File 'lib/metanorma/html/base_renderer.rb', line 118 def to_html @output end |
#toc_entries ⇒ Object
122 123 124 |
# File 'lib/metanorma/html/base_renderer.rb', line 122 def toc_entries @toc_entries end |
#validate_presentation_xml! ⇒ Object
— Validation —
305 306 307 308 309 310 311 312 313 |
# File 'lib/metanorma/html/base_renderer.rb', line 305 def validate_presentation_xml! has_presentation = check_presentation_markers(@document) return if has_presentation raise ArgumentError, "HTML generation requires Presentation XML input. " \ "Semantic XML does not contain formatting data needed for HTML. " \ "Use a '.presentation.xml' file instead." end |
#walk_ordered(node, allow_filter: nil) ⇒ Object
Walk element_order in document order, resolving each element to its Ruby object via the XML mapping, and yielding (element_order_entry, resolved_object) to the given block. Handles tab elements as nbsp. Skips elements not in the mapping.
This is the single ordered-walk primitive used by all mixed content renderers:
-
render_mixed_inline (inline-only)
-
render_cell_content (mixed block/inline)
-
render_semx_content (filtered display attrs only)
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 |
# File 'lib/metanorma/html/base_renderer.rb', line 1041 def walk_ordered(node, allow_filter: nil) return false unless node.is_a?(Lutaml::Model::Serializable) return false unless node.element_order.is_a?(Array) && !node.element_order.empty? xml_mapping = node.class.mappings_for(:xml, node.lutaml_register) return false unless xml_mapping element_to_attr = {} xml_mapping.mapping_elements_hash.each_value do |rule_or_array| Array(rule_or_array).each do |rule| element_to_attr[rule.name] = rule.to element_to_attr[rule.name.to_s] = rule.to if rule.name.is_a?(Symbol) end end skip_indices = build_semx_skip_set(node) indices = Hash.new(0) node.element_order.each_with_index do |el, i| next if skip_indices.include?(i) if el.text? text = el.text_content yield :text, text if text && block_given? elsif el.element? # Handle <tab/> elements if el.name == "tab" yield :tab, nil if block_given? next end attr_name = element_to_attr[el.name] next unless attr_name # Apply optional filter (used by semx to skip semantic attrs) next if allow_filter && !allow_filter.include?(attr_name) coll = node.public_send(attr_name) obj = if coll.is_a?(Array) idx = indices[attr_name] indices[attr_name] += 1 coll[idx] else coll end yield :element, obj if obj && block_given? end end true end |