Changeset 1096

Show
Ignore:
Timestamp:
2008-07-07 09:30:23 (6 months ago)
Author:
gaspard
Message:

Better parse/unparse assets. Fixed [summary] to properly use html_tag. Added 'title' and 'text' alias for 'v_title' and 'v_text'. Fixed 'order' clause
so that 'name' is the same as 'name asc'.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/app/controllers/application.rb

    r1094 r1096  
    346346           
    347347          dev_box << "  <li><a class='group' onclick='$(\"_dev_tools\").toggle();' href='#'>tools</a>\n" 
    348           dev_box << "    <ul id='_dev_tools'>\n" 
     348          dev_box << "    <ul id='_dev_tools' style='display:none;'>\n" 
    349349          dev_box << "      <li><a href='?rebuild=true'>#{_('rebuild')}</a></li>\n" 
    350350          dev_box << "<% if @node.kind_of?(Skin) -%>      <li><a href='<%= export_node_path(@node[:zip]) %>'>#{_('export')}</a></li>\n<% end -%>" 
     
    380380     
    381381    # Return the zen_path ('/en/image34.png') for an asset given its name ('img/footer.png'). 
     382    # The rule is not the same whether we are rendering a template and find <img/> <link rel='stylesheet'/> tags 
     383    # or if we are parsing assets in a CSS file. 
    382384    def template_url_for_asset(opts) 
    383       return nil unless res = find_document_for_template(opts) 
    384       asset, url = *res 
    385       @renamed_assets[url] = asset unless opts[:parse_assets] 
     385      if opts[:parse_assets] 
     386        src = opts[:src] 
     387        current_folder = opts[:current_folder] || '' 
     388        current_folder = current_folder[1..-1] if current_folder[0..0] == '/' 
     389       
     390        if src =~ /\A(.*)\.(\w+)\Z/ 
     391          src, format = $1, $2 
     392        end 
     393       
     394        if src[0..0] == '/' 
     395          path = src[1..-1] 
     396        else 
     397          path = current_folder + '/' + src 
     398        end 
     399       
     400        return nil unless asset = secure(Document) { Document.find_by_path(path) } 
     401      else 
     402        return nil unless res = find_document_for_template(opts) 
     403        asset, url = *res 
     404        @renamed_assets[url] = asset 
     405      end 
     406       
    386407      data_path(asset) 
    387408    end 
     
    390411    # ('skin/template/path'). If the path starts with a slash, the skin_name in the path is searched first. Otherwise, 
    391412    # the current skin is searched first. 
     413    # <r:include template='Node'/> 
     414    #   find: #{skin_path(main_skin)}/Node 
     415    #  
     416    # <r:include template='/default/Node'/> 
     417    #   find: #{skin_path('default')}/Node 
     418    #  
    392419    def find_document_for_template(opts) 
    393420      src    = opts[:src] 
  • trunk/app/controllers/versions_controller.rb

    r1094 r1096  
    6363        if @node.kind_of?(TextDocument) 
    6464          if params['parse_assets'] 
    65             @node.parse_assets!(self) 
     65            @node.version.text = @node.parse_assets(@node.version.text, self) 
    6666          elsif @node.kind_of?(TextDocument) && params['unparse_assets'] 
    67             @node.unparse_assets 
     67            @node.version.text = @node.unparse_assets(@node.version.text) 
    6868          end 
    6969        end 
  • trunk/app/helpers/application_helper.rb

    r1090 r1096  
    7171        ref = params[:reference] || "#{params[:dom_id]}_add" 
    7272        page.insert_html pos.to_sym, ref, :file => fullpath_from_template_url + ".erb" 
    73         instance_variable_set("@#{base_class.to_s.underscore}", obj.clone) 
     73        if obj.kind_of?(Node) 
     74          @node = @node.parent.new_child(:class => @node.class) 
     75        else 
     76          instance_variable_set("@#{base_class.to_s.underscore}", obj.clone) 
     77        end 
    7478        page.replace "#{params[:dom_id]}_form", :file => fullpath_from_template_url + "_form.erb" 
    7579        if params[:done] 
  • trunk/app/models/node.rb

    r1094 r1096  
    132132                     :section_zip, :skin, :ref_lang, :fullpath, :rootpath, :position, :publish_from, :max_status, :rgroup_id,  
    133133                     :wgroup_id, :pgroup_id, :basepath, :custom_base, :klass, :zip, :score, :comments_count, :l_status, :l_comment, 
    134                      :custom_a, :custom_b                      
     134                     :custom_a, :custom_b, :title, :text                  
    135135  zafu_context       :author => "Contact", :parent => "Node",  
    136136                     :project => "Project", :section => "Section",  
     
    268268        return node 
    269269      end 
    270       node = Node.with_exclusive_scope do 
    271         Node.find(:first, :conditions => ['site_id = ? AND name = ? AND parent_id = ?',  
     270      begin 
     271        klass = Node.get_class(attributes['klass'] || 'Node') 
     272        klass = klass.real_class if klass.kind_of?(VirtualClass) 
     273      rescue NameError 
     274        klass = Node 
     275      end 
     276      node = klass.with_exclusive_scope do 
     277        klass.find(:first, :conditions => ['site_id = ? AND name = ? AND parent_id = ?',  
    272278                                          current_site[:id], attributes['name'].url_name, attributes['parent_id']]) 
    273279      end 
     
    935941  def author 
    936942    user.contact 
     943  end 
     944   
     945  def title 
     946    version.title 
     947  end 
     948   
     949  def text 
     950    version.text 
     951  end 
     952 
     953  def title=(t) 
     954    redaction.title = t 
     955  end 
     956   
     957  def text=(t) 
     958    redaction.text = t 
    937959  end 
    938960   
  • trunk/app/models/text_document.rb

    r1094 r1096  
    2828   
    2929  # Parse text content and replace all reference to relative urls ('img/footer.png') by their zen_path ('/en/image34.png') 
    30   def parse_assets!(helper) 
     30  def parse_assets(text, helper) 
     31    res = text.dup 
    3132    ctype = version.content.content_type 
    3233    case ctype 
     
    4243      end 
    4344       
    44       current_folder = skin.name + parent.fullpath[(skin.fullpath.size)..-1] 
     45      current_folder = parent.fullpath 
    4546       
    46       # create/use redaction 
    47       edit! 
    48        
    49       version.text.gsub!(/url\(\s*(.*?)\s*\)/) do 
     47      res.gsub!(/url\(\s*(.*?)\s*\)/) do 
    5048        match, src = $&, $1 
    5149        if src =~ /('|")(.*?)\1/ 
     
    5654        if src[0..6] == 'http://' 
    5755          match 
     56        elsif src =~ %r{/\w\w/.*\d+\.} 
     57          # already parsed, ignore 
     58          "url(#{quote}#{src}#{quote})" 
    5859        else 
    59           if src =~ /\A\// 
    60             # absolute path: do not touch 
     60          if new_src = helper.send(:template_url_for_asset, :src => src, :current_folder=>current_folder, :parse_assets => true) 
     61            "url(#{quote}#{new_src}#{quote})" 
     62          else 
     63            errors.add('base', "could not find asset #{src.inspect}") 
    6164            "url(#{quote}#{src}#{quote})" 
    62           else 
    63             new_src = helper.send(:template_url_for_asset, :current_folder=>current_folder, :src => src, :parse_assets => true) || src 
    64             "url(#{quote}#{new_src}#{quote})" 
    6565          end 
    6666        end 
     
    7070      errors.add('base', "Invalid content-type #{ctype.inspect} to parse assets.") 
    7171    end 
     72    res 
    7273  end 
    7374   
    7475  # Parse text and replace absolute urls ('/en/image30.jpg') by their relative value in the current skin ('img/bird.jpg') 
    75   def unparse_assets 
     76  def unparse_assets(text) 
     77    res = text.dup 
    7678    ctype = version.content.content_type 
    7779    case ctype 
    7880    when 'text/css' 
    79       # use skin as root 
    80       skin = section 
     81      # use parent as relative root 
     82      current_folder = parent.fullpath 
    8183       
    82       # not in a Skin. Cannot replace assets in CSS. 
    83       # error 
    84       unless skin.kind_of?(Skin) 
    85         errors.add('base', 'Cannot parse assets if not in a Skin.') 
    86         return 
    87       end 
    88        
    89       version.text.gsub!(/url\(('|")(.*?)\1\)/) do 
     84      res.gsub!(/url\(('|")(.*?)\1\)/) do 
    9085        if $2[0..6] == 'http://' 
    9186          $& 
     
    9388          quote, url   = $1, $2 
    9489          if url =~ /\A\/\w\w.*?(\d+)\./ 
    95             unless asset = secure(Node) { Node.find_by_zip($1) } 
    96               errors.add('base', "could not find asset node #{url.inspect}") 
     90            zip = $1 
     91            unless asset = secure(Node) { Node.find_by_zip(zip) } 
     92              errors.add('base', "could not find asset node #{zip}") 
    9793              "url(#{quote}#{url}#{quote})" 
    9894            end 
    99             if asset.fullpath =~ /\A#{skin.fullpath}\/(.+)/ 
     95            if asset.fullpath =~ /\A#{current_folder}\/(.+)/ 
    10096              "url(#{quote}#{$1}.#{asset.c_ext}#{quote})" 
    10197            else 
    102               errors.add('base', "could not find asset node #{url.inspect}") 
    103               "url(#{quote}#{url}#{quote})" 
     98              "url(#{quote}/#{asset.fullpath}.#{asset.c_ext}#{quote})" 
    10499            end 
    105100          else 
     
    114109      errors.add('base', "invalid content-type #{ctype.inspect} to unparse assets.") 
    115110    end 
     111    res 
    116112  end 
    117113   
  • trunk/app/models/text_document_content.rb

    r1094 r1096  
    1111    # unparse needed in 'export' 
    1212    if node.can_parse_assets? 
    13       node.unparse_assets 
     13      StringIO.new(node.unparse_assets(version.text)) 
     14    else 
     15      StringIO.new(version.text) 
    1416    end 
    15      
    16     t = StringIO.new(version.text) 
    17     t 
    1817  end 
    1918   
  • trunk/app/models/version.rb

    r1094 r1096  
    114114  def redaction_content 
    115115    return @redaction_content if @redaction_content 
    116     return unless content_class 
     116    return nil unless content_class 
    117117    @content = content 
    118118    if @content && @content[:version_id] == self[:id] 
  • trunk/app/views/comments/create.rjs

    r1062 r1096  
    1414#  page.visual_effect :highlight, "comment#{@comment[:id]}", :duration => 0.3 
    1515#end 
    16  
     16update_page_content(page, @comment) 
     17=begin 
    1718if @comment.new_record? 
    1819  page.replace "#{params[:dom_id]}_form", :file => fullpath_from_template_url + "_form.erb" 
     
    2930  end 
    3031end 
     32=end 
  • trunk/app/views/nodes/create.rjs

    r1067 r1096  
     1update_page_content(page, @node) 
     2=begin 
    13if @node.new_record? 
    24  page.replace "#{params[:dom_id]}_form", :file => fullpath_from_template_url + "_form.erb" 
     
    1315  end 
    1416end 
     17=end 
  • trunk/lib/parser/lib/rules/zena.rb

    r1091 r1096  
    691691    def r_summary 
    692692      limit  = @params[:limit] ? ", :limit=>#{@params[:limit].to_i}" : "" 
     693      @html_tag ||= 'div' 
     694      @html_tag_params[:id] = "v_summary#{erb_node_id}" 
     695      if c = @html_tag_params[:class] || @params[:class] 
     696        @html_tag_params[:class] = "#{c} zazen" 
     697      else 
     698        @html_tag_params[:class] = 'zazen' 
     699      end 
    693700      unless @params[:or] 
    694701        text = @params[:text] ? @params[:text].inspect : node_attribute('v_summary') 
    695         "<div id='v_summary#{erb_node_id}' class='zazen'><%= zazen(#{text}#{limit}, :node=>#{node}) %></div>" 
     702        out "<%= zazen(#{text}#{limit}, :node=>#{node}) %>" 
    696703      else 
    697704        limit ||= ', :limit => 2' 
     
    701708        second_name = @params[:or].gsub(/[^a-z_]/,'') # FIXME: ist this still needed ? (ERB injection) 
    702709        second = node_attribute(second_name) 
    703         "<div id='#{first_name}#{erb_node_id}' class='zazen'><% if #{first} != '' %>" + 
    704         "<%= zazen(#{first}, :node=>#{node}) %>" + 
    705         "<% else %>" + 
    706         "<%= zazen(#{second}#{limit}, :node=>#{node}) %>" + 
    707         "<% end %></div>" 
     710        out "<% if #{first} != '' %>" 
     711        out "<%= zazen(#{first}, :node=>#{node}) %>" 
     712        out "<% else %>" 
     713        out "<%= zazen(#{second}#{limit}, :node=>#{node}) %>" 
     714        out "<% end %>" 
    708715      end 
    709716    end 
     
    849856      hidden_fields = {} 
    850857      set_fields = [] 
     858      id_hash    = {:class => @html_tag_params[:class] || @params[:class] || 'form'} 
    851859      var_name   = base_class.to_s.underscore 
    852860      (descendants('input') + descendants('select')).each do |tag| 
     
    857865        if @context[:in_add] 
    858866          # inline form used to create new elements: set values to '' and 'parent_id' from context 
    859           id_hash = {:id => "#{erb_dom_id}_form", :style => "display:none;"} 
     867          id_hash[:id] = "#{erb_dom_id}_form" 
     868          id_hash[:style] = "display:none;" 
    860869           
    861           cancel =  "<p class='btn_x'><a href='#' onclick='[\"#{erb_dom_id}_add\", \"#{erb_dom_id}_form\"].each(Element.toggle);return false;'>#{_('btn_x')}</a></p>\n" 
     870          cancel =  "<span class='btn_x'><a href='#' onclick='[\"#{erb_dom_id}_add\", \"#{erb_dom_id}_form\"].each(Element.toggle);return false;'>#{_('btn_x')}</a></span>\n" 
    862871          form  =  "<%= form_remote_tag(:url => #{base_class.to_s.underscore.pluralize}_path, :html => {:id => \"#{dom_id}_form_t\"}) %>\n" 
    863872        else 
    864873          # saved form 
    865874           
    866           id_hash = {:id => "#{erb_dom_id}"} 
     875          id_hash[:id] = erb_dom_id 
    867876           
    868877          cancel = <<-END_TXT 
    869878<% if #{node}.new_record? -%> 
    870   <p class='btn_x'><a href='#' onclick='[\"<%= params[:dom_id] %>_add\", \"<%= params[:dom_id] %>_form\"].each(Element.toggle);return false;'>#{_('btn_x')}</a></p
     879  <span class='btn_x'><a href='#' onclick='[\"<%= params[:dom_id] %>_add\", \"<%= params[:dom_id] %>_form\"].each(Element.toggle);return false;'>#{_('btn_x')}</a></span
    871880<% else -%> 
    872   <p class='btn_x'><%= link_to_remote(#{_('btn_x').inspect}, :url => #{base_class.to_s.underscore}_path(#{node_id}) + \"/zafu?t_url=#{CGI.escape(template_url)}&dom_id=\#{params[:dom_id]}#{@context[:need_link_id] ? "&link_id=\#{#{node}.link_id}" : ''}\", :method => :get) %></p
     881  <span class='btn_x'><%= link_to_remote(#{_('btn_x').inspect}, :url => #{base_class.to_s.underscore}_path(#{node_id}) + \"/zafu?t_url=#{CGI.escape(template_url)}&dom_id=\#{params[:dom_id]}#{@context[:need_link_id] ? "&link_id=\#{#{node}.link_id}" : ''}\", :method => :get) %></span
    873882<% end -%> 
    874883END_TXT 
     
    887896        hidden_fields['link_id'] = "<%= #{node}.link_id %>" if @context[:need_link_id] 
    888897         
    889         if block = ancestor('block') 
    890           # set update template url 
     898        if @params[:update] || (@context[:add] && @context[:add].params[:update]) 
     899          upd = @params[:update] || @context[:add].params[:update] 
     900          if target = find_target(upd) 
     901            hidden_fields['u_url']   = target.template_url 
     902            hidden_fields['udom_id'] = target.erb_dom_id 
     903          end 
     904        elsif block = ancestor('block') && node_kind_of?(DataEntry) 
     905          # updates template url 
    891906          hidden_fields['u_url']  = block.template_url 
    892907          hidden_fields['udom_id'] = block.erb_dom_id 
     
    941956      else 
    942957        # no ajax 
    943         id_hash = {} 
    944958        # FIXME 
    945959        cancel = "" # link to normal node ? 
     
    11641178        revert_effect = 'Element.move' 
    11651179      end 
    1166       out expand_with 
    1167       out "</div>" 
     1180      out render_html_tag(expand_with) 
    11681181      out "<script type='text/javascript'> 
    11691182      //<![CDATA[ 
     
    22832296      if method == 'each' && !@context[:make_form] 
    22842297        "#{res}_<%= #{var}.zip %>" 
     2298      elsif method == 'draggable' 
     2299        "#{res}_<%= #{node}.zip %>" 
    22852300      elsif method == 'unlink' 
    22862301        target = nil 
     
    24992514        elsif node_name == 'main' 
    25002515          return [node_attr, '@node', Node] 
     2516        elsif node_name == 'visitor' 
     2517          return [node_attr, 'visitor.contact', Contact] 
    25012518        else 
    25022519          out parser_error("invalid node name #{node_name.inspect} in attribute #{str.inspect}") 
  • trunk/lib/query_builder/lib/query_builder.rb

    r1084 r1096  
    310310       
    311311      order.split(',').each do |clause| 
    312         if clause =~ /^\s*([^\s]+) (ASC|asc|DESC|desc)/ 
    313           fld_name, direction = $1, $2 
     312        if clause == 'random' 
     313          res << "RAND()" 
     314        else 
     315          if clause =~ /^\s*([^\s]+) (ASC|asc|DESC|desc)/ 
     316            fld_name, direction = $1, $2 
     317          else 
     318            fld_name = clause 
     319            direction = 'ASC' 
     320          end 
    314321          if fld = field_or_param(fld_name, table, :order) 
    315322            res << "#{fld} #{direction.upcase}" 
     
    317324            @errors << "invalid field '#{fld_name}'" 
    318325          end 
    319         elsif clause == 'random' 
    320           res << "RAND()" 
    321         else 
    322           @errors << "invalid order clause '#{clause}'" 
    323326        end 
    324327      end 
  • trunk/public/stylesheets/zena.css

    r1082 r1096  
    3636 
    3737/* ========================= Ajax css ========================= */ 
    38 .drop_hover { background:#ace; } 
     38.drop_hover { background:#ace;  border:2px dashed #99f;} 
     39.drop { min-height:1em;} 
    3940 
    4041/* ========================= Zazen css ======================== */ 
     
    6061#dev li img { border:0; } 
    6162#dev li li { clear:both;} 
     63#dev li table { display:none;} 
     64#dev:hover table { display:block;} 
  • trunk/test/unit/text_document_test.rb

    r1094 r1096  
    5050    node = secure!(Node) { nodes(:style_css) } 
    5151    bird = secure!(Node) { nodes(:bird_jpg)} 
    52     assert bird.update_attributes(:parent_id => nodes_id(:default)
     52    assert bird.update_attributes(:parent_id => node[:parent_id]
    5353    start =<<-END_CSS 
    5454    body { font-size:10px; } 
    55     #footer { background:url('bird.jpg') } 
     55    #header { background:url('bird.jpg') } 
     56    #footer { background:url('/projects/wiki/flower.jpg') } 
    5657    END_CSS 
    5758    node.v_text = start.dup 
     
    5960    helper = ApplicationController.new 
    6061    helper.instance_variable_set(:@visitor, visitor) 
    61     node.parse_assets!(helper) 
     62    text = node.parse_assets(start, helper) 
    6263    assert node.errors.empty? 
    6364    res =<<-END_CSS 
    6465    body { font-size:10px; } 
    65     #footer { background:url('/en/image30.jpg') } 
     66    #header { background:url('/en/image30.jpg') } 
     67    #footer { background:url('/en/image31.jpg') } 
    6668    END_CSS 
    67     assert_equal res, node.v_text 
    68     node.parse_assets!(helper) 
    69     assert_equal res, node.v_text 
    70     node.unparse_assets 
    71     assert_equal start, node.v_text 
    72     node.unparse_assets 
    73     assert_equal start, node.v_text 
     69    assert_equal res, text 
     70    text = node.parse_assets(text, helper) 
     71    assert_equal res, text 
     72    text = node.unparse_assets(text) 
     73    assert_equal start, text 
     74    text = node.unparse_assets(text) 
     75    assert_equal start, text 
    7476  end 
    7577   
     
    7880    node = secure!(Node) { nodes(:style_css) } 
    7981    bird = secure!(Node) { nodes(:bird_jpg)} 
    80     assert bird.update_attributes(:parent_id => nodes_id(:default)
     82    assert bird.update_attributes(:parent_id => node[:parent_id]
    8183    text =<<-END_CSS 
    8284    body { font-size:10px; } 
    83     #footer { background:url('/en/image30.jpg') } 
     85    #header { background:url('/en/image30.jpg') } 
     86    #footer { background:url('/en/image31.jpg') } 
    8487    END_CSS 
    8588    assert_equal 'text/css', node.c_content_type