Clean the parsers code

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

No files matched your search

+5 -53
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
+48 -5
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
+3 -54
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