class NestedText::Line

Constants

ALLOWED_LINE_TAGS

Reference: nestedtext.org/en/latest/file_format.html

PATTERN_DICT_ITEM

Attributes

attribs[R]
content[R]
indentation[R]
lineno[R]
prev[RW]
tag[R]

Public Class Methods

new(content, lineno, prev_line) click to toggle source
# File lib/nestedtext/scanners.rb, line 91
def initialize(content, lineno, prev_line)
  @content = content
  @lineno = lineno
  @prev = prev_line
  @attribs = Hash.new(nil)
  @tag = nil
  @indentation = 0
  detect_line_tag_and_indentation
end

Public Instance Methods

tag=(tag) click to toggle source

def [](index) @content end

# File lib/nestedtext/scanners.rb, line 109
def tag=(tag)
  @tag = tag
  raise Errors::ParseLineTagUnknownError.new(self, tag) unless ALLOWED_LINE_TAGS.include?(@tag)
end
to_s() click to toggle source
# File lib/nestedtext/scanners.rb, line 114
def to_s
  "[##{@lineno}] #{' ' * @indentation}#{@content}"
end

Private Instance Methods

detect_line_tag() click to toggle source
# File lib/nestedtext/scanners.rb, line 134
def detect_line_tag
  if @content.length.zero?
    self.tag = :blank
  elsif @content[0] == '#'
    self.tag = :comment
  elsif @content =~ /^:(?: |$)/
    self.tag = :key_item
    @attribs['key'] = @content[2..] || ''
  elsif @content =~ /^-(?: |$)/
    self.tag = :list_item
    @attribs['value'] = @content[2..]
  elsif @content =~ /^>(?: |$)/
    self.tag = :string_item
    @attribs['value'] = @content[2..] || ''
  elsif @content[0] == '{'
    self.tag = :inline_dict
  elsif @content[0] == '['
    self.tag = :inline_list
  elsif @content =~ PATTERN_DICT_ITEM
    self.tag = :dict_item
    @attribs['key'] = Regexp.last_match(:key)
    @attribs['value'] = Regexp.last_match(:value)
  else
    # Don't raise error here, as this line might not have been consumed yet,
    # thus could hide an error that we detect when parsing the previous line.
    self.tag = :unrecognized
  end
end
detect_line_tag_and_indentation() click to toggle source
# File lib/nestedtext/scanners.rb, line 163
def detect_line_tag_and_indentation
  fast_forward_indentation
  detect_line_tag
end
fast_forward_indentation() click to toggle source
# File lib/nestedtext/scanners.rb, line 129
def fast_forward_indentation
  @indentation += 1 while @indentation < @content.length && @content[@indentation] == ' '
  @content = @content[@indentation..]
end