Modifications for images in html code

This commit is contained in:
chris2tof 2011-10-18 11:21:24 +08:00
parent 0ca354cfd0
commit 2e86d8081f
10 changed files with 110 additions and 118 deletions

2
.gitignore vendored
View File

@ -13,3 +13,5 @@ uploads/**/*
public/javascripts public/javascripts
public/stylesheets public/stylesheets
config/application.rb config/application.rb
.rvmrc

View File

@ -1,6 +1,6 @@
source 'http://rubygems.org' source 'http://rubygems.org'
gem 'rails', '>=3.1.0.rc4' gem 'rails'
gem 'yajl-ruby', :require => 'yajl' gem 'yajl-ruby', :require => 'yajl'
gem 'bson_ext' gem 'bson_ext'
@ -16,7 +16,7 @@ gem 'mongoid'
gem 'rake', '>=0.9.2' gem 'rake', '>=0.9.2'
gem 'jquery-rails', '>= 1.0.3' gem 'jquery-rails', '>= 1.0.3'
gem 'sprockets', '2.0.0.beta.13' gem 'sprockets'
# Gems used only for assets and not required # Gems used only for assets and not required
# in production environments by default. # in production environments by default.

View File

@ -1,50 +1,50 @@
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
specs: specs:
actionmailer (3.1.0.rc6) actionmailer (3.1.0)
actionpack (= 3.1.0.rc6) actionpack (= 3.1.0)
mail (~> 2.3.0) mail (~> 2.3.0)
actionpack (3.1.0.rc6) actionpack (3.1.0)
activemodel (= 3.1.0.rc6) activemodel (= 3.1.0)
activesupport (= 3.1.0.rc6) activesupport (= 3.1.0)
builder (~> 3.0.0) builder (~> 3.0.0)
erubis (~> 2.7.0) erubis (~> 2.7.0)
i18n (~> 0.6) i18n (~> 0.6)
rack (~> 1.3.2) rack (~> 1.3.2)
rack-cache (~> 1.0.2) rack-cache (~> 1.0.3)
rack-mount (~> 0.8.1) rack-mount (~> 0.8.2)
rack-test (~> 0.6.0) rack-test (~> 0.6.1)
sprockets (~> 2.0.0.beta.12) sprockets (~> 2.0.0)
activemodel (3.1.0.rc6) activemodel (3.1.0)
activesupport (= 3.1.0.rc6) activesupport (= 3.1.0)
bcrypt-ruby (~> 2.1.4) bcrypt-ruby (~> 3.0.0)
builder (~> 3.0.0) builder (~> 3.0.0)
i18n (~> 0.6) i18n (~> 0.6)
activerecord (3.1.0.rc6) activerecord (3.1.0)
activemodel (= 3.1.0.rc6) activemodel (= 3.1.0)
activesupport (= 3.1.0.rc6) activesupport (= 3.1.0)
arel (~> 2.2.1) arel (~> 2.2.1)
tzinfo (~> 0.3.29) tzinfo (~> 0.3.29)
activeresource (3.1.0.rc6) activeresource (3.1.0)
activemodel (= 3.1.0.rc6) activemodel (= 3.1.0)
activesupport (= 3.1.0.rc6) activesupport (= 3.1.0)
activesupport (3.1.0.rc6) activesupport (3.1.0)
multi_json (~> 1.0) multi_json (~> 1.0)
archive-tar-minitar (0.5.2) archive-tar-minitar (0.5.2)
arel (2.2.1) arel (2.2.1)
bcrypt-ruby (2.1.4) bcrypt-ruby (3.0.1)
bson (1.3.1) bson (1.4.0)
bson_ext (1.3.1) bson_ext (1.4.0)
builder (3.0.0) builder (3.0.0)
carrierwave (0.5.4) carrierwave (0.5.4)
activesupport (~> 3.0) activesupport (~> 3.0)
chronic (0.6.2) chronic (0.6.4)
ckeditor (3.6.1) ckeditor (3.6.2)
mime-types (~> 1.16) mime-types (~> 1.16)
orm_adapter (~> 0.0.5) orm_adapter (~> 0.0.5)
coffee-rails (3.1.0.rc.6) coffee-rails (3.1.1)
coffee-script (>= 2.2.0) coffee-script (>= 2.2.0)
railties (~> 3.1.0.rc1) railties (~> 3.1.0)
coffee-script (2.2.0) coffee-script (2.2.0)
coffee-script-source coffee-script-source
execjs execjs
@ -52,21 +52,22 @@ GEM
columnize (0.3.4) columnize (0.3.4)
delorean (1.1.0) delorean (1.1.0)
chronic chronic
devise (1.4.2) devise (1.4.7)
bcrypt-ruby (~> 2.1.2) bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.0.3) orm_adapter (~> 0.0.3)
warden (~> 1.0.3) warden (~> 1.0.3)
diff-lcs (1.1.2) diff-lcs (1.1.3)
erubis (2.7.0) erubis (2.7.0)
execjs (1.2.4) execjs (1.2.9)
multi_json (~> 1.0) multi_json (~> 1.0)
factory_girl (2.0.4) factory_girl (2.1.2)
factory_girl_rails (1.1.0) activesupport
factory_girl (~> 2.0.0) factory_girl_rails (1.2.0)
factory_girl (~> 2.1.0)
railties (>= 3.0.0) railties (>= 3.0.0)
hike (1.2.1) hike (1.2.1)
i18n (0.6.0) i18n (0.6.0)
jquery-rails (1.0.13) jquery-rails (1.0.14)
railties (~> 3.0) railties (~> 3.0)
thor (~> 0.14) thor (~> 0.14)
linecache19 (0.5.12) linecache19 (0.5.12)
@ -80,40 +81,40 @@ GEM
subexec (~> 0.1.0) subexec (~> 0.1.0)
mongo (1.3.1) mongo (1.3.1)
bson (>= 1.3.1) bson (>= 1.3.1)
mongoid (2.2.0) mongoid (2.2.1)
activemodel (~> 3.0) activemodel (~> 3.0)
mongo (~> 1.3) mongo (>= 1.3, < 1.4)
tzinfo (~> 0.3.22) tzinfo (~> 0.3.22)
multi_json (1.0.3) multi_json (1.0.3)
orm_adapter (0.0.5) orm_adapter (0.0.5)
polyglot (0.3.2) polyglot (0.3.2)
rack (1.3.2) rack (1.3.3)
rack-cache (1.0.2) rack-cache (1.0.3)
rack (>= 0.4) rack (>= 0.4)
rack-mount (0.8.2) rack-mount (0.8.3)
rack (>= 1.0.0) rack (>= 1.0.0)
rack-ssl (1.3.2) rack-ssl (1.3.2)
rack rack
rack-test (0.6.1) rack-test (0.6.1)
rack (>= 1.0) rack (>= 1.0)
rails (3.1.0.rc6) rails (3.1.0)
actionmailer (= 3.1.0.rc6) actionmailer (= 3.1.0)
actionpack (= 3.1.0.rc6) actionpack (= 3.1.0)
activerecord (= 3.1.0.rc6) activerecord (= 3.1.0)
activeresource (= 3.1.0.rc6) activeresource (= 3.1.0)
activesupport (= 3.1.0.rc6) activesupport (= 3.1.0)
bundler (~> 1.0) bundler (~> 1.0)
railties (= 3.1.0.rc6) railties (= 3.1.0)
railties (3.1.0.rc6) railties (3.1.0)
actionpack (= 3.1.0.rc6) actionpack (= 3.1.0)
activesupport (= 3.1.0.rc6) activesupport (= 3.1.0)
rack-ssl (~> 1.3.2) rack-ssl (~> 1.3.2)
rake (>= 0.8.7) rake (>= 0.8.7)
rdoc (~> 3.4) rdoc (~> 3.4)
thor (~> 0.14.6) thor (~> 0.14.6)
rake (0.9.2) rake (0.9.2)
rcov (0.9.10) rcov (0.9.10)
rdoc (3.9.2) rdoc (3.9.4)
rspec (2.6.0) rspec (2.6.0)
rspec-core (~> 2.6.0) rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0) rspec-expectations (~> 2.6.0)
@ -139,32 +140,34 @@ GEM
archive-tar-minitar (>= 0.5.2) archive-tar-minitar (>= 0.5.2)
rubyzip (0.9.4) rubyzip (0.9.4)
sass (3.1.7) sass (3.1.7)
sass-rails (3.1.0.rc.6) sass-rails (3.1.3)
actionpack (~> 3.1.0.rc6) actionpack (~> 3.1.0)
railties (~> 3.1.0.rc6) railties (~> 3.1.0)
sass (>= 3.1.4) sass (>= 3.1.4)
sprockets (~> 2.0.0)
tilt (~> 1.3.2)
shoulda-matchers (1.0.0.beta3) shoulda-matchers (1.0.0.beta3)
sinatra (1.2.6) sinatra (1.2.6)
rack (~> 1.1) rack (~> 1.1)
tilt (< 2.0, >= 1.2.2) tilt (>= 1.2.2, < 2.0)
sprockets (2.0.0.beta.13) sprockets (2.0.0)
hike (~> 1.2) hike (~> 1.2)
rack (~> 1.0) rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1) tilt (~> 1.1, != 1.3.0)
subexec (0.1.0) subexec (0.1.0)
thor (0.14.6) thor (0.14.6)
tilt (1.3.2) tilt (1.3.3)
treetop (1.4.10) treetop (1.4.10)
polyglot polyglot
polyglot (>= 0.3.1) polyglot (>= 0.3.1)
tzinfo (0.3.29) tzinfo (0.3.29)
uglifier (1.0.1) uglifier (1.0.3)
execjs (>= 0.3.0) execjs (>= 0.3.0)
multi_json (>= 1.0.2) multi_json (>= 1.0.2)
warden (1.0.5) warden (1.0.5)
rack (>= 1.0) rack (>= 1.0)
watchr (0.7) watchr (0.7)
yajl-ruby (0.8.3) yajl-ruby (1.0.0)
PLATFORMS PLATFORMS
ruby ruby
@ -180,7 +183,7 @@ DEPENDENCIES
jquery-rails (>= 1.0.3) jquery-rails (>= 1.0.3)
mini_magick mini_magick
mongoid mongoid
rails (>= 3.1.0.rc4) rails
rake (>= 0.9.2) rake (>= 0.9.2)
rcov rcov
rspec (~> 2.0) rspec (~> 2.0)
@ -190,7 +193,7 @@ DEPENDENCIES
sass-rails (~> 3.1.0.rc) sass-rails (~> 3.1.0.rc)
shoulda-matchers shoulda-matchers
sinatra sinatra
sprockets (= 2.0.0.beta.13) sprockets
uglifier uglifier
watchr watchr
yajl-ruby yajl-ruby

View File

@ -16,6 +16,7 @@ class Design
embeds_many :themes embeds_many :themes
embeds_many :javascripts embeds_many :javascripts
embeds_many :images embeds_many :images
embeds_many :custom_images, as: :design_image
validates_presence_of :title validates_presence_of :title
validates_presence_of :author validates_presence_of :author
@ -75,7 +76,6 @@ class Design
if self.layout.to_save if self.layout.to_save
self.layout.to_save = false self.layout.to_save = false
self.layout.save self.layout.save
self.layout.parse_urls
end end
[[self.default_css], [self.reset_css], self.themes].each do |objects| [[self.default_css], [self.reset_css], self.themes].each do |objects|
objects.each do |object| objects.each do |object|
@ -91,11 +91,12 @@ class Design
def process_object(object) def process_object(object)
if object.to_save if object.to_save
if object.file_filename.nil? if object.file_filename.nil?
new_object = self.send(object._type.downcase.pluralize).build new_object = self.send(object._type.downcase.pluralize).build(object)
new_object.file = object.file
object.destroy object.destroy
new_object.to_save = false
new_object.save new_object.save
else else
object.to_save = false
object.save object.save
end end
end end

View File

@ -1,5 +1,11 @@
class Image < DesignFile class Image < DesignFile
embedded_in :design
field :in_css, :type => Boolean
field :name
field :html_id
field :html_class
embedded_in :design_image, polymorphic: true
mount_uploader :file, ImageUploader mount_uploader :file, ImageUploader
end end

View File

@ -1,8 +1,6 @@
class Layout < DesignFile class Layout < DesignFile
include Parser include Parser
mount_uploader :file_orig, AssetUploader
attr_reader :content attr_reader :content
embeds_one :menu embeds_one :menu
@ -23,38 +21,4 @@ class Layout < DesignFile
parse_layout_contents(self) parse_layout_contents(self)
end end
def parse_urls
orig_content = content = self.content
self.remove_file!
self.remove_file_orig!
names = []
images = self.design.images
html_name = nil
content.scan(/(?<=\<img)(.*?)(?=\/\>)/){
$1.gsub(' ','').scan(/(?<=src=\")(.*?)(?=\")/){
html_name = $1
}
name = File.basename(html_name).gsub(/[\\\"]/, '')
file_name = images.detect{ |i| i.file_filename.eql?(name) }.file_url rescue nil
names << [html_name, file_name]
}
names.each do |name|
content.gsub!(name[0], name[1]) rescue nil
end
Dir.mktmpdir('f_path') { |dir|
orig_file_name = self.file_filename
temp_file = File.new(dir + '/' + orig_file_name, 'w+')
temp_file.write orig_content.force_encoding("UTF-8")
self.file_orig = temp_file
temp_file = File.new(dir + '/' + orig_file_name, 'w+')
temp_file.write content.force_encoding("UTF-8")
self.file = temp_file
Layout.without_callback(:save, :after, :parse_layout) do
self.save
end
}
end
end end

View File

@ -6,6 +6,8 @@ class Page < Item
belongs_to :design belongs_to :design
has_many :page_parts has_many :page_parts
embeds_many :custom_images, as: :design_image
def create_parts def create_parts
page_design = self.design page_design = self.design
page_design.layout.layout_parts.each do |p| page_design.layout.layout_parts.each do |p|

View File

@ -6,7 +6,7 @@ class AssetUploader < CarrierWave::Uploader::Base
process :set_content_type process :set_content_type
def set_content_type(*args) def set_content_type(*args)
content_type = file.content_type == 'binary/octet-stream' || 'application/octet-stream' || file.content_type.blank? ? MIME::Types.type_for(original_filename).first.to_s : file.content_type content_type = file.content_type == ('binary/octet-stream' || 'application/octet-stream') || file.content_type.blank? ? MIME::Types.type_for(original_filename).first.to_s : file.content_type
self.file.instance_variable_set(:@content_type, content_type) self.file.instance_variable_set(:@content_type, content_type)
end end

View File

@ -12,7 +12,7 @@ class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick include CarrierWave::MiniMagick
def set_content_type(*args) def set_content_type(*args)
content_type = file.content_type == 'binary/octet-stream' || 'application/octet-stream' || file.content_type.blank? ? MIME::Types.type_for(original_filename).first.to_s : file.content_type content_type = file.content_type == ('binary/octet-stream' || 'application/octet-stream') || file.content_type.blank? ? MIME::Types.type_for(original_filename).first.to_s : file.content_type
self.file.instance_variable_set(:@content_type, content_type) self.file.instance_variable_set(:@content_type, content_type)
end end
@ -58,14 +58,14 @@ class ImageUploader < CarrierWave::Uploader::Base
# "something.jpg" if original_filename # "something.jpg" if original_filename
# end # end
# def manipulate! def manipulate!
# raise current_path.inspect cache_stored_file! if !cached?
# image = ::MiniMagick::Image.open(current_path) image = ::MiniMagick::Image.open(current_path)
# image = yield(image) image = yield(image)
# image.write(current_path) image.write(current_path)
# ::MiniMagick::Image.open(current_path) ::MiniMagick::Image.open(current_path)
# rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e rescue
# raise CarrierWave::ProcessingError.new("Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: #{e}") nil
# end end
end end

View File

@ -164,9 +164,11 @@ module Parser
res res
end end
c.define_tag 'image' do |tag| c.define_tag 'image' do |tag|
asset = Asset.find(tag.attr['id']) image = page.custom_images.detect{|image| image.name.eql?(tag.attr['name']) }
if asset image = page.design.custom_images.detect{|image| image.name.eql?(tag.attr['name']) } unless image
res = "<img src=#{asset.data.file.url} " image = page.design.images.detect{|image| image.name.eql?(tag.attr['name']) } unless image
if image
res = "<img src=#{image.file.url} "
tag.attr.each do |l| tag.attr.each do |l|
res << "#{l[0]}='#{l[1]}' " res << "#{l[0]}='#{l[1]}' "
end end
@ -278,8 +280,20 @@ module Parser
c.define_tag 'content' do |tag| c.define_tag 'content' do |tag|
layout.layout_parts.create(:name => tag.attr['name'], :editable => true) layout.layout_parts.create(:name => tag.attr['name'], :editable => true)
end end
c.define_tag 'image' do |tag|
image = layout.design.images.detect{ |i| i.file_filename.eql?(parse_html_image(tag.expand)) }
image.update_attributes(:name => tag.attr['name'], :html_id => tag.attr['id'], :html_class => tag.attr['class'])
end end
end end
end
def parse_html_image(html)
html.scan(/(?<=\<img)(.*?)(?=\/\>)/){
$1.gsub(' ','').scan(/(?<=src=\")(.*?)(?=\")/){
return File.basename($1).gsub(/[\\\"]/, '')
}
}
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