Changeset 560

Show
Ignore:
Timestamp:
2007-05-21 17:14:10 (2 years ago)
Author:
gaspard
Message:

[fix] parser now replaces include/with during 'parse' and not during 'render'. Better inclusion of elements far away

Files:

Legend:

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

    r549 r560  
    141141  end 
    142142   
    143   def r_with 
    144     return unless part = @params[:part] 
    145     if @context[:preflight] 
    146       @pass[:part] = {part => self} 
    147       "" 
    148     else 
    149       r_void 
    150     end 
    151   end 
    152    
    153143  def r_void 
    154144    expand_with 
     145  end 
     146   
     147  def r_ignore 
    155148  end 
    156149   
     
    164157  end 
    165158   
    166   def r_include 
    167     expand_with(:preflight=>true) 
    168     if @parts != {} 
    169       # first definitions in inclusion history have precedence 
    170       expand_with(:parts  => (@parts).merge(@context[:parts] || {}), :blocks => @included_blocks) 
    171     else 
    172       expand_with(:blocks => @included_blocks) 
    173     end 
    174   end 
    175    
    176159  # basic rule to display errors 
    177160  def r_unknown 
     
    181164    end 
    182165       
    183     res = "<span class='parser_unknown'>&lt;z:#{@method}#{sp}" 
     166    res = "<span class='parser_unknown'>&lt;r:#{@method}#{sp}" 
    184167    inner = expand_with 
    185168    if inner != '' 
    186       res + "&gt;</span>#{inner}<span class='parser_unknown'>&lt;z:/#{@method}&gt;</span>" 
     169      res + "&gt;</span>#{inner}<span class='parser_unknown'>&lt;r:/#{@method}&gt;</span>" 
    187170    else 
    188171      res + "/&gt;</span>" 
     
    220203   
    221204  def include_template 
     205    @method = 'void' 
     206    text    = @text 
     207     
    222208    # fetch text 
    223     text = @text 
    224209    @options[:included_history] ||= [] 
    225210     
     
    239224    enter(:void) # scan fetched text 
    240225    if @params[:part] 
    241       @included_blocks = [find_part(@params[:part])] 
    242     else 
    243       @included_blocks = @blocks 
     226      included_blocks = [find_part(@blocks, @params[:part])] 
     227    else 
     228      included_blocks = @blocks 
    244229    end 
    245230     
    246231    @blocks = [] 
    247     @text = text 
     232    @text   = text 
    248233    enter(:void) # normal scan on content 
    249   end 
    250    
    251   def find_part(path) 
     234     
     235    # replace 'with' 
     236    @blocks.each do |b| 
     237      next if b.kind_of?(String) || b.method != 'with' 
     238      if target = find_part(included_blocks, b.params[:part]) 
     239        if target.kind_of?(String) 
     240          # error 
     241        elsif b.empty? 
     242          target.method = 'ignore' 
     243        else 
     244          name = target.params[:name] 
     245          target.replace_with(b) if !target.kind_of?(String) 
     246          target.params[:name] = name # make sure it is kept during 'replace' 
     247        end 
     248      else 
     249        # part not found 
     250      end 
     251    end 
     252    @blocks = included_blocks 
     253  end 
     254   
     255  def find_part(blocks, path) 
    252256    res    = self 
    253257    found  = [] 
    254258    path.split('/').reject {|e| e==''}.each do |name| 
    255       if res = find_name(res.blocks, name) 
     259      if res = find_name(blocks, name) 
    256260        found << name 
     261        blocks = res.blocks 
    257262      else 
    258263        return "<span class='parser_error'>'#{(found + [name]).join('/')}' not found in template '#{@params[:template]}'</span>" 
  • trunk/lib/parser/lib/rules/zafu.rb

    r542 r560  
    7777      @html_tag_done = true 
    7878      (@space_before || '') + res + (@space_after || '') 
     79    end 
     80     
     81    def r_ignore 
     82      super 
     83      @html_tag_done = true 
     84      "" 
    7985    end 
    8086     
  • trunk/lib/parser/lib/rules/zena.rb

    r554 r560  
    791791      end 
    792792      @context[:trans] ? "(#{res})" : "<%= #{res} %>" 
    793     end 
    794      
    795     def r_ignore 
    796       @html_tag_done = true 
    797       "" 
    798793    end 
    799794     
  • trunk/lib/parser/test/parser_test.rb

    r548 r560  
    4444  testfile :zafu, :zafu_asset, :zafu_insight, :zazen 
    4545  def test_single 
    46     do_test('zafu', 'include_self') 
     46    do_test('zafu', 'include_part_far_away') 
    4747  end 
    4848   
  • trunk/lib/parser/test/zafu.yml

    r549 r560  
    160160unknown: 
    161161  src:  "this looks bad: <r:system truc='lala'/>" 
    162   res: "this looks bad: <span class='parser_unknown'>&lt;z:system truc='lala'/&gt;</span>" 
     162  res: "this looks bad: <span class='parser_unknown'>&lt;r:system truc='lala'/&gt;</span>" 
    163163   
    164164check_params: 
     
    228228  src: "include_super: <r:include template='/name'><p do='with' part='super/man' do='hello'/>" 
    229229  res: "include_super: super: hey <p>hello world!</p>" 
    230  
     230   
     231include_part_far_away: 
     232  src: "IPFA: <r:include template='/include/super/man' part='super'/> AFPI." 
     233  res: "IPFA: hey <p>hello world!</p> AFPI." 
     234     
    231235name_title: 
    232236  src: "title: <h1 do='void' name='title' do='text' text='dummy'>blah <b do='void' name='comment'>nothing</b></h1>" 
     
    249253  res: "from ii: keep name: title: <h1>hello world! <i>rabbit</i></h1>" 
    250254 
    251 include_for_reverse: 
    252   src: "from ifr: <r:include template='/name'><r:with part='super/comment'>IFR</r:with></r:include>" 
    253   res: "from ifr: super: hey ho" 
    254  
    255255include_replace_same: 
    256256  src: "from IRS: <r:include template='/include/super/man'><span do='with' part='super/man'>goldorak</span></r:include>" 
    257257  res: "from IRS: include_super: super: hey <span>goldorak</span>" 
    258  
    259 include_reverse: 
    260   src: "from ir: <r:include template='/include/for/reverse'><h1 do='with' part='super'><i do='void' name='comment'>none</i> <r:hello/></h1></r:include>" 
    261   res: "from ir: from ifr: super: <h1><i>IFR</i> hello world!</h1>" 
    262258 
    263259include_empty: 
  • trunk/lib/parser/test/zafu_insight.yml

    r540 r560  
    1 simple: 
    2   src: "<r:inspect><r:with part='a'>this is a</r:with><r:with part='b'>this is b</r:with></r:inspect>" 
    3   res: "[inspect {< 'a'=>['this is a'], 'b'=>['this is b']}/]" 
    4  
    5 with_sub_tags: 
    6   src: "<r:inspect><r:with part='hello'>I say <h3 do='hello'>hello</h3></r:with></r:inspect>" 
    7   res: "[inspect {< 'hello'=>['I say <h3>hello world!</h3>']}/]" 
    8  
    91menu: 
    102  src: "var a: <r:set_context menu='true'><r:void name='a'><r:hello/></r:void></r:set_context>" 
    113  res: "var a: hello world!" 
    12  
    13 include: 
    14   src: "<r:include template='/menu'><r:with part='a'>do <r:inspect/></r:with></r:include>" 
    15   res: "var a: do [inspect {> :menu=>'true', :name=>'a', :parts=>'ado [inspect {> :preflight=>'true'}/]'}/]"