Commit 9180b5c4 by Dean Mao

add callback support and add newlines at end of files

parent c4ce49a4
...@@ -6,13 +6,12 @@ Node bindings to libsass ...@@ -6,13 +6,12 @@ Node bindings to libsass
## Install ## Install
cd libsass && make && cd .. npm install
node-waf configure && node-waf build
## Usage ## Usage
var sass = require('./sass'); var sass = require('./sass');
sass.render('body{background:blue; a{color:black;}}', function(css){ sass.render('body{background:blue; a{color:black;}}', function(err, css){
console.log(css) console.log(css)
}); });
......
#include <v8.h> #include <v8.h>
#include <node.h> #include <node.h>
#include <string>
#include <cstdlib>
#include "libsass/sass_interface.h" #include "libsass/sass_interface.h"
using namespace v8; using namespace v8;
void WorkOnContext(uv_work_t* req) {
sass_context* ctx = static_cast<sass_context*>(req->data);
sass_compile(ctx);
}
void MakeCallback(uv_work_t* req) {
HandleScope scope;
TryCatch try_catch;
sass_context* ctx = static_cast<sass_context*>(req->data);
if (ctx->error_status == 0) {
// if no error, do callback(null, result)
const unsigned argc = 2;
Local<Value> argv[argc] = {
Local<Value>::New(Null()),
Local<Value>::New(String::New(ctx->output_string))
};
ctx->callback->Call(Context::GetCurrent()->Global(), argc, argv);
} else {
// if error, do callback(error)
const unsigned argc = 1;
Local<Value> argv[argc] = {
Local<Value>::New(String::New(ctx->error_message))
};
ctx->callback->Call(Context::GetCurrent()->Global(), argc, argv);
}
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
}
Handle<Value> Render(const Arguments& args) { Handle<Value> Render(const Arguments& args) {
HandleScope scope; HandleScope scope;
struct sass_context* ctx = sass_new_context(); sass_context* ctx = sass_new_context();
String::AsciiValue astr(args[0]); String::AsciiValue astr(args[0]);
char * cs = *astr; Local<Function> callback = Local<Function>::Cast(args[1]);
ctx->source_string = cs; ctx->source_string = new char[strlen(*astr)+1];
strcpy(ctx->source_string, *astr);
ctx->options.include_paths = 0; ctx->options.include_paths = 0;
ctx->options.output_style = SASS_STYLE_NESTED; ctx->options.output_style = SASS_STYLE_NESTED;
ctx->callback = Persistent<Function>::New(callback);
ctx->request.data = ctx;
sass_compile(ctx); int status = uv_queue_work(uv_default_loop(), &ctx->request, WorkOnContext, MakeCallback);
assert(status == 0);
return scope.Close(String::New(ctx->output_string)); return Undefined();
} }
void RegisterModule(v8::Handle<v8::Object> target) { void RegisterModule(v8::Handle<v8::Object> target) {
......
...@@ -99,8 +99,8 @@ module.exports = function(options){ ...@@ -99,8 +99,8 @@ module.exports = function(options){
var style = options.compile(); var style = options.compile();
var paths = []; var paths = [];
delete imports[sassPath]; delete imports[sassPath];
style.render(str, function(css){ style.render(str, function(err, css){
// if (err) return next(err); if (err) return next(err);
if (debug) log('render', sassPath); if (debug) log('render', sassPath);
imports[sassPath] = paths; imports[sassPath] = paths;
mkdirp(dirname(cssPath), 0700, function(err){ mkdirp(dirname(cssPath), 0700, function(err){
......
...@@ -124,3 +124,4 @@ namespace Sass { ...@@ -124,3 +124,4 @@ namespace Sass {
} }
} }
...@@ -63,3 +63,4 @@ namespace Sass { ...@@ -63,3 +63,4 @@ namespace Sass {
}; };
} }
...@@ -121,3 +121,4 @@ namespace Sass { ...@@ -121,3 +121,4 @@ namespace Sass {
return retval; return retval;
} }
} }
...@@ -173,3 +173,4 @@ namespace Sass { ...@@ -173,3 +173,4 @@ namespace Sass {
}; };
} }
...@@ -976,3 +976,4 @@ namespace Sass { ...@@ -976,3 +976,4 @@ namespace Sass {
} }
} }
...@@ -15,3 +15,4 @@ namespace Sass { ...@@ -15,3 +15,4 @@ namespace Sass {
}; };
} }
...@@ -855,3 +855,4 @@ namespace Sass { ...@@ -855,3 +855,4 @@ namespace Sass {
{ return selector_but(sel, new_Node, 0, 1); } { return selector_but(sel, new_Node, 0, 1); }
} }
...@@ -29,3 +29,4 @@ namespace Sass { ...@@ -29,3 +29,4 @@ namespace Sass {
Node selector_butlast(Node sel, Node_Factory& new_Node); Node selector_butlast(Node sel, Node_Factory& new_Node);
} }
...@@ -648,3 +648,4 @@ namespace Sass { ...@@ -648,3 +648,4 @@ namespace Sass {
} }
} }
...@@ -156,3 +156,4 @@ namespace Sass { ...@@ -156,3 +156,4 @@ namespace Sass {
} }
} }
...@@ -319,3 +319,4 @@ namespace Sass { ...@@ -319,3 +319,4 @@ namespace Sass {
} }
} }
...@@ -390,3 +390,4 @@ namespace Sass { ...@@ -390,3 +390,4 @@ namespace Sass {
inline bool Node::is_null_ptr() const { return !ip_; } inline bool Node::is_null_ptr() const { return !ip_; }
} }
...@@ -406,3 +406,4 @@ namespace Sass { ...@@ -406,3 +406,4 @@ namespace Sass {
void Node::echo(stringstream& buf, size_t depth) { } void Node::echo(stringstream& buf, size_t depth) { }
void Node::emit_expanded_css(stringstream& buf, const string& prefix) { } void Node::emit_expanded_css(stringstream& buf, const string& prefix) { }
} }
...@@ -85,3 +85,4 @@ namespace Sass { ...@@ -85,3 +85,4 @@ namespace Sass {
{ for (size_t i = 0, S = pool_.size(); i < S; ++i) delete pool_[i]; } { for (size_t i = 0, S = pool_.size(); i < S; ++i) delete pool_[i]; }
} }
...@@ -35,3 +35,4 @@ namespace Sass { ...@@ -35,3 +35,4 @@ namespace Sass {
}; };
} }
...@@ -368,3 +368,4 @@ namespace Sass { ...@@ -368,3 +368,4 @@ namespace Sass {
} }
} }
} }
...@@ -436,3 +436,4 @@ namespace Sass { ...@@ -436,3 +436,4 @@ namespace Sass {
} }
} }
...@@ -130,3 +130,4 @@ extern "C" { ...@@ -130,3 +130,4 @@ extern "C" {
} }
} }
#include <node.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
...@@ -18,6 +20,8 @@ struct sass_context { ...@@ -18,6 +20,8 @@ struct sass_context {
struct sass_options options; struct sass_options options;
int error_status; int error_status;
char* error_message; char* error_message;
uv_work_t request;
v8::Persistent<v8::Function> callback;
}; };
struct sass_file_context { struct sass_file_context {
...@@ -51,3 +55,4 @@ int sass_compile_folder (struct sass_folder_context* ctx); ...@@ -51,3 +55,4 @@ int sass_compile_folder (struct sass_folder_context* ctx);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
...@@ -93,3 +93,4 @@ int main() ...@@ -93,3 +93,4 @@ int main()
new_Node.free(); new_Node.free();
return 0; return 0;
} }
var binding = require('./build/Release/binding') var binding = require('./build/Release/binding')
var render = function(str, cb){ exports.render = binding.render
cb(binding.render(str))
}
exports.render = render
exports.middleware = require('./lib/middleware'); exports.middleware = require('./lib/middleware');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment