HTML code can be modify (paser: update)

This commit is contained in:
chris2tof 2011-06-10 10:31:50 +08:00
parent dda4b30d67
commit 247b8ec4dc
12 changed files with 127 additions and 13 deletions

View File

@ -25,8 +25,10 @@ class Admin::PagesController < ApplicationController
def edit def edit
@item = Page.find(params[:id]) @item = Page.find(params[:id])
@item.content = parse_content(@item.content, {:locale => 'show'}) # @item.content = parse_content(@item.content, {:locale => 'show'})
@item.content = parse_page_code(@item)
@i18n_variable = @item.i18n_variable @i18n_variable = @item.i18n_variable
@code = params[:code]
end end
def create def create
@ -50,12 +52,15 @@ class Admin::PagesController < ApplicationController
def update def update
@item = Page.find(params[:id]) @item = Page.find(params[:id])
parse_content(@item.content, {:locale => 'destroy'}) parse_content_update_code(@item, params[:page][:content])
if @item.update_attributes(params[:page]) if @item.update_attributes(params[:item])
@item.content = parse_content(@item.content, {:locale => 'create'})
@item.save!
flash[:notice] = t('admin.update_success_page') flash[:notice] = t('admin.update_success_page')
redirect_to admin_items_url( :parent_id => @item.parent_id ) respond_to do |format|
format.html {
redirect_to admin_page_url(@item)
}
format.js {}
end
else else
render :action => "edit" render :action => "edit"
end end

View File

@ -11,6 +11,7 @@ module Admin::ItemHelper
end end
ret << "<li>" ret << "<li>"
ret << (link_to node.name, dest) ret << (link_to node.name, dest)
ret << ' | ' << (link_to t('admin.code'), eval("edit_admin_#{node._type.downcase}_path(node, :code => true)")) if node._type.eql?('Page')
ret << ' | ' << (link_to t('admin.new_page'), new_admin_page_path(:parent_id => node.id)) if node._type.eql?('Page') ret << ' | ' << (link_to t('admin.new_page'), new_admin_page_path(:parent_id => node.id)) if node._type.eql?('Page')
ret << ' | ' << (link_to t('admin.new_link'), new_admin_link_path(:parent_id => node.id)) if node._type.eql?('Page') ret << ' | ' << (link_to t('admin.new_link'), new_admin_link_path(:parent_id => node.id)) if node._type.eql?('Page')
ret << ' | ' << (link_to t(:delete), eval("delete_admin_#{node._type.downcase}_path(node, :authenticity_token => form_authenticity_token)"), :confirm => t('sure?'), :class => 'delete') ret << ' | ' << (link_to t(:delete), eval("delete_admin_#{node._type.downcase}_path(node, :authenticity_token => form_authenticity_token)"), :confirm => t('sure?'), :class => 'delete')

View File

@ -20,7 +20,7 @@ class PagePart
if self.i18n_variable_id if self.i18n_variable_id
self.i18n_variable.update_attributes(attr) self.i18n_variable.update_attributes(attr)
else else
var = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class, :parent_id => self.page.i18n_variable_id})) var = I18nVariable.new(attr.merge({:key => self.name, :document_class => self.class, :parent_id => self.page.i18n_variable_id}))
var.save var.save
self.i18n_variable_id = var.id self.i18n_variable_id = var.id
end end

View File

@ -1,2 +1,3 @@
$('#sidebar').html("<%= escape_javascript(render(:partial => 'admin/items/site_map_left_bar')) %>"); $('#sidebar').html("<%= escape_javascript(render(:partial => 'admin/items/site_map_left_bar')) %>");
$('#main').html("<%= escape_javascript(render(:partial => 'admin/links/show')) %>"); $('#main').html("<%= escape_javascript(render(:partial => 'admin/links/show')) %>");
history.pushState(null, document.title, "<%= escape_javascript(admin_link_url(@item)) %>");

View File

@ -0,0 +1,8 @@
<%= flash_messages %>
<%= form_for @item, :url => admin_page_path(@item), :html => { :class => 'form' } do |f| %>
<%= render :partial => "admin/pages/form_code", :locals => { :f => f, :locale => locale } %>
<p>
<%= f.submit t(:update) %> <%= link_back %>
</p>
<% end %>

View File

@ -0,0 +1,3 @@
<%= f.error_messages %>
<%= f.text_area :content %>

View File

@ -1,2 +1,3 @@
$('#sidebar').html("<%= escape_javascript(render(:partial => 'admin/items/site_map_left_bar')) %>"); $('#sidebar').html("<%= escape_javascript(render(:partial => 'admin/items/site_map_left_bar')) %>");
$('#main').html("<%= escape_javascript(render(:partial => 'admin/pages/show')) %>"); $('#main').html("<%= escape_javascript(render(:partial => 'admin/pages/show')) %>");
history.pushState(null, document.title, "<%= escape_javascript(admin_page_url(@item)) %>");

View File

@ -0,0 +1 @@
$('#main').html("<%= escape_javascript(render(:partial => @code ? 'edit_code' : 'edit')) %>");

View File

@ -1 +1 @@
<%= render 'show' %> <%= render @code ? 'show_code' : 'show' %>

View File

@ -0,0 +1,2 @@
$('#main').html("<%= escape_javascript(render(:partial => 'admin/pages/show')) %>");
history.pushState(null, document.title, "<%= escape_javascript(admin_page_url(@item)) %>");

View File

@ -184,9 +184,102 @@ module Parser
end end
end end
def parse_page_code(page)
layout_content = page.layout.content
context = parser_context_code(page)
parser = Radius::Parser.new(context, :tag_prefix => 'r')
parser.parse(layout_content)
end
def parser_context_code(page)
Radius::Context.new do |c|
c.define_tag 'layout_part' do |tag|
part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s }
res = ''
res << "<r:layout_part"
tag.attr.each do |l|
res << " #{l[0]}='#{l[1]}'"
end
res << ">"
res << part.content if part
res << "</r:layout_part>"
end
end
end
def parse_content_update_code(page, page_content)
context = parser_context_update_code(page, page_content)
parser = Radius::Parser.new(context, :tag_prefix => 'r')
parser.parse(page_content)
end
def parser_context_update_code(page, page_content)
Radius::Context.new do |c|
c.define_tag 'layout_part' do |tag|
@part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s }
tag.locals.name = tag.attr['name'].to_s
if @part
part_update = tag.expand
if part_update
if !part_update.eql?(@part.content)
@part.content = tag.expand
@part.save
tag.locals.action = 'update'
end
else
tag.locals.action = 'destroy'
end
else
tag.locals.action = 'new'
end
res = ''
res << "<r:layout_part"
tag.attr.each do |l|
res << " #{l[0]}='#{l[1]}'"
end
res << ">"
res << tag.expand
res << "</r:layout_part>"
end
c.define_tag 'multi_lang' do |tag|
case tag.locals.action
when 'new', 'update'
@h = {}
tag.expand
@part.i18n_variable = @h
when 'destroy'
else
res = ''
res << "<r:multi_lang"
tag.attr.each do |l|
res << " #{l[0]}='#{l[1]}'"
end
res << ">"
res << tag.expand
res << "</r:multi_lang>"
end
end
c.define_tag 'multi_lang:lang' do |tag|
if tag.locals.action
@h.merge!(tag.attr['name'] => "#{tag.expand}")
else
res = ''
res << "<r:lang"
tag.attr.each do |l|
res << " #{l[0]}='#{l[1]}'"
end
res << ">"
res << tag.expand
res << "</r:lang>"
end
end
end
end
def self.included(base) def self.included(base)
base.send :helper_method, :parse_page if base.respond_to? :helper_method base.send :helper_method, :parse_page if base.respond_to? :helper_method
base.send :helper_method, :parse_page_edit if base.respond_to? :helper_method base.send :helper_method, :parse_page_edit if base.respond_to? :helper_method
base.send :helper_method, :parse_page_code if base.respond_to? :helper_method
end end
end end

View File

@ -1,5 +1,4 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en"> <html lang="en">
<head> <head>