Class: Coradoc::Html::Converters::Verse

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

Overview

Converter for CoreModel::Block (verse) to HTML <div class=“verse”>

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



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

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

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

  attrs.join
end

.build_attribution(verse) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/coradoc/html/converters/verse.rb', line 79

def self.build_attribution(verse)
  attribution_text = verse.&.dig(:attribution)
  return nil unless attribution_text

  attribution_text = attribution_text.to_s.strip
  return nil if attribution_text.empty?

  %(<footer>#{escape_html(attribution_text)}</footer>)
end

.build_title(verse) ⇒ Object



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

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

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

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

.process_content(content) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/coradoc/html/converters/verse.rb', line 89

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

  # For verse, preserve the content as-is with line breaks
  if content.is_a?(String)
    escape_html(content)
  elsif content.is_a?(Array)
    # Join array items with newlines
    content.map { |line| escape_html(line.to_s) }.join("\n")
  else
    escape_html(content.to_s)
  end
end

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

Convert HTML <div class=“verse”> to CoreModel::Block (verse)



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
# File 'lib/coradoc/html/converters/verse.rb', line 34

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

  # Extract title if present
  title_elem = element.at_css('.verse-title')
  title = title_elem&.text&.strip

  # Extract content from <pre class="verse-content">
  content_elem = element.at_css('.verse-content, pre')
  content = content_elem&.text || ''

  # Extract attribution from <cite> or <footer>
  cite_elem = element.at_css('cite, footer')
  attribution = cite_elem&.text&.strip

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

  Coradoc::CoreModel::VerseBlock.new(
    content: content,
    title: title,
    id: id,
    attribution: attribution
  )
end

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

Convert CoreModel::Block (verse) to HTML <div class=“verse”>



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

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

  # Build div attributes
  attrs = build_attributes(verse)

  # Build title if present
  title_html = build_title(verse)

  # Build attribution if present
  attribution = build_attribution(verse)

  # Process verse content - preserve line breaks
  content = process_content(verse.content)

  # Combine title, content, and attribution
  verse_html = ''
  verse_html += "#{title_html}\n" if title_html
  verse_html += %(<pre class="verse-content">#{content}</pre>)
  verse_html += "\n#{attribution}" if attribution

  %(<div#{attrs}>\n#{verse_html}\n</div>)
end