Class: Coradoc::Html::Converters::Bibliography

Inherits:
Base
  • Object
show all
Defined in:
lib/coradoc/html/converters/bibliography.rb

Overview

Converter for CoreModel::Block (bibliography) to HTML bibliography section

Class Method Summary collapse

Methods inherited from Base

build_class_attribute, build_element, build_html_attributes, convert_content_to_html, convert_element_to_core, convert_node_to_core, escape_attribute, escape_html, extract_model_attributes, extract_node_attributes, extract_text_fallback, find_converter_class_by_name, find_converter_for_model, handle_unknown_content, render_core_abbreviation, render_core_annotation_block, render_core_bibliography, render_core_bibliography_entry, render_core_block, render_core_block_image, render_core_definition_item, render_core_definition_list, render_core_footnote, render_core_footnote_reference, render_core_inline_element, render_core_inline_image, render_core_list_block, render_core_list_item, render_core_span, render_core_structural_element, render_core_table_cell, render_core_table_row, render_core_term, render_core_toc, render_core_toc_entry, resolve_block_semantic_type, resolve_format_specific_semantic, transform_to_coremodel, treat_children

Class Method Details

.build_attributes(bibliography) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/coradoc/html/converters/bibliography.rb', line 62

def self.build_attributes(bibliography)
  attrs = [%( class="bibliography")]

  # Add ID if present
  attrs << %( id="#{escape_attribute(bibliography.id)}") if bibliography.id

  attrs.join
end

.build_title(bibliography) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/coradoc/html/converters/bibliography.rb', line 71

def self.build_title(bibliography)
  return nil unless bibliography.title

  title_text = bibliography.title.to_s
  return nil if title_text.empty?

  %(<h2 class="bibliography-title">#{escape_html(title_text)}</h2>)
end

.to_coradoc(element, _options = {}) ⇒ Object

Convert HTML bibliography section to CoreModel::Block (bibliography)



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/coradoc/html/converters/bibliography.rb', line 33

def self.to_coradoc(element, _options = {})
  return nil unless element.name == 'section'
  return nil unless element['class']&.include?('bibliography')

  # Extract title
  title_elem = element.at_css('h1, h2, h3, h4, h5, h6, .bibliography-title')
  title = title_elem&.text&.strip

  # Extract entries
  entries_container = element.at_css('.bibliography-entries')
  entries = if entries_container
              entries_container.css('.bibliography-entry').map do |entry_elem|
                BibliographyEntry.to_coradoc(entry_elem)
              end.compact
            else
              []
            end

  # Extract ID if present
  id = element['id']

  Coradoc::CoreModel::Block.new(
    element_type: 'bibliography',
    title: title,
    id: id,
    children: entries
  )
end

.to_html(bibliography, _options = {}) ⇒ Object

Convert CoreModel::Block (bibliography) to HTML bibliography section



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/coradoc/html/converters/bibliography.rb', line 9

def self.to_html(bibliography, _options = {})
  return '' unless bibliography

  # Build section attributes
  attrs = build_attributes(bibliography)

  # Build title
  title_html = build_title(bibliography)

  # Process bibliography entries (children)
  entries = bibliography.children || []
  entries_html = entries.map do |entry|
    BibliographyEntry.to_html(entry)
  end.join("\n")

  # Combine into bibliography section
  bib_html = ''
  bib_html += "#{title_html}\n" if title_html
  bib_html += %(<div class="bibliography-entries">\n#{entries_html}\n</div>) unless entries_html.empty?

  %(<section#{attrs}>\n#{bib_html}\n</section>)
end