sickill-rack-lesscss 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,35 @@
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
+ end
12
+
13
+ def call(env)
14
+ if env['PATH_INFO'] =~ /#{Regexp.escape(@css_route)}\/([^\.]+)\.css/
15
+ begin
16
+ headers = { 'Content-Type' => 'text/css', 'Cache-Control' => 'private' }
17
+ body = "/* Generated from #{$1}.less by Rack::LessCss middleware */\n\n"
18
+ body << Less::Engine.new(get_source($1)).to_css
19
+ return [200, headers, body]
20
+ rescue SyntaxError, StandardError => e
21
+ puts "Rack::LessCss Error: #{e.message}"
22
+ end
23
+ end
24
+ @app.call(env)
25
+ end
26
+
27
+ private
28
+ def get_source(stylesheet)
29
+ ::File.read(::File.join(@less_path, stylesheet + ".less"))
30
+ rescue
31
+ nil
32
+ end
33
+
34
+ end
35
+ 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,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sickill-rack-lesscss
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Kulik
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-25 00:00:00 -07: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: false
28
+ homepage: http://sickill.net
29
+ post_install_message:
30
+ rdoc_options: []
31
+
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: "0"
39
+ version:
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ requirements: []
47
+
48
+ rubyforge_project:
49
+ rubygems_version: 1.2.0
50
+ signing_key:
51
+ specification_version: 2
52
+ summary: Rack middleware for compiling lesscss files into css
53
+ test_files: []
54
+