Commit b5450464 by Aaron Leung

C lib interface + necessary amendments.

parent 6980ee00
......@@ -9,6 +9,7 @@ namespace Sass {
: global_env(Environment()),
function_env(map<pair<string, size_t>, Function>()),
source_refs(vector<char*>()),
include_paths(vector<string>()),
ref_count(0)
{
register_functions();
......
......@@ -39,9 +39,12 @@ namespace Sass {
};
struct Context {
string sass_path;
string css_path;
vector<string> include_paths;
Environment global_env;
map<pair<string, size_t>, Function> function_env;
vector<char*> source_refs;
vector<char*> source_refs; // all the source c-strings
size_t ref_count;
Context();
......
#include <cstdio>
#include <cstring>
#include "document.hpp"
#include "eval_apply.hpp"
#include <iostream>
namespace Sass {
Document::Document(char* path_str, char* source_str, Context& ctx)
: path(string()),
source(source_str),
line_number(1),
context(ctx),
root(Node(Node::root, 1)),
lexed(Token::make())
{
if (source_str) {
own_source = false;
position = source;
end = position + std::strlen(source);
}
else if (path_str) {
path = string(path_str);
std::FILE *f;
// TO DO: CHECK f AGAINST NULL/0
f = std::fopen(path.c_str(), "rb");
std::fseek(f, 0, SEEK_END);
int len = std::ftell(f);
std::rewind(f);
// TO DO: WRAP THE new[] IN A TRY/CATCH BLOCK
source = new char[len + 1];
std::fread(source, sizeof(char), len, f);
source[len] = '\0';
end = source + len;
std::fclose(f);
own_source = true;
position = source;
context.source_refs.push_back(source);
}
else {
// report an error
}
++context.ref_count;
}
Document::Document(string path, char* source)
: path(path), source(source),
line_number(1), own_source(false),
......
......@@ -27,6 +27,7 @@ namespace Sass {
Node root;
Token lexed;
Document(char* path_str, char* source_str, Context& ctx);
Document(string path, char* source = 0);
Document(string path, Context& context);
Document(size_t line_number, Token t, Context& context);
......
/*
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <unistd.h>
#include "document.hpp"
#include "eval_apply.hpp"
#include "sass_interface.h"
using namespace std;
extern "C" sass_context* sass_new_context()
{ return (sass_context*) malloc(sizeof(sass_context)); }
extern "C" char* sass_compile(sass_context* c_ctx)
{
using namespace Sass;
// TO DO: CATCH ALL EXCEPTIONS
Context cpp_ctx;
cpp_ctx.sass_path = string(c_ctx->sass_path);
cpp_ctx.css_path = string(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 (c_ctx->include_paths) {
const char* beg = c_ctx->include_paths;
const char* end = Prelexer::find_first<':'>(beg);
while (end) {
cpp_ctx.include_paths.push_back(string(beg, end - beg));
beg = end + 1;
end = Prelexer::find_first<':'>(beg);
}
cpp_ctx.include_paths.push_back(beg);
}
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)));
char* c_output = (char*) malloc(output.size() + 1);
strcpy(c_output, output.c_str());
return c_output;
}
//
// 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
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