Class: TTYHue::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ttyhue/parser.rb,
lib/ttyhue/parser/tag.rb,
lib/ttyhue/parser/color_tag.rb,
lib/ttyhue/parser/style_tag.rb,
lib/ttyhue/parser/color_stack.rb

Defined Under Namespace

Classes: ColorStack, ColorTag, ParseError, StyleTag, Tag

Constant Summary collapse

TOKENS =
{
  tag: /{[^{}]+}/,
  content: /[^{]+/,
  unclosed_tag: /{[^{]*/
}

Instance Method Summary collapse

Constructor Details

#initialize(string, styles = {}) ⇒ Parser

Returns a new instance of Parser.



16
17
18
19
# File 'lib/ttyhue/parser.rb', line 16

def initialize(string, styles = {})
  @string = string
  @styles = TTYHue.global_style.merge(styles)
end

Instance Method Details

#parseObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ttyhue/parser.rb', line 21

def parse
  @color_stack = ColorStack.new
  @result = []
  scanner = StringScanner.new(@string)
  until scanner.eos?
    next if handle_content!(scanner.scan(TOKENS[:content]))
    while str = scanner.scan(TOKENS[:tag])
      handle_tag!(str)
    end
    next if handle_content!(scanner.scan(TOKENS[:unclosed_tag]))
  end
  flattened_result
end