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
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_scripts ⇒ Object
— Scripts —.
- #build_styles ⇒ Object
-
#build_toc_html(entries) ⇒ Object
— ToC generation —.
- #check_presentation_markers(node) ⇒ 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) ⇒ Object
Dispatch to the appropriate render method based on node class.
- #render_liquid(template_name, assigns) ⇒ Object
- #renderer_context ⇒ Object
-
#theme ⇒ Object
— Flavor configuration hooks (override in subclasses) —.
- #to_html ⇒ Object
- #toc_entries ⇒ Object
-
#validate_presentation_xml! ⇒ Object
— Validation —.
Constructor Details
#initialize ⇒ BaseRenderer
Returns a new instance of BaseRenderer.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/metanorma/html/base_renderer.rb', line 52 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.
419 420 421 |
# File 'lib/metanorma/html/base_renderer.rb', line 419 def footnote_collector @footnote_collector end |
#index_term_collector ⇒ Object (readonly)
Returns the value of attribute index_term_collector.
419 420 421 |
# File 'lib/metanorma/html/base_renderer.rb', line 419 def index_term_collector @index_term_collector end |
Instance Method Details
#assemble_document(body) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/metanorma/html/base_renderer.rb', line 157 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
237 238 239 240 241 242 243 |
# File 'lib/metanorma/html/base_renderer.rb', line 237 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 —
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/metanorma/html/base_renderer.rb', line 177 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
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/metanorma/html/base_renderer.rb', line 199 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 —
287 288 289 290 291 |
# File 'lib/metanorma/html/base_renderer.rb', line 287 def build_scripts pipeline = AssetPipeline.new compiled = pipeline.compile_js(flavor_js: flavor_js_module) "<script>\n#{compiled}\n</script>" end |
#build_styles ⇒ Object
301 302 303 304 305 |
# File 'lib/metanorma/html/base_renderer.rb', line 301 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 —
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/metanorma/html/base_renderer.rb', line 247 def build_toc_html(entries) top_lines = [] main_lines = if entries.empty? ["<li class=\"toc-empty\">No entries</li>"] else entries.map do |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 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 do |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 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 do |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 end top_lines << "<li class=\"toc-divider\"></li>" unless top_lines.empty? (top_lines + main_lines).join("\n") end |
#check_presentation_markers(node) ⇒ Object
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 349 350 351 352 353 354 355 356 357 |
# File 'lib/metanorma/html/base_renderer.rb', line 319 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 begin node.fmt_title rescue StandardError nil end return true if begin node.displayorder rescue StandardError nil end %i[preface sections annex bibliography].each do |attr| val = begin node.public_send(attr) rescue StandardError nil end 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 |
#extract_plain_text(node) ⇒ Object
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 462 463 464 465 466 467 468 469 470 |
# File 'lib/metanorma/html/base_renderer.rb', line 421 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
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/metanorma/html/base_renderer.rb', line 387 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
297 298 299 |
# File 'lib/metanorma/html/base_renderer.rb', line 297 def flavor_css_module nil end |
#flavor_font_url ⇒ Object
139 140 141 |
# File 'lib/metanorma/html/base_renderer.rb', line 139 def flavor_font_url theme.font_url end |
#flavor_js_module ⇒ Object
293 294 295 |
# File 'lib/metanorma/html/base_renderer.rb', line 293 def flavor_js_module nil end |
#flavor_publisher_name ⇒ Object
129 130 131 132 |
# File 'lib/metanorma/html/base_renderer.rb', line 129 def flavor_publisher_name pubs = flavor_publishers(extract_primary_doc_id) pubs.empty? ? nil : pubs.join("/") end |
#flavor_publishers(_doc_id) ⇒ Object
125 126 127 |
# File 'lib/metanorma/html/base_renderer.rb', line 125 def flavor_publishers(_doc_id) [] end |
#generate_full_document(document) ⇒ Object
Generate a complete HTML document from a presentation XML document.
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/metanorma/html/base_renderer.rb', line 108 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
194 195 196 197 |
# File 'lib/metanorma/html/base_renderer.rb', line 194 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
374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/metanorma/html/base_renderer.rb', line 374 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 —
361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/metanorma/html/base_renderer.rb', line 361 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
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 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 |
#publisher_logo_map ⇒ Object
Map of publisher name => logo filename. Override in flavor renderers.
135 136 137 |
# File 'lib/metanorma/html/base_renderer.rb', line 135 def publisher_logo_map {} end |
#register_figure_entry(id:, text:) ⇒ Object
411 412 413 |
# File 'lib/metanorma/html/base_renderer.rb', line 411 def register_figure_entry(id:, text:) @figure_entries << { id: id, text: text } end |
#register_table_entry(id:, text:) ⇒ Object
415 416 417 |
# File 'lib/metanorma/html/base_renderer.rb', line 415 def register_table_entry(id:, text:) @table_entries << { id: id, text: text } end |
#register_toc_entry(id:, level:, text:) ⇒ Object
— CSS loader —
407 408 409 |
# File 'lib/metanorma/html/base_renderer.rb', line 407 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 based on node class.
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/metanorma/html/base_renderer.rb', line 473 def render(node, **) case node when Metanorma::Document::Components::Paragraphs::ParagraphBlock render_paragraph(node, **) when Metanorma::Document::Components::Tables::TableBlock render_table(node, **) when Metanorma::Document::Components::Lists::UnorderedList render_unordered_list(node, **) when Metanorma::Document::Components::Lists::OrderedList render_ordered_list(node, **) when Metanorma::Document::Components::Lists::DefinitionList render_definition_list(node, **) when Metanorma::Document::Components::AncillaryBlocks::FigureBlock render_figure(node, **) when Metanorma::Document::Components::Blocks::NoteBlock render_note(node, **) when Metanorma::Document::Components::AncillaryBlocks::ExampleBlock render_example(node, **) when Metanorma::Document::Components::AncillaryBlocks::SourcecodeBlock render_sourcecode(node, **) when Metanorma::Document::Components::AncillaryBlocks::FormulaBlock render_formula(node, **) when Metanorma::Document::Components::MultiParagraph::QuoteBlock render_quote(node, **) when Metanorma::Document::Components::MultiParagraph::AdmonitionBlock render_admonition(node, **) when Metanorma::Document::Components::Sections::HierarchicalSection render_hierarchical_section(node, **) when Metanorma::Document::Components::Sections::BasicSection render_basic_section(node, **) when Metanorma::Document::Components::Sections::ContentSection render_content_section(node, **) 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
148 149 150 151 152 153 154 155 |
# File 'lib/metanorma/html/base_renderer.rb', line 148 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 |
#renderer_context ⇒ Object
95 96 97 |
# File 'lib/metanorma/html/base_renderer.rb', line 95 def renderer_context @renderer_context ||= RendererContext.new(self) end |
#theme ⇒ Object
— Flavor configuration hooks (override in subclasses) —
121 122 123 |
# File 'lib/metanorma/html/base_renderer.rb', line 121 def theme @theme ||= Theme.new end |
#to_html ⇒ Object
99 100 101 |
# File 'lib/metanorma/html/base_renderer.rb', line 99 def to_html @output end |
#toc_entries ⇒ Object
103 104 105 |
# File 'lib/metanorma/html/base_renderer.rb', line 103 def toc_entries @toc_entries end |
#validate_presentation_xml! ⇒ Object
— Validation —
309 310 311 312 313 314 315 316 317 |
# File 'lib/metanorma/html/base_renderer.rb', line 309 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 |