Skip to content

Commit

Permalink
merged railsdog-less
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudhead committed Dec 10, 2009
1 parent c48a813 commit e46d98c
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/less.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
LESS_ROOT = File.expand_path(File.dirname(__FILE__))
LESS_PARSER = File.join(LESS_ROOT, 'less', 'engine', 'parser.rb')
LESS_GRAMMAR = File.join(LESS_ROOT, 'less', 'engine', 'grammar')
LESS_SOURCE_PATHS = []

$:.unshift File.dirname(__FILE__)

Expand Down
20 changes: 16 additions & 4 deletions lib/less/engine/grammar/less.tt
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,28 @@ module Less
rule import
ws "@import" S url:(string / url) medias? s ';' ws {
def build env
path = File.join(env.root.file || Dir.pwd, url.value)
path += '.less' unless path =~ /\.(le|c)ss$/
if File.exist? path
standard_path = File.join(env.root.file || Dir.pwd, url.value)

# Compile a list of possible paths for this file
paths = LESS_SOURCE_PATHS.map { |p| File.join(p, url.value) } + [standard_path]
# Standardize and make uniq
paths = paths.map do |p|
p = File.expand_path(p)
p += '.less' unless p =~ /\.(le|c)ss$/
p
end.uniq

# Use the first that exists if any
if path = paths.detect {|p| File.exists?(p)}
puts "importing from path #{path}"
unless env.root.imported.include?(path)
env.root.imported << path
env.rules += Less::Engine.new(File.new(path)).to_tree.rules
end
else
raise ImportError, path
raise ImportError, standard_path
end

end
}
end
Expand Down
8 changes: 8 additions & 0 deletions spec/css/import-with-extra-paths.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#css { color: yellow; }
#import { color: red; }
.mixin {
height: 10px;
color: red;
}
body { font-size: 0.75em; }
h2 { font-size: 2em; }
6 changes: 6 additions & 0 deletions spec/css/import-with-partial-in-extra-path.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.mixin { font-size: 0.75em; }
#import-test {
font-size: 0.75em;
width: 10px;
height: 30%;
}
14 changes: 14 additions & 0 deletions spec/engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ def add a, b
it "should work with import" do
lessify(:import).should == css(:import)
end

it "should work tih import using extra paths" do
lambda {
lessify(:import_with_extra_paths).should == css(:import_with_extra_paths)
}.should raise_error(Less::ImportError)
# finding a partial in another location
LESS_SOURCE_PATHS = ["spec/less/extra_import_path"]
lessify(:import_with_extra_paths).should == css(:import_with_extra_paths)
# overriding a partial in another location so this takes priority over the same named partial in the same directory
puts '-'
puts '-'
puts '-'*100
lessify(:import).should == css(:import_with_partial_in_extra_path)
end

it "should parse a big file"
it "should handle complex color operations"
Expand Down
1 change: 1 addition & 0 deletions spec/less/extra_import_path/extra.less
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body { font-size: 0.75em; }
1 change: 1 addition & 0 deletions spec/less/extra_import_path/import/import-test-a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.mixin { font-size: 0.75em; }
4 changes: 4 additions & 0 deletions spec/less/extra_import_path/import/import-test-a.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@a: 20%;
.mixin {
font-size: 0.75em;
}
4 changes: 4 additions & 0 deletions spec/less/import-with-extra-paths.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@import url("import/import-test-b.less");
@import url("extra.less");

h2 { font-size: 2em; }

0 comments on commit e46d98c

Please sign in to comment.