From 82fa85def8838382c1ba8d89818c06835c6291cd Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Sun, 13 May 2012 19:32:05 +0800 Subject: [PATCH] Clean the parsers code --- app/controllers/application_controller.rb | 4 +- app/controllers/pages_controller.rb | 20 ++++---- app/helpers/application_helper.rb | 6 +-- lib/parsers/parser_back_end.rb | 58 ++--------------------- lib/parsers/parser_common.rb | 53 +++++++++++++++++++-- lib/parsers/parser_front_end.rb | 57 ++-------------------- 6 files changed, 70 insertions(+), 128 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 494b497e..4d8d6d48 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -95,9 +95,9 @@ class ApplicationController < ActionController::Base end # Render the page - def render_page(param={}) + def render_page if @item - render :text => process_page(@item, param[:id], param), :layout => 'page_layout' + render :text => parse_page_noko(@item), :layout => 'page_layout' else render :text => '404 Not Found' end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index b781f202..c4a571dd 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -19,7 +19,7 @@ class PagesController < ApplicationController if @item && @item.is_published && (@item.enabled_for.nil? ? true : @item.enabled_for.include?(I18n.locale.to_s)) case @item._type when 'Page' - render_page(params) + render_page when 'Link' redirect_to "http://#{@item[:url]}" end @@ -32,17 +32,19 @@ class PagesController < ApplicationController end def index_from_link - if params[:page] - redirect_to "/#{@item.path}?page=#{params[:page]}&category_id=#{params[:category_id]}&tag_id=#{params[:tag_id]}" - else - redirect_to "/#{@item.path}?category_id=#{params[:category_id]}&tag_id=#{params[:tag_id]}" - end + url = "/#{@item.path}" + options = '' + options << "?page=#{params[:page]}" unless params[:page].blank? + options << "#{options.blank? ? '?' : '&'}category_id=#{params[:category_id]}" unless params[:category_id].blank? + options << "#{options.blank? ? '?' : '&'}tag_id=#{params[:tag_id]}" unless params[:tag_id].blank? + redirect_to url + options end def show_from_link - # debugger - # a=1 - redirect_to "/#{@item.path}?id=#{params[:id]}&preview=#{params[:preview]}" + url = "/#{@item.path}?id=#{params[:id]}" + options = '' + options << "&preview=#{params[:preview]}" unless params[:preview].blank? + redirect_to url + options end def load_orbit_bar diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 37c2d0d2..32800d80 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -89,11 +89,7 @@ module ApplicationHelper def active_for_action(controller_name, action_name) ((controller.controller_name.eql?(controller_name) || request.fullpath.eql?(controller_name)) && controller.action_name.eql?(action_name)) ? 'active' : nil end - - def process_page(page, id, params) - parse_page_noko(page, id, params) - end - + def page_metas(page) tmp_meta = {} metas = '' diff --git a/lib/parsers/parser_back_end.rb b/lib/parsers/parser_back_end.rb index e9c08a01..aed8f36b 100644 --- a/lib/parsers/parser_back_end.rb +++ b/lib/parsers/parser_back_end.rb @@ -23,67 +23,19 @@ module ParserBackEnd # ret << "" # end - def parse_page_edit_noko(page, id = nil) + def parse_page_edit_noko(page) body = Nokogiri::HTML(page.design.layout.body) parse_menu(body, page, true) - public_r_tags = parse_content_edits(body, page, id) + public_r_tags = parse_contents(body, page, true) parse_images(body, page) - parse_footer(body, page, true) - parse_sub_menu(body, page, true) - + parse_footer(body, page) + parse_sub_menu(body, page) public_r_tags.each do |tag| - send("parse_#{tag}s", body, page, id, true) + send("parse_#{tag}s", body, page, true) end - body.to_html end - # page_contents - def parse_content_edits(body, page, id) - public_r_tags = [] - body.css('.page_content').each do |content| - ret = '' - if (content["main"] == "true" && !page.module_app.nil?) - ret << "
" - else - part = page.page_parts.detect{ |p| p.name.to_s == content['name'].to_s } rescue nil - ret << "
" if part - ret << "' - case part.kind - when 'text' - ret << part.i18n_variable[I18n.locale] rescue '' - when 'module_widget' - if !part[:category].blank? - ret << "
" - elsif !part[:tag].blank? - ret << "
" - else - ret << "
" - end - when 'public_r_tag' - ret << "" - public_r_tags << part.public_r_tag - else - '' - end if part - end - scope = "<#{content.name}" - content.attributes.each_pair do |key, value| - scope << " #{key}='#{value}'" - end - scope << ">#{ret}" - fragment = Nokogiri::HTML::DocumentFragment.new(body, scope) - content.swap(fragment) - end - public_r_tags.uniq - end - - def self.included(base) base.send :helper_method, :parse_page_edit_noko if base.respond_to? :helper_method end diff --git a/lib/parsers/parser_common.rb b/lib/parsers/parser_common.rb index 59bd85b3..180914b3 100644 --- a/lib/parsers/parser_common.rb +++ b/lib/parsers/parser_common.rb @@ -36,7 +36,7 @@ module ParserCommon end # ad_banners - def parse_ad_banners(body = nil, page = nil, id = nil, edit=nil) + def parse_ad_banners(body = nil, page = nil, edit=nil) body.css('ad_banner').each do |banner| res = '' ad_banner = AdBanner.find(banner["id"]) rescue nil @@ -82,7 +82,7 @@ module ParserCommon end # page_images - def parse_images(body, page, id = nil, edit=nil) + def parse_images(body, page, edit=nil) body.css('.page_image').each do |page_image| # image = page.custom_images.detect{|image| image.name.eql?(tag.attr['name']) } # image = page.design.custom_images.detect{|image| image.name.eql?(tag.attr['name']) } unless image @@ -109,7 +109,7 @@ module ParserCommon end # sub_menus - def parse_sub_menus(body = nil, page = nil, id = nil, edit=nil) + def parse_sub_menus(body = nil, page = nil, edit=nil) body.css('sub_menu').each do |sub_menu| menu_page = Page.find(sub_menu['id']) rescue nil res = '' @@ -132,7 +132,7 @@ module ParserCommon end # page_footer - def parse_footer(body, page, edit=nil) + def parse_footer(body, page) page_footer = body.css('.page_footer').first if page_footer res = "
" @@ -146,7 +146,7 @@ module ParserCommon end # page_sub_menu - def parse_sub_menu(body, page, edit=nil) + def parse_sub_menu(body, page) page_sub_menu = body.css('.page_sub_menu').first if page_sub_menu res = "
" @@ -159,4 +159,47 @@ module ParserCommon end end + # page_contents + def parse_contents(body, page, edit=nil) + public_r_tags = [] + body.css('.page_content').each do |content| + ret = '' + if (content["main"] == "true" && !page.module_app.nil?) + ret << "
" + else + part = page.page_parts.detect{ |p| p.name.to_s == content['name'].to_s } rescue nil + if edit + ret << "
" if part + ret << "' if edit + end + scope = "<#{content.name}" + content.attributes.each_pair do |key, value| + scope << " #{key}='#{value}'" + end + scope << ">#{ret}" + fragment = Nokogiri::HTML::DocumentFragment.new(body, scope) + content.swap(fragment) + end + public_r_tags.uniq + end + end diff --git a/lib/parsers/parser_front_end.rb b/lib/parsers/parser_front_end.rb index 6320f808..3d39bd5f 100644 --- a/lib/parsers/parser_front_end.rb +++ b/lib/parsers/parser_front_end.rb @@ -21,69 +21,18 @@ module ParserFrontEnd require 'nokogiri' - def parse_page_noko(page, id = nil,params) + def parse_page_noko(page) body = Nokogiri::HTML(page.design.layout.body) parse_menu(body, page) - public_r_tags = parse_contents(body, page, id,params[:preview]) + public_r_tags = parse_contents(body, page) parse_images(body, page) parse_footer(body, page) parse_sub_menu(body, page) - public_r_tags.each do |tag| - send("parse_#{tag}s", body, page,id) + send("parse_#{tag}s", body, page) end body.to_html end - - # page_contents - def parse_contents(body, page, id,preview = false) - public_r_tags = [] - body.css('.page_content').each do |content| - ret = '' - if (content["main"] == "true" && !page.module_app.nil?) - ret << "
" - else - part = page.page_parts.detect{ |p| p.name.to_s == content['name'].to_s } rescue nil - case part.kind - when 'text' - ret << part.i18n_variable[I18n.locale] rescue '' - when 'module_widget' - # if part[:category] - # ret << "
" - # else - # ret << "
" - # end - if !part[:category].blank? - ret << "
" - elsif !part[:tag].blank? - ret << "
" - else - ret << "
" - end - when 'public_r_tag' - ret << "" - public_r_tags << part.public_r_tag - else - '' - end if part - end - scope = "<#{content.name}" - content.attributes.each_pair do |key, value| - scope << " #{key}='#{value}'" - end - scope << ">#{ret}" - fragment = Nokogiri::HTML::DocumentFragment.new(body, scope) - content.swap(fragment) - end - public_r_tags.uniq - end end