Class: Coradoc::Html::Converters::Source

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

Overview

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

Direct Known Subclasses

SourceCode

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



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

def self.build_code_attributes(source)
  attrs = []

  # Add language class if present
  lang = source.language

  attrs << %( class="language-#{escape_attribute(lang)}") if lang && !lang.empty?

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

  attrs.join
end

.build_pre_attributes(_source) ⇒ Object



88
89
90
# File 'lib/coradoc/html/converters/source.rb', line 88

def self.build_pre_attributes(_source)
  %( class="source")
end

.build_title(source) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/coradoc/html/converters/source.rb', line 92

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

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

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

.extract_language(element) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/coradoc/html/converters/source.rb', line 115

def self.extract_language(element)
  return nil unless element['class']

  # Extract language from class like "language-ruby", "lang-python", etc.
  classes = element['class'].split
  lang_class = classes.find { |c| c.start_with?('language-', 'lang-') }
  return nil unless lang_class

  lang_class.sub(/^(language-|lang-)/, '')
end

.process_content(content) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/coradoc/html/converters/source.rb', line 101

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

  # For source code, preserve the content exactly
  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><code> to CoreModel::Block (source)



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
67
68
69
70
71
72
# File 'lib/coradoc/html/converters/source.rb', line 37

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

  return nil unless code_elem

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

  # Extract language from class
  language = extract_language(code_elem)

  # Extract content
  content = code_elem.text

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

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

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

Convert CoreModel::Block (source) to HTML <pre><code>



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

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

  # Build title if present
  title_html = build_title(source)

  # Build code attributes with language
  code_attrs = build_code_attributes(source)

  # Build pre attributes
  pre_attrs = build_pre_attributes(source)

  # Process source content
  content = process_content(source.content)

  # Combine into source block
  source_html = ''
  source_html += "#{title_html}\n" if title_html
  source_html += %(<pre#{pre_attrs}><code#{code_attrs}>#{content}</code></pre>)

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