sinatra-minify 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .DS_Store
2
+ *~
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ Usage
2
+ -----
3
+
4
+ Add these to your `init.rb`;
5
+
6
+ require 'sinatra/minify'
7
+ class Main
8
+ register Sinatra::Minify
9
+ end
10
+
11
+ Add this to your `Rakefile`:
12
+
13
+ load 'vendor/sinatra-minify/lib/tasks.rake'
14
+
15
+ Type `rake minify:build` to build the compressed JS/CSS files.
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |s|
4
+ s.name = "sinatra-minify"
5
+ s.authors = ["sinefunc"]
6
+ s.email = "info@sinefunc.com"
7
+ s.summary = "CSS/JS compressor for Sinatra"
8
+ s.homepage = "http://www.github.com/sinefunc/sinatra-minify"
9
+ s.description = "sinatra-minify is an extension for Sinatra to compress assets."
10
+ s.add_dependency('jsmin', '>= 1.0.1')
11
+ end
12
+ Jeweler::GemcutterTasks.new
13
+ rescue LoadError
14
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
15
+ end
16
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,15 @@
1
+ require 'sinatra/base'
2
+ require File.join(File.dirname(__FILE__), 'minify/builder')
3
+
4
+ module Sinatra
5
+ module Minify
6
+ def self.registered( app )
7
+ app.helpers Helpers
8
+ app.set :js_url, '/js' # => http://site.com/js
9
+ app.set :js_path, '/public/js' # => ~/myproject/public/js
10
+ app.set :css_url, '/css'
11
+ app.set :css_path, '/public/css'
12
+ end
13
+ end
14
+ register Minify
15
+ end
@@ -0,0 +1,212 @@
1
+ module Sinatra
2
+ module Minify
3
+ module Helpers
4
+ def js_assets( set )
5
+ Builder.new(self.class).js_assets set
6
+ end
7
+ def css_assets( set )
8
+ Builder.new(self.class).css_assets set
9
+ end
10
+ end
11
+
12
+ class Builder
13
+ def build
14
+ out = []
15
+ [:js, :css].each do |type|
16
+ assets_config(type).keys.each do |set|
17
+ prefix = type == :js ? settings.js_path : settings.css_path
18
+ path = root_path File.join(prefix, "#{set}.min." + type.to_s)
19
+ File.open(path, 'w') << compress(type, set)
20
+ out << path
21
+ end
22
+ end
23
+ out
24
+ end
25
+
26
+ def initialize( app_class = ::Main )
27
+ @app_class = app_class
28
+ end
29
+
30
+ def settings
31
+ @app_class
32
+ end
33
+
34
+ # Returns the file sets for a given type as defined in the `assets.yml` config file.
35
+ #
36
+ # Params:
37
+ # - `type` (Symbol/string) - Can be either `:javascripts` or `:stylesheets`
38
+ #
39
+ def assets_config(type)
40
+ YAML::load(File.open(root_path "config/assets.yml")) [type.to_s]
41
+ end
42
+
43
+ # Returns HTML code with `<script>` tags to include the scripts in a given `set`.
44
+ #
45
+ # Params:
46
+ # - `set` (String) - The set name, as defined in `config/assets.yml`.
47
+ #
48
+ # Example:
49
+ #
50
+ # <%= js_assets 'base' %>
51
+ #
52
+ def js_assets( set )
53
+ if settings.production?
54
+ "<script src='#{settings.js_url}/#{set}.min.js' type='text/javascript'></script>\n"
55
+ else
56
+ js_assets_all set
57
+ end
58
+ end
59
+
60
+ def js_assets_all( set )
61
+ ret = ''
62
+ assets(:js, set).each do |script|
63
+ ret << "<script src=\"#{script[:url]}\" type=\"text/javascript\"></script>\n"
64
+ end
65
+ ret
66
+ end
67
+
68
+ # Returns HTML code with `<link>` tags to include the stylesheets in a given `set`.
69
+ #
70
+ # Params:
71
+ # - `set` (String) - The set name, as defined in `config/assets.yml`.
72
+ #
73
+ # Example:
74
+ #
75
+ # <%= css_assets 'base' %>
76
+ #
77
+ def css_assets( set )
78
+ if settings.production?
79
+ "<link rel='stylesheet' href='#{settings.css_url}/#{set}.min.css' media='screen' />\n"
80
+ else
81
+ css_assets_all set
82
+ end
83
+ end
84
+
85
+ def css_assets_all(set)
86
+ ret = ''
87
+ (assets_config :css) [set].each do |filename|
88
+ ret << "<link rel='stylesheet' href='#{settings.css_url}/#{filename}' media='screen' />\n"
89
+ end
90
+ ret
91
+ end
92
+
93
+ # Returns the raw consolidated CSS/JS contents of a given type/set
94
+ def combine( type, set )
95
+ assets(type, set).map { |asset| File.open(asset[:path]).read }.join "\n"
96
+ end
97
+
98
+ # Returns compressed code
99
+ def compress( type, set )
100
+ code = combine type, set
101
+ if type == :js
102
+ minify_js code
103
+ elsif type == :css
104
+ minify_css code
105
+ else
106
+ raise Exception.new
107
+ end
108
+ end
109
+
110
+ def minify_css( src )
111
+ src.gsub!(/\s+/, " ")
112
+ src.gsub!(/\/\*(.*?)\*\//, "")
113
+ src.gsub!(/\} /, "}\n")
114
+ src.gsub!(/\n$/, "")
115
+ src.gsub!(/ \{ /, " {")
116
+ src.gsub!(/; \}/, "}")
117
+ src
118
+ end
119
+
120
+ def minify_js( src )
121
+ require 'jsmin'
122
+ JSMin.minify src
123
+ end
124
+
125
+
126
+ # Returns the file path of where assets of a certain type are stored.
127
+ #
128
+ # Params:
129
+ # - `type` (Symbol) - Either `:js` or `:css`.
130
+ #
131
+ # Example:
132
+ # get_path :js
133
+ # # Possible value: "/home/rsc/myproject/public/js"
134
+ #
135
+ def get_path( type )
136
+ if type == :js
137
+ path = settings.js_path
138
+ else
139
+ path = settings.css_path
140
+ end
141
+ root_path(path.split('/').inject([]) { |arr, item| arr << item unless item.empty?; arr })
142
+ end
143
+
144
+ # Returns the URL for a given filename and a type.
145
+ #
146
+ # Params:
147
+ # - `type` (Symbol) - Either `:js` or `:css`.
148
+ #
149
+ # Example:
150
+ # get_url :js, '/path/to/file.js'
151
+ #
152
+ def get_url( type, filename )
153
+ if type == :js
154
+ prefix = settings.js_url
155
+ else
156
+ prefix = settings.css_url
157
+ end
158
+ # Remove the js_path from it (/home/rsc/project/public/js/aa/lol.js => aa/lol.js)
159
+ url = File.join(prefix, filename.split(get_path type).join(''))
160
+
161
+ # Remove duplicate slashes
162
+ url = url.split('/').inject([]) { |arr, item| arr << item unless item.empty?; arr }
163
+ '/' + url.join('/')
164
+ end
165
+
166
+ # Returns a list of assets of a given type for a given set.
167
+ #
168
+ # Params:
169
+ # - `type` (Symbol) - Either `:js` or `:css`.
170
+ # - `set` (String) - The set name, as defined in `config/assets.yml`.
171
+ #
172
+ # Returns:
173
+ # An array of objects.
174
+ #
175
+ # Example:
176
+ #
177
+ # puts assets(:js, 'base').to_json
178
+ # # Possible output:
179
+ # # [ { 'url': '/js/app.js', 'path': '/home/rsc/projects/assets/public/js/app.js' },
180
+ # # { 'url': '/js/main.js', 'path': '/home/rsc/projects/assets/public/js/main.js' },
181
+ # # ...
182
+ # # ]
183
+ #
184
+ # See also:
185
+ # - js_assets
186
+ #
187
+ def assets( type, set )
188
+ # type is either js or css
189
+ specs = (assets_config type) [set]
190
+ path = get_path type
191
+ ret = []
192
+ done = []
193
+ # `specs` will be a list of filespecs. Find all files that
194
+ # match all specs.
195
+ if specs.class == Array
196
+ specs.each do |spec|
197
+ Dir["#{path}/#{spec}"].each do |filename|
198
+ unless done.include? filename
199
+ ret << {
200
+ :url => get_url(type, filename),
201
+ :path => filename
202
+ }
203
+ done << filename
204
+ end
205
+ end
206
+ end
207
+ end
208
+ ret
209
+ end
210
+ end
211
+ end
212
+ end
data/lib/tasks.rake ADDED
@@ -0,0 +1,10 @@
1
+ namespace :minify do
2
+ desc "Builds the minified CSS and JS assets."
3
+ task :build do
4
+ require 'init'
5
+ puts "Building..."
6
+ files = Sinatra::Minify::Builder.new.build
7
+ files.each { |f| puts " * #{File.basename f}" }
8
+ puts "Construction complete!"
9
+ end
10
+ end
@@ -0,0 +1,47 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sinatra-minify}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["sinefunc"]
12
+ s.date = %q{2010-04-22}
13
+ s.description = %q{sinatra-minify is an extension for Sinatra to compress assets.}
14
+ s.email = %q{info@sinefunc.com}
15
+ s.extra_rdoc_files = [
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "README.md",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "lib/sinatra/minify.rb",
24
+ "lib/sinatra/minify/builder.rb",
25
+ "lib/tasks.rake",
26
+ "sinatra-minify.gemspec"
27
+ ]
28
+ s.homepage = %q{http://www.github.com/sinefunc/sinatra-minify}
29
+ s.rdoc_options = ["--charset=UTF-8"]
30
+ s.require_paths = ["lib"]
31
+ s.rubygems_version = %q{1.3.6}
32
+ s.summary = %q{CSS/JS compressor for Sinatra}
33
+
34
+ if s.respond_to? :specification_version then
35
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
36
+ s.specification_version = 3
37
+
38
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
39
+ s.add_runtime_dependency(%q<jsmin>, [">= 1.0.1"])
40
+ else
41
+ s.add_dependency(%q<jsmin>, [">= 1.0.1"])
42
+ end
43
+ else
44
+ s.add_dependency(%q<jsmin>, [">= 1.0.1"])
45
+ end
46
+ end
47
+
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-minify
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - sinefunc
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-22 00:00:00 +08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: jsmin
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 0
30
+ - 1
31
+ version: 1.0.1
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: sinatra-minify is an extension for Sinatra to compress assets.
35
+ email: info@sinefunc.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - README.md
42
+ files:
43
+ - .gitignore
44
+ - README.md
45
+ - Rakefile
46
+ - VERSION
47
+ - lib/sinatra/minify.rb
48
+ - lib/sinatra/minify/builder.rb
49
+ - lib/tasks.rake
50
+ - sinatra-minify.gemspec
51
+ has_rdoc: true
52
+ homepage: http://www.github.com/sinefunc/sinatra-minify
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --charset=UTF-8
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.3.6
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: CSS/JS compressor for Sinatra
81
+ test_files: []
82
+