Class: Coradoc::Html::Converters::BibliographyEntry

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

Overview

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

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(_entry) ⇒ Object



72
73
74
# File 'lib/coradoc/html/converters/bibliography_entry.rb', line 72

def self.build_attributes(_entry)
  %( class="bibliography-entry")
end

.convert_item(item) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/coradoc/html/converters/bibliography_entry.rb', line 88

def self.convert_item(item)
  case item
  when String
    escape_html(item)
  else
    convert_content_to_html(item)
  end
end

.extract_content(nodes) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/coradoc/html/converters/bibliography_entry.rb', line 97

def self.extract_content(nodes)
  # Extract and convert content nodes
  nodes.map do |node|
    if node.text?
    end
    node.text
  end.compact.join
end

.process_content(content) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/coradoc/html/converters/bibliography_entry.rb', line 76

def self.process_content(content)
  return '' if content.nil?

  if content.is_a?(String)
    escape_html(content)
  elsif content.is_a?(Array)
    content.map { |item| convert_item(item) }.join
  else
    convert_item(content)
  end
end

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

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/coradoc/html/converters/bibliography_entry.rb', line 43

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

  # Extract anchor/ID
  anchor = element.at_css('.bibliography-anchor, a[id]')
  entry_id = anchor&.[]('id')

  # Extract label
  label_elem = element.at_css('.bibliography-label')
  label = label_elem&.text&.strip

  # Extract content (everything except anchor and label)
  content_nodes = element.children.reject do |node|
    node == anchor || node == label_elem || (node.text? && node.text.strip.empty?)
  end

  content = extract_content(content_nodes)

  Coradoc::CoreModel::Block.new(
    element_type: 'bibliography_entry',
    content: content,
    id: entry_id,
    metadata: {
      label: label
    }
  )
end

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

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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/coradoc/html/converters/bibliography_entry.rb', line 9

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

  # Build entry attributes
  attrs = build_attributes(entry)

  # Get entry ID from metadata
  entry_id = entry.&.dig(:anchor_name) || entry.&.dig(:document_id) || entry.id

  # Build anchor if ID present
  anchor_html = if entry_id
                  %(<a id="#{escape_attribute(entry_id)}" class="bibliography-anchor"></a>)
                else
                  ''
                end

  # Get citation label
  label = entry.&.dig(:label) || entry_id || ''

  # Get entry reference text
  content = entry.content || ''

  # Process content
  content_html = process_content(content)

  # Combine into entry
  entry_html = anchor_html
  entry_html += %(<span class="bibliography-label">#{escape_html(label)}</span> ) unless label.empty?
  entry_html += content_html

  %(<div#{attrs}>#{entry_html}</div>)
end