Changeset 311

Show
Ignore:
Timestamp:
2007-02-24 15:45:58 (2 years ago)
Author:
gaspard
Message:

Parser: better 'with' tag, now the full tag is replaced instead of the blocks only.

Files:

Legend:

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

    r305 r311  
    8282  def render(context={}) 
    8383    return '' if context["no_#{@method}".to_sym] 
     84    return '' unless before_render 
    8485    @context = context 
    85     @context[:name] = @params[:name] if @params[:name] # name param is propagated into children (used to label parts of a large template) 
     86    # name param is propagated into children (used to label parts of a large template) 
     87    if name = @params[:name] 
     88      @params.delete(:name) 
     89      if @context[:name] 
     90        @context[:name] << "/#{name}" 
     91      else 
     92        @context[:name] = name 
     93      end 
     94      if replacer = (@context[:parts] || {})[@context[:name]] 
     95        new_parts = @context[:parts].dup 
     96        new_parts.delete(@context[:name]) 
     97        return replacer.render(context.merge(:parts=>new_parts)) 
     98      end 
     99    end 
    86100    @result  = "" 
    87101    @pass    = {} # used to pass information to the parent 
    88     if blocks = @context[@params[:name]] 
    89       # replaced content throught name='..' param 
    90       @context.delete(@params[:name]) 
    91       @blocks = blocks 
    92     end 
    93102    res = nil 
    94103    if self.respond_to?("r_#{@method}".to_sym) 
     
    100109      res = @result 
    101110    elsif !res.kind_of?(String) 
    102       res = "#{@method}" 
     111      res = @method 
    103112    end 
    104113    res + @text 
     114  end 
     115   
     116  def r_with 
     117    return unless part = @params[:part] 
     118    if @context[:preflight] 
     119      @pass[:part] = {part => self} 
     120      "" 
     121    else 
     122      r_void 
     123    end 
    105124  end 
    106125   
     
    108127    expand_with 
    109128  end 
     129   
    110130  alias to_s r_void 
    111131   
    112   def r_with 
    113     return unless @params[:part] 
    114     @pass[@params[:part]] = @blocks 
    115     "" 
    116   end 
    117    
    118132  def r_inspect 
    119     expand_with 
     133    expand_with(:preflight=>true) 
    120134    @blocks = [] 
     135    @pass.merge!(@parts||{}) 
    121136    self.inspect 
    122137  end 
    123138   
    124139  def r_include 
    125     expand_with 
     140    expand_with(:preflight=>true) 
    126141    @blocks = @included_blocks || @blocks 
    127     expand_with(@pass) 
     142    if @parts != {} 
     143      expand_with(:parts=>@parts) 
     144    else 
     145      expand_with 
     146    end 
    128147  end 
    129148   
     
    142161      res + "/&gt;</span>" 
    143162    end 
     163  end 
     164   
     165  def before_render 
     166    true 
    144167  end 
    145168   
     
    331354  def expand_with(acontext={}) 
    332355    res = "" 
    333     @pass = {} # current object sees some information from it's direct descendants 
     356    @pass  = {} # current object sees some information from it's direct descendants 
     357    @parts = {} 
    334358    new_context = @context.merge(acontext) 
    335359    @blocks.each do |b| 
     
    338362      else 
    339363        res << b.render(new_context) 
    340         @pass.merge!(b.pass||{}) 
     364        if pass = b.pass 
     365          if pass[:part] 
     366            @parts.merge!(pass[:part]) 
     367            pass.delete(:part) 
     368          end 
     369          @pass.merge!(pass) 
     370        end 
    341371      end 
    342372    end 
     
    368398          pass << "#{k.inspect.gsub('"', "'")}=>#{v.inspect.gsub('"', "'")}" 
    369399        elsif v.kind_of?(Parser) 
    370           pass << "#{k.inspect.gsub('"', "'")}=>'<#{v.method}>'
     400          pass << "#{k.inspect.gsub('"', "'")}=>['#{v}']
    371401        else 
    372           pass << "#{k.inspect.gsub('"', "'")}=>'#{v.inspect.gsub('"', "'")}'
     402          pass << "#{k.inspect.gsub('"', "'")}=>#{v.inspect.gsub('"', "'")}
    373403        end 
    374404      end 
  • trunk/lib/parser/lib/rules/zafu.rb

    r305 r311  
    55     
    66    def render(context={}) 
     7      @context = context 
    78      @html_tag_done = false 
    8       res = render_html_tag(super) 
     9      res = super 
     10      if (@context[:parts] || {})[@context[:name]] 
     11        res 
     12      else 
     13        render_html_tag(res) 
     14      end 
    915    end 
    1016     
  • trunk/lib/parser/lib/rules/zena.rb

    r310 r311  
    2626    inline_methods :login_link, :visitor_link, :search_box, :menu, :path_links, :lang_links 
    2727    direct_methods :uses_calendar 
     28 
     29    def before_render 
     30      if @params[:store] 
     31        @context["stored_#{@params[:store]}".to_sym] = node 
     32        @params.delete(:store) 
     33      end 
     34      true 
     35    end 
    2836 
    2937    def r_show 
     
    499507    end 
    500508     
    501     def r_void 
    502       if @params[:store] 
    503         @context["stored_#{@params[:store]}".to_sym] = node 
    504       end 
    505       expand_with 
    506     end 
    507      
    508509    def r_ignore 
    509510      @html_tag_done = true 
  • trunk/lib/parser/test/parser_test.rb

    r305 r311  
    44    def r_hello 
    55      'hello world!' 
     6    end 
     7     
     8    def r_text 
     9      @params[:text] 
    610    end 
    711     
     
    2226  testfile :zafu, :zafu_asset, :zafu_insight 
    2327  def test_single 
    24     do_test('zafu', 'do_class_params') 
     28    do_test('zafu_insight', 'simple') 
    2529  end 
    2630  make_tests 
  • trunk/lib/parser/test/zafu.yml

    r305 r311  
    199199  src: "<z:hello tag='p'/>" 
    200200  res: "<p>hello world!</p>" 
     201 
     202name: 
     203  src: "super: <z:void name='super'>hey <z:text text='ho' name='man'/></z:void>" 
     204  res: "super: hey ho" 
     205 
     206include_super: 
     207  src: "include_super: <z:include template='/name'><p do='with' part='super' do='hello'/>" 
     208  res: "include_super: super: <p>hello world!</p>" 
     209 
     210include_super_man: 
     211  src: "include_super: <z:include template='/name'><p do='with' part='super/man' do='hello'/>" 
     212  res: "include_super: super: hey <p>hello world!</p>" 
     213 
     214name_title: 
     215  src: "title: <h1 do='text' text='dummy' name='title'>blah</h1>" 
     216  res: "title: <h1>dummy</h1>" 
     217 
     218include_with: 
     219  src: "include_with: <z:include template='/name/title'><h1 do='with' part='title' do='hello'/></z:include>" 
     220  res: "include_with: title: <h1>hello world!</h1>" 
  • trunk/lib/parser/test/zafu_insight.yml

    r304 r311  
    55with_sub_tags: 
    66  src: "<z:inspect><z:with part='hello'>I say <h3 do='hello'>hello</h3></z:with></z:inspect>" 
    7   res: "[inspect {< 'hello'=>['I say ', [hello]<h3>hello</h3>[/hello]]}/]" 
     7  res: "[inspect {< 'hello'=>['I say <h3>hello world!</h3>']}/]" 
    88 
    99menu: 
     
    1313include: 
    1414  src: "<z:include template='/menu'><z:with part='a'>do <z:inspect/></z:with></z:include>" 
    15   res: "var a: do [inspect {> :menu=>'true', :name=>'a'}/]" 
     15  res: "var a: do [inspect {> :menu=>'true', :name=>'a', :parts=>''}/]" 
  • trunk/test/helpers/basic.yml

    r307 r311  
    331331  src: "<ul do='ignore' do='children' do='each' tag='li'><z:link/></ul>" 
    332332  res: "" 
     333 
     334name_title: 
     335  src: "title: <h1 do='show' name='title' attr='name'>blah</h1>" 
     336  res: "title: <h1>status</h1>" 
     337 
     338include_with: 
     339  src: "include_with: <z:include template='/name/title'><h1 do='with' part='title' do='show' attr='id'/>" 
     340  res: "include_with: title: <h1>12</h1>" 
  • trunk/test/helpers/test_all.rb

    r309 r311  
    44  testfile :relations, :basic 
    55  def test_single 
    6     do_test('relations', 'date_select') 
     6    do_test('basic', 'include_with') 
    77  end 
    88