Skip to content
This repository was archived by the owner on Mar 1, 2022. It is now read-only.

Commit 226a081

Browse files
committed
Adds support for LESS css
This support behaves very similar to the SASS support. It will attempt to compile the css prior to checking for redundancies. For c-versions of ruby, therubyracer is required. For jruby, therubyrhino. More information can be found at http://lesscss.org/ refs: zmoazeni#52, zmoazeni#10
1 parent d558685 commit 226a081

File tree

6 files changed

+78
-24
lines changed

6 files changed

+78
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* 0 and 0px are now reconciled as redundancies
44
* Disables color support by default for windows & ruby < 2.0
55
* Fixes bug where unquoted url(data...) isn't parsed correctly
6+
* Adds support for LESS files
67

78
## 1.1.0 - 4/12/2013 ##
89

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
* Martin Kuckert @MKuckert
44
* Ivan Lazarevic @kopipejst
55
* Matt DuVall @mduvall twitter:@mduvall_
6+
* Mekka Okereke @mekka @mekkaokereke

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ gemspec
66
# optional runtime dependencies
77
gem "sass"
88
gem "compass"
9+
gem "less"
10+
gem "therubyracer", :platform => :mri
911

1012
gem "rake", :require => false
1113
gem "debugger"

Gemfile.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ GEM
1212
chunky_png (1.2.7)
1313
colorize (0.5.8)
1414
columnize (0.3.6)
15+
commonjs (0.2.6)
1516
compass (0.12.2)
1617
chunky_png (~> 1.2)
1718
fssm (>= 0.2.7)
@@ -23,6 +24,9 @@ GEM
2324
debugger-linecache (1.2.0)
2425
debugger-ruby_core_source (1.2.0)
2526
fssm (0.2.10)
27+
less (2.3.1)
28+
commonjs (~> 0.2.6)
29+
libv8 (3.11.8.17)
2630
m (1.3.1)
2731
method_source (>= 0.6.7)
2832
rake (>= 0.9.2.2)
@@ -32,8 +36,12 @@ GEM
3236
parslet (1.5.0)
3337
blankslate (~> 2.0)
3438
rake (10.0.3)
39+
ref (1.0.4)
3540
ruby-prof (0.13.0)
3641
sass (3.2.7)
42+
therubyracer (0.11.4)
43+
libv8 (~> 3.11.8.12)
44+
ref
3745

3846
PLATFORMS
3947
ruby
@@ -42,9 +50,11 @@ DEPENDENCIES
4250
compass
4351
csscss!
4452
debugger
53+
less
4554
m
4655
minitest
4756
minitest-rg
4857
rake
4958
ruby-prof
5059
sass
60+
therubyracer

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,20 @@ rulesets that have fewer matches.
4545

4646
$ csscss -n 10 -v path/to/style.css # ignores rulesets with < 10 matches
4747

48-
If you prefer writing in sass, you can also parse your sass/scss files.
48+
If you prefer writing in [sass](http://sass-lang.com/), you can also parse your sass/scss files.
4949

5050
$ gem install sass
5151
$ csscss path/to/style.scss
5252

53+
If you prefer writing in [LESS](http://lesscss.org/), you can also parse your LESS files.
54+
55+
$ gem install less
56+
$ csscss path/to/style.less
57+
58+
LESS requires an additional javascript runtime.
59+
[v8/therubyracer](https://rubygems.org/gems/therubyracer) on most
60+
rubies, and [therubyrhino](https://rubygems.org/gems/therubyrhino) on
61+
jruby.
5362

5463
## I found bugs ##
5564

lib/csscss/cli.rb

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,30 @@ def initialize(argv)
88
@compass = false
99
@ignored_properties = []
1010
@ignored_selectors = []
11-
@match_shorthand = true
11+
@match_shorthand = true
1212
end
1313

1414
def run
1515
parse(@argv)
1616
execute
1717
end
1818

19+
private
1920
def execute
2021
warn_old_debug_flag if ENV["CSSCSS_DEBUG"]
2122

22-
all_contents = @argv.map do |filename|
23-
if %w(.scss .sass).include?(File.extname(filename).downcase) && !(filename =~ URI.regexp)
24-
begin
25-
require "sass"
26-
rescue LoadError
27-
abort "Must install sass gem before parsing sass/scss files"
28-
end
29-
30-
sass_options = {cache:false}
31-
sass_options[:load_paths] = Compass.configuration.sass_load_paths if @compass
32-
begin
33-
Sass::Engine.for_file(filename, sass_options).render
34-
rescue Sass::SyntaxError => e
35-
if e.message =~ /compass/ && !@compass
36-
puts "Enable --compass option to use compass's extensions"
37-
exit 1
38-
else
39-
raise e
40-
end
41-
end
23+
all_contents= @argv.map do |filename|
24+
if filename =~ URI.regexp
25+
load_css_file(filename)
4226
else
43-
open(filename) {|f| f.read }
27+
case File.extname(filename).downcase
28+
when ".scss", ".sass"
29+
load_sass_file(filename)
30+
when ".less"
31+
load_less_file(filename)
32+
else
33+
load_css_file(filename)
34+
end
4435
end
4536
end.join("\n")
4637

@@ -133,7 +124,6 @@ def parse(argv)
133124
print_help(opts)
134125
end
135126

136-
private
137127
def print_help(opts)
138128
puts opts
139129
exit
@@ -159,6 +149,47 @@ def windows_1_9
159149
RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ && RUBY_VERSION =~ /^1\.9/
160150
end
161151

152+
def gem_installed?(gem_name)
153+
begin
154+
require gem_name
155+
true
156+
rescue LoadError
157+
false
158+
end
159+
end
160+
161+
def load_sass_file(filename)
162+
if !gem_installed?("sass") then
163+
abort 'Must install the "sass" gem before parsing sass/scss files'
164+
end
165+
166+
sass_options = {cache:false}
167+
sass_options[:load_paths] = Compass.configuration.sass_load_paths if @compass
168+
begin
169+
Sass::Engine.for_file(filename, sass_options).render
170+
rescue Sass::SyntaxError => e
171+
if e.message =~ /compass/ && !@compass
172+
puts "Enable --compass option to use compass's extensions"
173+
exit 1
174+
else
175+
raise e
176+
end
177+
end
178+
end
179+
180+
def load_less_file(filename)
181+
if !gem_installed?("less") then
182+
abort 'Must install the "less" gem before parsing less files'
183+
end
184+
185+
contents = load_css_file(filename)
186+
Less::Parser.new.parse(contents).to_css
187+
end
188+
189+
def load_css_file(filename)
190+
open(filename) {|f| f.read }
191+
end
192+
162193
class << self
163194
def run(argv)
164195
new(argv).run

0 commit comments

Comments
 (0)