rack-lesscss 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,37 @@
1
+ require 'less'
2
+
3
+ module Rack
4
+ class LessCss
5
+
6
+ def initialize(app, opts)
7
+ @app = app
8
+ @less_path = opts[:less_path] or raise ArgumentError, "You must specify less_path (path to directory containing .less files)"
9
+ css_route = opts[:css_route] || "/stylesheets"
10
+ css_route = css_route[0..-2] if css_route[-1] == "/"
11
+ @css_route_regexp = /#{Regexp.escape(css_route)}\/([^\.]+)\.css/
12
+ end
13
+
14
+ def call(env)
15
+ if env['PATH_INFO'] =~ @css_route_regexp
16
+ begin
17
+ headers = { 'Content-Type' => 'text/css', 'Cache-Control' => 'private' }
18
+ body = "/* Generated from #{$1}.less by Rack::LessCss middleware */\n\n"
19
+ body << Less::Engine.new(get_source($1)).to_css
20
+ headers["Content-Length"] = body.size.to_s
21
+ return [200, headers, [body]]
22
+ rescue SyntaxError, StandardError => e
23
+ puts "Rack::LessCss Error: #{e.message}"
24
+ end
25
+ end
26
+ @app.call(env)
27
+ end
28
+
29
+ private
30
+ def get_source(stylesheet)
31
+ ::File.read(::File.join(@less_path, stylesheet + ".less"))
32
+ rescue
33
+ nil
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'rack/builder'
4
+ require 'rack/mock'
5
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'rack-lesscss.rb')
6
+
7
+ class Rack::LessCss
8
+ private
9
+ def get_source(stylesheet)
10
+ <<EOF
11
+ #body.#{stylesheet} {
12
+ color: black;
13
+ p {
14
+ margin-bottom: 10px;
15
+ span {
16
+ font-weight: bold;
17
+ }
18
+ }
19
+ }
20
+ EOF
21
+ end
22
+ end
23
+
24
+ describe "Rack::LessCss" do
25
+ it "should raise if less_path not given" do
26
+ app = Rack::Builder.new do
27
+ use Rack::LessCss
28
+ run lambda { |env| [404, { 'Content-Type' => 'text/plain' }, ["Not found"]] }
29
+ end
30
+ lambda do
31
+ Rack::MockRequest.new(app).get('/stylesheets/jola.css')
32
+ end.should raise_error(ArgumentError)
33
+ end
34
+
35
+ it "should successfully convert less to css (with default css route)" do
36
+ app = Rack::Builder.new do
37
+ use Rack::LessCss, :less_path => "/some/path/to/less/files"
38
+ run lambda { |env| [404, { 'Content-Type' => 'text/plain' }, ["Not found"]] }
39
+ end
40
+ response = Rack::MockRequest.new(app).get('/stylesheets/jola.css')
41
+ response.status.should == 200
42
+ response.body.should include("#body.jola")
43
+ end
44
+
45
+ it "should successfully convert less to css (with custom css route)" do
46
+ app = Rack::Builder.new do
47
+ use Rack::LessCss, :less_path => "/some/path/to/less/files", :css_route => "/css"
48
+ run lambda { |env| [404, { 'Content-Type' => 'text/plain' }, ["Not found"]] }
49
+ end
50
+ response = Rack::MockRequest.new(app).get('/css/misio.css')
51
+ response.status.should == 200
52
+ response.body.should include("#body.misio")
53
+ end
54
+
55
+ it "should not intercept request for non-matching request path" do
56
+ app = Rack::Builder.new do
57
+ use Rack::LessCss, :less_path => "/some/path/to/less/files", :css_route => "/css"
58
+ run lambda { |env| [404, { 'Content-Type' => 'text/plain' }, ["Not found"]] }
59
+ end
60
+ response = Rack::MockRequest.new(app).get('/stylesheets/jola.css')
61
+ response.status.should == 404
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-lesscss
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.2"
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Kulik
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-18 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: marcin.kulik@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/rack-lesscss.rb
26
+ - spec/rack-lesscss-spec.rb
27
+ has_rdoc: true
28
+ homepage: http://sickill.net
29
+ licenses: []
30
+
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.3.3
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Rack middleware for compiling lesscss files into css
55
+ test_files: []
56
+