Module: Coradoc::Html::Base

Defined in:
lib/coradoc/html/base.rb

Overview

Base module for HTML processing utilities

Class Method Summary collapse

Class Method Details

.build_attributes(attributes) ⇒ Object

Build HTML attributes string



105
106
107
108
109
110
111
112
113
114
# File 'lib/coradoc/html/base.rb', line 105

def build_attributes(attributes)
  return '' if attributes.nil? || attributes.empty?

  attributes.map do |key, value|
    next if value.nil?

    escaped_value = escape_html(value.to_s)
    %(#{key}="#{escaped_value}")
  end.compact.join(' ')
end

.build_element(tag, content = nil, attributes = {}) ⇒ Object

Build HTML element with attributes



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/coradoc/html/base.rb', line 80

def build_element(tag, content = nil, attributes = {})
  attrs = build_attributes(attributes)
  attr_string = attrs.empty? ? '' : " #{attrs}"

  # Handle empty content (String, Array, or nil)
  content_empty = case content
                  when nil, String, Array
                    content.nil? || content.empty?
                  else
                    false
                  end

  if content_empty
    # Self-closing for void elements
    if void_element?(tag)
      "<#{tag}#{attr_string}>"
    else
      "<#{tag}#{attr_string}></#{tag}>"
    end
  else
    "<#{tag}#{attr_string}>#{content}</#{tag}>"
  end
end

.convert_content(content, state = {}) ⇒ Object

Convert content to HTML, handling various input types



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

def convert_content(content, state = {})
  return '' if content.nil?

  case content
  when String
    return '' if content.empty?

    escape_html(content)
  when Array
    return '' if content.empty?

    content.map { |item| convert_content(item, state) }.join
  else
    # If content responds to a converter, use it
    converter = find_converter(content.class)
    if converter
      converter.to_html(content, state)
    else
      content.to_s
    end
  end
end

.escape_html(text) ⇒ Object

Escape HTML special characters



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/coradoc/html/base.rb', line 53

def escape_html(text)
  return '' if text.nil?
  return text unless text.is_a?(String)

  text
    .gsub('&', '&amp;')
    .gsub('<', '&lt;')
    .gsub('>', '&gt;')
    .gsub('"', '&quot;')
    .gsub("'", '&#39;')
end

.extract_attributes(model) ⇒ Hash

Extract attributes from a CoreModel

Parameters:

  • model (Coradoc::CoreModel::Base)

    Model to extract attributes from

Returns:

  • (Hash)

    Attributes hash



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/coradoc/html/base.rb', line 125

def extract_attributes(model)
  attrs = {}

  attrs[:id] = model.id if model.id
  attrs[:title] = model.title if model.title

  if model.is_a?(Coradoc::CoreModel::StructuralElement) && model.
    attrs[:class] = model.[:class] || model.[:role]
    attrs.merge!(model..except(:class, :role))
  end

  attrs
end

.find_converter(model_class) ⇒ Object

Find the appropriate converter for a model class



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/coradoc/html/base.rb', line 33

def find_converter(model_class)
  return nil unless defined?(Coradoc::Html::Converters)

  converter_name = model_class.name.split('::').last

  # Use const_get on the Converters module to trigger autoload
  begin
    klass = Coradoc::Html::Converters.const_get(converter_name, false)
    # Return nil if this is the Base class itself (not a real converter)
    # or if it doesn't inherit from Converters::Base
    return nil if klass == Coradoc::Html::Converters::Base
    return nil unless klass <= Coradoc::Html::Converters::Base

    klass
  rescue NameError
    nil
  end
end

.treat_children(children, state = {}) ⇒ Object

Process children of a node (common operation)



147
148
149
150
151
152
153
# File 'lib/coradoc/html/base.rb', line 147

def treat_children(children, state = {})
  return [] if children.nil? || children.empty?

  Array(children).map do |child|
    convert_content(child, state)
  end
end

.unescape_html(text) ⇒ Object

Unescape HTML entities



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/coradoc/html/base.rb', line 66

def unescape_html(text)
  return '' if text.nil?
  return text unless text.is_a?(String)

  text
    .gsub('&amp;', '&')
    .gsub('&lt;', '<')
    .gsub('&gt;', '>')
    .gsub('&quot;', '"')
    .gsub('&#39;', "'")
    .gsub('&#x27;', "'")
end

.void_element?(tag) ⇒ Boolean

Check if element is a void element (self-closing)

Returns:

  • (Boolean)


117
118
119
# File 'lib/coradoc/html/base.rb', line 117

def void_element?(tag)
  %w[area base br col embed hr img input link meta param source track wbr].include?(tag.to_s)
end

.wrap_lines(content) ⇒ Object

Wrap content with line breaks if needed



140
141
142
143
144
# File 'lib/coradoc/html/base.rb', line 140

def wrap_lines(content)
  return content unless content.is_a?(String)

  content.split("\n").join("<br>\n")
end