Changeset 700

Show
Ignore:
Timestamp:
2007-08-14 17:21:29 (1 year ago)
Author:
gaspard
Message:

Allow multiple values per key by using auto generated keys (:key, :key1, :key2) in hash (zafu parser). Fixes #45.

Files:

Legend:

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

    r648 r700  
    363363  end 
    364364   
     365  # Parse parameters into a hash. This parsing supports multiple values for one key by creating additional keys: 
     366  # <tag do='hello' or='goodbye' or='gotohell'> creates the hash {:do=>'hello', :or=>'goodbye', :or1=>'gotohell'} 
    365367  def parse_params(text) 
    366368    return {} unless text 
     
    374376        if rest =~ /('|")([^\1]*?[^\\]|)\1/ 
    375377          rest = rest[$&.length..-1].strip 
     378          key_counter = 1 
     379          while params[key] 
     380            key = "#{key}#{key_counter}".to_sym 
     381            key_counter += 1 
     382          end 
     383             
    376384          if $1 == "'" 
    377385            params[key] = $2.gsub("\\'", "'") 
  • trunk/lib/parser/lib/rules/zena.rb

    r699 r700  
    10201020    end 
    10211021     
     1022    # <r:pages from='site' project='stored' limit='5'> 
     1023    # is the same as 
     1024    # <div do='pages from site' project='stored' limit='5'> 
     1025    # <div do='references' or='references_for'> 
    10221026    def get_list_finder(rel,params=@params) 
    10231027      # FIXME: could SQL injection be possible here ? (all params are passed to the 'find') 
    1024       erb_params = {} 
     1028      relations    = [rel] 
     1029      conditions   = [] 
     1030      query_params = {} 
     1031       
     1032      if params[:from] 
     1033        relations[0] << " from #{params[:from]}" 
     1034      end 
     1035       
     1036      if params[:or] 
     1037        relations << params[:or] 
     1038        key_counter = 1 
     1039        while or_value = params["or#{key_counter}".to_sym] 
     1040          key_counter += 1 
     1041          finders << or_value 
     1042        end 
     1043      end 
     1044       
    10251045      if order = params[:order] 
    10261046        if order == 'random' 
    1027           erb_params[:order] = 'RAND()' 
     1047          query_params[:order] = 'RAND()' 
    10281048        elsif order =~ /\A(\w+)( ASC| DESC|)\Z/ 
    1029           erb_params[:order] = order 
     1049          query_params[:order] = order 
    10301050        else 
    10311051          # ignore 
    10321052        end 
    10331053      end 
    1034       erb_params[:from] = params[:from] if params[:from] 
     1054       
    10351055      [:limit, :offset].each do |k| 
    10361056        next unless params[k] 
    1037         erb_params[k] = params[k].to_i.to_s 
    1038       end 
    1039       [:from, :direction, :or].each do |k| 
    1040         next unless params[k] 
    1041         erb_params[k] = params[k] 
    1042       end 
    1043       conditions = [] 
     1057        query_params[k] = params[k].to_i.to_s 
     1058      end 
     1059       
    10441060       
    10451061      # FIXME: stored should be clarified and managed in a single way through links and contexts. 
     
    10941110      end 
    10951111 
    1096       res_params = params_to_erb(erb_params) 
    1097       if conditions != [] 
    1098         conditions = conditions.join(' AND ') 
    1099         if res_params != '' 
    1100           res_params << ", :conditions=>\"#{conditions}\"" 
    1101         else 
    1102           res_params = ":conditions=>\"#{conditions}\"" 
    1103         end 
    1104       end 
    1105       "#{node}.relation(#{rel.inspect}#{res_params})" 
    1106     end 
     1112      query_params[:conditions] = conditions.join(' AND ') if conditions != [] 
     1113      query_params[:relations ] = relations 
     1114      query_params[:node_name ] = node 
     1115       
     1116      sql_query = Node.build_find(node, query_params) 
     1117       
     1118      "#{node}.do_find(\"#{sql_query}\")" 
     1119    end 
     1120     
    11071121    # <r:hot else='project'/> 
    11081122    # <r:relation role='hot,project'> = get relation if empty get project 
  • trunk/lib/parser/test/parser_test.rb

    r679 r700  
    5656  testfile :zafu, :zafu_asset, :zafu_insight, :zazen 
    5757  def test_single 
    58     do_test('zafu', 'include_replace_same_with') 
     58    do_test('zafu', 'multiple_param') 
    5959  end 
    6060   
  • trunk/lib/parser/test/zafu.yml

    r679 r700  
    249249  res: "[test][hello]a thing [hello/] here[/hello][/test]" 
    250250 
     251multiple_param: 
     252  src: "<r:test do='hello' param='1' param='2'>a thing <r:hello/> here</r:test>" 
     253  res: "[test][hello {= :param1=>'2', :param=>'1'}]a thing [hello/] here[/hello][/test]" 
     254 
    251255ztag_tag: 
    252256  src: "<r:hello tag='p'/>"