Permalink
Browse files

Merge pull request #1 from josh/cargo-cult

Code clean-up.
  • Loading branch information...
2 parents 7efb927 + 3b22cee commit aea48cdda4f4f0ecebcf4c11d1f5771527607e1d @thisduck committed May 21, 2011
Showing with 21 additions and 36 deletions.
  1. +0 −7 README.md
  2. +20 −28 lib/less_js.rb
  3. +1 −1 test/test_less_js.rb
View
@@ -24,13 +24,6 @@ the `less-js-source` gem.
*Note: verion 1.1.1 is tagged as 1.1.1.1 on the `less-js-source` gem.*
-### JSON
-
-The `json` library is also required but is not explicitly stated as a
-gem dependency. If you're on Ruby 1.8 you'll need to install the
-`json` or `json_pure` gem. On Ruby 1.9, `json` is included in the
-standard library.
-
### ExecJS
The [ExecJS](https://github.com/sstephenson/execjs) library is used to automatically choose the best JavaScript engine for your platform. Check out its [README](https://github.com/sstephenson/execjs/blob/master/README.md) for a complete list of supported engines.
View
@@ -2,16 +2,15 @@
require 'less_js/source'
module LessJs
- EngineError = ExecJS::RuntimeError
- CompilationError = ExecJS::ProgramError
+ class ParseError < StandardError; end
module Source
def self.path
@path ||= ENV['LESSJS_SOURCE_PATH'] || bundled_path
end
def self.path=(path)
- @contents = @version = @bare_option = @context = nil
+ @contents = @version = @context = nil
@path = path
end
@@ -20,46 +19,39 @@ def self.contents
end
def self.version
- @version ||= contents[/Less.js Compiler v([\d.]+)/, 1]
+ @version ||= contents[/LESS - Leaner CSS v([\d.]+)/, 1]
end
def self.context
- @context ||= ExecJS.compile(contents)
+ @context ||= ExecJS.compile <<-EOS
+ #{contents}
+
+ function compile(data) {
+ var result;
+ new less.Parser().parse(data, function(error, tree) {
+ result = [error, tree.toCSS()];
+ });
+ return result;
+ }
+ EOS
end
end
class << self
- def engine
- end
-
- def engine=(engine)
- end
-
def version
Source.version
end
- def callback(error, tree)
- puts tree.inspect
- end
-
# Compile a script (String or IO) to CSS.
def compile(script, options = {})
script = script.read if script.respond_to?(:read)
+ error, data = Source.context.call('compile', script)
- code = <<-EOS
-(function(input) {
- var resp = "error";
- new(less.Parser)().parse(input, function(error, tree) {
- resp = [error, tree.toCSS()]
- });
- return resp;
-})
- EOS
-
- (error, response) = Source.context.call(code, script)
- raise CompilationError, error if error
- response
+ if error
+ raise ParseError, error['message']
+ else
+ data
+ end
end
end
end
View
@@ -15,7 +15,7 @@ def test_compile_with_io
end
def test_compilation_error
- assert_raise LessJs::CompilationError do
+ assert_raise LessJs::ParseError do
LessJs.compile("&&&&.a")
end
end

0 comments on commit aea48cd

Please sign in to comment.