Class: Coradoc::Markdown::AttributeList
- 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
-
.parse(str) ⇒ AttributeList
Parse an IAL string into an AttributeList.
- .parse_attributes(content, attr_list) ⇒ Object
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Check if this has any attributes.
-
#merge(other) ⇒ AttributeList
Create a merged copy.
-
#merge!(other) ⇒ AttributeList
Merge another AttributeList into this one.
Methods inherited from Base
Class Method Details
.parse(str) ⇒ AttributeList
Parse an IAL string into an AttributeList
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
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/coradoc/markdown/model/attribute_list.rb', line 72 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 << NamedValue.new(name: token[:key], value: token[:value]) end end end |
Instance Method Details
#empty? ⇒ Boolean
Check if this has any attributes
68 69 70 |
# File 'lib/coradoc/markdown/model/attribute_list.rb', line 68 def empty? id.nil? && classes.empty? && attributes.empty? end |
#merge(other) ⇒ AttributeList
Create a merged copy
62 63 64 |
# File 'lib/coradoc/markdown/model/attribute_list.rb', line 62 def merge(other) dup.merge!(other) end |
#merge!(other) ⇒ AttributeList
Merge another AttributeList into this one
48 49 50 51 52 53 54 55 56 57 |
# 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 merged = {} (attributes + other.attributes).each { |nv| merged[nv.name] = nv } self.attributes = merged.values self end |