6 #include "sass/context.h"
8 Sass_Import_List sass_importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp)
10 // get the cookie from importer descriptor
11 void* cookie = sass_importer_get_cookie(cb);
12 Sass_Import_List list = sass_make_import_list(2);
13 char* local = sass_copy_c_string("local { color: green; }");
14 char* remote = sass_copy_c_string("remote { color: red; }");
15 list[0] = sass_make_import_entry("/tmp/styles.scss", local, 0);
16 list[1] = sass_make_import_entry("http://www.example.com", remote, 0);
20 int main( int argc, const char* argv[] )
23 // get the input file from first argument or use default
24 const char* input = argc > 1 ? argv[1] : "styles.scss";
26 // create the file context and get all related structs
27 struct Sass_File_Context* file_ctx = sass_make_file_context(input);
28 struct Sass_Context* ctx = sass_file_context_get_context(file_ctx);
29 struct Sass_Options* ctx_opt = sass_context_get_options(ctx);
31 // allocate custom importer
32 Sass_Importer_Entry c_imp =
33 sass_make_importer(sass_importer, 0, 0);
34 // create list for all custom importers
35 Sass_Importer_List imp_list = sass_make_importer_list(1);
36 // put only the importer on to the list
37 sass_importer_set_list_entry(imp_list, 0, c_imp);
38 // register list on to the context options
39 sass_option_set_c_importers(ctx_opt, imp_list);
40 // context is set up, call the compile step now
41 int status = sass_compile_file_context(file_ctx);
43 // print the result or the error to the stdout
44 if (status == 0) puts(sass_context_get_output_string(ctx));
45 else puts(sass_context_get_error_message(ctx));
47 // release allocated memory
48 sass_delete_file_context(file_ctx);
59 gcc -c importer.c -o importer.o
60 gcc -o importer importer.o -lsass
61 echo "@import 'foobar';" > importer.scss
62 ./importer importer.scss
65 ## Importer Behavior Examples
68 Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
69 // let LibSass handle the import request
73 Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
74 // let LibSass handle the request
75 // swallows »@import "http://…"« pass-through
77 Sass_Import_List list = sass_make_import_list(1);
78 list[0] = sass_make_import_entry(path, 0, 0);
82 Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
83 // return an error to halt execution
84 Sass_Import_List list = sass_make_import_list(1);
85 const char* message = "some error message";
86 list[0] = sass_make_import_entry(path, 0, 0);
87 sass_import_set_error(list[0], sass_copy_c_string(message), 0, 0);
91 Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
92 // let LibSass load the file identifed by the importer
93 Sass_Import_List list = sass_make_import_list(1);
94 list[0] = sass_make_import_entry("/tmp/file.scss", 0, 0);
98 Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
99 // completely hide the import
101 Sass_Import_List list = sass_make_import_list(0);
105 Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
106 // completely hide the import
108 Sass_Import_List list = sass_make_import_list(1);
109 list[0] = sass_make_import_entry(0, 0, 0);