Class: Coradoc::Html::Converters::Listing

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

Overview

Converter for CoreModel::Block (listing) 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(listing) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/coradoc/html/converters/listing.rb', line 65

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

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

  attrs.join
end

.build_title(listing) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/coradoc/html/converters/listing.rb', line 74

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

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

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

.process_content(content) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/coradoc/html/converters/listing.rb', line 83

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

  # For listing, preserve the content 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 (listing)



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

def self.to_coradoc(element, _options = {})
  # Handle both <pre> and <div class="listing-block"><pre>
  pre_elem = if element.name == 'div' && element['class']&.include?('listing-block')
               element.at_css('pre')
             elsif element.name == 'pre'
               element
             else
               return nil
             end

  return nil unless pre_elem

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

  # Extract content
  content = pre_elem.text

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

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

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

Convert CoreModel::Block (listing) 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/listing.rb', line 9

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

  # Build pre attributes
  attrs = build_attributes(listing)

  # Build title if present
  title_html = build_title(listing)

  # Process listing content - preserve formatting
  content = process_content(listing.content)

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

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