UNPKG

2.12 kBtext/x-cView Raw
1#include <cstring>
2#include <iostream>
3#include <stdint.h>
4#include <sass.h>
5
6// gcc: g++ -shared plugin.cpp -o plugin.so -fPIC -Llib -lsass
7// mingw: g++ -shared plugin.cpp -o plugin.dll -Llib -lsass
8
9extern "C" const char* ADDCALL libsass_get_version() {
10 return libsass_version();
11}
12
13union Sass_Value* custom_function(const union Sass_Value* s_args, Sass_Function_Entry cb, struct Sass_Compiler* comp)
14{
15 // get context/option struct associated with this compiler
16 struct Sass_Context* ctx = sass_compiler_get_context(comp);
17 struct Sass_Options* opts = sass_compiler_get_options(comp);
18 // get the cookie from function descriptor
19 void* cookie = sass_function_get_cookie(cb);
20 // we actually abuse the void* to store an "int"
21 return sass_make_number((intptr_t)cookie, "px");
22}
23
24extern "C" Sass_Function_List ADDCALL libsass_load_functions()
25{
26 // allocate a custom function caller
27 Sass_Function_Entry c_func =
28 sass_make_function("foo()", custom_function, (void*)42);
29 // create list of all custom functions
30 Sass_Function_List fn_list = sass_make_function_list(1);
31 // put the only function in this plugin to the list
32 sass_function_set_list_entry(fn_list, 0, c_func);
33 // return the list
34 return fn_list;
35}
36
37Sass_Import_List custom_importer(const char* cur_path, Sass_Importer_Entry cb, struct Sass_Compiler* comp)
38{
39 // get the cookie from importer descriptor
40 void* cookie = sass_importer_get_cookie(cb);
41 // create a list to hold our import entries
42 Sass_Import_List incs = sass_make_import_list(1);
43 // create our only import entry (route path back)
44 incs[0] = sass_make_import_entry(cur_path, 0, 0);
45 // return imports
46 return incs;
47}
48
49extern "C" Sass_Importer_List ADDCALL libsass_load_importers()
50{
51 // allocate a custom function caller
52 Sass_Importer_Entry c_imp =
53 sass_make_importer(custom_importer, - 99, (void*)42);
54 // create list of all custom functions
55 Sass_Importer_List imp_list = sass_make_importer_list(1);
56 // put the only function in this plugin to the list
57 sass_importer_set_list_entry(imp_list, 0, c_imp);
58 // return the list
59 return imp_list;
60}