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/stylesheets
config/application.rb
.rvmrc

View File

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

View File

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

View File

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

View File

@ -1,5 +1,11 @@
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
end

View File

@ -1,8 +1,6 @@
class Layout < DesignFile
include Parser
mount_uploader :file_orig, AssetUploader
attr_reader :content
embeds_one :menu
@ -23,38 +21,4 @@ class Layout < DesignFile
parse_layout_contents(self)
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

View File

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

View File

@ -6,7 +6,7 @@ class AssetUploader < CarrierWave::Uploader::Base
process :set_content_type
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)
end

View File

@ -12,7 +12,7 @@ class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
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)
end
@ -58,14 +58,14 @@ class ImageUploader < CarrierWave::Uploader::Base
# "something.jpg" if original_filename
# end
# def manipulate!
# raise current_path.inspect
# image = ::MiniMagick::Image.open(current_path)
# image = yield(image)
# image.write(current_path)
# ::MiniMagick::Image.open(current_path)
# rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e
# raise CarrierWave::ProcessingError.new("Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: #{e}")
# end
def manipulate!
cache_stored_file! if !cached?
image = ::MiniMagick::Image.open(current_path)
image = yield(image)
image.write(current_path)
::MiniMagick::Image.open(current_path)
rescue
nil
end
end

View File

@ -164,9 +164,11 @@ module Parser
res
end
c.define_tag 'image' do |tag|
asset = Asset.find(tag.attr['id'])
if asset
res = "<img src=#{asset.data.file.url} "
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?(tag.attr['name']) } unless image
if image
res = "<img src=#{image.file.url} "
tag.attr.each do |l|
res << "#{l[0]}='#{l[1]}' "
end
@ -278,8 +280,20 @@ module Parser
c.define_tag 'content' do |tag|
layout.layout_parts.create(:name => tag.attr['name'], :editable => true)
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
def parse_html_image(html)
html.scan(/(?<=\<img)(.*?)(?=\/\>)/){
$1.gsub(' ','').scan(/(?<=src=\")(.*?)(?=\")/){
return File.basename($1).gsub(/[\\\"]/, '')
}
}
end
def self.included(base)
base.send :helper_method, :parse_page if base.respond_to? :helper_method