Class: TracWiki::Env
- Inherits:
-
Object
- Object
- TracWiki::Env
- Defined in:
- lib/trac-wiki/env.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #arg(idx) ⇒ Object
- #at(key, default = nil, to_str = true) ⇒ Object
- #atput(key, val = nil) ⇒ Object
- #do_macro_arg_to_env(args) ⇒ Object
-
#do_macro_cmd(macro_name, args) ⇒ Object
calls {!cmd} (if exists r: result of {!cmd}.
-
#do_macro_templ(macro_name, args) ⇒ Object
expand macro ‘macro_name` with `args` afer expansion all {macros} will be expanded recursively r: expanded string.
- #do_macro_var(var_name, args) ⇒ Object
- #expand(str) ⇒ Object
-
#expand_arg(idx) ⇒ Object
template expand.
-
#initialize(parser, env = {}) ⇒ Env
constructor
A new instance of Env.
-
#parse_balanced(str) ⇒ Object
r: [args], rest-of-str, num-of-lines, offset-last-line.
-
#parse_macro_all(macro_name, str, macro_name_size = nil) ⇒ Object
r: expanded-macro, rest-of-str, lines, offset-in-line.
-
#parse_macro_cmd(macro_name, args) ⇒ Object
parse to next }} (with balanced {..}) like parse_macro_vartempl but not expand content r: [expansion, rest_of_str, count_of_consumed_lines].
-
#parse_macro_vartempl(macro_name, args) ⇒ Object
read to args to }} (while balancing and }) ret: (arg, rest, lines) mac_out – string to }} (macros inside expanded) rest – str aftrer }} lines – howmany n eaten from str (from begining to }}).
- #pp_env ⇒ Object
- #prepare_y ⇒ Object
Constructor Details
#initialize(parser, env = {}) ⇒ Env
Returns a new instance of Env.
5 6 7 8 |
# File 'lib/trac-wiki/env.rb', line 5 def initialize(parser, env = {}) @parser = parser @env = env end |
Instance Method Details
#[](key) ⇒ Object
187 188 189 |
# File 'lib/trac-wiki/env.rb', line 187 def [](key) at(key) end |
#[]=(key, val) ⇒ Object
191 192 193 194 |
# File 'lib/trac-wiki/env.rb', line 191 def []=(key,val) #print "set #{key} to #{val}\n" atput(key,val) end |
#arg(idx) ⇒ Object
115 116 117 |
# File 'lib/trac-wiki/env.rb', line 115 def arg(idx) @env[:cmd_args][idx] || '' end |
#at(key, default = nil, to_str = true) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/trac-wiki/env.rb', line 134 def at(key, default = nil, to_str = true) #print "at(#{key}), env:" #pp @env return @env[key] || default if key.is_a? Symbol prepare_y if key =~ /^y\./ key = "argv.#{key}" if key =~ /^\d+$/ #print "key: #{key}\n" cur = @env key.split(/\./).each do |subkey| subkey = at($1, '') if subkey =~ /\A\$(.*)/ #print "at:subkey: #{subkey}\n" if cur.is_a? Hash cur = cur[subkey] elsif cur.is_a? Array cur = cur[subkey.to_i] else #print "at(#{key})->: default" return default end #print "at(#{key}) -> default\n" if cur.nil? return default if cur.nil? end #print "at(#{key})->#{cur}\n" to_str ? cur.to_s : cur end |
#atput(key, val = nil) ⇒ Object
159 160 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 |
# File 'lib/trac-wiki/env.rb', line 159 def atput(key, val = nil) #print "atput: #{key}, #{val} env:" #pp @env cur = @env if val.is_a? Symbol @env[key] = val return end keys = key.split(/\./) lastkey = keys.pop keys.each do |subkey| if cur.is_a? Hash cur = cur[subkey] elsif cur.is_a? Array cur = cur[subkey.to_i] else return end end if cur.is_a? Hash cur[lastkey] = val elsif cur.is_a? Array cur[lastkey.to_i] = val end #print "atput:env:" #pp @env end |
#do_macro_arg_to_env(args) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/trac-wiki/env.rb', line 229 def do_macro_arg_to_env(args) env = {} @env.each do |k,v| env[k] = v if k != :depth && k != 'arg' && k!='argv' end env[:depth] = (@env[:depth]||0)+1 env['arg'] = args.join('|') env['argv'] = {} idx = 1 args.each do |arg| if arg =~ /\A\s*(\w+)\s*=\s*(.*)/m env[$1] = $2 else env['argv'][idx.to_s] = arg idx+=1 end end return Env.new(@parser, env) end |
#do_macro_cmd(macro_name, args) ⇒ Object
calls {!cmd} (if exists r: result of {!cmd}
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/trac-wiki/env.rb', line 104 def do_macro_cmd(macro_name, args) return '|' if macro_name == '!' if @parser.macro_commands.key?(macro_name) @env[:cmd_args] = args @env[:cmd_arg0] = macro_name #print "mac: #{macro_name} env:" ; pp (@env) ret = @parser.macro_commands[macro_name].call(self) return ret end "UCMD(#{macro_name}|#{@env['arg']})" end |
#do_macro_templ(macro_name, args) ⇒ Object
expand macro ‘macro_name` with `args` afer expansion all {macros} will be expanded recursively r: expanded string
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/trac-wiki/env.rb', line 199 def do_macro_templ(macro_name, args) return "!{{toc}}" if macro_name == 'toc' return args.join('|').strip if macro_name == '#echo' return '' if macro_name == '#' env = do_macro_arg_to_env(args) #print "templ:#{macro_name}env:" #pp(args) if ! @parser.template_handler.nil? str = @parser.template_handler.call(macro_name, env) if !str.nil? #print "dep:#{env[:depth]}(#{macro_name}, #{str})\n" if env[:depth] > 32 return "TOO_DEEP_RECURSION(`#{str}`)\n" #return "TOO_DEEP_RECURSION" end # FIXME: melo by nahlasit jestli to chce expandovat | wiki expadnovat |raw html #print "temp(#{macro_name}) --> : #{str}\n" #print "bf ex: [#{str}]\n" str = env.(str) #print "af ex: [#{str}]\n" return str end end #print "UMACRO(#{macro_name}|#{args})\n" "UMACRO(#{macro_name}|#{args.join('|')})" end |
#do_macro_var(var_name, args) ⇒ Object
250 251 252 253 254 255 256 257 258 |
# File 'lib/trac-wiki/env.rb', line 250 def do_macro_var(var_name, args) #print "var(#{var_name})env:" #pp(@env) ret = at(var_name, nil) return ret if !ret.nil? return args.join('|') if args.size > 0 return '' "UVAR(#{var_name}|#{@env['arg']})" end |
#expand(str) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/trac-wiki/env.rb', line 268 def (str) ret = '' return '' if str.nil? while str =~ TracWiki::Parser::MACRO_BEG_INSIDE_REX prefix, macro_name2, str = $1, $2, $' ret << prefix # FIXME if macro_name2 =~ /^!/ mac_out, str, lines, offset = parse_macro_all(macro_name2, str, nil) ret << mac_out #print "Too long macro expadion" if ret.size > 1_000_000 raise TooLongException if ret.size > 1_000_000 end #print "text: #{text.nil?}\n" #print "ret: #{ret.nil?}\n" ret += str return ret.gsub(/\\\r?\n\s*/, '') end |
#expand_arg(idx) ⇒ Object
template expand
261 262 263 |
# File 'lib/trac-wiki/env.rb', line 261 def (idx) (@env[:cmd_args][idx]) end |
#parse_balanced(str) ⇒ Object
r: [args], rest-of-str, num-of-lines, offset-last-line
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/trac-wiki/env.rb', line 72 def parse_balanced(str) str.sub!(/\A\s*\|?/, '') offset = $&.size return [], $', 0, 2 if str =~ /\A}}/ #print "_parse_macro_cmd: #{macro_name}, str#{str}\n" dep = 0 lines = 0 args = [''] while str =~ /{{|}}|\n|\|/ prefix, match, str = $`, $&, $' offset += prefix.size + match.size #raise "offset is nil" if offset.nil? args[-1] += prefix if match == '{{' dep += 1 elsif match == '}}' dep -= 1 return args, str, lines, offset if dep < 0 elsif match == "\n" lines += 1 offset = 0 elsif match == '|' && dep == 0 args.push('') next end args[-1] += $& end raise "eol in parsing macro params" end |
#parse_macro_all(macro_name, str, macro_name_size = nil) ⇒ Object
r: expanded-macro, rest-of-str, lines, offset-in-line
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/trac-wiki/env.rb', line 11 def parse_macro_all(macro_name, str, macro_name_size = nil) str_size = str.size args, rest, lines, offset = parse_balanced(str) atput('maclen', str_size - rest.size + macro_name_size) if ! macro_name_size.nil? if macro_name =~ /\A!/ # {{!cmd}} mac_out = parse_macro_cmd(macro_name, args) else # {{$cmd}}, {{template}}, ... mac_out = parse_macro_vartempl(macro_name, args) end return mac_out || '', rest, lines, offset end |
#parse_macro_cmd(macro_name, args) ⇒ Object
parse to next }} (with balanced {..}) like parse_macro_vartempl but not expand content r: [expansion, rest_of_str, count_of_consumed_lines]
67 68 69 |
# File 'lib/trac-wiki/env.rb', line 67 def parse_macro_cmd(macro_name, args) return do_macro_cmd(macro_name, args) end |
#parse_macro_vartempl(macro_name, args) ⇒ Object
read to args to }} (while balancing and }) ret: (arg, rest, lines) mac_out – string to }} (macros inside expanded) rest – str aftrer }} lines – howmany n eaten from str (from begining to }})
30 31 32 33 34 |
# File 'lib/trac-wiki/env.rb', line 30 def parse_macro_vartempl(macro_name, args) args.map! { |arg| (arg) } return do_macro_var($1, args) if macro_name =~ /^\$(.*)/ return do_macro_templ(macro_name, args) end |
#pp_env ⇒ Object
265 266 267 |
# File 'lib/trac-wiki/env.rb', line 265 def pp_env pp(@env) end |
#prepare_y ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/trac-wiki/env.rb', line 119 def prepare_y #print "prepare_y\n" return if @env.key? 'y' arg = @env['arg'] return if arg.nil? begin @env['y'] = YAML.load(arg) #print "y" #pp @env['y'] rescue @env['y'] = nil #print "y:nil\n" end end |