Class: Coradoc::Html::Converters::Include

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

Overview

Converter for include directives

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

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

Convert HTML comment with include directive to CoreModel::Block (include)



31
32
33
34
35
36
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
# File 'lib/coradoc/html/converters/include.rb', line 31

def self.to_coradoc(element, _options = {})
  return nil unless element.comment?

  # Check if comment contains include directive
  text = element.text.to_s.strip
  return nil unless text.match?(/^include::/)

  # Parse include directive
  # Format: include::path[attributes]
  return unless text =~ /^include::([^\[]+)(\[([^\]]*)\])?/

  path = ::Regexp.last_match(1).strip
  attrs_str = ::Regexp.last_match(3)

  attrs = {}
  if attrs_str && !attrs_str.empty?
    # Parse attributes (simplified - doesn't handle complex cases)
    attrs_str.split(',').each do |attr|
      if attr.include?('=')
        k, v = attr.split('=', 2)
        attrs[k.strip.to_sym] = v.strip
      end
    end
  end

  Coradoc::CoreModel::Block.new(
    element_type: 'include',
    content: path,
    metadata: {
      path: path,
      attributes: attrs
    }
  )
end

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

Convert CoreModel::Block (include) to HTML comment with include directive Note: HTML doesn’t have native include support, so we use a comment



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

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

  # Get include path from metadata
  path = include_directive.&.dig(:path) || ''
  path = escape_html(path)

  # Build include directive as comment
  comment_text = "include::#{path}[]"

  # Add attributes if present
  attrs = include_directive.&.dig(:attributes) || {}
  if attrs && !attrs.empty?
    attrs_str = attrs.map { |k, v| "#{k}=#{v}" }.join(',')
    comment_text = "include::#{path}[#{attrs_str}]" unless attrs_str.empty?
  end

  "<!-- #{comment_text} -->"
end