class Asciidoctor::AttributeList

Public: Handles parsing AsciiDoc attribute lists into a Hash of key/value pairs. By default, attributes must each be separated by a comma and quotes may be used around the value. If a key is not detected, the value is assigned to a 1-based positional key, The positional attributes can be “rekeyed” when given a positional_attrs array either during parsing or after the fact.

Examples

attrlist = Asciidoctor::AttributeList.new('astyle')

attrlist.parse
=> { 0 => 'astyle' }

attrlist.rekey(['style'])
=> { 'style' => 'astyle' }

attrlist = Asciidoctor::AttributeList.new('quote, Famous Person, Famous Book (2001)')

attrlist.parse(['style', 'attribution', 'citetitle'])
=> { 'style' => 'quote', 'attribution' => 'Famous Person', 'citetitle' => 'Famous Book (2001)' }

Constants

APOS
BACKSLASH
BlankRx
BoundaryRx

Public: Regular expressions for detecting the boundary of a value

EscapedQuotes

Public: Regular expressions for unescaping quoted characters

NameRx

Public: A regular expression for an attribute name (approx. name token from XML) TODO named attributes cannot contain dash characters

QUOT
SkipRx

Public: Regular expressions for skipping delimiters

Public Class Methods

new(source, block = nil, delimiter = ',') click to toggle source
# File lib/asciidoctor/attribute_list.rb, line 53
def initialize source, block = nil, delimiter = ','
  @scanner = ::StringScanner.new source
  @block = block
  @delimiter = delimiter
  @delimiter_skip_pattern = SkipRx[delimiter]
  @delimiter_boundary_pattern = BoundaryRx[delimiter]
  @attributes = nil
end
rekey(attributes, positional_attrs) click to toggle source
# File lib/asciidoctor/attribute_list.rb, line 86
def self.rekey attributes, positional_attrs
  positional_attrs.each_with_index do |key, index|
    if key && (val = attributes[index + 1])
      # QUESTION should we delete the positional key?
      attributes[key] = val
    end
  end
  attributes
end

Public Instance Methods

parse(positional_attrs = []) click to toggle source
# File lib/asciidoctor/attribute_list.rb, line 66
def parse positional_attrs = []
  # return if already parsed
  return @attributes if @attributes

  @attributes = {}
  index = 0

  while parse_attribute index, positional_attrs
    break if @scanner.eos?
    skip_delimiter
    index += 1
  end

  @attributes
end
parse_into(attributes, positional_attrs = []) click to toggle source
# File lib/asciidoctor/attribute_list.rb, line 62
def parse_into attributes, positional_attrs = []
  attributes.update parse positional_attrs
end
rekey(positional_attrs) click to toggle source
# File lib/asciidoctor/attribute_list.rb, line 82
def rekey positional_attrs
  AttributeList.rekey @attributes, positional_attrs
end