orbit-basic/lib/parsers/parser_front_end.rb

201 lines
7.4 KiB
Ruby
Raw Normal View History

2011-12-23 10:34:21 +00:00
module ParserFrontEnd
2012-03-21 17:51:16 +00:00
require 'nokogiri'
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)
2013-02-01 09:07:16 +00:00
parse_breadcrumb(body, page, site)
2012-12-28 02:36:18 +00:00
parse_counter(body)
i18n.merge!({locale => body.to_html})
2012-03-27 09:57:50 +00:00
end
i18n
end
2012-03-21 17:51:16 +00:00
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|
2012-12-28 03:51:56 +00:00
if front['value']
fragment = Nokogiri::HTML::DocumentFragment.new(body, "<div id='#{front['id']}' class='#{front['class']}'>#{eval(front['value']).to_s}</div>")
else
2012-12-28 02:36:18 +00:00
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>")
end
front.swap(fragment)
end
2012-03-27 09:57:50 +00:00
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
2013-02-01 09:07:16 +00:00
# page_breadcrumb
def parse_breadcrumb(body, page, site)
page_breadcrumb = body.css('.page_breadcrumb').first
if page_breadcrumb
res = "<orbit_front path='\#{front_show_breadcrumb_path(@item)}' class='dymanic_load widget'></orbit_front>"
2013-02-01 09:07:16 +00:00
fragment = Nokogiri::HTML::DocumentFragment.new(body, res)
page_breadcrumb.swap(fragment)
else
''
end
end
# page_contents
def parse_contents(body, page, edit=nil, locale)
public_r_tags = []
2013-01-21 08:56:37 +00:00
url = ''
body.css('.page_content').each do |content|
ret = ''
if (content["main"] == "true" && !page.module_app.nil?)
2013-01-21 08:56:37 +00:00
if page.app_frontend_url == 'default_widget'
url = "/panel/orbit_app/widget/#{page.frontend_style}?inner=true"
url << "&id=\#{params[:id] if params[:id] && !params[:id].eql?(page.id.to_s)}"
2013-01-21 08:56:37 +00:00
else
2013-01-29 02:05:50 +00:00
url = "/panel/#{page.module_app.key}/front_end/#{page.app_frontend_url}\#{(\"/\" + params[:id]) if params[:id] && !params[:id].eql?(page.id.to_s)}\#{(\"/\" + params[:controller_action]) if params[:controller_action] && params[:id]}?inner=true"
2013-01-21 08:56:37 +00:00
end
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]}&item_type=page"
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'
2012-12-25 02:45:08 +00:00
# url = "/panel/\#{part.module_app.key}/widget/\#{part.widget_path}?inner=true"
# options = "&part_id=\#{part.id}&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>"
url = case part.widget_path
when 'default_widget'
"/panel/orbit_app/widget/\#{part.widget_style}?inner=true"
else
"/panel/\#{part.module_app.key}/widget/\#{part.widget_path}?inner=true"
end
2013-01-21 08:56:37 +00:00
options = "&part_id=\#{part.id}&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}&item_type=page_part"
Merge commit 'fc702a6210d901cd3beea220e72f71a3b7f7d41e' into sidebar_config Conflicts: app/controllers/admin/page_parts_controller.rb app/controllers/default_widget_controller.rb app/uploaders/gprs_uploader.rb app/uploaders/location_uploader.rb app/views/admin/page_parts/_module_widget.html.erb app/views/admin/page_parts/reload_widget_styles.js.erb app/views/admin/page_parts/reload_widgets.js.erb app/views/layouts/_side_bar_content.html.erb config/locales/zh_tw.yml lib/parsers/parser_front_end.rb vendor/built_in_modules/announcement/init.rb vendor/built_in_modules/gprs/app/assets/javascripts/panel/gprs/back_end/locations.js vendor/built_in_modules/gprs/app/assets/stylesheets/panel/gprs/back_end/locations.css vendor/built_in_modules/gprs/app/controllers/panel/gprs/back_end/locations_controller.rb vendor/built_in_modules/gprs/app/views/panel/gprs/back_end/locations/index.html.erb vendor/built_in_modules/gprs/test/functional/panel/gprs/back_end/locations_controller_test.rb vendor/built_in_modules/location/README.rdoc vendor/built_in_modules/location/app/assets/javascripts/panel/gprs/back_end/locations.js vendor/built_in_modules/location/app/assets/javascripts/panel/location/back_end/locations.js vendor/built_in_modules/location/app/assets/stylesheets/panel/gprs/back_end/locations.css vendor/built_in_modules/location/app/assets/stylesheets/panel/location/back_end/locations.css vendor/built_in_modules/location/app/controllers/panel/location/back_end/locations_controller.rb vendor/built_in_modules/location/app/controllers/panel/locations/back_end/locations_controller.rb vendor/built_in_modules/location/app/models/location.rb vendor/built_in_modules/location/app/uploaders/locations_uploader.rb vendor/built_in_modules/location/app/views/panel/location/back_end/locations/index.html.erb vendor/built_in_modules/location/app/views/panel/locations/back_end/locations/_form.html.erb vendor/built_in_modules/location/app/views/panel/locations/back_end/locations/edit.html.erb vendor/built_in_modules/location/app/views/panel/locations/back_end/locations/index.html.erb vendor/built_in_modules/location/app/views/panel/locations/back_end/locations/new.html.erb vendor/built_in_modules/location/config/routes.rb vendor/built_in_modules/location/lib/location.rb vendor/built_in_modules/location/location.gemspec vendor/built_in_modules/location/test/functional/panel/gprs/back_end/locations_controller_test.rb vendor/built_in_modules/location/test/functional/panel/location/back_end/locations_controller_test.rb
2013-01-24 08:30:02 +00:00
2012-12-25 02:45:08 +00:00
ret << "<orbit_front path='#{url + options}' part_id=#{part.id} class='dymanic_load widget'></orbit_front>"
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
2012-12-28 02:36:18 +00:00
def parse_counter(body = nil)
body.css('.page_counter').each do |counter|
ret = ''
case counter['option']
when 'all'
ret << "display_visitors"
when 'today'
ret << "display_visitors_today"
when 'this_week'
ret << "display_visitors_this_week"
when 'this_month'
ret << "display_visitors_this_month"
when 'this_year'
ret << "display_visitors_this_year"
end
fragment = Nokogiri::HTML::DocumentFragment.new(body, "<orbit_front id='#{counter['id']}' class='#{counter['class']}' value='#{ret}'></orbit_front>")
counter.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
2013-01-31 11:20:55 +00:00
# breadcrumb
def generate_breadcrumbs(*args)
2013-02-01 09:07:16 +00:00
"<div class='dymanic_load' path='#{front_show_breadcrumb_path(args[1])}'></div>"
2013-01-31 11:20:55 +00:00
end
2011-12-23 10:34:21 +00:00
end