Class: Tml::Tokenizers::Decoration

Inherits:
Object
  • Object
show all
Defined in:
lib/tml/tokenizers/decoration.rb

Constant Summary collapse

RESERVED_TOKEN =
'tml'
RE_SHORT_TOKEN_START =
'\[[\w]*:'
RE_SHORT_TOKEN_END =
'\]'
RE_LONG_TOKEN_START =
link
'\[[\w]*\]'
RE_LONG_TOKEN_END =
/link
'\[\/[\w]*\]'
RE_HTML_TOKEN_START =

<link>

'<[^\>]*>'
RE_HTML_TOKEN_END =

</link>

'<\/[^\>]*>'
RE_TEXT =

‘[ws!.:{}()|,?]*’

'[^\[\]<>]+'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, context = {}, opts = {}) ⇒ Decoration

Returns a new instance of Decoration.



72
73
74
75
76
77
# File 'lib/tml/tokenizers/decoration.rb', line 72

def initialize(label, context = {}, opts = {})
  @label = label
  @context = context
  @opts = opts
  tokenize
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



56
57
58
# File 'lib/tml/tokenizers/decoration.rb', line 56

def context
  @context
end

#fragmentsObject (readonly)

Returns the value of attribute fragments.



56
57
58
# File 'lib/tml/tokenizers/decoration.rb', line 56

def fragments
  @fragments
end

#labelObject (readonly)

Returns the value of attribute label.



56
57
58
# File 'lib/tml/tokenizers/decoration.rb', line 56

def label
  @label
end

#optsObject (readonly)

Returns the value of attribute opts.



56
57
58
# File 'lib/tml/tokenizers/decoration.rb', line 56

def opts
  @opts
end

#tokensObject (readonly)

Returns the value of attribute tokens.



56
57
58
# File 'lib/tml/tokenizers/decoration.rb', line 56

def tokens
  @tokens
end

Class Method Details

.required?(label) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/tml/tokenizers/decoration.rb', line 68

def self.required?(label)
  label.index('[') or label.index('<')
end

Instance Method Details

#apply(token_name, value) ⇒ Object



139
140
141
142
# 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) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# 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

#parseObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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

#substituteObject



156
157
158
# File 'lib/tml/tokenizers/decoration.rb', line 156

def substitute
  evaluate(parse).gsub(/[<\[]\/tml[>\]]/, '')
end

#tokenizeObject



79
80
81
82
83
84
85
86
87
88
89
# 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