rack-less 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +32 -15
- data/lib/rack/less.rb +18 -6
- data/lib/rack/less/options.rb +20 -14
- data/lib/rack/less/request.rb +7 -4
- data/lib/rack/less/response.rb +1 -1
- data/lib/rack/less/source.rb +5 -6
- data/lib/rack/less/version.rb +2 -2
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -16,46 +16,63 @@ or uses Rack::Builder to construct the application pipeline, simply require
|
|
16
16
|
and use as follows:
|
17
17
|
|
18
18
|
require 'rack/less'
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
|
20
|
+
use Rack::Less,
|
21
|
+
:source => 'app/less'
|
22
|
+
:compress => true
|
23
|
+
|
24
24
|
run app
|
25
25
|
|
26
26
|
== Using with Rails
|
27
27
|
|
28
28
|
Add this to your `config/environment.rb`:
|
29
29
|
|
30
|
-
config.middleware.use Rack::Less
|
30
|
+
config.middleware.use "Rack::Less"
|
31
31
|
|
32
32
|
You should now see `Rack::Less` listed in the middleware pipeline:
|
33
33
|
|
34
34
|
rake middleware
|
35
35
|
|
36
36
|
== Available Options
|
37
|
+
[<default_value>]
|
37
38
|
|
38
|
-
* *root*["."]:
|
39
|
-
the app root. the reference point for the source and public options.
|
39
|
+
* *root* ["."]:
|
40
|
+
the app root. the reference point for the source and public options.
|
40
41
|
|
41
|
-
* *source*['app/stylesheets']:
|
42
|
+
* *source* ['app/stylesheets']:
|
42
43
|
the path (relative to the root) where LESS files are located
|
43
44
|
|
44
|
-
* *public*['public']:
|
45
|
+
* *public* ['public']:
|
45
46
|
the path (relative to the root) where LESS files are located
|
46
47
|
|
47
|
-
* *hosted_at*['/stylesheets']:
|
48
|
+
* *hosted_at* ['/stylesheets']:
|
48
49
|
the public HTTP root path for stylesheets
|
49
50
|
|
50
|
-
* *cache*[false]:
|
51
|
+
* *cache* [false]:
|
51
52
|
whether to cache the compilation output to a corresponding file in the hosted_root
|
52
53
|
|
53
|
-
* *compress*[false]:
|
54
|
+
* *compress* [false]:
|
54
55
|
whether to remove extraneous whitespace from compilation output
|
56
|
+
|
57
|
+
== Combinations
|
58
|
+
|
59
|
+
At times, it is useful to combine many stylesheets and serve them as one resource. Rails' has a helper for this:
|
60
|
+
|
61
|
+
stylesheet_link_tag 'one', two', :cache => 'app'
|
62
|
+
|
63
|
+
Rack::Less supports this concept by providing combinations. Combinations are specified using a hash, where the key is the combined resource name and the value is an array of stylesheets to combine as that resource. For example:
|
64
|
+
|
65
|
+
Rack::Less.combinations = {
|
66
|
+
'app' => ['one', 'two']
|
67
|
+
}
|
68
|
+
|
69
|
+
would service the above stylesheet_link_tag. Even better, you could re-factor the stylesheet_link_tag as follows to DRY it up:
|
70
|
+
|
71
|
+
stylesheet_link_tag Rack::Less.combinations['app'], :cache => 'app'
|
55
72
|
|
56
|
-
* *
|
73
|
+
* *combine* [{}]:
|
57
74
|
directives for combining the ouput of one or more LESS compilations, for example:
|
58
|
-
|
75
|
+
{ 'app' => ['one', 'two', 'three'] }
|
59
76
|
will direct Rack::Less to respond to a request for app.css with the concatenated output of compiling one.less, two.less, and three.less
|
60
77
|
|
61
78
|
== Links
|
data/lib/rack/less.rb
CHANGED
@@ -13,15 +13,27 @@ require 'rack/less/source'
|
|
13
13
|
#
|
14
14
|
# Within a rackup file (or with Rack::Builder):
|
15
15
|
# require 'rack/less'
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
16
|
+
#
|
17
|
+
# use Rack::Less,
|
18
|
+
# :source => 'app/less'
|
19
|
+
# :compress => true
|
20
|
+
#
|
21
21
|
# run app
|
22
22
|
|
23
23
|
module Rack::Less
|
24
|
-
|
24
|
+
MIME_TYPE = "text/css"
|
25
|
+
|
26
|
+
class << self
|
27
|
+
|
28
|
+
# Proxy calls to Base.combinations
|
29
|
+
def combinations
|
30
|
+
Rack::Less::Base.combinations
|
31
|
+
end
|
32
|
+
def combinations=(value={})
|
33
|
+
Rack::Less::Base.combinations = value
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
25
37
|
|
26
38
|
# Create a new Rack::Less middleware component
|
27
39
|
# => the +options+ Hash can be used to specify default configuration values
|
data/lib/rack/less/options.rb
CHANGED
@@ -20,16 +20,10 @@ module Rack::Less
|
|
20
20
|
# => compress
|
21
21
|
# whether to remove extraneous whitespace from
|
22
22
|
# compilation output
|
23
|
-
# => combine
|
24
|
-
# expects a hash containing directives for combining
|
25
|
-
# the ouput of one or more LESS compilations, for example:
|
26
|
-
# { 'app' => ['one', 'two', 'three']}
|
27
|
-
# will respond to a request for app.css with the concatenated
|
28
|
-
# output of compiling one.less, two.less, and three.less
|
29
23
|
|
30
|
-
# Note: the following code is
|
24
|
+
# Note: the following code is heavily influenced by:
|
31
25
|
# => http://github.com/rtomayko/rack-cache/blob/master/lib/rack/cache/options.rb
|
32
|
-
# => thanks to rtomayko, I thought
|
26
|
+
# => thanks to rtomayko, I thought his approach was really smart.
|
33
27
|
|
34
28
|
RACK_ENV_NS = "rack-less"
|
35
29
|
|
@@ -42,8 +36,7 @@ module Rack::Less
|
|
42
36
|
option_name(:public) => 'public',
|
43
37
|
option_name(:hosted_at) => '/stylesheets',
|
44
38
|
option_name(:cache) => false,
|
45
|
-
option_name(:compress) => false
|
46
|
-
option_name(:combine) => {}
|
39
|
+
option_name(:compress) => false
|
47
40
|
}
|
48
41
|
end
|
49
42
|
|
@@ -58,6 +51,21 @@ module Rack::Less
|
|
58
51
|
end
|
59
52
|
end
|
60
53
|
|
54
|
+
# Rack::Less uses combinations to combine the output of many stylesheets
|
55
|
+
# and serve them as a single resource. Combinations are specified using
|
56
|
+
# a hash, where the key is the combined resource name and the value is an
|
57
|
+
# array of stylesheets to combine as that resource. For example:
|
58
|
+
# Rack::Less.combinations = {
|
59
|
+
# 'app' => ['one', 'two']
|
60
|
+
# }
|
61
|
+
@@combinations = {}
|
62
|
+
def combinations
|
63
|
+
@@combinations || {}
|
64
|
+
end
|
65
|
+
def combinations=(value={})
|
66
|
+
@@combinations = value
|
67
|
+
end
|
68
|
+
|
61
69
|
end
|
62
70
|
|
63
71
|
module InstanceMethods
|
@@ -92,10 +100,8 @@ module Rack::Less
|
|
92
100
|
# exactly as specified. The +option+ argument may also be a Hash in
|
93
101
|
# which case each key/value pair is merged into the environment as if
|
94
102
|
# the #set method were called on each.
|
95
|
-
def set(option, value=nil
|
96
|
-
if
|
97
|
-
write_option option, block
|
98
|
-
elsif value.nil?
|
103
|
+
def set(option, value=nil)
|
104
|
+
if value.nil?
|
99
105
|
self.options = option.to_hash
|
100
106
|
else
|
101
107
|
write_option option, value
|
data/lib/rack/less/request.rb
CHANGED
@@ -28,7 +28,10 @@ module Rack::Less
|
|
28
28
|
@env['PATH_INFO']
|
29
29
|
end
|
30
30
|
|
31
|
-
|
31
|
+
def http_accept
|
32
|
+
@env['HTTP_ACCEPT']
|
33
|
+
end
|
34
|
+
|
32
35
|
def path_resource_name
|
33
36
|
path_info =~ CSS_PATH_REGEX ? path_info.match(CSS_PATH_REGEX)[1] : nil
|
34
37
|
end
|
@@ -48,15 +51,15 @@ module Rack::Less
|
|
48
51
|
source_opts = {
|
49
52
|
:folder => File.join(options(:root), options(:source)),
|
50
53
|
:cache => cache,
|
51
|
-
:compress => options(:compress)
|
52
|
-
:combine => options(:combine)
|
54
|
+
:compress => options(:compress)
|
53
55
|
}
|
54
56
|
Source.new(path_resource_name, source_opts)
|
55
57
|
end
|
56
58
|
end
|
57
59
|
|
58
60
|
def for_css?
|
59
|
-
|
61
|
+
(http_accept && http_accept.include?(Rack::Less::MIME_TYPE)) ||
|
62
|
+
(media_type && media_type.include?(Rack::Less::MIME_TYPE )) ||
|
60
63
|
CSS_PATH_FORMATS.include?(path_resource_format)
|
61
64
|
end
|
62
65
|
|
data/lib/rack/less/response.rb
CHANGED
@@ -33,7 +33,7 @@ module Rack::Less
|
|
33
33
|
@status = 200 # OK
|
34
34
|
@headers = Rack::Utils::HeaderHash.new
|
35
35
|
|
36
|
-
headers["Content-Type"] = Rack::Less::
|
36
|
+
headers["Content-Type"] = Rack::Less::MIME_TYPE
|
37
37
|
headers["Content-Length"] = self.class.content_length(body).to_s
|
38
38
|
end
|
39
39
|
|
data/lib/rack/less/source.rb
CHANGED
@@ -19,7 +19,6 @@ module Rack::Less
|
|
19
19
|
@compress = options[:compress]
|
20
20
|
@cache = options[:cache]
|
21
21
|
@folder = get_required_path(options, :folder)
|
22
|
-
@combine = options[:combine] || {}
|
23
22
|
end
|
24
23
|
|
25
24
|
def compress?
|
@@ -29,9 +28,9 @@ module Rack::Less
|
|
29
28
|
!@cache.nil?
|
30
29
|
end
|
31
30
|
|
32
|
-
# Use named css sources before using
|
31
|
+
# Use named css sources before using combination sources
|
33
32
|
def files
|
34
|
-
@files ||= (css_sources.empty? ?
|
33
|
+
@files ||= (css_sources.empty? ? combination_sources : css_sources)
|
35
34
|
end
|
36
35
|
|
37
36
|
def compiled
|
@@ -62,9 +61,9 @@ module Rack::Less
|
|
62
61
|
end
|
63
62
|
|
64
63
|
# Preferred, existing source files matching a corresponding
|
65
|
-
#
|
66
|
-
def
|
67
|
-
@
|
64
|
+
# Rack::Less::Base combination directive, if any
|
65
|
+
def combination_sources
|
66
|
+
@combination_sources ||= preferred_sources(Base.combinations[@css_name] || [])
|
68
67
|
end
|
69
68
|
|
70
69
|
private
|
data/lib/rack/less/version.rb
CHANGED