Only one parser for front end
Separate site map for i18n
This commit is contained in:
1 parent
f1c628f2d6
commit
fcbba6660b
20 files changed
+445
-168
No files matched your search
@@ -25,13 +25,13 @@ module ParserBackEnd
|
||||
|
||||
def parse_page_edit_noko(page)
|
||||
body = Nokogiri::HTML(page.design.layout.body)
|
||||
parse_menu(body, page, true)
|
||||
public_r_tags = parse_contents(body, page, true)
|
||||
parse_images(body, page)
|
||||
parse_footer(body, page)
|
||||
parse_sub_menu(body, page)
|
||||
parse_menu_edit(body, page, true)
|
||||
public_r_tags = parse_contents_edit(body, page, true)
|
||||
parse_images_edit(body, page)
|
||||
parse_footer_edit(body, page)
|
||||
parse_sub_menu_edit(body, page)
|
||||
public_r_tags.each do |tag|
|
||||
send("parse_#{tag}s", body, page, true)
|
||||
send("parse_#{tag}s_edit", body, page, true)
|
||||
end
|
||||
body.to_html
|
||||
end
|
||||
|
||||
@@ -38,7 +38,7 @@ module ParserCommon
|
||||
end
|
||||
|
||||
# ad_banners
|
||||
def parse_ad_banners(body = nil, page = nil, edit=nil)
|
||||
def parse_ad_banners_edit(body = nil, page = nil, edit=nil)
|
||||
body.css('ad_banner').each do |banner|
|
||||
res = ''
|
||||
ad_banner = AdBanner.find(banner["id"]) rescue nil
|
||||
@@ -94,7 +94,7 @@ module ParserCommon
|
||||
end
|
||||
|
||||
# page_images
|
||||
def parse_images(body, page, edit=nil)
|
||||
def parse_images_edit(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
|
||||
@@ -112,20 +112,16 @@ module ParserCommon
|
||||
end
|
||||
|
||||
# page_menu
|
||||
def parse_menu(body, page, edit=nil)
|
||||
def parse_menu_edit(body, page, edit=nil)
|
||||
page_menu = body.css('.page_menu').first
|
||||
if page_menu
|
||||
home = get_homepage
|
||||
menu = page.design.layout.menu
|
||||
fragment = Nokogiri::HTML::DocumentFragment.new(body, menu_level(home, page, 1, menu, edit))
|
||||
page_menu.swap(fragment)
|
||||
else
|
||||
''
|
||||
end
|
||||
home = get_homepage
|
||||
menu = page.design.layout.menu
|
||||
fragment = Nokogiri::HTML::DocumentFragment.new(body, menu_level(home, page, 1, menu, edit))
|
||||
page_menu.swap(fragment)
|
||||
end
|
||||
|
||||
# sub_menus
|
||||
def parse_sub_menus(body = nil, page = nil, edit=nil)
|
||||
def parse_sub_menus_edit(body = nil, page = nil, edit=nil)
|
||||
body.css('sub_menu').each do |sub_menu|
|
||||
menu_page = Page.find(sub_menu['id']) rescue nil
|
||||
res = ''
|
||||
@@ -149,7 +145,7 @@ module ParserCommon
|
||||
end
|
||||
|
||||
# page_footer
|
||||
def parse_footer(body, page)
|
||||
def parse_footer_edit(body, page)
|
||||
page_footer = body.css('.page_footer').first
|
||||
if page_footer
|
||||
res = "<div id='#{page_footer['id']}', class='#{page_footer['class']}'>"
|
||||
@@ -163,7 +159,7 @@ module ParserCommon
|
||||
end
|
||||
|
||||
# page_sub_menu
|
||||
def parse_sub_menu(body, page)
|
||||
def parse_sub_menu_edit(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']}'>"
|
||||
@@ -177,20 +173,16 @@ module ParserCommon
|
||||
end
|
||||
|
||||
# sitemap
|
||||
def parse_sitemaps(body = nil, page = nil, edit=nil)
|
||||
def parse_sitemaps_edit(body = nil, page = nil, edit=nil)
|
||||
sitemap = body.css('sitemap').first
|
||||
if sitemap
|
||||
url = admin_site_sitemap_frontend_path(@site)
|
||||
options = "?inner=true"
|
||||
fragment = Nokogiri::HTML::DocumentFragment.new(body, "<div class='dymanic_load' path='#{url + options}'></div>")
|
||||
sitemap.swap(fragment)
|
||||
else
|
||||
''
|
||||
end
|
||||
url = admin_site_sitemap_frontend_path(@site)
|
||||
options = "?inner=true"
|
||||
fragment = Nokogiri::HTML::DocumentFragment.new(body, "<div class='dymanic_load' path='#{url + options}'></div>")
|
||||
sitemap.swap(fragment)
|
||||
end
|
||||
|
||||
# page_contents
|
||||
def parse_contents(body, page, edit=nil)
|
||||
def parse_contents_edit(body, page, edit=nil)
|
||||
public_r_tags = []
|
||||
body.css('.page_content').each do |content|
|
||||
ret = ''
|
||||
@@ -199,7 +191,6 @@ module ParserCommon
|
||||
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 << "/#{params[:controller_action]}" if params[:controller_action] && params[:id]
|
||||
ret << "?inner=true&page_id=#{page.id}&category_id=#{category}&tag_id=#{tag}&preview=#{params[:preview]}&page_main=#{params[:page_main]}&search_query=#{params[:search_query]}&name=#{params[:name]}"
|
||||
ret << "'></div>"
|
||||
else
|
||||
@@ -216,7 +207,7 @@ module ParserCommon
|
||||
ret << part.content 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? : category}&tag_id=#{!part[:tag].blank? ? part[:tag] : tag}&page=#{params[:page]}&search_query=#{params[:search_query]}&part_title=#{Rack::Utils.escape(part_title).gsub("+", "%20") rescue nil}&part_id=#{part.id}"
|
||||
options = "&category_id=#{!part[:category].blank? ? part[:category].blank? : category}&tag_id=#{!part[:tag].blank? ? part[:tag] : tag}&page=#{params[:page]}&search_query=#{params[:search_query]}&part_title=#{Rack::Utils.escape(part_title).gsub("+", "%20") rescue nil}"
|
||||
ret << "<div class='dymanic_load widget' path='#{url + options}'></div>"
|
||||
when 'public_r_tag'
|
||||
ret << "<r:#{part.public_r_tag} id='#{part.public_r_tag_object_id}'/>"
|
||||
|
||||
+129
-28
@@ -1,38 +1,139 @@
|
||||
module ParserFrontEnd
|
||||
include ParserCommon
|
||||
# c.define_tag 'language_bar' do
|
||||
# @site.in_use_locales.map{ |locale|
|
||||
# lang = I18nVariable.first(:conditions => {:key => locale})[locale]
|
||||
# if I18n.locale.to_s.eql?(locale)
|
||||
# lang
|
||||
# else
|
||||
# "<a href='?locale=#{locale}'>#{lang}</a>"
|
||||
# end
|
||||
# }.join(' | ')
|
||||
# end
|
||||
# c.define_tag 'link' do |tag|
|
||||
# item = Item.first(:conditions => { :path => tag.attr['name'] })
|
||||
# ret = ''
|
||||
# ret << "<a href='#{tag.attr['name']}'>"
|
||||
# ret << item.i18n_variable[I18n.locale]
|
||||
# ret << '</a>'
|
||||
# end
|
||||
# end
|
||||
|
||||
require 'nokogiri'
|
||||
|
||||
def parse_page_noko(page)
|
||||
body = Nokogiri::HTML(page.design.layout.body)
|
||||
parse_menu(body, page)
|
||||
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)
|
||||
def parse_page_noko(page, site)
|
||||
i18n = {}
|
||||
Site.first.valid_locales.each do |locale|
|
||||
body = Nokogiri::HTML(page.design.layout.body)
|
||||
parse_contents(body, page, locale)
|
||||
parse_footer(body, page, site)
|
||||
parse_images(body, page)
|
||||
parse_menu(body, page)
|
||||
parse_sub_menu(body, page, site)
|
||||
i18n.merge!({locale => body.to_html})
|
||||
end
|
||||
i18n
|
||||
end
|
||||
|
||||
def parse_page_content(page)
|
||||
category = params[:category_id].blank? ? page[:category] : params[:category_id]
|
||||
tag = params[:tag_id].blank? ? page[:tag] : params[:tag_id]
|
||||
body = Nokogiri::HTML(page.content)
|
||||
body.css('orbit_front').each do |front|
|
||||
ret = ''
|
||||
part = PagePart.find(front['part_id']) if front['part_id']
|
||||
ret << eval("\"#{front['path']}\"") rescue ''
|
||||
fragment = Nokogiri::HTML::DocumentFragment.new(body, "<div class='dymanic_load widget' path='#{ret}'></div>")
|
||||
front.swap(fragment)
|
||||
end
|
||||
body.to_html
|
||||
end
|
||||
|
||||
|
||||
# page_footer
|
||||
def parse_footer(body, page, site)
|
||||
page_footer = body.css('.page_footer').first
|
||||
if page_footer
|
||||
res = "<div id='#{page_footer['id']}', class='#{page_footer['class']}'>"
|
||||
res << "<div class='dymanic_load' path='#{front_show_footer_path}'></div>"
|
||||
res << "</div>"
|
||||
fragment = Nokogiri::HTML::DocumentFragment.new(body, res)
|
||||
page_footer.swap(fragment) rescue nil
|
||||
else
|
||||
''
|
||||
end
|
||||
end
|
||||
|
||||
# page_images
|
||||
def parse_images(body, page, edit=nil)
|
||||
body.css('.page_image').each do |page_image|
|
||||
image = page.design.images.detect{|image| image.name.eql?(File.basename(page_image['src'])) } unless image
|
||||
if image
|
||||
res = "<img src=#{image.file.url} "
|
||||
page_image.attributes.each do |l|
|
||||
res << "#{l[0]}='#{l[1]}' "
|
||||
end
|
||||
res << '>'
|
||||
end
|
||||
fragment = Nokogiri::HTML::DocumentFragment.new(body, res)
|
||||
page_image.swap(fragment)
|
||||
end
|
||||
end
|
||||
|
||||
# page_menu
|
||||
def parse_menu(body, page)
|
||||
page_menu = body.css('.page_menu').first
|
||||
if page_menu
|
||||
fragment = Nokogiri::HTML::DocumentFragment.new(body, "<div class='dymanic_load' path='#{front_show_menu_path(page.id)}'></div>")
|
||||
page_menu.swap(fragment)
|
||||
else
|
||||
''
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# page_sub_menu
|
||||
def parse_sub_menu(body, page, site)
|
||||
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']}'>"
|
||||
res << "<div class='dymanic_load' path='#{front_show_site_sub_menu_path}'></div>"
|
||||
res << "</div>"
|
||||
fragment = Nokogiri::HTML::DocumentFragment.new(body, res)
|
||||
page_sub_menu.swap(fragment)
|
||||
else
|
||||
''
|
||||
end
|
||||
end
|
||||
|
||||
# page_contents
|
||||
def parse_contents(body, page, edit=nil, locale)
|
||||
public_r_tags = []
|
||||
body.css('.page_content').each do |content|
|
||||
ret = ''
|
||||
if (content["main"] == "true" && !page.module_app.nil?)
|
||||
url = "/panel/#{page.module_app.key}/front_end/#{page.app_frontend_url}\#{(\"/\" + params[:id]) if params[:id] && !params[:id].eql?(page.id.to_s)}?inner=true"
|
||||
options = "&page_id=#{page.id}&category_id=\#{category}&tag_id=\#{tag}&preview=\#{params[:preview]}&page_main=\#{params[:page_main]}&search_query=\#{params[:search_query]}&name=\#{params[:name]}"
|
||||
ret << "<orbit_front path='#{url + options}'></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.content_translations[locale.to_s] 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? : category}&tag_id=\#{!part[:tag].blank? ? part[:tag] : tag}&page=\#{params[:page]}&search_query=\#{params[:search_query]}&part_title=\#{Rack::Utils.escape(part_title).gsub(\"\+\", \"\%20\") rescue nil}"
|
||||
ret << "<orbit_front path='#{url + options}' part_id=#{part.id}></div>"
|
||||
when 'public_r_tag'
|
||||
ret << send("generate_#{part.public_r_tag}s", part.public_r_tag_object_id, page)
|
||||
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
|
||||
end
|
||||
|
||||
# ad_banners
|
||||
def generate_ad_banners(*args)
|
||||
"<div class='dymanic_load' path='#{front_show_banner_path(args[0])}'></div>"
|
||||
end
|
||||
|
||||
# sitemap
|
||||
def generate_sitemaps(*args)
|
||||
"<div class='dymanic_load' path='#{front_show_sitemap_path}'></div>"
|
||||
end
|
||||
|
||||
# sub_menus
|
||||
def generate_sub_menus(*args)
|
||||
"<div class='dymanic_load' path='#{front_show_page_sub_menu_path({:menu_page_id => args[0], :page_id => args[1]})}'></div>"
|
||||
end
|
||||
|
||||
end
|
||||
@@ -304,5 +304,12 @@ namespace :migrate do
|
||||
i += 1
|
||||
end if news_bulletins
|
||||
end
|
||||
|
||||
task :sitemap_i18n => :environment do
|
||||
Item.all.each do |item|
|
||||
item.sitemap_enabled = item.sitemap_enabled ? {'en' => 'true', 'zh_tw' => 'true'} : nil
|
||||
item.save
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Reference in new issue
Block a user