1## Example importer.c 2 3```C 4#include <stdio.h> 5#include <string.h> 6#include "sass/context.h" 7 8Sass_Import_List sass_importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) 9{ 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); 17 return list; 18} 19 20int main( int argc, const char* argv[] ) 21{ 22 23 // get the input file from first argument or use default 24 const char* input = argc > 1 ? argv[1] : "styles.scss"; 25 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); 30 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); 42 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)); 46 47 // release allocated memory 48 sass_delete_file_context(file_ctx); 49 50 // exit status 51 return status; 52 53} 54``` 55 56Compile importer.c 57 58```bash 59gcc -c importer.c -o importer.o 60gcc -o importer importer.o -lsass 61echo "@import 'foobar';" > importer.scss 62./importer importer.scss 63``` 64 65## Importer Behavior Examples 66 67```C 68Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) { 69 // let LibSass handle the import request 70 return NULL; 71} 72 73Sass_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 76 // (arguably a bug) 77 Sass_Import_List list = sass_make_import_list(1); 78 list[0] = sass_make_import_entry(path, 0, 0); 79 return list; 80} 81 82Sass_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); 88 return list; 89} 90 91Sass_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); 95 return list; 96} 97 98Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) { 99 // completely hide the import 100 // (arguably a bug) 101 Sass_Import_List list = sass_make_import_list(0); 102 return list; 103} 104 105Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) { 106 // completely hide the import 107 // (arguably a bug) 108 Sass_Import_List list = sass_make_import_list(1); 109 list[0] = sass_make_import_entry(0, 0, 0); 110 return list; 111} 112``` 113