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 —
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) ⇒ 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.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/metanorma/html/base_renderer.rb', line 51 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.
424 425 426 |
# File 'lib/metanorma/html/base_renderer.rb', line 424 def footnote_collector @footnote_collector end |
#index_term_collector ⇒ Object (readonly)
Returns the value of attribute index_term_collector.
424 425 426 |
# File 'lib/metanorma/html/base_renderer.rb', line 424 def index_term_collector @index_term_collector end |
Instance Method Details
#assemble_document(body) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/metanorma/html/base_renderer.rb', line 153 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
242 243 244 245 246 247 248 |
# File 'lib/metanorma/html/base_renderer.rb', line 242 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 —
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/metanorma/html/base_renderer.rb', line 173 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
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 200 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
196 197 198 |
# File 'lib/metanorma/html/base_renderer.rb', line 196 def build_reader_controls "" end |
#build_scripts ⇒ Object
— Scripts —
292 293 294 295 296 |
# File 'lib/metanorma/html/base_renderer.rb', line 292 def build_scripts pipeline = AssetPipeline.new compiled = pipeline.compile_js(flavor_js: flavor_js_module) "<script>\n#{compiled}\n</script>" end |
#build_styles ⇒ Object
306 307 308 309 310 |
# File 'lib/metanorma/html/base_renderer.rb', line 306 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 —
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 284 285 286 287 288 |
# File 'lib/metanorma/html/base_renderer.rb', line 252 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
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 358 359 360 361 362 |
# File 'lib/metanorma/html/base_renderer.rb', line 324 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 |
#detect_publishers ⇒ Object
219 220 221 |
# File 'lib/metanorma/html/base_renderer.rb', line 219 def detect_publishers flavor_publishers(extract_primary_doc_id) end |
#extract_plain_text(node) ⇒ Object
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 471 472 473 474 475 |
# File 'lib/metanorma/html/base_renderer.rb', line 426 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
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/metanorma/html/base_renderer.rb', line 392 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
302 303 304 |
# File 'lib/metanorma/html/base_renderer.rb', line 302 def flavor_css_module nil end |
#flavor_font_url ⇒ Object
138 139 140 |
# File 'lib/metanorma/html/base_renderer.rb', line 138 def flavor_font_url theme.font_url end |
#flavor_js_module ⇒ Object
298 299 300 |
# File 'lib/metanorma/html/base_renderer.rb', line 298 def flavor_js_module nil end |
#flavor_publisher_name ⇒ Object
128 129 130 131 |
# File 'lib/metanorma/html/base_renderer.rb', line 128 def flavor_publisher_name pubs = flavor_publishers(extract_primary_doc_id) pubs.empty? ? nil : pubs.join("/") end |
#flavor_publishers(_doc_id) ⇒ Object
124 125 126 |
# File 'lib/metanorma/html/base_renderer.rb', line 124 def flavor_publishers(_doc_id) [] end |
#generate_full_document(document) ⇒ Object
Generate a complete HTML document from a presentation XML document.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/metanorma/html/base_renderer.rb', line 107 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
190 191 192 193 |
# File 'lib/metanorma/html/base_renderer.rb', line 190 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
379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/metanorma/html/base_renderer.rb', line 379 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 —
366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/metanorma/html/base_renderer.rb', line 366 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
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/metanorma/html/base_renderer.rb', line 223 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.
134 135 136 |
# File 'lib/metanorma/html/base_renderer.rb', line 134 def publisher_logo_map {} end |
#register_figure_entry(id:, text:) ⇒ Object
416 417 418 |
# File 'lib/metanorma/html/base_renderer.rb', line 416 def register_figure_entry(id:, text:) @figure_entries << { id: id, text: text } end |
#register_table_entry(id:, text:) ⇒ Object
420 421 422 |
# File 'lib/metanorma/html/base_renderer.rb', line 420 def register_table_entry(id:, text:) @table_entries << { id: id, text: text } end |
#register_toc_entry(id:, level:, text:) ⇒ Object
— CSS loader —
412 413 414 |
# File 'lib/metanorma/html/base_renderer.rb', line 412 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.
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 517 518 519 520 521 |
# File 'lib/metanorma/html/base_renderer.rb', line 478 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
146 147 148 149 150 151 |
# File 'lib/metanorma/html/base_renderer.rb', line 146 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 |
#renderer_context ⇒ Object
94 95 96 |
# File 'lib/metanorma/html/base_renderer.rb', line 94 def renderer_context @renderer_context ||= RendererContext.new(self) end |
#theme ⇒ Object
— Flavor configuration hooks (override in subclasses) —
120 121 122 |
# File 'lib/metanorma/html/base_renderer.rb', line 120 def theme @theme ||= Theme.new end |
#to_html ⇒ Object
98 99 100 |
# File 'lib/metanorma/html/base_renderer.rb', line 98 def to_html @output end |
#toc_entries ⇒ Object
102 103 104 |
# File 'lib/metanorma/html/base_renderer.rb', line 102 def toc_entries @toc_entries end |
#validate_presentation_xml! ⇒ Object
— Validation —
314 315 316 317 318 319 320 321 322 |
# File 'lib/metanorma/html/base_renderer.rb', line 314 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 |