Clean the parsers code

This commit is contained in:
Christophe Vilayphiou 2012-05-13 19:32:05 +08:00
parent 803253ebcf
commit 82fa85def8
6 changed files with 70 additions and 128 deletions

View File

@ -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

View File

@ -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

View File

@ -90,10 +90,6 @@ module ApplicationHelper
((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 = ''

View File

@ -23,67 +23,19 @@ module ParserBackEnd
# ret << "</a>"
# 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 << "<div id='appfrontend' class='dymanic_load' path='/panel/#{page.module_app.key}/front_end/#{page.app_frontend_url}?inner=true&page_id=#{page.id}"
ret << "&category_id=#{page.category}" if page[:category]
ret << "&tag_id=#{page.tag}" if page[:tag]
ret << "'></div>"
else
part = page.page_parts.detect{ |p| p.name.to_s == content['name'].to_s } rescue nil
ret << "<div id='#{content['name']}' part_id='#{part.id}' class='editable' style='border:solid 1px; margin:5px; padding:5px;'>" if part
ret << "<div class='edit_link' style='display:none'>"
ret << " <a href='#{edit_admin_page_part_path(part.id)}' class='nav'>#{t(:edit)}</a>" if part
ret << '</div>'
case part.kind
when 'text'
ret << part.i18n_variable[I18n.locale] rescue ''
when 'module_widget'
if !part[:category].blank?
ret << "<div class='dymanic_load' path='/panel/#{part.module_app.key}/widget/#{part.widget_path}?category_id=#{part[:category]}}'></div>"
elsif !part[:tag].blank?
ret << "<div class='dymanic_load' path='/panel/#{part.module_app.key}/widget/#{part.widget_path}?inner=true&tag_id=#{part[:tag]}'></div>"
else
ret << "<div class='dymanic_load' path='/panel/#{part.module_app.key}/widget/#{part.widget_path}'></div>"
end
when 'public_r_tag'
ret << "<r:#{part.public_r_tag} id='#{part.public_r_tag_object_id}'/>"
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}</#{content.name}>"
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

View File

@ -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 = "<div id='#{page_footer['id']}', class='#{page_footer['class']}'>"
@ -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 = "<div id='#{page_sub_menu['id']}', class='#{page_sub_menu['class']}'>"
@ -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 << "<div id='appfrontend' class='dymanic_load' path='/panel/#{page.module_app.key}/front_end/#{page.app_frontend_url}"
ret << "/#{params[:id]}" if params[:id] && !params[:id].eql?(page.id.to_s)
ret << "?inner=true&page_id=#{page.id}&category_id=#{params[:category_id]}&tag_id=#{params[:tag_id]}&preview=#{params[:preview]}&page=#{params[:page]}"
ret << "'></div>"
else
part = page.page_parts.detect{ |p| p.name.to_s == content['name'].to_s } rescue nil
if edit
ret << "<div id='#{content['name']}' part_id='#{part.id}' class='editable' style='border:solid 1px; margin:5px; padding:5px;'>" if part
ret << "<div class='edit_link' style='display:none'>"
ret << " <a href='#{edit_admin_page_part_path(part.id)}' class='nav'>#{t(:edit)}</a>" if part
end
case part.kind
when 'text'
ret << part.i18n_variable[I18n.locale] rescue ''
when 'module_widget'
url = "/panel/#{part.module_app.key}/widget/#{part.widget_path}?inner=true"
options = "&category_id=#{!part[:category].blank? ? part[:category].blank? : params[:category_id]}&tag_id=#{!part[:tag].blank? ? part[:tag] : params[:tag_id]}&page=#{params[:page]}"
ret << "<div class='dymanic_load' path='#{url + options}'></div>"
when 'public_r_tag'
ret << "<r:#{part.public_r_tag} id='#{part.public_r_tag_object_id}'/>"
public_r_tags << part.public_r_tag
else
''
end if part
ret << '</div>' if edit
end
scope = "<#{content.name}"
content.attributes.each_pair do |key, value|
scope << " #{key}='#{value}'"
end
scope << ">#{ret}</#{content.name}>"
fragment = Nokogiri::HTML::DocumentFragment.new(body, scope)
content.swap(fragment)
end
public_r_tags.uniq
end
end

View File

@ -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 << "<div id='appfrontend' class='dymanic_load' path='/panel/#{page.module_app.key}/front_end/#{page.app_frontend_url}"
ret << "/#{id}" if id
ret << "?inner=true&page_id=#{page.id}"
# ret << "&category_id=#{page.category}" if page[:category]
ret << "&category_id=#{params[:category_id]}" if !params[:category_id].blank?
ret << "&tag_id=#{params[:tag_id]}" if !params[:tag_id].blank?
ret << "&preview=true" if preview.eql?('true')
ret << "'></div>"
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 << "<div class='dymanic_load' path='/panel/#{part.module_app.key}/widget/#{part.widget_path}?inner=true&category_id=#{part[:category]}'></div>"
# else
# ret << "<div class='dymanic_load' path='/panel/#{part.module_app.key}/widget/#{part.widget_path}?inner=true'></div>"
# end
if !part[:category].blank?
ret << "<div class='dymanic_load' path='/panel/#{part.module_app.key}/widget/#{part.widget_path}?inner=true&category_id=#{part[:category]}'></div>"
elsif !part[:tag].blank?
ret << "<div class='dymanic_load' path='/panel/#{part.module_app.key}/widget/#{part.widget_path}?inner=true&tag_id=#{part[:tag]}'></div>"
else
ret << "<div class='dymanic_load' path='/panel/#{part.module_app.key}/widget/#{part.widget_path}?inner=true'></div>"
end
when 'public_r_tag'
ret << "<r:#{part.public_r_tag} id='#{part.public_r_tag_object_id}'/>"
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}</#{content.name}>"
fragment = Nokogiri::HTML::DocumentFragment.new(body, scope)
content.swap(fragment)
end
public_r_tags.uniq
end
end