Class: Coradoc::Html::Converters::Section
- Defined in:
- lib/coradoc/html/converters/section.rb
Overview
Converter for Section structural element
Class Method Summary collapse
-
.to_coradoc(node, state = {}) ⇒ Coradoc::CoreModel::StructuralElement
Convert HTML <section> to CoreModel::StructuralElement.
-
.to_html(model, state = {}) ⇒ String
Convert CoreModel::StructuralElement to HTML <section>.
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
.to_coradoc(node, state = {}) ⇒ Coradoc::CoreModel::StructuralElement
Convert HTML <section> to CoreModel::StructuralElement
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/coradoc/html/converters/section.rb', line 13 def to_coradoc(node, state = {}) # Extract section title from heading title_node = node.at('h1, h2, h3, h4, h5, h6') title = title_node&.text&.strip level = title_node ? title_node.name[1].to_i : 1 # Extract attributes attrs = extract_node_attributes(node) # Process children (skip the heading as we already extracted it) child_nodes = node.children.reject { |child| child.name =~ /^h[1-6]$/ } children = child_nodes.flat_map do |child| convert_node_to_core(child, state) end.compact # Create CoreModel section section = Coradoc::CoreModel::StructuralElement.new( element_type: 'section', level: level, title: title, children: children ) # Set ID if present section.id = attrs[:id] if attrs[:id] section end |
.to_html(model, state = {}) ⇒ String
Convert CoreModel::StructuralElement to HTML <section>
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 73 |
# File 'lib/coradoc/html/converters/section.rb', line 46 def to_html(model, state = {}) parts = [] # Add title as heading if model.title # Calculate heading level (level 0 -> h1, level 1 -> h2, etc.) level = model.level || 1 heading_level = [[level + 1, 1].max, 6].min # Clamp between h1-h6 heading_tag = "h#{heading_level}" title_text = escape_html(model.title) title_attrs = {} title_attrs[:id] = model.id if model.id parts << build_element(heading_tag, title_text, title_attrs) end # Add section children (paragraphs, lists, nested sections, etc.) model.children&.each do |child| parts << convert_content_to_html(child, state) end # Wrap in section tag content = parts.join("\n") attributes = extract_section_attributes(model) build_element('section', content, attributes) end |