compressible 0.0.2.6 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rake/gempackagetask'
5
5
  # http://docs.rubygems.org/read/chapter/20
6
6
  spec = Gem::Specification.new do |s|
7
7
  s.name = "compressible"
8
- s.version = "0.0.2.6"
8
+ s.version = "0.0.3"
9
9
  s.author = "Lance Pollard"
10
10
  s.summary = "Compressible: Quick asset compression for Ruby - Perfect for Heroku"
11
11
  s.homepage = "http://github.com/viatropos/compressible"
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
3
  require 'yui/compressor'
4
-
4
+ raise 'stop'
5
5
  namespace :compress do
6
6
 
7
7
  desc "Compress JS"
@@ -1,6 +1,8 @@
1
1
  require 'rubygems'
2
2
  require 'yaml'
3
+ require 'open-uri'
3
4
  require 'active_support'
5
+ require 'active_support/core_ext'
4
6
 
5
7
  this = File.dirname(__FILE__)
6
8
  require File.join(this, "ext.rb")
@@ -7,39 +7,39 @@ module Compressible
7
7
 
8
8
  module ClassMethods
9
9
 
10
- def javascripts(hash)
10
+ def javascripts(hash, &block)
11
11
  hash.each do |to, paths|
12
12
  paths << {:to => to}
13
- javascript(*paths)
13
+ javascript(*paths, &block)
14
14
  end
15
15
  end
16
16
  alias_method :add_javascripts, :javascripts
17
17
 
18
- def stylesheets(hash)
18
+ def stylesheets(hash, &block)
19
19
  hash.each do |to, paths|
20
20
  paths << {:to => to}
21
- stylesheet(*paths)
21
+ stylesheet(*paths, &block)
22
22
  end
23
23
  end
24
24
  alias_method :add_stylesheets, :stylesheets
25
25
 
26
- def javascript(*args)
27
- paths = args.dup
26
+ def javascript(*args, &block)
27
+ paths = args.dup.flatten
28
28
  options = paths.extract_options!
29
29
  to = asset_name(options[:to])
30
- add_to_config(:js, to, paths.flatten)
31
- write_javascript(*args) unless config[:read_only] == true
30
+ add_to_config(:js, to, paths)
31
+ write_javascript(*args, &block) unless config[:read_only] == true
32
32
  to
33
33
  end
34
34
  alias_method :add_javascript, :javascript
35
35
  alias_method :js, :javascript
36
36
 
37
- def stylesheet(*args)
38
- paths = args.dup
37
+ def stylesheet(*args, &block)
38
+ paths = args.dup.flatten
39
39
  options = paths.extract_options!
40
40
  to = asset_name(options[:to])
41
- add_to_config(:css, to, paths.flatten)
42
- write_stylesheet(*args) unless config[:read_only] == true
41
+ add_to_config(:css, to, paths)
42
+ write_stylesheet(*args, &block) unless config[:read_only] == true
43
43
  to
44
44
  end
45
45
  alias_method :add_stylesheet, :stylesheet
@@ -14,6 +14,23 @@ module Compressible
14
14
  Compressible.configure(:read_only => value)
15
15
  end
16
16
 
17
+ def stylesheet_path(value)
18
+ Compressible.configure(:stylesheet_path => value)
19
+ end
20
+
21
+ def javascript_path(value)
22
+ Compressible.configure(:javascript_path => value)
23
+ end
24
+
25
+ # used to customize the output
26
+ def format(context = nil, &block)
27
+ if block_given?
28
+ context ||= @context
29
+ @format ||= {}
30
+ @format[context] = block
31
+ end
32
+ end
33
+
17
34
  def stylesheets(&block)
18
35
  @context = "stylesheet"
19
36
  instance_eval(&block) if block_given?
@@ -28,10 +45,20 @@ module Compressible
28
45
 
29
46
  def method_missing(meth, *args, &block)
30
47
  if @context
31
- Compressible.send(@context, args, :to => [meth])
48
+ Compressible.send(@context, args, :to => [meth]) do |name, output|
49
+ modify(name, output)
50
+ end
32
51
  else
33
52
  super(meth, *args, &block)
34
53
  end
35
54
  end
55
+
56
+ protected
57
+ def modify(name, output)
58
+ if @format
59
+ return @format[@context].call(name, output) if @format[@context]
60
+ end
61
+ output
62
+ end
36
63
  end
37
64
  end
@@ -34,7 +34,7 @@ module Compressible
34
34
  :read_only => false
35
35
  }
36
36
  end
37
-
37
+
38
38
  def config
39
39
  @config ||= defaults
40
40
  end
@@ -13,17 +13,17 @@ module Compressible
13
13
  def uncached_javascript_paths(*keys)
14
14
  uncached_paths_for(:js, *keys)
15
15
  end
16
-
16
+
17
17
  def uncached_paths_for(type, *keys)
18
- returning [] do |result|
19
- config[type].each do |item|
20
- keys.each do |key|
21
- result.concat(item[:paths]) if item[:to] == key.to_s
22
- end
18
+ result = []
19
+ config[type].each do |item|
20
+ keys.each do |key|
21
+ result.concat(item[:paths]) if item[:to] == key.to_s
23
22
  end
24
23
  end
24
+ result.flatten
25
25
  end
26
-
26
+
27
27
  def stylesheets_for(*keys)
28
28
  assets_for(:stylesheet, *keys)
29
29
  end
@@ -34,12 +34,21 @@ module Compressible
34
34
 
35
35
  def assets_for(type, *keys)
36
36
  options = keys.extract_options!
37
- environment = defined?(Rails) ? Rails.env.to_s : (options[:current] || "production")
38
- environment = environment.to_s
37
+ keys.map!(&:to_s)
38
+ if !options[:current].blank?
39
+ environment = options[:current].to_s
40
+ elsif defined?(::Rails)
41
+ environment = Rails.env.to_s
42
+ elsif defined?(::Sinatra::Application)
43
+ environment = Sinatra::Application.environment.to_s
44
+ else
45
+ environment = "production"
46
+ end
47
+ puts "ENV #{environment}"
39
48
  cache_environments = options[:environments] || "production"
40
49
  cache_environments = [cache_environments] unless cache_environments.is_a?(Array)
41
50
  cache_environments = cache_environments.collect(&:to_s)
42
-
51
+
43
52
  assets = cache_environments.include?(environment) ? keys : send("uncached_#{type.to_s}_paths", *keys)
44
53
  assets
45
54
  end
@@ -60,6 +69,9 @@ module Compressible
60
69
 
61
70
  if config && config[key]
62
71
  path = File.join(config[key], file.to_s)
72
+
73
+ elsif remote?(file)
74
+ path = file.to_s
63
75
  elsif defined?(Rails)
64
76
  path = File.join(Rails.root.to_s, "public/#{type.to_s.pluralize}", file.to_s)
65
77
  else
@@ -86,7 +98,11 @@ module Compressible
86
98
  end
87
99
 
88
100
  def read(type, from)
89
- IO.read(path_for(type, from))
101
+ open(path_for(type, from)).read
102
+ end
103
+
104
+ def remote?(path)
105
+ !!(path =~ /^http(?:s)?/)
90
106
  end
91
107
  end
92
108
 
@@ -25,24 +25,24 @@ module Compressible
25
25
 
26
26
  # figure out how to do alias_method_chain or something
27
27
  # otherwise the modules are tightly coupled
28
- def write_javascript(*args)
29
- paths = args.dup
28
+ def write_javascript(*args, &block)
29
+ paths = args.dup.flatten
30
30
  options = paths.extract_options!
31
31
  options[:to] = asset_name(options[:to])
32
32
  options[:munge] = options.has_key?(:munge) ? options[:munge] : true
33
33
  paths << options
34
- process(:javascript, *paths)
34
+ process(:javascript, *paths, &block)
35
35
  end
36
36
 
37
- def write_stylesheet(*args)
38
- paths = args.dup
37
+ def write_stylesheet(*args, &block)
38
+ paths = args.dup.flatten
39
39
  options = paths.extract_options!
40
40
  options[:to] = asset_name(options[:to])
41
41
  paths << options
42
- process(:stylesheet, *paths)
42
+ process(:stylesheet, *paths, &block)
43
43
  end
44
44
 
45
- def process(type, *paths)
45
+ def process(type, *paths, &block)
46
46
  require 'yui/compressor' unless defined?(::YUI)
47
47
  options = paths.extract_options!
48
48
  to = options[:to]
@@ -51,15 +51,22 @@ module Compressible
51
51
 
52
52
  compressor = compressor_for(type, options)
53
53
 
54
- start_size = size(type, *paths)
54
+ paths = localize(to, type, *paths)
55
55
 
56
- compressed = paths.collect do |path|
57
- puts "Compressing '#{path}'... (#{size(type, path)})"
58
- compressor.compress(read(type, path))
59
- end.join("\n\n")
56
+ start_size = size(type, *paths.map(&:first))
57
+
58
+ compressed = paths.collect do |path, print_path|
59
+ puts "Compressing '#{print_path}'... (#{size(type, path)})"
60
+ result = compressor.compress(read(type, path))
61
+ next if result.blank?
62
+ result = yield(path, result).to_s if block_given?
63
+ result
64
+ end.join("")
60
65
 
61
66
  write(type, to, compressed)
62
67
 
68
+ destroy(*paths)
69
+
63
70
  end_size = size(type, to)
64
71
 
65
72
  puts "Compressed to '#{to.to_s}' (#{end_size} from #{start_size})"
@@ -77,6 +84,73 @@ module Compressible
77
84
  def write(type, to, result)
78
85
  File.open(path_for(type, to), "w") {|f| f.puts result}
79
86
  end
87
+
88
+ def localize(to, type, *paths)
89
+ FileUtils.mkdir_p(to) unless File.exists?(to)
90
+ local_paths = paths.map do |path|
91
+ if remote?(path)
92
+ local = File.join(to, File.basename(path))
93
+ File.open(local, "w+") do |file|
94
+ begin
95
+ file.puts read(type, path)
96
+ rescue Exception => e
97
+ paths.delete(path)
98
+ puts "#{e.message}: #{path}"
99
+ end
100
+ end
101
+ [local, path]
102
+ else
103
+ [path, path]
104
+ end
105
+ end
106
+ end
107
+
108
+ def remote_path(domain, path, asset)
109
+ # full
110
+ if asset =~ /^http(?:s)?:\/\//
111
+ asset
112
+ # absolute
113
+ elsif asset =~ /^\//
114
+ asset = "#{domain}#{asset}"
115
+ # relative
116
+ else
117
+ asset = "#{domain}#{path}/#{asset}"
118
+ end
119
+ end
120
+
121
+ # returns css and javascripts {:js => [], :css => []}
122
+ # requires nokogiri
123
+ def scrape(page)
124
+ require 'nokogiri'
125
+ url = URI.parse(page)
126
+ domain = "#{url.scheme}://#{url.host}"
127
+ domain << ":#{url.port.to_s}"
128
+ path = url.path.squeeze("/")
129
+ html = Nokogiri::HTML(open(page).read)
130
+ scripts = []
131
+
132
+ html.css("script").each do |script|
133
+ next if script["src"].blank?
134
+ scripts << remote_path(domain, path, script["src"])
135
+ end
136
+
137
+ csses = []
138
+
139
+ html.css("link[rel=stylesheet]").each do |css|
140
+ next if css["href"].blank?
141
+ csses << remote_path(domain, path, css["href"])
142
+ end
143
+
144
+ {:js => scripts, :css => csses}
145
+ end
146
+
147
+ def destroy(*paths)
148
+ paths.each do |path, print_path|
149
+ if path != print_path
150
+ File.delete(path) if File.exists?(path)
151
+ end
152
+ end
153
+ end
80
154
  end
81
155
  end
82
156
  end
metadata CHANGED
@@ -1,14 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compressible
3
3
  version: !ruby/object:Gem::Version
4
- hash: 75
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 0
9
- - 2
10
- - 6
11
- version: 0.0.2.6
8
+ - 3
9
+ version: 0.0.3
12
10
  platform: ruby
13
11
  authors:
14
12
  - Lance Pollard
@@ -16,18 +14,16 @@ autorequire:
16
14
  bindir: bin
17
15
  cert_chain: []
18
16
 
19
- date: 2010-07-02 00:00:00 -07:00
17
+ date: 2010-08-24 00:00:00 -05:00
20
18
  default_executable:
21
19
  dependencies:
22
20
  - !ruby/object:Gem::Dependency
23
21
  name: activesupport
24
22
  prerelease: false
25
23
  requirement: &id001 !ruby/object:Gem::Requirement
26
- none: false
27
24
  requirements:
28
25
  - - ">="
29
26
  - !ruby/object:Gem::Version
30
- hash: 9
31
27
  segments:
32
28
  - 2
33
29
  - 3
@@ -39,11 +35,9 @@ dependencies:
39
35
  name: yui-compressor
40
36
  prerelease: false
41
37
  requirement: &id002 !ruby/object:Gem::Requirement
42
- none: false
43
38
  requirements:
44
39
  - - ">="
45
40
  - !ruby/object:Gem::Version
46
- hash: 3
47
41
  segments:
48
42
  - 0
49
43
  version: "0"
@@ -93,27 +87,23 @@ rdoc_options: []
93
87
  require_paths:
94
88
  - lib
95
89
  required_ruby_version: !ruby/object:Gem::Requirement
96
- none: false
97
90
  requirements:
98
91
  - - ">="
99
92
  - !ruby/object:Gem::Version
100
- hash: 3
101
93
  segments:
102
94
  - 0
103
95
  version: "0"
104
96
  required_rubygems_version: !ruby/object:Gem::Requirement
105
- none: false
106
97
  requirements:
107
98
  - - ">="
108
99
  - !ruby/object:Gem::Version
109
- hash: 3
110
100
  segments:
111
101
  - 0
112
102
  version: "0"
113
103
  requirements: []
114
104
 
115
105
  rubyforge_project: compressible
116
- rubygems_version: 1.3.7
106
+ rubygems_version: 1.3.6
117
107
  signing_key:
118
108
  specification_version: 3
119
109
  summary: "Compressible: Quick asset compression for Ruby - Perfect for Heroku"