Class: Coradoc::Html::Converters::Attribute

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

Overview

Converts document attributes to/from HTML

Attributes are document-level directives (e.g., :author:, :toc:). In HTML, we represent them as HTML comments to preserve the attribute information without affecting the rendered output.

Examples:

:author: John Doe  => <!-- :author: John Doe -->
:toc:              => <!-- :toc: -->

Class Method Summary collapse

Class Method Details

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

Convert HTML comment to CoreModel::Block (attribute)



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/attribute.rb', line 37

def self.to_coradoc(element, _options = {})
  return nil unless element.is_a?(Nokogiri::XML::Comment)

  content = element.content.strip

  # Match attribute pattern: :key: or :key: value
  return nil unless content.match?(/^:([^:]+):(.*)$/)

  match = content.match(/^:([^:]+):(.*)$/)
  key = match[1].strip
  value_part = match[2].strip

  # Parse value(s) - could be comma-separated
  values = if value_part.empty?
             []
           else
             value_part.split(',').map(&:strip)
           end

  Coradoc::CoreModel::Block.new(
    element_type: 'attribute',
    content: key,
    metadata: {
      key: key,
      value: values
    }
  )
end

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

Convert CoreModel::Block (attribute) to HTML comment



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/coradoc/html/converters/attribute.rb', line 19

def self.to_html(model, _options = {})
  key = model.&.dig(:key).to_s
  key = escape_html(key)

  # Handle single value or array of values
  values = Array(model.&.dig(:value)).compact

  if values.empty?
    # Attribute with no value (e.g., :toc:)
    "<!-- :#{key}: -->"
  else
    # Attribute with value(s)
    value_str = values.map { |v| escape_html(v.to_s) }.join(', ')
    "<!-- :#{key}: #{value_str} -->"
  end
end