Class: Coradoc::Markdown::AttributeList

Inherits:
Base
  • Object
show all
Defined in:
lib/coradoc/markdown/model/attribute_list.rb

Overview

Represents an Inline Attribute List (IAL) or Attribute List Definition (ALD)

IAL syntax: #id key=“value” ALD syntax: #id .class key=“value”

Examples:

{:.highlight} - adds class "highlight"
{#introduction} - sets id to "introduction"
{:key="value"} - sets attribute key="value"
{:name: #id .class} - defines ALD named "name"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#serialize_content, #to_h, visit, #visit

Class Method Details

.parse(str) ⇒ AttributeList

Parse an IAL string into an AttributeList

Parameters:

  • str (String)

    The IAL string (e.g., ‘#id key=“val”’)

Returns:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/coradoc/markdown/model/attribute_list.rb', line 25

def self.parse(str)
  return nil if str.nil? || str.empty?

  # Remove surrounding braces
  content = str.strip.gsub(/\A\{?:?|\}?\z/, '')

  attr_list = new

  # Check for ALD (has a name before colon)
  if content =~ /\A(\w+):\s*/
    attr_list.name = ::Regexp.last_match(1)
    content = ::Regexp.last_match.post_match
  end

  # Parse the content
  parse_attributes(content, attr_list)

  attr_list
end

.parse_attributes(content, attr_list) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/coradoc/markdown/model/attribute_list.rb', line 83

def self.parse_attributes(content, attr_list)
  # Use shared IalParser for consistent parsing
  ParserUtil::IalParser.tokenize(content).each do |token|
    case token[:type]
    when :class
      attr_list.classes << token[:value]
    when :id
      attr_list.id = token[:value]
    when :attribute
      attr_list.attributes[token[:key]] = token[:value]
    end
  end
end

Instance Method Details

#empty?Boolean

Check if this has any attributes

Returns:

  • (Boolean)


66
67
68
# File 'lib/coradoc/markdown/model/attribute_list.rb', line 66

def empty?
  id.nil? && classes.empty? && attributes.empty?
end

#merge(other) ⇒ AttributeList

Create a merged copy

Parameters:

Returns:



60
61
62
# File 'lib/coradoc/markdown/model/attribute_list.rb', line 60

def merge(other)
  dup.merge!(other)
end

#merge!(other) ⇒ AttributeList

Merge another AttributeList into this one

Parameters:

Returns:



48
49
50
51
52
53
54
55
# File 'lib/coradoc/markdown/model/attribute_list.rb', line 48

def merge!(other)
  return self unless other

  self.id = other.id if other.id
  self.classes = (classes + other.classes).uniq
  self.attributes = attributes.merge(other.attributes)
  self
end

#to_mdString

Convert to Markdown IAL syntax

Returns:

  • (String)


72
73
74
75
76
77
78
79
80
81
# File 'lib/coradoc/markdown/model/attribute_list.rb', line 72

def to_md
  return '' if empty?

  parts = []
  parts << "##{id}" if id
  parts += classes.map { |c| ".#{c}" }
  parts += attributes.map { |k, v| %(#{k}="#{v}") }

  "{:#{parts.join(' ')}}"
end