|
Packit Service |
7770af |
## Example main.c
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
```C
|
|
Packit Service |
7770af |
#include <stdio.h>
|
|
Packit Service |
7770af |
#include <stdint.h>
|
|
Packit Service |
7770af |
#include "sass/context.h"
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
union Sass_Value* call_fn_foo(const union Sass_Value* s_args, Sass_Function_Entry cb, struct Sass_Compiler* comp)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
// get context/option struct associated with this compiler
|
|
Packit Service |
7770af |
struct Sass_Context* ctx = sass_compiler_get_context(comp);
|
|
Packit Service |
7770af |
struct Sass_Options* opts = sass_compiler_get_options(comp);
|
|
Packit Service |
7770af |
// get information about previous importer entry from the stack
|
|
Packit Service |
7770af |
Sass_Import_Entry import = sass_compiler_get_last_import(comp);
|
|
Packit Service |
7770af |
const char* prev_abs_path = sass_import_get_abs_path(import);
|
|
Packit Service |
7770af |
const char* prev_imp_path = sass_import_get_imp_path(import);
|
|
Packit Service |
7770af |
// get the cookie from function descriptor
|
|
Packit Service |
7770af |
void* cookie = sass_function_get_cookie(cb);
|
|
Packit Service |
7770af |
// we actually abuse the void* to store an "int"
|
|
Packit Service |
7770af |
return sass_make_number((intptr_t)cookie, "px");
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
int main( int argc, const char* argv[] )
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
// get the input file from first argument or use default
|
|
Packit Service |
7770af |
const char* input = argc > 1 ? argv[1] : "styles.scss";
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
// create the file context and get all related structs
|
|
Packit Service |
7770af |
struct Sass_File_Context* file_ctx = sass_make_file_context(input);
|
|
Packit Service |
7770af |
struct Sass_Context* ctx = sass_file_context_get_context(file_ctx);
|
|
Packit Service |
7770af |
struct Sass_Options* ctx_opt = sass_context_get_options(ctx);
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
// allocate a custom function caller
|
|
Packit Service |
7770af |
Sass_Function_Entry fn_foo =
|
|
Packit Service |
7770af |
sass_make_function("foo()", call_fn_foo, (void*)42);
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
// create list of all custom functions
|
|
Packit Service |
7770af |
Sass_Function_List fn_list = sass_make_function_list(1);
|
|
Packit Service |
7770af |
sass_function_set_list_entry(fn_list, 0, fn_foo);
|
|
Packit Service |
7770af |
sass_option_set_c_functions(ctx_opt, fn_list);
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
// context is set up, call the compile step now
|
|
Packit Service |
7770af |
int status = sass_compile_file_context(file_ctx);
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
// print the result or the error to the stdout
|
|
Packit Service |
7770af |
if (status == 0) puts(sass_context_get_output_string(ctx));
|
|
Packit Service |
7770af |
else puts(sass_context_get_error_message(ctx));
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
// release allocated memory
|
|
Packit Service |
7770af |
sass_delete_file_context(file_ctx);
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
// exit status
|
|
Packit Service |
7770af |
return status;
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
```
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
### Compile main.c
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
```bash
|
|
Packit Service |
7770af |
gcc -c main.c -o main.o
|
|
Packit Service |
7770af |
gcc -o sample main.o -lsass
|
|
Packit Service |
7770af |
echo "foo { margin: foo(); }" > foo.scss
|
|
Packit Service |
7770af |
./sample foo.scss => "foo { margin: 42px }"
|
|
Packit Service |
7770af |
```
|
|
Packit Service |
7770af |
|