Commit a5792b84 by Aaron Leung

More fine-grained C interface. Reasonably well factored.

parent 14dd0037
...@@ -5,7 +5,40 @@ using std::cerr; using std::endl; ...@@ -5,7 +5,40 @@ using std::cerr; using std::endl;
namespace Sass { namespace Sass {
using std::pair; using std::pair;
Context::Context() void Context::collect_include_paths(const char* paths_str)
{
const size_t wd_len = 1024;
char wd[wd_len];
include_paths.push_back(getcwd(wd, wd_len));
if (*include_paths.back().rbegin() != '/') include_paths.back() += '/';
if (paths_str) {
const char* beg = paths_str;
const char* end = Prelexer::find_first<':'>(beg);
while (end) {
string path(beg, end - beg);
if (!path.empty()) {
if (*path.rbegin() != '/') path += '/';
include_paths.push_back(path);
}
beg = end + 1;
end = Prelexer::find_first<':'>(beg);
}
string path(beg);
if (!path.empty()) {
if (*path.rbegin() != '/') path += '/';
include_paths.push_back(path);
}
}
for (int i = 0; i < include_paths.size(); ++i) {
cerr << include_paths[i] << endl;
}
}
Context::Context(const char* paths_str)
: global_env(Environment()), : global_env(Environment()),
function_env(map<pair<string, size_t>, Function>()), function_env(map<pair<string, size_t>, Function>()),
source_refs(vector<char*>()), source_refs(vector<char*>()),
...@@ -13,6 +46,7 @@ namespace Sass { ...@@ -13,6 +46,7 @@ namespace Sass {
ref_count(0) ref_count(0)
{ {
register_functions(); register_functions();
collect_include_paths(paths_str);
} }
Context::~Context() Context::~Context()
......
...@@ -47,7 +47,8 @@ namespace Sass { ...@@ -47,7 +47,8 @@ namespace Sass {
vector<char*> source_refs; // all the source c-strings vector<char*> source_refs; // all the source c-strings
size_t ref_count; size_t ref_count;
Context(); void collect_include_paths(const char* paths_str);
Context(const char* paths_str = 0);
~Context(); ~Context();
void register_function(Function_Descriptor d, Implementation ip); void register_function(Function_Descriptor d, Implementation ip);
......
...@@ -6,74 +6,64 @@ ...@@ -6,74 +6,64 @@
#include "eval_apply.hpp" #include "eval_apply.hpp"
#include "sass_interface.h" #include "sass_interface.h"
using namespace std; extern "C" {
extern "C" sass_context* sass_new_context() using namespace std;
{ return (sass_context*) malloc(sizeof(sass_context)); }
extern "C" char* sass_compile(sass_context* c_ctx) sass_context* sass_new_context()
{ { return (sass_context*) malloc(sizeof(sass_context)); }
using namespace Sass;
// TO DO: CATCH ALL EXCEPTIONS
Context cpp_ctx;
cpp_ctx.sass_path = string(c_ctx->sass_path ? c_ctx->sass_path : "");
cpp_ctx.css_path = string(c_ctx->css_path ? c_ctx->css_path : "");
const size_t wd_len = 1024; void sass_free_context(sass_context* ctx)
char wd[wd_len]; {
cpp_ctx.include_paths.push_back(getcwd(wd, wd_len)); free(ctx->output_string);
if (*cpp_ctx.include_paths.back().rbegin() != '/') cpp_ctx.include_paths.back() += '/'; free(ctx);
}
if (c_ctx->include_paths) { sass_file_context* sass_new_file_context()
const char* beg = c_ctx->include_paths; { return (sass_file_context*) malloc(sizeof(sass_file_context)); }
const char* end = Prelexer::find_first<':'>(beg);
while (end) { void sass_free_file_context(sass_file_context* ctx)
string path(beg, end - beg); {
if (!path.empty()) { free(ctx->output_string);
if (*path.rbegin() != '/') path += '/'; free(ctx);
cpp_ctx.include_paths.push_back(path);
}
beg = end + 1;
end = Prelexer::find_first<':'>(beg);
} }
string path(beg); sass_folder_context* sass_new_folder_context()
if (!path.empty()) { { return (sass_folder_context*) malloc(sizeof(sass_folder_context)); }
if (*path.rbegin() != '/') path += '/';
cpp_ctx.include_paths.push_back(path); static char* process_document(Sass::Document& doc, int style)
} {
for (int i = 0; i < cpp_ctx.include_paths.size(); ++i) { using namespace Sass;
cerr << cpp_ctx.include_paths[i] << endl;
}
}
Document doc(c_ctx->input_file, c_ctx->input_string, cpp_ctx);
doc.parse_scss(); doc.parse_scss();
eval(doc.root, doc.context.global_env, doc.context.function_env); eval(doc.root, doc.context.global_env, doc.context.function_env);
string output(doc.emit_css(static_cast<Document::CSS_Style>(c_ctx->output_style))); string output(doc.emit_css(static_cast<Document::CSS_Style>(style)));
char* c_output = (char*) malloc(output.size() + 1); char* c_output = (char*) malloc(output.size() + 1);
strcpy(c_output, output.c_str()); strcpy(c_output, output.c_str());
return c_output; return c_output;
} }
// int sass_compile(sass_context* c_ctx)
// This is when you want to compile a whole folder of stuff {
// using namespace Sass;
// var opts = sass_new_context(); // TO DO: CATCH ALL EXCEPTIONS
// opts->sassPath = "/Users/hcatlin/dev/asset/stylesheet"; Context cpp_ctx(c_ctx->options.include_paths);
// opts->cssPath = "/Users/hcatlin/dev/asset/stylesheets/.css";
// opts->includePaths = "/Users/hcatlin/dev/asset/stylesheets:/Users/hcatlin/sasslib"; Document doc(0, c_ctx->input_string, cpp_ctx);
// opts->outputStyle => SASS_STYLE_COMPRESSED; c_ctx->output_string = process_document(doc, c_ctx->options.output_style);
// sass_compile(opts, &callbackfunction); return 0;
// }
//
// This is when you want to compile a string int sass_compile_file(sass_file_context* c_ctx)
// {
// opts = sass_new_context(); using namespace Sass;
// opts->inputString = "a { width: 50px; }"; // TO DO: CATCH ALL EXCEPTIONS
// opts->includePaths = "/Users/hcatlin/dev/asset/stylesheets:/Users/hcatlin/sasslib"; Context cpp_ctx(c_ctx->options.include_paths);
// opts->outputStyle => SASS_STYLE_EXPANDED;
// var cssResult = sass_compile(opts, &callbackfunction); Document doc(c_ctx->input_path, 0, cpp_ctx);
\ No newline at end of file c_ctx->output_string = process_document(doc, c_ctx->options.output_style);
return 0;
}
}
\ No newline at end of file
...@@ -7,18 +7,36 @@ extern "C" { ...@@ -7,18 +7,36 @@ extern "C" {
#define SASS_STYLE_COMPACT 2; #define SASS_STYLE_COMPACT 2;
#define SASS_STYLE_COMPRESSED 3; #define SASS_STYLE_COMPRESSED 3;
struct sass_context { struct sass_options {
char* sass_path; int output_style;
char* css_path;
char* include_paths; char* include_paths;
char* input_file; };
struct sass_context {
char* input_string; char* input_string;
unsigned int output_style; char* output_string;
struct sass_options options;
};
struct sass_folder_context {
char* search_path;
char* output_path;
struct sass_options options;
};
struct sass_file_context {
char* input_path;
char* output_string;
struct sass_options options;
}; };
struct sass_context* sass_new_context(); struct sass_context* sass_new_context ();
struct sass_folder_context* sass_new_folder_context ();
struct sass_file_context* sass_new_file_context ();
char* sass_compile(struct sass_context*); int sass_compile (struct sass_context*);
// int sass_folder_compile (struct sass_folder_context*);
int sass_file_compile (struct sass_file_context*);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -27,17 +27,15 @@ int main(int argc, char** argv) ...@@ -27,17 +27,15 @@ int main(int argc, char** argv)
return 0; return 0;
} }
struct sass_context* ctx = sass_new_context(); struct sass_file_context* ctx = sass_new_file_context();
ctx->sass_path = NULL; ctx->options.include_paths = "::/blah/bloo/fuzz:/slub/flub/chub::/Users/Aaron/dev/libsass/::::/huzz/buzz:::";
ctx->css_path = NULL; ctx->options.output_style = SASS_STYLE_NESTED;
ctx->include_paths = "::/blah/bloo/fuzz:/slub/flub/chub::/Users/Aaron/dev/libsass/::::/huzz/buzz:::"; ctx->input_path = argv[1];
ctx->output_style = SASS_STYLE_NESTED;
ctx->input_file = argv[1];
ctx->input_string = NULL;
char* output = sass_compile(ctx); sass_compile_file(ctx);
printf("%s", output); printf("%s", ctx->output_string);
sass_free_file_context(ctx);
return 0; return 0;
} }
\ No newline at end of file
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