Class: Coradoc::Html::Converters::Table

Inherits:
Base
  • Object
show all
Defined in:
lib/coradoc/html/converters/table.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(table) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/coradoc/html/converters/table.rb', line 47

def self.build_attributes(table)
  attrs = []

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

  # CoreModel::Table with frame attribute
  attrs << %( class="frame-#{escape_attribute(table.frame)}") if table.frame

  attrs.join
end

.build_caption(table) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/coradoc/html/converters/table.rb', line 59

def self.build_caption(table)
  return nil unless table.title

  caption_text = table.title.to_s
  return nil if caption_text.empty?

  "<caption>#{escape_html(caption_text)}</caption>"
end

.extract_table_attributes(element) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/coradoc/html/converters/table.rb', line 68

def self.extract_table_attributes(element)
  attrs = {}

  # Extract id attribute
  attrs[:id] = element['id'] if element['id']

  # Extract frame attribute from class
  if element['class']&.include?('frame-')
    frame = element['class'][/frame-(\w+)/, 1]
    attrs[:frame] = frame if frame
  end

  attrs
end

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

Convert HTML <table> to CoreModel::Table



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/coradoc/html/converters/table.rb', line 23

def self.to_coradoc(element, _options = {})
  return nil unless element.name == 'table'

  rows = element.css('tr').map do |tr|
    TableRow.to_coradoc(tr)
  end.compact

  # Extract caption if present
  caption_elem = element.at_css('caption')
  title = caption_elem&.text&.strip

  # Extract table attributes
  attrs = extract_table_attributes(element)

  table = Coradoc::CoreModel::Table.new(
    rows: rows,
    title: title
  )
  table.id = attrs[:id] if attrs[:id]
  table.frame = attrs[:frame] if attrs[:frame]

  table
end

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

Convert CoreModel::Table to HTML <table>



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/coradoc/html/converters/table.rb', line 8

def self.to_html(table, _options = {})
  attrs = build_attributes(table)
  caption = build_caption(table)

  rows_html = table.rows.map do |row|
    TableRow.to_html(row)
  end.join("\n")

  table_content = rows_html
  table_content = "#{caption}\n#{table_content}" if caption

  "<table#{attrs}>\n#{table_content}\n</table>"
end