Class: Coradoc::Html::Converters::TableCell

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

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

Build HTML attributes from CoreModel::TableCell



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/coradoc/html/converters/table_cell.rb', line 52

def self.build_attributes(cell)
  attrs = []

  attrs << %( id="#{escape_attribute(cell.id)}") if cell.id

  attrs << %( colspan="#{cell.colspan}") if cell.colspan
  attrs << %( rowspan="#{cell.rowspan}") if cell.rowspan

  style_parts = []

  style_parts << "text-align: #{escape_attribute(cell.alignment)}" if cell.alignment
  style_parts << "vertical-align: #{escape_attribute(cell.vertical_alignment)}" if cell.vertical_alignment
  style_parts << "background-color: #{escape_attribute(cell.bgcolor)}" if cell.bgcolor
  style_parts << "color: #{escape_attribute(cell.color)}" if cell.color
  style_parts << "width: #{escape_attribute(cell.width)}" if cell.width
  style_parts << "height: #{escape_attribute(cell.height)}" if cell.height

  # Add style attribute if we have any styles
  attrs << %( style="#{style_parts.join('; ')}") if style_parts.any?

  attrs.join
end

.convert_item(item) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/coradoc/html/converters/table_cell.rb', line 118

def self.convert_item(item)
  case item
  when String
    escape_html(item)
  else
    # Use convert_content_to_html for CoreModel types
    convert_content_to_html(item, {})
  end
end

.extract_cell_attributes(element) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/coradoc/html/converters/table_cell.rb', line 148

def self.extract_cell_attributes(element)
  attrs = {}

  # Extract colspan
  attrs[:colspan] = element['colspan'].to_i if element['colspan']

  # Extract rowspan
  attrs[:rowspan] = element['rowspan'].to_i if element['rowspan']

  # Extract styles from style attribute
  if element['style']
    style = element['style']

    # Horizontal alignment
    if style.include?('text-align')
      align = style[/text-align:\s*(\w+)/, 1]
      attrs[:alignment] = align if align
    end

    # Vertical alignment
    if style.include?('vertical-align')
      valign = style[/vertical-align:\s*(\w+)/, 1]
      attrs[:vertical_alignment] = valign if valign
    end

    # Background color
    if style.include?('background-color')
      bgcolor = style[/background-color:\s*([^;]+)/, 1]
      attrs[:bgcolor] = bgcolor.strip if bgcolor
    end

    # Text color
    if style.include?('color:')
      color = style[/color:\s*([^;]+)/, 1]
      attrs[:color] = color.strip if color
    end

    # Width
    if style.include?('width:')
      width = style[/width:\s*([^;]+)/, 1]
      attrs[:width] = width.strip if width
    end

    # Height
    if style.include?('height:')
      height = style[/height:\s*([^;]+)/, 1]
      attrs[:height] = height.strip if height
    end
  end

  attrs
end

.extract_content(element) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/coradoc/html/converters/table_cell.rb', line 128

def self.extract_content(element)
  # Extract content from the cell
  children = element.children

  if children.size == 1 && children.first.text?
    # Simple text content
    children.first.text
  else
    # Complex content with nested elements
    children.map do |child|
      if child.text?
        child.text
      else
        # Convert HTML element to CoreModel
        convert_node_to_core(child, {})
      end
    end.compact
  end
end

.process_content(cell) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/coradoc/html/converters/table_cell.rb', line 97

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

  # Use renderable_content if available (prefers children over content)
  content = if cell.renderable_content
              cell.renderable_content
            elsif cell.children.any?
              cell.children
            elsif cell.content
              cell.content
            else
              cell
            end

  if content.is_a?(Array)
    content.map { |item| convert_item(item) }.join
  else
    convert_item(content)
  end
end

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

Convert HTML <td> or <th> to CoreModel::TableCell



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

def self.to_coradoc(element, _options = {})
  return nil unless %w[td th].include?(element.name)

  # Determine if this is a header cell
  is_header = element.name == 'th'

  # Extract content - could be text or nested elements
  content = extract_content(element)

  # Extract cell attributes
  attrs = extract_cell_attributes(element)

  Coradoc::CoreModel::TableCell.new(
    content: content,
    header: is_header,
    **attrs.compact
  )
end

.to_html(cell, _options = {}) ⇒ String

Convert CoreModel::TableCell to HTML <td> or <th>

Parameters:

  • cell (Coradoc::CoreModel::TableCell)

    Table cell model

  • _options (Hash) (defaults to: {})

    Conversion options

Returns:

  • (String)

    HTML string



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

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

  # Check if this is a header cell
  is_header = cell.header == true
  tag = is_header ? 'th' : 'td'

  # Build cell attributes
  attrs = build_attributes(cell)

  # Process cell content
  content = process_content(cell)

  # Wrap content in style tags if needed
  content = wrap_with_style(content, cell)

  "<#{tag}#{attrs}>#{content}</#{tag}>"
end

.wrap_with_style(content, cell) ⇒ Object

Wrap content with style tags based on cell style



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/coradoc/html/converters/table_cell.rb', line 76

def self.wrap_with_style(content, cell)
  return content unless cell.style

  case cell.style.to_s.downcase
  when 'strong', 's'
    "<strong>#{content}</strong>"
  when 'emphasis', 'e'
    "<em>#{content}</em>"
  when 'monospace', 'm'
    "<code>#{content}</code>"
  when 'literal', 'l'
    # Literal content - preserve whitespace
    "<pre>#{content}</pre>"
  when 'verse', 'v'
    # Verse content - preserve formatting
    "<div class=\"verse\">#{content}</div>"
  else
    content
  end
end