Trying nokogiri
This commit is contained in:
parent
a126935d2c
commit
4f741418a0
1
Gemfile
1
Gemfile
|
@ -16,6 +16,7 @@ gem 'kaminari', :git => 'git://github.com/amatsuda/kaminari.git'
|
|||
gem 'mini_magick'
|
||||
gem 'mongoid'
|
||||
gem "mongo_session_store-rails3"
|
||||
gem 'nokogiri'
|
||||
gem 'radius'
|
||||
gem 'rake'
|
||||
gem 'ruby-debug19'
|
||||
|
|
|
@ -41,6 +41,7 @@ GEM
|
|||
archive-tar-minitar (0.5.2)
|
||||
arel (2.2.3)
|
||||
bcrypt-ruby (3.0.1)
|
||||
bcrypt-ruby (3.0.1-x86-mingw32)
|
||||
brakeman (1.5.1)
|
||||
activesupport
|
||||
erubis (~> 2.6)
|
||||
|
@ -115,6 +116,8 @@ GEM
|
|||
mongo (~> 1.3)
|
||||
tzinfo (~> 0.3.22)
|
||||
multi_json (1.1.0)
|
||||
nokogiri (1.5.2)
|
||||
nokogiri (1.5.2-x86-mingw32)
|
||||
orm_adapter (0.0.6)
|
||||
pdf-writer (1.1.8)
|
||||
color (>= 1.4.0)
|
||||
|
@ -252,6 +255,7 @@ DEPENDENCIES
|
|||
mini_magick
|
||||
mongo_session_store-rails3
|
||||
mongoid
|
||||
nokogiri
|
||||
radius
|
||||
rails (>= 3.1.0, < 3.2.0)
|
||||
rake
|
||||
|
|
|
@ -47,7 +47,7 @@ class ApplicationController < ActionController::Base
|
|||
# Render the page
|
||||
def render_page(id = nil)
|
||||
if @item
|
||||
render :text => parse_page(@item, id)
|
||||
render :text => process_page(@item, id), :layout => 'page_layout'
|
||||
else
|
||||
render :text => '404 Not Found'
|
||||
end
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
class PagesController < ApplicationController
|
||||
|
||||
include ApplicationHelper
|
||||
|
||||
before_filter :get_item, :only => [:index_from_link, :show_from_link]
|
||||
|
||||
def index
|
||||
|
|
|
@ -107,4 +107,51 @@ module ApplicationHelper
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
def process_page(page, id)
|
||||
parse_page_noko(page, id)
|
||||
end
|
||||
|
||||
def page_metas(page)
|
||||
metas = ''
|
||||
@site.page_metas.each do |meta|
|
||||
name, content = meta.get_name_content
|
||||
metas << "<meta name=#{name} content=#{content} />\n"
|
||||
end rescue nil
|
||||
return metas
|
||||
end
|
||||
|
||||
def page_title(page)
|
||||
"<title>#{page.title ? page.title[I18n.locale] : page.i18n_variable[I18n.locale]}</title>\n"
|
||||
end
|
||||
|
||||
def page_stylesheets(page)
|
||||
stylesheets = ''
|
||||
stylesheets << "<link href='/assets/bootstrap.css.erb' rel='stylesheet' type='text/css' />\n"
|
||||
stylesheets << "<link href='/assets/bootstrap-orbit.css' rel='stylesheet' type='text/css' />\n"
|
||||
stylesheets << "<link href='/assets/style.css.erb' rel='stylesheet' type='text/css' />\n"
|
||||
stylesheets << "<link href='#{page.design.reset_css.file.url}' rel='stylesheet' type='text/css' />\n" if page.design.reset_css
|
||||
stylesheets << "<link href='#{page.design.default_css.file.url}' rel='stylesheet' type='text/css' />\n" if page.design.default_css
|
||||
theme = page.design.themes.detect{ |d| d.id == page.theme_id }
|
||||
stylesheets << "<link href='#{theme.file.url}' rel='stylesheet' type='text/css' />\n" if theme
|
||||
stylesheets
|
||||
end
|
||||
|
||||
def page_javascripts(page)
|
||||
javascripts = ''
|
||||
javascripts << "<script type='text/javascript' src='/static/jquery.js'></script>\n"
|
||||
javascripts << "<script type='text/javascript' src='/static/jquery.cycle.all.latest.js'></script>\n"
|
||||
javascripts << "<script type='text/javascript' src='/static/kernel.js'></script>\n"
|
||||
javascripts << "<script type='text/javascript' src='/assets/bootstrap.js'></script>\n"
|
||||
javascripts << "<script type='text/javascript' src='/assets/jquery.tinyscrollbar.min.js'></script>\n"
|
||||
javascripts << "<script type='text/javascript' src='/assets/jquery.isotope.min.js'></script>\n"
|
||||
javascripts << "<script type='text/javascript' src='/assets/orbit-1.0.js'></script>\n"
|
||||
javascripts << "<script type='text/javascript' src='/assets/orbit_bar.js'></script>\n"
|
||||
javascripts << "<script type='text/javascript' src='/assets/event.js'></script>\n"
|
||||
page.design.javascripts.each do |js|
|
||||
# javascripts << "<script type='text/javascript' src='#{js.file.url}'></script>"
|
||||
end
|
||||
javascripts
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -2,6 +2,8 @@ class Layout < DesignFile
|
|||
include ParserLayout
|
||||
|
||||
attr_reader :content
|
||||
|
||||
field :body
|
||||
|
||||
embeds_one :menu
|
||||
embedded_in :design
|
||||
|
@ -17,8 +19,10 @@ class Layout < DesignFile
|
|||
Layout.count > 0
|
||||
end
|
||||
|
||||
def parse_layout
|
||||
parse_layout_contents(self)
|
||||
def parse_layout
|
||||
html = Nokogiri::HTML(self.file.read)
|
||||
self.body = html.at_css("body").inner_html
|
||||
parse_body(self)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -7,5 +7,9 @@ class Meta
|
|||
field :value, :default => nil
|
||||
|
||||
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
|
||||
|
||||
def get_name_content
|
||||
[self.key, self.value ? self.value : self.i18n_variable[I18n.locale]]
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<%= page_title(@item).html_safe %>
|
||||
<%= page_metas(@item).html_safe %>
|
||||
<%= @metas %>
|
||||
<!--[if lt IE 9]>
|
||||
<%= javascript_include_tag "html5" %>
|
||||
<![endif]-->
|
||||
<%= page_stylesheets(@item).html_safe %>
|
||||
<%= page_javascripts(@item).html_safe %>
|
||||
</head>
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
|
@ -63,22 +63,6 @@ module ParserFrontEnd
|
|||
res << '>'
|
||||
end
|
||||
end
|
||||
c.define_tag 'javascripts' do |tag|
|
||||
res = ''
|
||||
res << "<script type='text/javascript' src='/static/jquery.js'></script>"
|
||||
res << "<script type='text/javascript' src='/static/jquery.cycle.all.latest.js'></script>"
|
||||
res << "<script type='text/javascript' src='/static/kernel.js'></script>"
|
||||
res << "<script type='text/javascript' src='/assets/bootstrap.js'></script>"
|
||||
res << "<script type='text/javascript' src='/assets/jquery.tinyscrollbar.min.js'></script>"
|
||||
res << "<script type='text/javascript' src='/assets/jquery.isotope.min.js'></script>"
|
||||
res << "<script type='text/javascript' src='/assets/orbit-1.0.js'></script>"
|
||||
res << "<script type='text/javascript' src='/assets/orbit_bar.js'></script>"
|
||||
res << "<script type='text/javascript' src='/assets/event.js'></script>"
|
||||
page.design.javascripts.each do |js|
|
||||
# res << "<script type='text/javascript' src='#{js.file.url}'></script>"
|
||||
end
|
||||
res
|
||||
end
|
||||
c.define_tag 'language_bar' do
|
||||
@site.in_use_locales.map{ |locale|
|
||||
lang = I18nVariable.first(:conditions => {:key => locale})[locale]
|
||||
|
@ -101,38 +85,87 @@ module ParserFrontEnd
|
|||
menu = page.design.layout.menu
|
||||
menu_level(home, 0, menu)
|
||||
end
|
||||
c.define_tag 'meta' do |tag|
|
||||
res = ''
|
||||
#res << "<meta name='bob' content='#{@site.school}' />"
|
||||
end
|
||||
c.define_tag 'stylesheets' do |tag|
|
||||
res = ''
|
||||
res << "<link href='/assets/bootstrap.css.erb' rel='stylesheet' type='text/css'/>"
|
||||
res << "<link href='/assets/bootstrap-orbit.css' rel='stylesheet' type='text/css'/>"
|
||||
res << "<link href='/assets/style.css.erb' rel='stylesheet' type='text/css'/>"
|
||||
res << "<link href='#{page.design.reset_css.file.url}' rel='stylesheet' type='text/css' />" if page.design.reset_css
|
||||
res << "<link href='#{page.design.default_css.file.url}' rel='stylesheet' type='text/css' /> " if page.design.default_css
|
||||
theme = page.design.themes.detect{ |d| d.id == page.theme_id }
|
||||
res << "<link href='#{theme.file.url}' rel='stylesheet' type='text/css' />" if theme
|
||||
res
|
||||
end
|
||||
c.define_tag 'title' do |tag|
|
||||
"<title>#{page.title ? page.title[I18n.locale] : page.i18n_variable[I18n.locale]}</title>"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def parse_page(page, id = nil)
|
||||
if page._type == 'Page'
|
||||
layout_content = page.design.layout.content.force_encoding('UTF-8') rescue ''
|
||||
context = parser_context(page, {}, id)
|
||||
parser = Radius::Parser.new(context, :tag_prefix => 'r')
|
||||
parser.parse(parser.parse(layout_content))
|
||||
parser.parse(parser.parse(page.design.layout.body))
|
||||
end
|
||||
end
|
||||
|
||||
def self.included(base)
|
||||
base.send :helper_method, :parse_page if base.respond_to? :helper_method
|
||||
end
|
||||
|
||||
|
||||
|
||||
require 'nokogiri'
|
||||
|
||||
def parse_page_noko(page, id = nil)
|
||||
body = Nokogiri::HTML(page.design.layout.body, nil, 'UTF-8')
|
||||
|
||||
# page_contents
|
||||
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 << "'></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
|
||||
when 'public_r_tag'
|
||||
ret << "<r:#{part.public_r_tag} id='#{part.public_r_tag_object_id}'/>"
|
||||
else
|
||||
''
|
||||
end
|
||||
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
|
||||
|
||||
# page_menu
|
||||
page_menu = body.css('.page_menu').first
|
||||
home = get_homepage
|
||||
menu = page.design.layout.menu
|
||||
fragment = Nokogiri::HTML::DocumentFragment.new(body, menu_level(home, 0, menu))
|
||||
page_menu.swap(fragment)
|
||||
|
||||
# page_image
|
||||
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
|
||||
image = page.design.images.detect{|image| image.name.eql?(page_image['name']) } 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
|
||||
|
||||
body.to_html
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,44 +1,27 @@
|
|||
module ParserLayout
|
||||
require 'radius'
|
||||
include ParserCommon
|
||||
require 'nokogiri'
|
||||
|
||||
def parse_layout_contents(layout)
|
||||
content = layout.content.force_encoding('UTF-8')
|
||||
context = parser_layout_contents(layout)
|
||||
parser = Radius::Parser.new(context, :tag_prefix => 'r')
|
||||
parser.parse(content)
|
||||
end
|
||||
|
||||
def parser_layout_contents(layout)
|
||||
Radius::Context.new do |c|
|
||||
c.define_tag 'ad_banner' do |tag|
|
||||
end
|
||||
c.define_tag 'content' do |tag|
|
||||
layout.layout_parts.new(:name => tag.attr['name'])
|
||||
end
|
||||
c.define_tag 'image' do |tag|
|
||||
image = layout.design.images.detect{ |i| i.file_identifier.eql?(parse_html_image(tag.expand)) }
|
||||
image.update_attributes(:name => tag.attr['name'], :html_id => tag.attr['id'], :html_class => tag.attr['class']) if image
|
||||
end
|
||||
c.define_tag 'javascripts' do |tag|
|
||||
end
|
||||
c.define_tag 'menu' do |tag|
|
||||
layout.build_menu(:levels => 0, :values => {}) unless layout.menu
|
||||
layout.menu.levels = i = tag.attr['level'].to_i
|
||||
layout.menu.values.merge!({'home' => tag.attr['home']}) if i == 1
|
||||
layout.menu.values.merge!({"id_#{i}" => tag.attr['id'], "class_#{i}" => tag.attr['class'], "li_class_#{i}" => tag.attr['li_class'], "li_incremental_#{i}" => tag.attr['li_incremental']})
|
||||
tag.expand
|
||||
end
|
||||
c.define_tag 'meta' do |tag|
|
||||
end
|
||||
c.define_tag 'stylesheets' do |tag|
|
||||
end
|
||||
c.define_tag 'title' do |tag|
|
||||
end
|
||||
def parse_body(layout)
|
||||
body = Nokogiri::HTML(layout.body)
|
||||
|
||||
body.css('.page_content').each do |content|
|
||||
layout.layout_parts.build(:name => content['name'])
|
||||
end
|
||||
|
||||
body.css('.page_image').each do |image|
|
||||
image = layout.design.images.detect{ |i| i.file_identifier.eql?(parse_html_image(image.to_html)) }
|
||||
image.update_attributes(:name => image['name'], :html_id => image['id'], :html_class => image['class']) if image
|
||||
end
|
||||
|
||||
body.css('.page_menu').each do |menu|
|
||||
layout.build_menu(:levels => 0, :values => {}) unless layout.menu
|
||||
layout.menu.levels = i = menu['level'].to_i
|
||||
layout.menu.values.merge!({'home' => menu['home']}) if i == 1
|
||||
layout.menu.values.merge!({"id_#{i}" => menu['id'], "class_#{i}" => menu['class'], "li_class_#{i}" => menu['li_class'], "li_incremental_#{i}" => menu['li_incremental']})
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
def parse_html_image(html)
|
||||
html.scan(/(?<=\<img)(.*?)(?=\/\>)/){
|
||||
$1.gsub(' ','').scan(/(?<=src=\")(.*?)(?=\")/){
|
||||
|
@ -46,4 +29,6 @@ module ParserLayout
|
|||
}
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue