Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Adds support for LESS css
Browse files Browse the repository at this point in the history
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: #52, #10
  • Loading branch information
zmoazeni committed Apr 14, 2013
1 parent d558685 commit 226a081
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* 0 and 0px are now reconciled as redundancies
* Disables color support by default for windows & ruby < 2.0
* Fixes bug where unquoted url(data...) isn't parsed correctly
* Adds support for LESS files

## 1.1.0 - 4/12/2013 ##

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
* Martin Kuckert @MKuckert
* Ivan Lazarevic @kopipejst
* Matt DuVall @mduvall twitter:@mduvall_
* Mekka Okereke @mekka @mekkaokereke
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ gemspec
# optional runtime dependencies
gem "sass"
gem "compass"
gem "less"
gem "therubyracer", :platform => :mri

gem "rake", :require => false
gem "debugger"
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ GEM
chunky_png (1.2.7)
colorize (0.5.8)
columnize (0.3.6)
commonjs (0.2.6)
compass (0.12.2)
chunky_png (~> 1.2)
fssm (>= 0.2.7)
Expand All @@ -23,6 +24,9 @@ GEM
debugger-linecache (1.2.0)
debugger-ruby_core_source (1.2.0)
fssm (0.2.10)
less (2.3.1)
commonjs (~> 0.2.6)
libv8 (3.11.8.17)
m (1.3.1)
method_source (>= 0.6.7)
rake (>= 0.9.2.2)
Expand All @@ -32,8 +36,12 @@ GEM
parslet (1.5.0)
blankslate (~> 2.0)
rake (10.0.3)
ref (1.0.4)
ruby-prof (0.13.0)
sass (3.2.7)
therubyracer (0.11.4)
libv8 (~> 3.11.8.12)
ref

PLATFORMS
ruby
Expand All @@ -42,9 +50,11 @@ DEPENDENCIES
compass
csscss!
debugger
less
m
minitest
minitest-rg
rake
ruby-prof
sass
therubyracer
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,20 @@ rulesets that have fewer matches.

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

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

$ gem install sass
$ csscss path/to/style.scss

If you prefer writing in [LESS](http://lesscss.org/), you can also parse your LESS files.

$ gem install less
$ csscss path/to/style.less

LESS requires an additional javascript runtime.
[v8/therubyracer](https://rubygems.org/gems/therubyracer) on most
rubies, and [therubyrhino](https://rubygems.org/gems/therubyrhino) on
jruby.

## I found bugs ##

Expand Down
77 changes: 54 additions & 23 deletions lib/csscss/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,30 @@ def initialize(argv)
@compass = false
@ignored_properties = []
@ignored_selectors = []
@match_shorthand = true
@match_shorthand = true
end

def run
parse(@argv)
execute
end

private
def execute
warn_old_debug_flag if ENV["CSSCSS_DEBUG"]

all_contents = @argv.map do |filename|
if %w(.scss .sass).include?(File.extname(filename).downcase) && !(filename =~ URI.regexp)
begin
require "sass"
rescue LoadError
abort "Must install sass gem before parsing sass/scss files"
end

sass_options = {cache:false}
sass_options[:load_paths] = Compass.configuration.sass_load_paths if @compass
begin
Sass::Engine.for_file(filename, sass_options).render
rescue Sass::SyntaxError => e
if e.message =~ /compass/ && !@compass
puts "Enable --compass option to use compass's extensions"
exit 1
else
raise e
end
end
all_contents= @argv.map do |filename|
if filename =~ URI.regexp
load_css_file(filename)
else
open(filename) {|f| f.read }
case File.extname(filename).downcase
when ".scss", ".sass"
load_sass_file(filename)
when ".less"
load_less_file(filename)
else
load_css_file(filename)
end
end
end.join("\n")

Expand Down Expand Up @@ -133,7 +124,6 @@ def parse(argv)
print_help(opts)
end

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

def gem_installed?(gem_name)
begin
require gem_name
true
rescue LoadError
false
end
end

def load_sass_file(filename)
if !gem_installed?("sass") then
abort 'Must install the "sass" gem before parsing sass/scss files'
end

sass_options = {cache:false}
sass_options[:load_paths] = Compass.configuration.sass_load_paths if @compass
begin
Sass::Engine.for_file(filename, sass_options).render
rescue Sass::SyntaxError => e
if e.message =~ /compass/ && !@compass
puts "Enable --compass option to use compass's extensions"
exit 1
else
raise e
end
end
end

def load_less_file(filename)
if !gem_installed?("less") then
abort 'Must install the "less" gem before parsing less files'
end

contents = load_css_file(filename)
Less::Parser.new.parse(contents).to_css
end

def load_css_file(filename)
open(filename) {|f| f.read }
end

class << self
def run(argv)
new(argv).run
Expand Down

0 comments on commit 226a081

Please sign in to comment.