class Tml::Tokenizers::Decoration

Constants

RESERVED_TOKEN
RE_HTML_TOKEN_END
RE_HTML_TOKEN_START
RE_LONG_TOKEN_END
RE_LONG_TOKEN_START
RE_SHORT_TOKEN_END
RE_SHORT_TOKEN_START
RE_TEXT

Attributes

context[R]
fragments[R]
label[R]
opts[R]
tokens[R]

Public Class Methods

new(label, context = {}, opts = {}) click to toggle source
# File lib/tml/tokenizers/decoration.rb, line 72
def initialize(label, context = {}, opts = {})
  @label = label
  @context = context
  @opts = opts
  tokenize
end
required?(label) click to toggle source
# File lib/tml/tokenizers/decoration.rb, line 68
def self.required?(label)
  label.index('[') or label.index('<')
end

Public Instance Methods

apply(token_name, value) click to toggle source
# File lib/tml/tokenizers/decoration.rb, line 139
def apply(token_name, value)
  token = ::Tml::Tokens::Decoration.new(label, token_name)
  token.apply(context, value, opts[:allowed_tokens])
end
evaluate(expr) click to toggle source
# File lib/tml/tokenizers/decoration.rb, line 144
def evaluate(expr)
  unless expr.is_a?(Array)
    return expr
  end

  token = expr[0]
  args = expr.drop(1)
  value = args.map { |a| self.evaluate(a) }.join('')

  apply(token, value)
end
parse() click to toggle source
# File lib/tml/tokenizers/decoration.rb, line 91
def parse
  return @label unless fragments
  token = fragments.shift

  if token.match(/#{RE_SHORT_TOKEN_START}/)
    return parse_tree(token.gsub(/[\[:]/, ''), :short)
  end

  if token.match(/#{RE_LONG_TOKEN_START}/)
    return parse_tree(token.gsub(/[\[\]]/, ''), :long)
  end

  if token.match(/#{RE_HTML_TOKEN_START}/)
    return token if token.index('/>')
    return parse_tree(token.gsub(/[<>]/, '').split(' ').first, :html)
  end

  token.to_s
end
parse_tree(name, type = :short) click to toggle source
# File lib/tml/tokenizers/decoration.rb, line 111
def parse_tree(name, type = :short)
  tree = [name]
  @tokens << name unless (@tokens.include?(name) or name == RESERVED_TOKEN)

  if type == :short
    first = true
    until fragments.first.nil? or fragments.first.match(/#{RE_SHORT_TOKEN_END}/)
      value = parse
      if first and value.is_a?(String)
        value = value.lstrip
        first = false
      end
      tree << value
    end
  elsif type == :long
    until fragments.first.nil? or fragments.first.match(/#{RE_LONG_TOKEN_END}/)
      tree << parse
    end
  elsif type == :html
    until fragments.first.nil? or fragments.first.match(/#{RE_HTML_TOKEN_END}/)
      tree << parse
    end
  end

  fragments.shift
  tree
end
substitute() click to toggle source
# File lib/tml/tokenizers/decoration.rb, line 156
def substitute
  evaluate(parse).gsub(/[<\[]\/tml[>\]]/, '')
end
tokenize() click to toggle source
# File lib/tml/tokenizers/decoration.rb, line 79
def tokenize
  re = [RE_SHORT_TOKEN_START,
        RE_SHORT_TOKEN_END,
        RE_LONG_TOKEN_START,
        RE_LONG_TOKEN_END,
        RE_HTML_TOKEN_START,
        RE_HTML_TOKEN_END,
        RE_TEXT].join('|')
  @fragments = "[#{RESERVED_TOKEN}]#{@label}[/#{RESERVED_TOKEN}]".scan(/#{re}/)
  @tokens = []
end