Class: Teepee::ParagraphParser

Inherits:
ParserNode show all
Defined in:
lib/teepee/paragraph-parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ParserNode

#use_scope

Constructor Details

#initialize(parser, tokens) ⇒ ParagraphParser

Returns a new instance of ParagraphParser.

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
# File 'lib/teepee/paragraph-parser.rb', line 53

def initialize(parser, tokens)
  @parser = parser
  @scope = Scope.new
  raise ArgumentError if not tokens.is_a? Array
  tokens.each {|token| raise ArgumentError if not token.kind_of? ParserNode}
  @tokens = tokens
  parse
end

Instance Attribute Details

#expressionsObject

Returns the value of attribute expressions.



51
52
53
# File 'lib/teepee/paragraph-parser.rb', line 51

def expressions
  @expressions
end

#parserObject

Returns the value of attribute parser.



51
52
53
# File 'lib/teepee/paragraph-parser.rb', line 51

def parser
  @parser
end

#scopeObject

Returns the value of attribute scope.



51
52
53
# File 'lib/teepee/paragraph-parser.rb', line 51

def scope
  @scope
end

#tokensObject

Returns the value of attribute tokens.



51
52
53
# File 'lib/teepee/paragraph-parser.rb', line 51

def tokens
  @tokens
end

Instance Method Details

#parseObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/teepee/paragraph-parser.rb', line 62

def parse
  @expressions = []
  rest = tokens
  while rest.length > 0
    if rest.first.is_a? WordToken
      @expressions << rest.shift
    elsif rest.first.is_a? WhitespaceToken
      @expressions << rest.shift
    elsif rest.first.is_a? BackslashToken
      command, rest = CommandParser.parse parser, scope.derive, rest
      @expressions << command
    else
      return self
    end
  end
end

#to_htmlObject



79
80
81
# File 'lib/teepee/paragraph-parser.rb', line 79

def to_html
  "<p>\n" + expressions.map(&:to_html).join + "\n</p>\n"
end