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