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
Constant Summary collapse
- LOGO_DIR =
File.("../../../data/logos", __dir__)
- CLASS_MAP =
Map legacy XML class names to clean, semantic names.
{ "zzSTDTitle1" => "doc-title", "coverpage_docnumber" => "cover-doc-id", "coverpage_docstage" => "cover-stage", "doctitle-en" => "cover-title", "ForewordTitle" => "foreword-title", "IntroTitle" => "intro-title", "Annex" => "annex-title", "Section3" => "section-sub", "TermNum" => "term-number", "Terms" => "term-name", "DeprecatedTerms" => "term-deprecated", "domain" => "term-domain", "boldtitle" => "bold-title", "note_label" => "note-label", "termnote_label" => "term-note-label", "example_label" => "example-label", "stddocNumber" => "std-doc-number", "stdyear" => "std-year", "sourcecode-name" => "code-name", "fmt-caption-label" => "caption-label", "fmt-autonum-delim" => "autonum-delim", "fmt-element-name" => "element-name", "fmt-caption-delim" => "caption-delim", "smallcap" => "small-caps", "obligation" => "obligation-text", "tableblock" => "table-block", "Biblio" => "biblio-entry", "Note" => "note-block", "source" => "term-source", "nonboldtitle" => "doc-subtitle", "stddocPartNumber" => "doc-part-number", "stddocTitle" => "doc-part-title", "std_publisher" => "doc-publisher", "stdpublisher" => "doc-publisher-name", "fmt-xref-label" => "xref-label", }.freeze
- METANORMA_LOGO =
"metanorma-logo.svg"- TEMPLATE_CACHE =
— Document Assembly —
Hash.new { |h, k| h[k] = Liquid::Template.parse(File.read(k)) }
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.
Instance Method Summary collapse
- #assemble_document(body) ⇒ Object
- #build_footer ⇒ Object
-
#build_header ⇒ Object
— Header and Footer —.
- #build_publisher_logos ⇒ Object
-
#build_reader_controls ⇒ Object
Reader controls — kept for backward compat with flavor renderers.
-
#build_scripts ⇒ Object
— Scripts —.
- #build_styles ⇒ Object
-
#build_toc_html(entries) ⇒ Object
— ToC generation —.
- #check_presentation_markers(node) ⇒ Object
- #detect_publishers ⇒ Object
- #extract_plain_text(node) ⇒ Object
- #extract_primary_doc_id ⇒ 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_title ⇒ Object
-
#initialize ⇒ BaseRenderer
constructor
A new instance of BaseRenderer.
-
#language ⇒ Object
— Metadata extraction —.
- #load_logo_svg(filename, height: 32) ⇒ Object
-
#publisher_logo_map ⇒ Object
Map of publisher name => logo filename.
- #register_figure_entry(id:, text:) ⇒ Object
- #register_table_entry(id:, text:) ⇒ Object
-
#register_toc_entry(id:, level:, text:) ⇒ Object
— CSS loader —.
-
#render(node, **opts) ⇒ Object
Dispatch to the appropriate render method based on node class.
- #render_liquid(template_name, assigns) ⇒ Object
-
#theme ⇒ Object
— Flavor configuration hooks (override in subclasses) —.
-
#to_html ⇒ Object
— Public API —.
- #toc_entries ⇒ Object
-
#validate_presentation_xml! ⇒ Object
— Validation —.
Constructor Details
#initialize ⇒ BaseRenderer
Returns a new instance of BaseRenderer.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/metanorma/html/base_renderer.rb', line 61 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.
388 389 390 |
# File 'lib/metanorma/html/base_renderer.rb', line 388 def footnote_collector @footnote_collector end |
#index_term_collector ⇒ Object (readonly)
Returns the value of attribute index_term_collector.
388 389 390 |
# File 'lib/metanorma/html/base_renderer.rb', line 388 def index_term_collector @index_term_collector end |
Instance Method Details
#assemble_document(body) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/metanorma/html/base_renderer.rb', line 129 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 |
#build_footer ⇒ Object
219 220 221 222 223 224 225 |
# File 'lib/metanorma/html/base_renderer.rb', line 219 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 —
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/metanorma/html/base_renderer.rb', line 149 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
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/metanorma/html/base_renderer.rb', line 176 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_reader_controls ⇒ Object
Reader controls — kept for backward compat with flavor renderers
172 173 174 |
# File 'lib/metanorma/html/base_renderer.rb', line 172 def build_reader_controls "" end |
#build_scripts ⇒ Object
— Scripts —
269 270 271 272 273 |
# File 'lib/metanorma/html/base_renderer.rb', line 269 def build_scripts pipeline = AssetPipeline.new compiled = pipeline.compile_js(flavor_js: flavor_js_module) "<script>\n#{compiled}\n</script>" end |
#build_styles ⇒ Object
283 284 285 286 287 |
# File 'lib/metanorma/html/base_renderer.rb', line 283 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 —
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/metanorma/html/base_renderer.rb', line 229 def build_toc_html(entries) top_lines = [] main_lines = if entries.empty? ["<li class=\"toc-empty\">No entries</li>"] else entries.map { |e| id = e[:id].to_s text = escape_html(e[:text].to_s) lvl = e[:level] "<li class=\"toc-level-#{lvl}\"><a href=\"##{id}\" class=\"toc-link\" data-target=\"#{id}\">#{text}</a></li>" } end # List of Figures — at top of sidebar unless @figure_entries.empty? top_lines << "<li class=\"toc-list-header\" data-list=\"figures\"><button class=\"toc-list-toggle\" aria-expanded=\"false\"><svg width=\"14\" height=\"14\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.5\"><rect x=\"1\" y=\"2\" width=\"14\" height=\"12\" rx=\"1\"/><circle cx=\"5\" cy=\"6.5\" r=\"1.5\"/><path d=\"M1 12l4-4 2 2 3-3 5 5\"/></svg> Figures <span class=\"toc-list-count\">(#{@figure_entries.size})</span></button></li>" @figure_entries.each { |f| id = f[:id].to_s text = escape_html(f[:text].to_s) top_lines << "<li class=\"toc-list-item toc-figures\" style=\"display:none\"><a href=\"##{id}\" class=\"toc-link\" data-target=\"#{id}\">#{text}</a></li>" } end # List of Tables — at top of sidebar unless @table_entries.empty? top_lines << "<li class=\"toc-list-header\" data-list=\"tables\"><button class=\"toc-list-toggle\" aria-expanded=\"false\"><svg width=\"14\" height=\"14\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.5\"><rect x=\"1\" y=\"2\" width=\"14\" height=\"12\" rx=\"1\"/><line x1=\"1\" y1=\"6\" x2=\"15\" y2=\"6\"/><line x1=\"1\" y1=\"10\" x2=\"15\" y2=\"10\"/><line x1=\"7\" y1=\"2\" x2=\"7\" y2=\"14\"/></svg> Tables <span class=\"toc-list-count\">(#{@table_entries.size})</span></button></li>" @table_entries.each { |t| id = t[:id].to_s text = escape_html(t[:text].to_s) top_lines << "<li class=\"toc-list-item toc-tables\" style=\"display:none\"><a href=\"##{id}\" class=\"toc-link\" data-target=\"#{id}\">#{text}</a></li>" } end top_lines << "<li class=\"toc-divider\"></li>" unless top_lines.empty? (top_lines + main_lines).join("\n") end |
#check_presentation_markers(node) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/metanorma/html/base_renderer.rb', line 301 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) return true if (node.public_send(:fmt_title) rescue nil) return true if (node.public_send(:displayorder) rescue nil) %i[preface sections annex bibliography].each do |attr| val = node.public_send(attr) rescue nil 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 |
#detect_publishers ⇒ Object
195 196 197 |
# File 'lib/metanorma/html/base_renderer.rb', line 195 def detect_publishers flavor_publishers(extract_primary_doc_id) end |
#extract_plain_text(node) ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 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 |
# File 'lib/metanorma/html/base_renderer.rb', line 390 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
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/metanorma/html/base_renderer.rb', line 356 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 |
#flavor_css_module ⇒ Object
279 280 281 |
# File 'lib/metanorma/html/base_renderer.rb', line 279 def flavor_css_module nil end |
#flavor_font_url ⇒ Object
114 115 116 |
# File 'lib/metanorma/html/base_renderer.rb', line 114 def flavor_font_url theme.font_url end |
#flavor_js_module ⇒ Object
275 276 277 |
# File 'lib/metanorma/html/base_renderer.rb', line 275 def flavor_js_module nil end |
#flavor_publisher_name ⇒ Object
104 105 106 107 |
# File 'lib/metanorma/html/base_renderer.rb', line 104 def flavor_publisher_name pubs = flavor_publishers(extract_primary_doc_id) pubs.empty? ? nil : pubs.join("/") end |
#flavor_publishers(_doc_id) ⇒ Object
100 101 102 |
# File 'lib/metanorma/html/base_renderer.rb', line 100 def flavor_publishers(_doc_id) [] end |
#generate_full_document(document) ⇒ Object
Generate a complete HTML document from a presentation XML document.
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/metanorma/html/base_renderer.rb', line 83 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
166 167 168 169 |
# File 'lib/metanorma/html/base_renderer.rb', line 166 def header_title_text raw = html_title.to_s.split(" — ").first.to_s.gsub(/<[^>]+>/, "") raw.length > 60 ? raw[0, 57] + "..." : raw end |
#html_title ⇒ Object
343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/metanorma/html/base_renderer.rb', line 343 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 —
330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/metanorma/html/base_renderer.rb', line 330 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
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/metanorma/html/base_renderer.rb', line 199 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" ') if svg.match?(/<svg[^>]*\sheight="[^"]*"/) svg = svg.sub(/(<svg[^>]*?)(\sheight="[^"]*")/, "\\1 height=\"#{height}\"") else svg = svg.sub(/(<svg\b)/, "\\1 height=\"#{height}\"") end svg = svg.sub(/(<svg[^>]*?)\swidth="[^"]*"/, '\1') svg rescue StandardError nil end |
#publisher_logo_map ⇒ Object
Map of publisher name => logo filename. Override in flavor renderers.
110 111 112 |
# File 'lib/metanorma/html/base_renderer.rb', line 110 def publisher_logo_map {} end |
#register_figure_entry(id:, text:) ⇒ Object
380 381 382 |
# File 'lib/metanorma/html/base_renderer.rb', line 380 def register_figure_entry(id:, text:) @figure_entries << { id: id, text: text } end |
#register_table_entry(id:, text:) ⇒ Object
384 385 386 |
# File 'lib/metanorma/html/base_renderer.rb', line 384 def register_table_entry(id:, text:) @table_entries << { id: id, text: text } end |
#register_toc_entry(id:, level:, text:) ⇒ Object
— CSS loader —
376 377 378 |
# File 'lib/metanorma/html/base_renderer.rb', line 376 def register_toc_entry(id:, level:, text:) @toc_entries << { id: id, level: level, text: text } end |
#render(node, **opts) ⇒ Object
Dispatch to the appropriate render method based on node class.
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/metanorma/html/base_renderer.rb', line 441 def render(node, **opts) case node when Metanorma::Document::Components::Paragraphs::ParagraphBlock render_paragraph(node, **opts) when Metanorma::Document::Components::Tables::TableBlock render_table(node, **opts) when Metanorma::Document::Components::Lists::UnorderedList render_unordered_list(node, **opts) when Metanorma::Document::Components::Lists::OrderedList render_ordered_list(node, **opts) when Metanorma::Document::Components::Lists::DefinitionList render_definition_list(node, **opts) when Metanorma::Document::Components::AncillaryBlocks::FigureBlock render_figure(node, **opts) when Metanorma::Document::Components::Blocks::NoteBlock render_note(node, **opts) when Metanorma::Document::Components::AncillaryBlocks::ExampleBlock render_example(node, **opts) when Metanorma::Document::Components::AncillaryBlocks::SourcecodeBlock render_sourcecode(node, **opts) when Metanorma::Document::Components::AncillaryBlocks::FormulaBlock render_formula(node, **opts) when Metanorma::Document::Components::MultiParagraph::QuoteBlock render_quote(node, **opts) when Metanorma::Document::Components::MultiParagraph::AdmonitionBlock render_admonition(node, **opts) when Metanorma::Document::Components::Sections::HierarchicalSection render_hierarchical_section(node, **opts) when Metanorma::Document::Components::Sections::BasicSection render_basic_section(node, **opts) when Metanorma::Document::Components::Sections::ContentSection render_content_section(node, **opts) when Metanorma::Document::Components::EmptyElements::PageBreakElement "" when Metanorma::Document::Components::IdElements::Bookmark render_bookmark(node) when Metanorma::Document::Components::Inline::SemxElement render_semx_content(node) when String escape_html(node) else "" end end |
#render_liquid(template_name, assigns) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/metanorma/html/base_renderer.rb', line 122 def render_liquid(template_name, assigns) template_path = File.join(TEMPLATES_ROOT, template_name) template = TEMPLATE_CACHE[template_path] assigns = assigns.transform_keys(&:to_s) if assigns.is_a?(Hash) template.render(assigns) end |
#theme ⇒ Object
— Flavor configuration hooks (override in subclasses) —
96 97 98 |
# File 'lib/metanorma/html/base_renderer.rb', line 96 def theme @theme ||= Theme.new end |
#to_html ⇒ Object
— Public API —
74 75 76 |
# File 'lib/metanorma/html/base_renderer.rb', line 74 def to_html @output end |
#toc_entries ⇒ Object
78 79 80 |
# File 'lib/metanorma/html/base_renderer.rb', line 78 def toc_entries @toc_entries end |
#validate_presentation_xml! ⇒ Object
— Validation —
291 292 293 294 295 296 297 298 299 |
# File 'lib/metanorma/html/base_renderer.rb', line 291 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 |