Changeset 305
- Timestamp:
- 2007-02-23 11:32:28 (2 years ago)
- Files:
-
- trunk/lib/parser/lib/parser.rb (modified) (3 diffs)
- trunk/lib/parser/lib/rules/zafu.rb (modified) (11 diffs)
- trunk/lib/parser/lib/rules/zena.rb (modified) (9 diffs)
- trunk/lib/parser/test/parser_test.rb (modified) (1 diff)
- trunk/lib/parser/test/zafu.yml (modified) (4 diffs)
- trunk/test/helpers/basic.yml (modified) (1 diff)
- trunk/test/helpers/test_all.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/parser/lib/parser.rb
r304 r305 46 46 end 47 47 end 48 text ||= "<span class=' zafu_error'>template '#{url}' not found</span>"48 text ||= "<span class='parser_error'>template '#{url}' not found</span>" 49 49 return [text, absolute_url] 50 50 end … … 135 135 end 136 136 137 res = "<span class=' zafu_unknown'><z:#{@method}#{sp}"137 res = "<span class='parser_unknown'><z:#{@method}#{sp}" 138 138 inner = expand_with 139 139 if inner != '' 140 res + "></span>#{inner}<span class=' zafu_unknown'><z:/#{@method}></span>"140 res + "></span>#{inner}<span class='parser_unknown'><z:/#{@method}></span>" 141 141 else 142 142 res + "/></span>" … … 158 158 if absolute_url 159 159 if @options[:included_history].include?(absolute_url) 160 @text = "<span class=' zafu_error'>[include error: #{(@options[:included_history] + [absolute_url]).join(' --> ')} ]</span>"160 @text = "<span class='parser_error'>[include error: #{(@options[:included_history] + [absolute_url]).join(' --> ')} ]</span>" 161 161 else 162 162 @options[:included_history] += [absolute_url] trunk/lib/parser/lib/rules/zafu.rb
r304 r305 5 5 6 6 def render(context={}) 7 @ zafu_tag_done = false8 res = render_ zafu_tag(super)7 @html_tag_done = false 8 res = render_html_tag(super) 9 9 end 10 10 11 11 def inspect 12 @ zafu_tag_done = false12 @html_tag_done = false 13 13 res = super 14 if (@zafu_tag || @params[:tag]) && !@zafu_tag_done14 if @html_tag && !@html_tag_done 15 15 if res =~ /\A\[(\w+)(.*)\/\]\Z/ 16 res = "[#{$1}#{$2}]<#{ tag}/>[/#{$1}]"16 res = "[#{$1}#{$2}]<#{@html_tag}/>[/#{$1}]" 17 17 elsif res =~ /\A\[([^\]]+)\](.*)\[\/(\w+)\]\Z/ 18 res = "[#{$1}]#{render_ zafu_tag($2)}[/#{$3}]"19 end 20 @ zafu_tag_done = true18 res = "[#{$1}]#{render_html_tag($2)}[/#{$3}]" 19 end 20 @html_tag_done = true 21 21 end 22 22 res … … 31 31 end 32 32 33 def render_zafu_tag(text) 34 return text unless ((@zafu_tag || @params[:tag]) && !@zafu_tag_done) 35 res = "<#{(@zafu_tag || @params[:tag])}#{params_to_html(@zafu_tag_params || {})}>#{text}</#{(@zafu_tag || @params[:tag])}>" 36 @zafu_tag_done = true 33 def render_html_tag(text) 34 return text unless @html_tag && !@html_tag_done 35 res = "<#{@html_tag}#{params_to_html(@html_tag_params || {})}" 36 if text != '' 37 res << ">#{text}</#{@html_tag}>" 38 else 39 res << "/>" 40 end 41 @html_tag_done = true 37 42 res 38 43 end 39 44 40 45 def r_rename_asset 41 return expand_with unless @tag 42 tag = @params[:tag] 46 return expand_with unless @html_tag 43 47 unless @params[:src][0..0] == '/' 44 case @ tag48 case @html_tag 45 49 when 'link' 46 50 if @params[:rel].downcase == 'stylesheet' … … 50 54 end 51 55 else 52 @params[:src] = @options[:helper].send(:template_url_for_asset, @tag.to_sym , @params[:src]) 53 end 54 end 55 res = "<#{@tag}#{params_to_html(@params)}" 56 @params[:src] = @options[:helper].send(:template_url_for_asset, @html_tag.to_sym , @params[:src]) 57 end 58 end 59 res = "<#{@html_tag}#{params_to_html(@params)}" 60 @html_tag_done = true 56 61 inner = expand_with 57 62 if inner == '' … … 67 72 module Rules 68 73 def start(mode) 69 if @zafu_tag = @options[:zafu_tag] 70 @options.delete(:zafu_tag) 71 @zafu_tag_params = parse_params(@options[:zafu_tag_params]) 72 @options.delete(:zafu_tag_params) 73 @zafu_tag_count = 1 74 elsif @zafu_tag = @options[:end_zafu] 75 @end_zafu = true 76 @options.delete(:end_zafu) 77 @zafu_tag_count = 1 78 else 79 @zafu_tag_count = 0 80 end 81 @end_ztag = @options[:end_ztag] || @method 82 @options.delete(:end_ztag) 74 # html_tag 75 if @html_tag = @options[:html_tag] 76 @options.delete(:html_tag) 77 @html_tag_params = parse_params(@options[:html_tag_params]) 78 @options.delete(:html_tag_params) 79 end 80 81 # end_tag 82 @end_tag = @html_tag || @options[:end_do] || "z:#{@method}" 83 @end_tag_count = 1 83 84 84 85 if @params =~ /\A([^>]*?)do\s*=('|")([^\2]*?[^\\])\2([^>]*)\Z/ … … 88 89 89 90 # the matching zafu tag will be parsed by the last 'do', we must inform it to halt properly : 90 if @zafu_tag 91 opts[:end_zafu] = @zafu_tag 92 else 93 opts[:end_ztag] = @method 94 end 91 opts[:end_do] = @end_tag 92 95 93 make(:void, opts) 96 94 if @method == 'include' … … 107 105 end 108 106 end 109 if @end_zafu 110 @zafu_tag = nil 107 108 if !@html_tag && (@html_tag = @params[:tag]) 109 @params.delete(:tag) 110 # get html tag parameters from @params 111 @html_tag_params = {} 112 [:class, :id].each do |k| 113 next unless @params[k] 114 @html_tag_params[k] = @params[k] 115 @params.delete(k) 116 end 111 117 end 112 118 end … … 135 141 136 142 def scan_close_tag 137 if @text =~ /\A<\/( z:|)([^>]+)>/143 if @text =~ /\A<\/([^>]+)>/ 138 144 # puts "CLOSE:[#{$&}]}" # ztag 139 145 # closing tag 140 if $1 == '' 141 # /html 142 if $2 == @zafu_tag 143 # zafu tag 144 @zafu_tag_count -= 1 145 if @zafu_tag_count == 0 146 eat $& 147 leave 148 else 149 # keep the tag (false alert) 150 flush $& 151 end 152 else 153 # other html tag closing 146 if $1 == @end_tag 147 @end_tag_count -= 1 148 if @end_tag_count == 0 149 eat $& 150 leave 151 else 152 # keep the tag (false alert) 154 153 flush $& 155 154 end 156 els e155 elsif $1[0..1] == 'z:' 157 156 # /ztag 158 157 eat $& 159 if $ 2 != @end_ztag158 if $1 != @end_tag 160 159 # error bad closing ztag 161 store "<span class=' zafu_error'>#{$&.gsub('<', '<').gsub('>','>')}</span>"160 store "<span class='parser_error'>#{$&.gsub('<', '<').gsub('>','>')}</span>" 162 161 end 163 162 leave 163 else 164 # other html tag closing 165 flush $& 164 166 end 165 167 else … … 187 189 if @text =~ /\A<z:(\w+)([^>]*?)(\/?)>/ 188 190 # puts "ZTAG:[#{$&}]}" # ztag 189 closed = ($3 != '')190 191 eat $& 191 192 opts = {:method=>$1, :params=>$2} … … 195 196 # puts "DO:[#{$&}]}" # do tag 196 197 eat $& 197 opts = {:method=>$4, : zafu_tag=>$1, :zafu_tag_params=>$2, :params=>$5}198 opts = {:method=>$4, :html_tag=>$1, :html_tag_params=>$2, :params=>$5} 198 199 opts.merge!(:text=>'') if $6 != '' 199 200 make(:void, opts) 200 elsif @ zafu_tag && @text =~ /\A<#{@zafu_tag}([^>]*?)(\/?)>/201 # puts "SAME:[#{$&}]}" # simple html tag same as zafu_tag202 flush $& 203 @ zafu_tag_count += 1 unless $2 == '/' # count opened zafu tags to be closed before return201 elsif @end_tag && @text =~ /\A<#{@end_tag}([^>]*?)(\/?)>/ 202 # puts "SAME:[#{$&}]}" # simple html tag same as end_tag 203 flush $& 204 @end_tag_count += 1 unless $2 == '/' 204 205 elsif @text =~ /\A<(link|img|script).*src\s*=/ 205 206 # puts "HTML:[#{$&}]}" # html … … 220 221 eat $& 221 222 @method = 'rename_asset' 222 @ tag = $1223 @html_tag = @end_tag = $1 223 224 closed = ($3 != '') 224 225 @params = parse_params($2) … … 236 237 237 238 def scan_inside_asset 238 if @text =~ /\A(.*?)<\/#{@ params[:tag]}>/239 if @text =~ /\A(.*?)<\/#{@end_tag}>/ 239 240 flush $& 240 241 leave(:asset) trunk/lib/parser/lib/rules/zena.rb
r304 r305 234 234 if @params[:text] 235 235 text = @params[:text] 236 text = "<div>#{text}</div>" unless @ options[:zafu_tag]236 text = "<div>#{text}</div>" unless @html_tag 237 237 elsif @params[:trans] 238 238 text = helper.trans(@params[:trans]) 239 text = "<div>#{text}</div>" unless @ options[:zafu_tag]239 text = "<div>#{text}</div>" unless @html_tag 240 240 else 241 241 text = expand_with … … 244 244 # ajax add 245 245 prefix = @context[:template_url].gsub('/','_') 246 if @ options[:zafu_tag]247 out "<#{@ params[:tag]} id='#{prefix}<%= @#{node_class.to_s.downcase}[:id] %>'>"246 if @html_tag 247 out "<#{@html_tag} id='#{prefix}<%= @#{node_class.to_s.downcase}[:id] %>'>" 248 248 else 249 249 text = add_params(text, :id=>"#{prefix}_add", :onclick=>"new Element.toggle('#{prefix}_add', '#{prefix}_form');return false;") … … 251 251 out text 252 252 out expand_block(@context[:form],:node=>"@#{node_class.to_s.downcase}", :tag_params=>{:id=>"#{prefix}_form", :style=>"display:none;"}) 253 if @ options[:zafu_tag]254 out "</#{@ options[:zafu_tag]}>"253 if @html_tag 254 out "</#{@html_tag}>" 255 255 end 256 256 else … … 281 281 # ajax, set id 282 282 id_hash = {:id=>"#{@context[:template_url].gsub('/', '_')}<%= #{var}[:id] %>"} 283 if @ zafu_tag284 @ zafu_tag_params.merge!(id_hash)283 if @html_tag 284 @html_tag_params.merge!(id_hash) 285 285 else 286 286 res = add_params(res, :id=>"#{@context[:template_url].gsub('/', '_')}<%= #{var}[:id] %>") 287 287 end 288 288 end 289 res = render_ zafu_tag(res)289 res = render_html_tag(res) 290 290 out res 291 291 out "<% end -%>" … … 319 319 320 320 def r_when 321 return "<span class=' zafu_error'>bad context for when clause</span>" unless @context[:case]321 return "<span class='parser_error'>bad context for when clause</span>" unless @context[:case] 322 322 if klass = @params[:kind_of] 323 323 begin Module::const_get(klass) rescue "NilClass" end … … 350 350 cond = nil 351 351 end 352 return "<span class=' zafu_error'>condition error for when clause</span>" unless cond352 return "<span class='parser_error'>condition error for when clause</span>" unless cond 353 353 out "<% elsif #{cond} -%>" 354 354 out expand_with(:case=>false) … … 376 376 cond = "find_by_path(#{@params[:path].split('/').inspect})" 377 377 else 378 return "<span class=' zafu_error'>Bad node parameters, should be (node_id or path)</span>"378 return "<span class='parser_error'>Bad node parameters, should be (node_id or path)</span>" 379 379 end 380 380 do_var("secure(Node) { Node.#{cond}}") … … 456 456 end 457 457 458 def r_set_attribute 459 if @zafu_tag 460 tag = @zafu_tag 461 params = @params.merge(@zafu_tag_params) 462 @zafu_tag_done = true 463 else 464 tag = 'div' 465 params = @params 466 end 467 res_params = {} 468 params.each do |k,v| 469 if k.to_s =~ /^set_(.+)$/ 470 key = $1 471 value = v.gsub(/\[([^\]]+)\]/) do 472 "<%= #{node}#{get_attribute($1)} %>" 473 end 474 res_params[key.to_sym] = value 475 else 476 res_params[k] = v unless res_params[k] 477 end 478 end 479 res = "<#{tag}#{params_to_html(res_params)}" 480 inner = expand_with 481 if inner == '' 482 res + "/>" 483 else 484 res + ">#{inner}</#{tag}>" 485 end 458 def r_void 459 expand_with 486 460 end 487 461 … … 687 661 "[#{attribute.to_sym.inspect}]" 688 662 end 663 end 664 665 def render_html_tag(text) 666 set_params = {} 667 @params.each do |k,v| 668 next unless k.to_s =~ /^set_/ 669 set_params[k] = v 670 end 671 @html_tag = 'div' if !@html_tag && set_params != {} 672 673 return text unless @html_tag && !@html_tag_done 674 @html_tag_params ||= {} 675 bak = @html_tag_params.dup 676 res_params = {} 677 set_params.merge(@html_tag_params).each do |k,v| 678 if k.to_s =~ /^set_(.+)$/ 679 key = $1 680 value = v.gsub(/\[([^\]]+)\]/) do 681 "<%= #{node}#{get_attribute($1)} %>" 682 end 683 res_params[key.to_sym] = value 684 else 685 res_params[k] = v unless res_params[k] 686 end 687 end 688 @html_tag_params = res_params 689 res = super(text) 690 @html_tag_params = bak 691 res 689 692 end 690 693 trunk/lib/parser/test/parser_test.rb
r303 r305 22 22 testfile :zafu, :zafu_asset, :zafu_insight 23 23 def test_single 24 do_test('zafu', ' ztag_tag')24 do_test('zafu', 'do_class_params') 25 25 end 26 26 make_tests trunk/lib/parser/test/zafu.yml
r304 r305 33 33 very_messy: 34 34 src: "this <z: blah> </z:truc> whak" 35 res: "this <z: blah> <span class=' zafu_error'></z:truc></span> whak"35 res: "this <z: blah> <span class='parser_error'></z:truc></span> whak" 36 36 37 37 bad_closing_tag: 38 38 src: "this <z:test>looks </z:truc> ok" 39 res: "this [test]looks <span class=' zafu_error'></z:truc></span>[/test] ok"40 41 zafu_tag:39 res: "this [test]looks <span class='parser_error'></z:truc></span>[/test] ok" 40 41 do_tag: 42 42 src: "I say <p do='hello'>blah blah</p>" 43 43 res: "I say <p>hello world!</p>" 44 44 45 zafu_tag_count:45 do_tag_count: 46 46 src: "I say <p do='hello'>blah <p>super</p> blah</p> is good" 47 47 res: "I say <p>hello world!</p> is good" 48 48 49 zafu_tag_closed:49 do_tag_closed: 50 50 src: "I say <p do='hello'/> is good" 51 51 res: "I say <p>hello world!</p> is good" 52 52 53 zafu_tag_block:53 do_tag_block: 54 54 src: "I say <div do='test'>this <div>test</div> is ok</div> right?" 55 55 res: "I say [test]<div>this <div>test</div> is ok</div>[/test] right?" 56 56 57 zafu_tag_at_end:57 do_tag_at_end: 58 58 src: "I say <div do='test'>this <div>test</div> is ok</div>" 59 59 res: "I say [test]<div>this <div>test</div> is ok</div>[/test]" 60 60 61 zafu_tag_with_ztags:61 do_tag_with_ztags: 62 62 src: "I say <div do='test'>this <z:hello/></div>" 63 63 res: "I say [test]<div>this [hello/]</div>[/test]" 64 64 65 zafu_keep_params:65 do_keep_params: 66 66 src: "I love <div class='truc' id='machin' do='test' depth='3'>my wife</div>" 67 67 res: "I love [test {= :depth=>'3'}]<div class='truc' id='machin'>my wife</div>[/test]" 68 68 69 zafu_and_ztags: 69 do_class_params: 70 src: "<div class='one' do='test' do='test' tag='p' class='two'>blah</div>" 71 res: "[test]<div class='one'>[test]<p class='two'>blah</p>[/test]</div>[/test]" 72 73 do_and_ztags: 70 74 src: "Hey <z:test>this is <p do='hello'>ok</p></z:test>" 71 75 res: "Hey [test]this is [hello]<p>ok</p>[/hello][/test]" 72 76 73 zafu_same_tag:77 do_same_tag: 74 78 src: "Hey <p do='test'> is <p do='hello'>hello <p>cool</p></p></p>" 75 79 res: "Hey [test]<p> is [hello]<p>hello <p>cool</p></p>[/hello]</p>[/test]" … … 96 100 infinite_loop: 97 101 src: "<z:include template='/infinite/loop'/>" 98 res: "<span class=' zafu_error'>[include error: /infinite/loop --> /infinite/loop ]</span>"102 res: "<span class='parser_error'>[include error: /infinite/loop --> /infinite/loop ]</span>" 99 103 100 104 infinite_foo: 101 105 src: "<z:include template='bar'/>" 102 res: "<span class=' zafu_error'>[include error: /infinite/foo --> /infinite/bar --> /infinite/foo ]</span>"106 res: "<span class='parser_error'>[include error: /infinite/foo --> /infinite/bar --> /infinite/foo ]</span>" 103 107 104 108 infinite_bar: 105 109 src: "<z:include template='foo'/>" 106 res: "<span class=' zafu_error'>[include error: /infinite/bar --> /infinite/foo --> /infinite/bar ]</span>"110 res: "<span class='parser_error'>[include error: /infinite/bar --> /infinite/foo --> /infinite/bar ]</span>" 107 111 108 112 other_context: … … 140 144 unknown: 141 145 src: "this looks bad: <z:system truc='lala'/>" 142 res: "this looks bad: <span class=' zafu_unknown'><z:system truc='lala'/></span>"146 res: "this looks bad: <span class='parser_unknown'><z:system truc='lala'/></span>" 143 147 144 148 check_params: … … 172 176 res: "[test][hello/]<!-- this is nothing > <z:hello/> -->[/test]" 173 177 174 html_ zafu_comment:178 html_do_comment: 175 179 src: "<z:test><z:hello/><!--|this is not a comment <z:hello/> --></z:test>" 176 180 res: "[test][hello/]this is not a comment [hello/] [/test]" trunk/test/helpers/basic.yml
r304 r305 252 252 253 253 set_attribute: 254 src: "<div class='machin' do='set_attribute' set_class='node[id]'/><z:set_attribute set_class='s[v_status]'/>" 255 tem: "<div class='node<%= @node[:id] %>'/><div class='s<%= @node.version[:status] %>'/>" 256 res: "<div class='node12'/><div class='s50'/>" 254 src: "<div class='machin' do='void' set_class='node[id]'/><z:void set_class='s[v_status]i[id]'/>" 255 res: "<div class='node12'/><div class='s50i12'/>" 256 257 set_attribute_with_inner: 258 src: "<p set_id='node[id]' do='show' attr='name' set_class='s[v_status]'>some blah blah</p>" 259 res: "<p class='s50' id='node12'>status</p>" 260 261 set_in_ztag: 262 src: "<z:show attr='name' set_id='node[id]'/>" 263 res: "<div id='node12'>status</div>" 257 264 258 265 img_no_image: trunk/test/helpers/test_all.rb
r304 r305 4 4 testfile :relations, :basic 5 5 def test_single 6 do_test(' relations', 'author_visitor')6 do_test('basic', 'set_in_ztag') 7 7 end 8 8 make_tests
