Class: Zena::Parser::Processor
- Inherits:
-
Object
- Object
- Zena::Parser::Processor
- Includes:
- RubyLess
- Defined in:
- lib/zena/parser.rb
Overview
DummyHelper
Instance Attribute Summary collapse
-
#blocks ⇒ Object
Returns the value of attribute blocks.
-
#defined_ids ⇒ Object
Returns the value of attribute defined_ids.
-
#ids ⇒ Object
Returns the value of attribute ids.
-
#method ⇒ Object
Returns the value of attribute method.
-
#options ⇒ Object
Returns the value of attribute options.
-
#params ⇒ Object
Returns the value of attribute params.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#pass ⇒ Object
Returns the value of attribute pass.
-
#text ⇒ Object
Returns the value of attribute text.
Class Method Summary collapse
-
.get_template_text(url, helper, base_path = nil) ⇒ Object
Retrieve the template text in the current folder or as an absolute path.
- .new_with_url(url, opts = {}) ⇒ Object
Instance Method Summary collapse
- #after_parse(text) ⇒ Object
- #after_render(text) ⇒ Object
-
#all_descendants ⇒ Object
(also: #public_descendants)
Return a hash of all descendants.
-
#ancestor(key) ⇒ Object
Return the last defined parent for the given key.
- #ancestors ⇒ Object
- #before_parse(text) ⇒ Object
- #before_render ⇒ Object
- #check_params(*args) ⇒ Object
-
#child ⇒ Object
Find a direct child with
child[method]
. -
#descendant(key) ⇒ Object
Return the last defined descendant for the given key.
- #descendants(key) ⇒ Object
- #do_method(sym) ⇒ Object
- #eat(arg) ⇒ Object
- #empty? ⇒ Boolean
- #enter(mode) ⇒ Object
- #expand_block(block, new_context = {}) ⇒ Object
- #expand_with(acontext = {}) ⇒ Object
- #fail ⇒ Object
- #flush(str = @text) ⇒ Object
-
#include_part(obj) ⇒ Object
Hook called when including a part “<r:include template=‘layout’ part=‘title’/>”.
- #include_template ⇒ Object
-
#initialize(text, opts = {}) ⇒ Processor
constructor
A new instance of Processor.
- #inspect ⇒ Object
- #leave(mode = nil) ⇒ Object
- #make(mode, opts = {}) ⇒ Object
-
#out(obj) ⇒ Object
Set output during render.
-
#parse_params(text) ⇒ Object
Parse parameters into a hash.
-
#r_expand_with ⇒ Object
Set context with variables (unsafe) from template.
- #r_ignore ⇒ Object
- #r_inspect ⇒ Object
-
#r_unknown ⇒ Object
basic rule to display errors.
- #r_void ⇒ Object (also: #to_s)
- #render(context = {}) ⇒ Object
-
#replace_with(obj) ⇒ Object
Hook called when replacing part of an included template with ‘<r:with part=’main’>…</r:with>‘ This replaces the current object ’self’ which is in the original included template, with the custom version ‘obj’.
-
#root ⇒ Object
Return the root block (the one opened first).
- #start(mode) ⇒ Object
-
#store(obj) ⇒ Object
Build blocks.
- #success? ⇒ Boolean
Constructor Details
#initialize(text, opts = {}) ⇒ Processor
Returns a new instance of Processor.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/zena/parser.rb', line 79 def initialize(text, opts={}) @stack = [] @ok = true @blocks = [] @options = {:mode=>:void, :method=>'void'}.merge(opts) @params = @options.delete(:params) || {} @method = @options.delete(:method) @ids = @options[:ids] ||= {} original_ids = @ids.dup @defined_ids = {} # ids defined in this node or this node's sub blocks mode = @options.delete(:mode) @parent = @options.delete(:parent) if opts[:sub] @text = text else @text = before_parse(text) end start(mode) # set name @name ||= @options[:name] || @params[:id] @options[:ids][@name] = self if @name unless opts[:sub] @text = after_parse(@text) end @ids.keys.each do |k| if original_ids[k] != @ids[k] @defined_ids[k] = @ids[k] end end @ok end |
Instance Attribute Details
#blocks ⇒ Object
Returns the value of attribute blocks.
52 53 54 |
# File 'lib/zena/parser.rb', line 52 def blocks @blocks end |
#defined_ids ⇒ Object
Returns the value of attribute defined_ids.
52 53 54 |
# File 'lib/zena/parser.rb', line 52 def defined_ids @defined_ids end |
#ids ⇒ Object
Returns the value of attribute ids.
52 53 54 |
# File 'lib/zena/parser.rb', line 52 def ids @ids end |
#method ⇒ Object
Returns the value of attribute method.
52 53 54 |
# File 'lib/zena/parser.rb', line 52 def method @method end |
#options ⇒ Object
Returns the value of attribute options.
52 53 54 |
# File 'lib/zena/parser.rb', line 52 def @options end |
#params ⇒ Object
Returns the value of attribute params.
52 53 54 |
# File 'lib/zena/parser.rb', line 52 def params @params end |
#parent ⇒ Object
Returns the value of attribute parent.
52 53 54 |
# File 'lib/zena/parser.rb', line 52 def parent @parent end |
#pass ⇒ Object
Returns the value of attribute pass.
52 53 54 |
# File 'lib/zena/parser.rb', line 52 def pass @pass end |
#text ⇒ Object
Returns the value of attribute text.
52 53 54 |
# File 'lib/zena/parser.rb', line 52 def text @text end |
Class Method Details
.get_template_text(url, helper, base_path = nil) ⇒ Object
Retrieve the template text in the current folder or as an absolute path. This method is used when ‘including’ text
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/zena/parser.rb', line 64 def get_template_text(url, helper, base_path=nil) if (url[0..0] != '/') && base_path url = "#{base_path}/#{url}" end res = helper.send(:get_template_text, url, nil) return ["<span class='parser_error'>[include] template '#{url}' not found</span>", nil, nil] unless res text, url, node = *res url = "/#{url}" unless url[0..0] == '/' # has to be an absolute path return [text, url, node] end |
.new_with_url(url, opts = {}) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/zena/parser.rb', line 55 def new_with_url(url, opts={}) helper = opts[:helper] || ParserModule::DummyHelper.new text, absolute_url = self.get_template_text(url, helper) base_path = absolute_url ? absolute_url.split('/')[1..-2].join('/') : nil self.new(text, :helper=>helper, :base_path=>base_path, :included_history=>[absolute_url], :root => url) end |
Instance Method Details
#after_parse(text) ⇒ Object
223 224 225 |
# File 'lib/zena/parser.rb', line 223 def after_parse(text) text end |
#after_render(text) ⇒ Object
215 216 217 |
# File 'lib/zena/parser.rb', line 215 def after_render(text) text end |
#all_descendants ⇒ Object Also known as: public_descendants
Return a hash of all descendants. Find a specific descendant with descendant for example.
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/zena/parser.rb', line 293 def all_descendants @all_descendants ||= begin d = {} @blocks.each do |b| next if b.kind_of?(String) b.public_descendants.each do |k,v| d[k] ||= [] d[k] += v end # latest is used first: use direct children before grandchildren. d[b.method] ||= [] d[b.method] << b end d end end |
#ancestor(key) ⇒ Object
Return the last defined parent for the given key.
334 335 336 337 338 339 340 341 342 343 |
# File 'lib/zena/parser.rb', line 334 def ancestor(key) res = nil ancestors.reverse_each do |a| if key == a.method res = a break end end res end |
#ancestors ⇒ Object
321 322 323 324 325 326 327 328 329 |
# File 'lib/zena/parser.rb', line 321 def ancestors @ancestors ||= begin if parent parent.ancestors + [parent] else [] end end end |
#before_parse(text) ⇒ Object
219 220 221 |
# File 'lib/zena/parser.rb', line 219 def before_parse(text) text end |
#before_render ⇒ Object
211 212 213 |
# File 'lib/zena/parser.rb', line 211 def before_render true end |
#check_params(*args) ⇒ Object
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/zena/parser.rb', line 481 def check_params(*args) missing = [] if args[0].kind_of?(Array) # or groups ok = false args.each_index do |i| unless args[i].kind_of?(Array) missing[i] = [args[i]] next end missing[i] = [] args[i].each do |arg| missing[i] << arg.to_s unless @params[arg] end if missing[i] == [] ok = true break end end if ok return true else out "[#{@method} parameter(s) missing:#{missing[0].sort.join(', ')}]" return false end else args.each do |arg| missing << arg.to_s unless @params[arg] end end if missing != [] out "[#{@method} parameter(s) missing:#{missing.sort.join(', ')}]" return false end true end |
#child ⇒ Object
Find a direct child with child[method]
.
311 312 313 314 315 |
# File 'lib/zena/parser.rb', line 311 def child Hash[*@blocks.map do |b| b.kind_of?(String) ? nil : [b.method, b] end.compact.flatten] end |
#descendant(key) ⇒ Object
Return the last defined descendant for the given key.
346 347 348 |
# File 'lib/zena/parser.rb', line 346 def descendant(key) descendants(key).last end |
#descendants(key) ⇒ Object
317 318 319 |
# File 'lib/zena/parser.rb', line 317 def descendants(key) all_descendants[key] || [] end |
#do_method(sym) ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/zena/parser.rb', line 159 def do_method(sym) res = self.send(sym) if @result != "" @result elsif !res.kind_of?(String) @method else res end end |
#eat(arg) ⇒ Object
383 384 385 386 387 388 389 390 391 392 |
# File 'lib/zena/parser.rb', line 383 def eat(arg) if arg.kind_of?(String) len = arg.length elsif arg.kind_of?(Fixnum) len = arg else raise end @text = @text[len..-1] end |
#empty? ⇒ Boolean
134 135 136 |
# File 'lib/zena/parser.rb', line 134 def empty? @blocks == [] && (@params == {} || @params == {:part => @params[:part]}) end |
#enter(mode) ⇒ Object
394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/zena/parser.rb', line 394 def enter(mode) @stack << mode # puts "ENTER(#{@method},:#{mode}) [#{@text}] #{@zafu_tag_count.inspect}" if mode == :void sym = :scan else sym = "scan_#{mode}".to_sym end while (@text != '' && @stack[-1] == mode) # puts "CONTINUE(#{@method},:#{mode}) [#{@text}] #{@zafu_tag_count.inspect}" self.send(sym) end # puts "LEAVE(#{@method},:#{mode}) [#{@text}] #{@zafu_tag_count.inspect}" end |
#expand_block(block, new_context = {}) ⇒ Object
518 519 520 |
# File 'lib/zena/parser.rb', line 518 def (block, new_context={}) block.render(@context.merge(new_context)) end |
#expand_with(acontext = {}) ⇒ Object
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
# File 'lib/zena/parser.rb', line 522 def (acontext={}) blocks = acontext.delete(:blocks) || @blocks res = "" # FIXME: I think we can delete @pass and @parts stuff now (test first). @pass = {} # current object sees some information from it's direct descendants @parts = {} only = acontext[:only] new_context = @context.merge(acontext) if acontext[:ignore] new_context[:ignore] = (@context[:ignore] || []) + (acontext[:ignore] || []).uniq end if acontext[:no_ignore] new_context[:ignore] = (new_context[:ignore] || []) - acontext[:no_ignore] end ignore = new_context[:ignore] blocks.each do |b| if b.kind_of?(String) if (!only || only.include?(:string)) && (!ignore || !ignore.include?(:string)) res << b end elsif (!only || only.include?(b.method)) && (!ignore || !ignore.include?(b.method)) res << b.render(new_context.dup) if pass = b.pass if pass[:part] @parts.merge!(pass[:part]) pass.delete(:part) end @pass.merge!(pass) end end end res end |
#fail ⇒ Object
440 441 442 443 |
# File 'lib/zena/parser.rb', line 440 def fail @ok = false @stack = [] end |
#flush(str = @text) ⇒ Object
359 360 361 362 363 364 365 366 367 |
# File 'lib/zena/parser.rb', line 359 def flush(str=@text) return if str == '' if @blocks.last.kind_of?(String) @blocks[-1] << str else @blocks << str end @text = @text[str.length..-1] end |
#include_part(obj) ⇒ Object
Hook called when including a part “<r:include template=‘layout’ part=‘title’/>”
130 131 132 |
# File 'lib/zena/parser.rb', line 130 def include_part(obj) [obj] end |
#include_template ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/zena/parser.rb', line 227 def include_template return "<span class='parser_error'>[include] missing 'template' attribute</span>" unless @params[:template] if @options[:part] && @options[:part] == @params[:part] # fetching only a part, do not open this element (same as original caller) as it is useless and will make us loop the loop. @method = 'ignore' enter(:void) return end @method = 'void' # fetch text @options[:included_history] ||= [] included_text, absolute_url = self.class.get_template_text(@params[:template], @options[:helper], @options[:base_path]) if included_text absolute_url += "::#{@params[:part].gsub('/','_')}" if @params[:part] absolute_url += "??#{@options[:part].gsub('/','_')}" if @options[:part] if @options[:included_history].include?(absolute_url) included_text = "<span class='parser_error'>[include] infinity loop: #{(@options[:included_history] + [absolute_url]).join(' --> ')}</span>" else included_history = @options[:included_history] + [absolute_url] base_path = absolute_url.split('/')[1..-2].join('/') end else return "<span class='parser_error'>[include] template '#{url}' not found</span>" end res = self.class.new(included_text, :helper=>@options[:helper], :base_path=>base_path, :included_history=>included_history, :part => @params[:part], :root=>@options[:root]) # we set :part to avoid loop failure when doing self inclusion if @params[:part] if iblock = res.ids[@params[:part]] included_blocks = include_part(iblock) # get all ids from inside the included part: @ids.merge! iblock.defined_ids else included_blocks = ["<span class='parser_error'>[include] '#{@params[:part]}' not found in template '#{@params[:template]}'</span>"] end else included_blocks = res.blocks @ids.merge! res.ids end enter(:void) # normal scan on content # replace 'with' not_found = [] @blocks.each do |b| next if b.kind_of?(String) || b.method != 'with' if target = res.ids[b.params[:part]] if target.kind_of?(String) # error elsif b.empty? target.method = 'ignore' else target.replace_with(b) end else # part not found not_found << "<span class='parser_error'>[with] '#{b.params[:part]}' not found in template '#{@params[:template]}'</span>" end end @blocks = included_blocks + not_found end |
#inspect ⇒ Object
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
# File 'lib/zena/parser.rb', line 562 def inspect attributes = [] params = [] (@params || {}).each do |k,v| unless v.nil? params << "#{k.inspect.gsub('"', "'")}=>'#{v}'" end end attributes << " {= #{params.sort.join(', ')}}" unless params == [] context = [] (@context || {}).each do |k,v| unless v.nil? context << "#{k.inspect.gsub('"', "'")}=>'#{v}'" end end attributes << " {> #{context.sort.join(', ')}}" unless context == [] pass = [] (@pass || {}).each do |k,v| unless v.nil? if v.kind_of?(Array) pass << "#{k.inspect.gsub('"', "'")}=>#{v.inspect.gsub('"', "'")}" elsif v.kind_of?(Parser) pass << "#{k.inspect.gsub('"', "'")}=>['#{v}']" else pass << "#{k.inspect.gsub('"', "'")}=>#{v.inspect.gsub('"', "'")}" end end end attributes << " {< #{pass.sort.join(', ')}}" unless pass == [] res = [] @blocks.each do |b| if b.kind_of?(String) res << b else res << b.inspect end end result = "[#{@method}#{attributes.join('')}" if res != [] result += "]#{res}[/#{@method}]" else result += "/]" end result + @text end |
#leave(mode = nil) ⇒ Object
428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/zena/parser.rb', line 428 def leave(mode=nil) if mode.nil? @stack = [] return end pop = true while @stack != [] && pop pop = @stack.pop break if pop == mode end end |
#make(mode, opts = {}) ⇒ Object
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/zena/parser.rb', line 409 def make(mode, opts={}) if opts[:text] custom_text = opts.delete(:text) end text = custom_text || @text opts = @options.merge(opts).merge(:sub=>true, :mode=>mode, :parent => self) new_obj = self.class.new(text,opts) if new_obj.success? @text = new_obj.text unless custom_text new_obj.text = "" store new_obj else flush @text[0..(new_obj.text.length - @text.length)] unless custom_text end # puts "MADE #{new_obj.inspect}" # puts "TEXT #{@text.inspect}" new_obj end |
#out(obj) ⇒ Object
Set output during render
379 380 381 |
# File 'lib/zena/parser.rb', line 379 def out(obj) @result << obj end |
#parse_params(text) ⇒ Object
Parse parameters into a hash. This parsing supports multiple values for one key by creating additional keys: <tag do=‘hello’ or=‘goodbye’ or=‘gotohell’> creates the hash :or=>‘goodbye’, :or1=>‘gotohell’
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/zena/parser.rb', line 447 def parse_params(text) return {} unless text return text if text.kind_of?(Hash) params = {} rest = text.strip while (rest != '') if rest =~ /(.+?)=/ key = $1.strip.to_sym rest = rest[$&.length..-1].strip if rest =~ /('|")(|[^\1]*?[^\\])\1/ rest = rest[$&.length..-1].strip key_counter = 1 while params[key] key = "#{key}#{key_counter}".to_sym key_counter += 1 end if $1 == "'" params[key] = $2.gsub("\\'", "'") else params[key] = $2.gsub('\\"', '"') end else # error, bad format, return found params. break end else # error, bad format break end end params end |
#r_expand_with ⇒ Object
Set context with variables (unsafe) from template.
203 204 205 206 207 208 209 |
# File 'lib/zena/parser.rb', line 203 def hash = {} @params.each do |k,v| hash["exp_#{k}"] = v.inspect end (hash) end |
#r_ignore ⇒ Object
174 175 |
# File 'lib/zena/parser.rb', line 174 def r_ignore end |
#r_inspect ⇒ Object
179 180 181 182 183 184 |
# File 'lib/zena/parser.rb', line 179 def r_inspect (:preflight=>true) @blocks = [] @pass.merge!(@parts||{}) self.inspect end |
#r_unknown ⇒ Object
basic rule to display errors
187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/zena/parser.rb', line 187 def r_unknown sp = "" @params.each do |k,v| sp += " #{k}=#{v.inspect.gsub("'","TMPQUOTE").gsub('"',"'").gsub("TMPQUOTE",'"')}" end res = "<span class='parser_unknown'><r:#{@method}#{sp}" inner = if inner != '' res + "></span>#{inner}<span class='parser_unknown'><r:/#{@method}></span>" else res + "/></span>" end end |
#r_void ⇒ Object Also known as: to_s
170 171 172 |
# File 'lib/zena/parser.rb', line 170 def r_void end |
#render(context = {}) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/zena/parser.rb', line 138 def render(context={}) if @name # we pass the name as 'context' in the children tags @context = context.merge(:name => @name) else @context = context end @result = '' return @result unless before_render @pass = {} # used to pass information to the parent res = nil if respond_to?("r_#{@method}".to_sym) res = do_method("r_#{@method}".to_sym) else res = do_method(:r_unknown) end after_render(res + @text) end |
#replace_with(obj) ⇒ Object
Hook called when replacing part of an included template with ‘<r:with part=’main’>…</r:with>‘ This replaces the current object ’self’ which is in the original included template, with the custom version ‘obj’.
123 124 125 126 127 |
# File 'lib/zena/parser.rb', line 123 def replace_with(obj) # keep @method (obj's method is always 'with') @blocks = obj.blocks.empty? ? @blocks : obj.blocks @params.merge!(obj.params) end |
#root ⇒ Object
Return the root block (the one opened first).
351 352 353 |
# File 'lib/zena/parser.rb', line 351 def root @root ||= parent ? parent.root : self end |
#start(mode) ⇒ Object
117 118 119 |
# File 'lib/zena/parser.rb', line 117 def start(mode) enter(mode) end |
#store(obj) ⇒ Object
Build blocks
370 371 372 373 374 375 376 |
# File 'lib/zena/parser.rb', line 370 def store(obj) if obj.kind_of?(String) && @blocks.last.kind_of?(String) @blocks[-1] << obj elsif obj != '' @blocks << obj end end |
#success? ⇒ Boolean
355 356 357 |
# File 'lib/zena/parser.rb', line 355 def success? return @ok end |