|
Packit |
bfcc33 |
## Example importer.c
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
```C
|
|
Packit |
bfcc33 |
#include <stdio.h>
|
|
Packit |
bfcc33 |
#include <string.h>
|
|
Packit |
bfcc33 |
#include "sass/context.h"
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
Sass_Import_List sass_importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp)
|
|
Packit |
bfcc33 |
{
|
|
Packit |
bfcc33 |
// get the cookie from importer descriptor
|
|
Packit |
bfcc33 |
void* cookie = sass_importer_get_cookie(cb);
|
|
Packit |
bfcc33 |
Sass_Import_List list = sass_make_import_list(2);
|
|
Packit |
bfcc33 |
char* local = sass_copy_c_string("local { color: green; }");
|
|
Packit |
bfcc33 |
char* remote = sass_copy_c_string("remote { color: red; }");
|
|
Packit |
bfcc33 |
list[0] = sass_make_import_entry("/tmp/styles.scss", local, 0);
|
|
Packit |
bfcc33 |
list[1] = sass_make_import_entry("http://www.example.com", remote, 0);
|
|
Packit |
bfcc33 |
return list;
|
|
Packit |
bfcc33 |
}
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
int main( int argc, const char* argv[] )
|
|
Packit |
bfcc33 |
{
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
// get the input file from first argument or use default
|
|
Packit |
bfcc33 |
const char* input = argc > 1 ? argv[1] : "styles.scss";
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
// create the file context and get all related structs
|
|
Packit |
bfcc33 |
struct Sass_File_Context* file_ctx = sass_make_file_context(input);
|
|
Packit |
bfcc33 |
struct Sass_Context* ctx = sass_file_context_get_context(file_ctx);
|
|
Packit |
bfcc33 |
struct Sass_Options* ctx_opt = sass_context_get_options(ctx);
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
// allocate custom importer
|
|
Packit |
bfcc33 |
Sass_Importer_Entry c_imp =
|
|
Packit |
bfcc33 |
sass_make_importer(sass_importer, 0, 0);
|
|
Packit |
bfcc33 |
// create list for all custom importers
|
|
Packit |
bfcc33 |
Sass_Importer_List imp_list = sass_make_importer_list(1);
|
|
Packit |
bfcc33 |
// put only the importer on to the list
|
|
Packit |
bfcc33 |
sass_importer_set_list_entry(imp_list, 0, c_imp);
|
|
Packit |
bfcc33 |
// register list on to the context options
|
|
Packit |
bfcc33 |
sass_option_set_c_importers(ctx_opt, imp_list);
|
|
Packit |
bfcc33 |
// context is set up, call the compile step now
|
|
Packit |
bfcc33 |
int status = sass_compile_file_context(file_ctx);
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
// print the result or the error to the stdout
|
|
Packit |
bfcc33 |
if (status == 0) puts(sass_context_get_output_string(ctx));
|
|
Packit |
bfcc33 |
else puts(sass_context_get_error_message(ctx));
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
// release allocated memory
|
|
Packit |
bfcc33 |
sass_delete_file_context(file_ctx);
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
// exit status
|
|
Packit |
bfcc33 |
return status;
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
}
|
|
Packit |
bfcc33 |
```
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
Compile importer.c
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
```bash
|
|
Packit |
bfcc33 |
gcc -c importer.c -o importer.o
|
|
Packit |
bfcc33 |
gcc -o importer importer.o -lsass
|
|
Packit |
bfcc33 |
echo "@import 'foobar';" > importer.scss
|
|
Packit |
bfcc33 |
./importer importer.scss
|
|
Packit |
bfcc33 |
```
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
## Importer Behavior Examples
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
```C
|
|
Packit |
bfcc33 |
Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
|
|
Packit |
bfcc33 |
// let LibSass handle the import request
|
|
Packit |
bfcc33 |
return NULL;
|
|
Packit |
bfcc33 |
}
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
|
|
Packit |
bfcc33 |
// let LibSass handle the request
|
|
Packit |
bfcc33 |
// swallows »@import "http://…"« pass-through
|
|
Packit |
bfcc33 |
// (arguably a bug)
|
|
Packit |
bfcc33 |
Sass_Import_List list = sass_make_import_list(1);
|
|
Packit |
bfcc33 |
list[0] = sass_make_import_entry(path, 0, 0);
|
|
Packit |
bfcc33 |
return list;
|
|
Packit |
bfcc33 |
}
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
|
|
Packit |
bfcc33 |
// return an error to halt execution
|
|
Packit |
bfcc33 |
Sass_Import_List list = sass_make_import_list(1);
|
|
Packit |
bfcc33 |
const char* message = "some error message";
|
|
Packit |
bfcc33 |
list[0] = sass_make_import_entry(path, 0, 0);
|
|
Packit |
bfcc33 |
sass_import_set_error(list[0], sass_copy_c_string(message), 0, 0);
|
|
Packit |
bfcc33 |
return list;
|
|
Packit |
bfcc33 |
}
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
|
|
Packit |
bfcc33 |
// let LibSass load the file identifed by the importer
|
|
Packit |
bfcc33 |
Sass_Import_List list = sass_make_import_list(1);
|
|
Packit |
bfcc33 |
list[0] = sass_make_import_entry("/tmp/file.scss", 0, 0);
|
|
Packit |
bfcc33 |
return list;
|
|
Packit |
bfcc33 |
}
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
|
|
Packit |
bfcc33 |
// completely hide the import
|
|
Packit |
bfcc33 |
// (arguably a bug)
|
|
Packit |
bfcc33 |
Sass_Import_List list = sass_make_import_list(0);
|
|
Packit |
bfcc33 |
return list;
|
|
Packit |
bfcc33 |
}
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
|
|
Packit |
bfcc33 |
// completely hide the import
|
|
Packit |
bfcc33 |
// (arguably a bug)
|
|
Packit |
bfcc33 |
Sass_Import_List list = sass_make_import_list(1);
|
|
Packit |
bfcc33 |
list[0] = sass_make_import_entry(0, 0, 0);
|
|
Packit |
bfcc33 |
return list;
|
|
Packit |
bfcc33 |
}
|
|
Packit |
bfcc33 |
```
|