Class: Tr8n::Tokens::DecorationTokenizer
- Inherits:
-
Object
- Object
- Tr8n::Tokens::DecorationTokenizer
- Defined in:
- lib/tr8n/tokens/decoration_tokenizer.rb
Constant Summary collapse
- RESERVED_TOKEN =
'tr8n'- RE_SHORT_TOKEN_START =
'\[[\w]*:'- RE_SHORT_TOKEN_END =
'\]'- RE_LONG_TOKEN_START =
'\[[\w]*\]'- RE_LONG_TOKEN_END =
'\[\/[\w]*\]'- RE_TEXT =
‘[ws!.:{}()|,?]*’
'[^\[\]]+'
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#fragments ⇒ Object
readonly
Returns the value of attribute fragments.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Class Method Summary collapse
Instance Method Summary collapse
- #allowed_token?(token) ⇒ Boolean
- #apply(token, value) ⇒ Object
- #default_decoration(token_name, token_value) ⇒ Object
- #evaluate(expr) ⇒ Object
-
#initialize(text, context = {}, opts = {}) ⇒ DecorationTokenizer
constructor
A new instance of DecorationTokenizer.
- #parse ⇒ Object
- #parse_tree(name, type = :short) ⇒ Object
- #substitute ⇒ Object
- #tokenize ⇒ Object
Constructor Details
#initialize(text, context = {}, opts = {}) ⇒ DecorationTokenizer
Returns a new instance of DecorationTokenizer.
59 60 61 62 63 64 |
# File 'lib/tr8n/tokens/decoration_tokenizer.rb', line 59 def initialize(text, context = {}, opts = {}) @text = "[#{RESERVED_TOKEN}]#{text}[/#{RESERVED_TOKEN}]" @context = context @opts = opts tokenize end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
45 46 47 |
# File 'lib/tr8n/tokens/decoration_tokenizer.rb', line 45 def context @context end |
#fragments ⇒ Object (readonly)
Returns the value of attribute fragments.
45 46 47 |
# File 'lib/tr8n/tokens/decoration_tokenizer.rb', line 45 def fragments @fragments end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
45 46 47 |
# File 'lib/tr8n/tokens/decoration_tokenizer.rb', line 45 def opts @opts end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
45 46 47 |
# File 'lib/tr8n/tokens/decoration_tokenizer.rb', line 45 def text @text end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
45 46 47 |
# File 'lib/tr8n/tokens/decoration_tokenizer.rb', line 45 def tokens @tokens end |
Class Method Details
.required?(label) ⇒ Boolean
55 56 57 |
# File 'lib/tr8n/tokens/decoration_tokenizer.rb', line 55 def self.required?(label) label.index("[") end |
Instance Method Details
#allowed_token?(token) ⇒ Boolean
156 157 158 159 |
# File 'lib/tr8n/tokens/decoration_tokenizer.rb', line 156 def allowed_token?(token) return true if opts[:allowed_tokens].nil? opts[:allowed_tokens].include?(token) end |
#apply(token, value) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/tr8n/tokens/decoration_tokenizer.rb', line 161 def apply(token, value) return value if token == RESERVED_TOKEN return value unless allowed_token?(token) method = context[token.to_sym] || context[token.to_s] if method if method.is_a?(Proc) return method.call(value) end if method.is_a?(Array) or method.is_a?(Hash) return default_decoration(token, value) end if method.is_a?(String) return method.to_s.gsub("{$0}", value) end Tr8n.logger.error("Invalid decoration token value for #{token} in #{text}") return value end if Tr8n.config.default_token_value(token, :decoration) return default_decoration(token, value) end Tr8n.logger.error("Missing decoration token value for #{token} in #{text}") value end |
#default_decoration(token_name, token_value) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/tr8n/tokens/decoration_tokenizer.rb', line 115 def default_decoration(token_name, token_value) default_decoration = Tr8n.config.default_token_value(token_name, :decoration) unless default_decoration Tr8n.logger.error("Invalid decoration token value for #{token_name} in #{text}") return token_value end default_decoration = default_decoration.clone decoration_token_values = context[token_name.to_sym] || context[token_name.to_s] || [] if decoration_token_values.is_a?(Array) params = [token_value, decoration_token_values].flatten params.each_with_index do |param, index| default_decoration.gsub!("{$#{index}}", param.to_s) end # clean all the rest of the {$num} params, if any param_index = params.size while default_decoration.index("{$#{param_index}}") default_decoration.gsub!("{$#{param_index}}", "") param_index += 1 end return default_decoration end if decoration_token_values.is_a?(Hash) default_decoration.gsub!("{$0}", token_value.to_s) decoration_token_values.keys.each do |key| default_decoration.gsub!("{$#{key}}", decoration_token_values[key].to_s) end return default_decoration end Tr8n.logger.error("Don't know how to process decoration token value for #{token_name} in #{text}") token_value end |
#evaluate(expr) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/tr8n/tokens/decoration_tokenizer.rb', line 192 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 ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/tr8n/tokens/decoration_tokenizer.rb', line 76 def parse return @text 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 token.to_s end |
#parse_tree(name, type = :short) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/tr8n/tokens/decoration_tokenizer.rb', line 91 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 end fragments.shift tree end |
#substitute ⇒ Object
204 205 206 |
# File 'lib/tr8n/tokens/decoration_tokenizer.rb', line 204 def substitute evaluate(parse) end |
#tokenize ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/tr8n/tokens/decoration_tokenizer.rb', line 66 def tokenize re = [RE_SHORT_TOKEN_START, RE_SHORT_TOKEN_END, RE_LONG_TOKEN_START, RE_LONG_TOKEN_END, RE_TEXT].join('|') @fragments = text.scan(/#{re}/) @tokens = [] end |