Skip to content

Commit

Permalink
v0.0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
zfben committed Aug 31, 2011
1 parent a978940 commit bc540eb
Show file tree
Hide file tree
Showing 20 changed files with 60 additions and 42 deletions.
4 changes: 2 additions & 2 deletions lib/zfben_libjs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def build!
lib.push(source)
end
end
lib = lib.flatten.uniq
lib = lib.flatten.uniq.compact

collection = Zfben_libjs::Collection.new(lib, @libs)
collection.merge!
Expand Down Expand Up @@ -142,7 +142,7 @@ def build!
=end
tip '== [2/2] Generate lib.js =='

libjs = File.read(@libs['lazyload']) << ';'
libjs = File.read(@libs['lazyload'][0]) << ';'

libjs_core = File.read(File.join(@path_gem, 'lib.coffee'))

Expand Down
8 changes: 4 additions & 4 deletions lib/zfben_libjs/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize sources, libs
@js = []
@image = []

process_sources sources
process_sources sources.flatten.uniq.compact, libs
end

def merge!
Expand All @@ -35,10 +35,10 @@ def merge!
def process_sources sources, libs
sources.each do |source|
if source.class.to_s == 'String'
process_sources libs[source]
elsif source.respond_to(:to_css)
process_sources libs[source], libs
elsif source.respond_to?(:to_css)
@css.push source
elsif source.respond_to(:to_js)
elsif source.respond_to?(:to_js)
@js.push source
else
@image.push source
Expand Down
37 changes: 22 additions & 15 deletions lib/zfben_libjs/source.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
module Zfben_libjs

def self.get_source filepath, *options
type = File.extname(filepath).delete('.')
type = File.extname(filepath).delete('.').capitalize

class_name = type.capitalize
if Zfben_libjs.const_defined?(class_name, false)
source_class = Zfben_libjs.const_get(class_name)
if Zfben_libjs.const_defined?(type, false)
source_class = Zfben_libjs.const_get(type)
else
raise Exception.new(class_name + ' isn\'t exists!')
raise Exception.new(type + ' isn\'t exists!')
end

return source_class.new :filepath => filepath, :options => options[0]
Expand All @@ -23,9 +22,7 @@ def initialize *opts

@options = {} if @options.nil?

if remote?
download!
end
download! if remote?

@source = @source || File.read(@filepath)

Expand All @@ -39,15 +36,11 @@ def remote?
def download!
@remote_path = @filepath
@filepath = File.join(@options['src/source'], '.download', File.basename(@remote_path))
FileUtils.mkdir(File.dirname(@filepath)) unless File.exists?(File.dirname(@filepath))
unless system 'wget ' + @remote_path + ' -O ' + @filepath
FileUtils.rm @filepath
raise Exception.new(@remote_path + ' download failed!')
end
download @remote_path, @filepath
end

def name
self.class.to_s.downcase.gsub(/Zfben_libjs::/, '')
def type
self.class.to_s.gsub(/Zfben_libjs::/, '').downcase
end

def compile
Expand All @@ -60,6 +53,20 @@ def minify
@minified = @minified || @compiled || @source
@minified = @minified.gsub(/\n/ , '')
end

private

def download url, path
if !File.exists?(path) || @options['download']
dir = File.dirname(path)
FileUtils.mkdir(dir) unless File.exists?(dir)
unless system 'wget ' + url + ' -O ' + path
FileUtils.rm path
raise Exception.new(url + ' download failed!')
end
end
end

end

end
24 changes: 24 additions & 0 deletions lib/zfben_libjs/support_source/css.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
class Zfben_libjs::Css < Zfben_libjs::Source

REGEXP_IMPORT_CSS = /@import\s+\(?"([^"]+)"\)?;?/

def after_initialize
@options = @options.merge({ :syntax => :sass, :style => :compressed, :cache => false })

@source = import_remote_css @source
end

def to_css
Expand All @@ -17,9 +22,28 @@ def to_scss

def download_images
@source

end

def before_minify
@minified = Sass::Engine.new(to_sass, @options).render
end

private

def import_remote_css url
unless File.exists?(url)
path = File.join(@options['src/source'], '.download', File.basename(url))
download(url, path)
else
path = url
end
return File.read(path).partition_all(REGEXP_IMPORT_CSS).map{ |f|
if REGEXP_IMPORT_CSS =~ f
url = REGEXP_IMPORT_CSS.match(f)[1]
f = import_remote_css(url)
end
f
}.join("\n")
end
end
6 changes: 3 additions & 3 deletions test/javascript/lib_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ module('support_filetype');
asyncTest('stylesheet', 4, function() {
lib.support_filetype(function(){
equal($('.css').css('color'), 'rgb(0, 0, 1)', 'css file loaded');
equal($('.sass').css('color'), 'rgb(0, 0, 2)', 'sass file loaded');
equal($('.scss').css('color'), 'rgb(0, 0, 3)', 'scss file loaded');
equal($('.rb_css').css('color'), 'rgb(0, 0, 4)', 'rb_css file loaded');
equal($('.sass').css('color'), 'rgb(0, 0, 1)', 'sass file loaded');
equal($('.scss').css('color'), 'rgb(0, 0, 1)', 'scss file loaded');
equal($('.rb_css').css('color'), 'rgb(0, 0, 1)', 'rb_css file loaded');
start();
});
});
Expand Down
1 change: 0 additions & 1 deletion test/support_filetype (copy)/coffee.coffee

This file was deleted.

1 change: 0 additions & 1 deletion test/support_filetype (copy)/css.css

This file was deleted.

1 change: 0 additions & 1 deletion test/support_filetype (copy)/js.js

This file was deleted.

1 change: 0 additions & 1 deletion test/support_filetype (copy)/rb_coffee.rb

This file was deleted.

1 change: 0 additions & 1 deletion test/support_filetype (copy)/rb_css.rb

This file was deleted.

1 change: 0 additions & 1 deletion test/support_filetype (copy)/rb_js.rb

This file was deleted.

1 change: 0 additions & 1 deletion test/support_filetype (copy)/rb_sass.rb

This file was deleted.

1 change: 0 additions & 1 deletion test/support_filetype (copy)/rb_scss.rb

This file was deleted.

2 changes: 0 additions & 2 deletions test/support_filetype (copy)/sass.sass

This file was deleted.

3 changes: 0 additions & 3 deletions test/support_filetype (copy)/scss.scss

This file was deleted.

2 changes: 1 addition & 1 deletion test/support_filetype/rb_css.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ :css => '.rb_css{ color: rgb(0,0,4) !important; }' }
{ :css => '.rb_css{ color: rgb(0,0,1) !important; }' }
2 changes: 1 addition & 1 deletion test/support_filetype/rb_sass.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ :sass => ".rb_sass\n color: rgb(0,0,4) !important" }
{ :sass => ".rb_sass\n color: rgb(0,0,1) !important" }
2 changes: 1 addition & 1 deletion test/support_filetype/rb_scss.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ :scss => '.rb_scss{ color: rgb(0,0,4) !important }' }
{ :scss => '.rb_scss{ color: rgb(0,0,1) !important }' }
2 changes: 1 addition & 1 deletion test/support_filetype/sass.sass
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.sass
color: rgb(0,0,4) !important
color: rgb(0,0,1) !important
2 changes: 1 addition & 1 deletion test/support_filetype/scss.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.scss {
color: rgb(0,0,4) !important
color: rgb(0,0,1) !important
}

0 comments on commit bc540eb

Please sign in to comment.