Changeset 305

Show
Ignore:
Timestamp:
2007-02-23 11:32:28 (2 years ago)
Author:
gaspard
Message:

Zafu set attributes now working on any node.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/parser/lib/parser.rb

    r304 r305  
    4646        end 
    4747      end 
    48       text ||= "<span class='zafu_error'>template '#{url}' not found</span>" 
     48      text ||= "<span class='parser_error'>template '#{url}' not found</span>" 
    4949      return [text, absolute_url] 
    5050    end 
     
    135135    end 
    136136       
    137     res = "<span class='zafu_unknown'>&lt;z:#{@method}#{sp}" 
     137    res = "<span class='parser_unknown'>&lt;z:#{@method}#{sp}" 
    138138    inner = expand_with 
    139139    if inner != '' 
    140       res + "&gt;</span>#{inner}<span class='zafu_unknown'>&lt;z:/#{@method}&gt;</span>" 
     140      res + "&gt;</span>#{inner}<span class='parser_unknown'>&lt;z:/#{@method}&gt;</span>" 
    141141    else 
    142142      res + "/&gt;</span>" 
     
    158158    if absolute_url 
    159159      if @options[:included_history].include?(absolute_url) 
    160         @text = "<span class='zafu_error'>[include error: #{(@options[:included_history] + [absolute_url]).join(' --&gt; ')} ]</span>" 
     160        @text = "<span class='parser_error'>[include error: #{(@options[:included_history] + [absolute_url]).join(' --&gt; ')} ]</span>" 
    161161      else 
    162162        @options[:included_history] += [absolute_url] 
  • trunk/lib/parser/lib/rules/zafu.rb

    r304 r305  
    55     
    66    def render(context={}) 
    7       @zafu_tag_done = false 
    8       res = render_zafu_tag(super) 
     7      @html_tag_done = false 
     8      res = render_html_tag(super) 
    99    end 
    1010     
    1111    def inspect 
    12       @zafu_tag_done = false 
     12      @html_tag_done = false 
    1313      res = super 
    14       if (@zafu_tag || @params[:tag]) && !@zafu_tag_done 
     14      if @html_tag && !@html_tag_done 
    1515        if res =~ /\A\[(\w+)(.*)\/\]\Z/ 
    16           res = "[#{$1}#{$2}]<#{tag}/>[/#{$1}]" 
     16          res = "[#{$1}#{$2}]<#{@html_tag}/>[/#{$1}]" 
    1717        elsif res =~ /\A\[([^\]]+)\](.*)\[\/(\w+)\]\Z/ 
    18           res = "[#{$1}]#{render_zafu_tag($2)}[/#{$3}]" 
    19         end 
    20         @zafu_tag_done = true 
     18          res = "[#{$1}]#{render_html_tag($2)}[/#{$3}]" 
     19        end 
     20        @html_tag_done = true 
    2121      end 
    2222      res 
     
    3131    end 
    3232     
    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 
    3742      res 
    3843    end 
    3944     
    4045    def r_rename_asset 
    41       return expand_with unless @tag 
    42       tag = @params[:tag] 
     46      return expand_with unless @html_tag 
    4347      unless @params[:src][0..0] == '/' 
    44         case @tag 
     48        case @html_tag 
    4549        when 'link' 
    4650          if @params[:rel].downcase == 'stylesheet' 
     
    5054          end 
    5155        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 
    5661      inner = expand_with 
    5762      if inner == '' 
     
    6772  module Rules 
    6873    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 
    8384       
    8485      if @params =~ /\A([^>]*?)do\s*=('|")([^\2]*?[^\\])\2([^>]*)\Z/   
     
    8889         
    8990        # 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         
    9593        make(:void, opts) 
    9694        if @method == 'include' 
     
    107105        end 
    108106      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 
    111117      end 
    112118    end 
     
    135141   
    136142    def scan_close_tag 
    137       if @text =~ /\A<\/(z:|)([^>]+)>/ 
     143      if @text =~ /\A<\/([^>]+)>/ 
    138144        # puts "CLOSE:[#{$&}]}" # ztag 
    139145        # 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) 
    154153            flush $& 
    155154          end 
    156         else 
     155        elsif $1[0..1] == 'z:' 
    157156          # /ztag 
    158157          eat $& 
    159           if $2 != @end_ztag 
     158          if $1 != @end_tag 
    160159            # error bad closing ztag 
    161             store "<span class='zafu_error'>#{$&.gsub('<', '&lt;').gsub('>','&gt;')}</span>" 
     160            store "<span class='parser_error'>#{$&.gsub('<', '&lt;').gsub('>','&gt;')}</span>" 
    162161          end 
    163162          leave 
     163        else   
     164          # other html tag closing 
     165          flush $& 
    164166        end 
    165167      else 
     
    187189      if @text =~ /\A<z:(\w+)([^>]*?)(\/?)>/ 
    188190        # puts "ZTAG:[#{$&}]}" # ztag 
    189         closed = ($3 != '') 
    190191        eat $& 
    191192        opts = {:method=>$1, :params=>$2} 
     
    195196        # puts "DO:[#{$&}]}" # do tag 
    196197        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} 
    198199        opts.merge!(:text=>'') if $6 != '' 
    199200        make(:void, opts) 
    200       elsif @zafu_tag && @text =~ /\A<#{@zafu_tag}([^>]*?)(\/?)>/ 
    201         # puts "SAME:[#{$&}]}" # simple html tag same as zafu_tag 
    202         flush $& 
    203         @zafu_tag_count += 1 unless $2 == '/' # count opened zafu tags to be closed before return 
     201      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 == '/' 
    204205      elsif @text =~ /\A<(link|img|script).*src\s*=/ 
    205206        # puts "HTML:[#{$&}]}" # html 
     
    220221        eat $& 
    221222        @method = 'rename_asset' 
    222         @tag = $1 
     223        @html_tag = @end_tag = $1 
    223224        closed = ($3 != '') 
    224225        @params = parse_params($2) 
     
    236237     
    237238    def scan_inside_asset 
    238       if @text =~ /\A(.*?)<\/#{@params[:tag]}>/ 
     239      if @text =~ /\A(.*?)<\/#{@end_tag}>/ 
    239240        flush $& 
    240241        leave(:asset) 
  • trunk/lib/parser/lib/rules/zena.rb

    r304 r305  
    234234      if @params[:text] 
    235235        text = @params[:text] 
    236         text = "<div>#{text}</div>" unless @options[:zafu_tag] 
     236        text = "<div>#{text}</div>" unless @html_tag 
    237237      elsif @params[:trans] 
    238238        text = helper.trans(@params[:trans]) 
    239         text = "<div>#{text}</div>" unless @options[:zafu_tag] 
     239        text = "<div>#{text}</div>" unless @html_tag 
    240240      else 
    241241        text = expand_with 
     
    244244        # ajax add 
    245245        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] %>'>" 
    248248        else 
    249249          text = add_params(text, :id=>"#{prefix}_add", :onclick=>"new Element.toggle('#{prefix}_add', '#{prefix}_form');return false;") 
     
    251251        out text 
    252252        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}>" 
    255255        end 
    256256      else 
     
    281281          # ajax, set id 
    282282          id_hash = {:id=>"#{@context[:template_url].gsub('/', '_')}<%= #{var}[:id] %>"} 
    283           if @zafu_tag 
    284             @zafu_tag_params.merge!(id_hash) 
     283          if @html_tag 
     284            @html_tag_params.merge!(id_hash) 
    285285          else 
    286286            res = add_params(res, :id=>"#{@context[:template_url].gsub('/', '_')}<%= #{var}[:id] %>") 
    287287          end 
    288288        end 
    289         res = render_zafu_tag(res) 
     289        res = render_html_tag(res) 
    290290        out res 
    291291        out "<% end -%>" 
     
    319319     
    320320    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] 
    322322      if klass = @params[:kind_of] 
    323323        begin Module::const_get(klass) rescue "NilClass" end 
     
    350350        cond = nil 
    351351      end 
    352       return "<span class='zafu_error'>condition error for when clause</span>" unless cond 
     352      return "<span class='parser_error'>condition error for when clause</span>" unless cond 
    353353      out "<% elsif #{cond} -%>" 
    354354      out expand_with(:case=>false) 
     
    376376        cond = "find_by_path(#{@params[:path].split('/').inspect})" 
    377377      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>" 
    379379      end 
    380380      do_var("secure(Node) { Node.#{cond}}") 
     
    456456    end 
    457457     
    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 
    486460    end 
    487461     
     
    687661        "[#{attribute.to_sym.inspect}]" 
    688662      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 
    689692    end 
    690693     
  • trunk/lib/parser/test/parser_test.rb

    r303 r305  
    2222  testfile :zafu, :zafu_asset, :zafu_insight 
    2323  def test_single 
    24     do_test('zafu', 'ztag_tag') 
     24    do_test('zafu', 'do_class_params') 
    2525  end 
    2626  make_tests 
  • trunk/lib/parser/test/zafu.yml

    r304 r305  
    3333very_messy: 
    3434  src: "this <z: blah> </z:truc> whak" 
    35   res: "this <z: blah> <span class='zafu_error'>&lt;/z:truc&gt;</span> whak" 
     35  res: "this <z: blah> <span class='parser_error'>&lt;/z:truc&gt;</span> whak" 
    3636 
    3737bad_closing_tag: 
    3838  src: "this <z:test>looks </z:truc> ok" 
    39   res: "this [test]looks <span class='zafu_error'>&lt;/z:truc&gt;</span>[/test] ok" 
    40  
    41 zafu_tag: 
     39  res: "this [test]looks <span class='parser_error'>&lt;/z:truc&gt;</span>[/test] ok" 
     40 
     41do_tag: 
    4242  src: "I say <p do='hello'>blah blah</p>" 
    4343  res: "I say <p>hello world!</p>" 
    4444 
    45 zafu_tag_count: 
     45do_tag_count: 
    4646  src: "I say <p do='hello'>blah <p>super</p> blah</p> is good" 
    4747  res: "I say <p>hello world!</p> is good" 
    4848 
    49 zafu_tag_closed: 
     49do_tag_closed: 
    5050  src: "I say <p do='hello'/> is good" 
    5151  res: "I say <p>hello world!</p> is good" 
    5252 
    53 zafu_tag_block: 
     53do_tag_block: 
    5454  src:  "I say <div do='test'>this <div>test</div> is ok</div> right?" 
    5555  res: "I say [test]<div>this <div>test</div> is ok</div>[/test] right?" 
    5656 
    57 zafu_tag_at_end: 
     57do_tag_at_end: 
    5858  src:  "I say <div do='test'>this <div>test</div> is ok</div>" 
    5959  res: "I say [test]<div>this <div>test</div> is ok</div>[/test]" 
    6060 
    61 zafu_tag_with_ztags: 
     61do_tag_with_ztags: 
    6262  src: "I say <div do='test'>this <z:hello/></div>" 
    6363  res: "I say [test]<div>this [hello/]</div>[/test]" 
    6464 
    65 zafu_keep_params: 
     65do_keep_params: 
    6666  src: "I love <div class='truc' id='machin' do='test' depth='3'>my wife</div>" 
    6767  res: "I love [test {= :depth=>'3'}]<div class='truc' id='machin'>my wife</div>[/test]" 
    6868 
    69 zafu_and_ztags: 
     69do_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 
     73do_and_ztags: 
    7074  src: "Hey <z:test>this is <p do='hello'>ok</p></z:test>" 
    7175  res: "Hey [test]this is [hello]<p>ok</p>[/hello][/test]" 
    7276 
    73 zafu_same_tag: 
     77do_same_tag: 
    7478  src: "Hey <p do='test'> is <p do='hello'>hello <p>cool</p></p></p>" 
    7579  res: "Hey [test]<p> is [hello]<p>hello <p>cool</p></p>[/hello]</p>[/test]"   
     
    96100infinite_loop: 
    97101  src: "<z:include template='/infinite/loop'/>" 
    98   res: "<span class='zafu_error'>[include error: /infinite/loop --&gt; /infinite/loop ]</span>" 
     102  res: "<span class='parser_error'>[include error: /infinite/loop --&gt; /infinite/loop ]</span>" 
    99103   
    100104infinite_foo: 
    101105  src: "<z:include template='bar'/>" 
    102   res: "<span class='zafu_error'>[include error: /infinite/foo --&gt; /infinite/bar --&gt; /infinite/foo ]</span>" 
     106  res: "<span class='parser_error'>[include error: /infinite/foo --&gt; /infinite/bar --&gt; /infinite/foo ]</span>" 
    103107   
    104108infinite_bar: 
    105109  src: "<z:include template='foo'/>" 
    106   res: "<span class='zafu_error'>[include error: /infinite/bar --&gt; /infinite/foo --&gt; /infinite/bar ]</span>" 
     110  res: "<span class='parser_error'>[include error: /infinite/bar --&gt; /infinite/foo --&gt; /infinite/bar ]</span>" 
    107111   
    108112other_context: 
     
    140144unknown: 
    141145  src:  "this looks bad: <z:system truc='lala'/>" 
    142   res: "this looks bad: <span class='zafu_unknown'>&lt;z:system truc='lala'/&gt;</span>" 
     146  res: "this looks bad: <span class='parser_unknown'>&lt;z:system truc='lala'/&gt;</span>" 
    143147   
    144148check_params: 
     
    172176  res: "[test][hello/]<!-- this is nothing > <z:hello/>  -->[/test]" 
    173177 
    174 html_zafu_comment: 
     178html_do_comment: 
    175179  src: "<z:test><z:hello/><!--|this is not a comment <z:hello/>  --></z:test>" 
    176180  res: "[test][hello/]this is not a comment [hello/]  [/test]" 
  • trunk/test/helpers/basic.yml

    r304 r305  
    252252 
    253253set_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 
     257set_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 
     261set_in_ztag: 
     262  src: "<z:show attr='name' set_id='node[id]'/>" 
     263  res: "<div id='node12'>status</div>" 
    257264 
    258265img_no_image: 
  • trunk/test/helpers/test_all.rb

    r304 r305  
    44  testfile :relations, :basic 
    55  def test_single 
    6     do_test('relations', 'author_visitor') 
     6    do_test('basic', 'set_in_ztag') 
    77  end 
    88  make_tests