Class: Tr8n::Tokenizers::Decoration
- Inherits:
-
Object
- Object
- Tr8n::Tokenizers::Decoration
- Defined in:
- lib/tr8n/tokenizers/decoration.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 = {}) ⇒ Decoration
constructor
A new instance of Decoration.
- #normalize_token(name) ⇒ Object
- #parse ⇒ Object
- #parse_tree(name, type = :short) ⇒ Object
- #substitute ⇒ Object
- #tokenize ⇒ Object
Constructor Details
#initialize(text, context = {}, opts = {}) ⇒ Decoration
Returns a new instance of Decoration.
68 69 70 71 72 73 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 68 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.
54 55 56 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 54 def context @context end |
#fragments ⇒ Object (readonly)
Returns the value of attribute fragments.
54 55 56 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 54 def fragments @fragments end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
54 55 56 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 54 def opts @opts end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
54 55 56 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 54 def text @text end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
54 55 56 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 54 def tokens @tokens end |
Class Method Details
.required?(label) ⇒ Boolean
64 65 66 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 64 def self.required?(label) label.index('[') end |
Instance Method Details
#allowed_token?(token) ⇒ Boolean
146 147 148 149 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 146 def allowed_token?(token) return true if opts[:allowed_tokens].nil? opts[:allowed_tokens].include?(token) end |
#apply(token, value) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 151 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(normalize_token(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
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 124 def default_decoration(token_name, token_value) default_decoration = Tr8n.config.default_token_value(normalize_token(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] default_decoration.gsub!('{$0}', token_value.to_s) if decoration_token_values.is_a?(Hash) decoration_token_values.keys.each do |key| default_decoration.gsub!("{$#{key}}", decoration_token_values[key].to_s) end end default_decoration end |
#evaluate(expr) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 186 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 |
#normalize_token(name) ⇒ Object
182 183 184 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 182 def normalize_token(name) name.to_s.gsub(/(\d)*$/, '') end |
#parse ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 85 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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 100 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
198 199 200 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 198 def substitute evaluate(parse) end |
#tokenize ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/tr8n/tokenizers/decoration.rb', line 75 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 |