Class: Coradoc::Html::Converters::Literal

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

Overview

Converter for CoreModel::Block (literal) to HTML <pre>

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



68
69
70
71
72
73
74
75
# File 'lib/coradoc/html/converters/literal.rb', line 68

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

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

  attrs.join
end

.build_title(literal) ⇒ Object



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

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

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

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

.process_content(content) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/coradoc/html/converters/literal.rb', line 86

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

  # For literal, preserve the content exactly as-is
  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 <pre> to CoreModel::Block (literal)



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
61
62
63
64
65
66
# File 'lib/coradoc/html/converters/literal.rb', line 34

def self.to_coradoc(element, _options = {})
  # Handle both <pre> and <div class="literal-block"><pre>
  pre_elem = if element.name == 'div' && element['class']&.include?('literal-block')
               element.at_css('pre')
             elsif element.name == 'pre'
               # Only convert if it's a literal (no code class)
               return nil if element['class']&.include?('code')

               element
             else
               return nil
             end

  return nil unless pre_elem

  # Extract title if in literal-block wrapper
  title = if element.name == 'div'
            title_elem = element.at_css('.literal-title')
            title_elem&.text&.strip
          end

  # Extract content
  content = pre_elem.text

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

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

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

Convert CoreModel::Block (literal) to HTML <pre>



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/literal.rb', line 9

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

  # Build pre attributes
  attrs = build_attributes(literal)

  # Build title if present
  title_html = build_title(literal)

  # Process literal content - preserve exact formatting
  content = process_content(literal.content)

  # Combine title and content
  literal_html = ''
  literal_html += "#{title_html}\n" if title_html
  literal_html += %(<pre#{attrs}>#{content}</pre>)

  if title_html
    %(<div class="literal-block">\n#{literal_html}\n</div>)
  else
    literal_html
  end
end