| 1 | ## Example for `data_context` |
| 2 | |
| 3 | ```C:data.c |
| 4 | #include <stdio.h> |
| 5 | #include "sass/context.h" |
| 6 | |
| 7 | int main( int argc, const char* argv[] ) |
| 8 | { |
| 9 | |
| 10 | // LibSass will take control of data you pass in |
| 11 | // Therefore we need to make a copy of static data |
| 12 | char* text = sass_copy_c_string("a{b:c;}"); |
| 13 | // Normally you'll load data into a buffer from i.e. the disk. |
| 14 | // Use `sass_alloc_memory` to get a buffer to pass to LibSass |
| 15 | // then fill it with data you load from disk or somewhere else. |
| 16 | |
| 17 | // create the data context and get all related structs |
| 18 | struct Sass_Data_Context* data_ctx = sass_make_data_context(text); |
| 19 | struct Sass_Context* ctx = sass_data_context_get_context(data_ctx); |
| 20 | struct Sass_Options* ctx_opt = sass_context_get_options(ctx); |
| 21 | |
| 22 | // configure some options ... |
| 23 | sass_option_set_precision(ctx_opt, 10); |
| 24 | |
| 25 | // context is set up, call the compile step now |
| 26 | int status = sass_compile_data_context(data_ctx); |
| 27 | |
| 28 | // print the result or the error to the stdout |
| 29 | if (status == 0) puts(sass_context_get_output_string(ctx)); |
| 30 | else puts(sass_context_get_error_message(ctx)); |
| 31 | |
| 32 | // release allocated memory |
| 33 | sass_delete_data_context(data_ctx); |
| 34 | |
| 35 | // exit status |
| 36 | return status; |
| 37 | |
| 38 | } |
| 39 | ``` |
| 40 | |
| 41 | ### Compile data.c |
| 42 | |
| 43 | ```bash |
| 44 | gcc -c data.c -o data.o |
| 45 | gcc -o sample data.o -lsass |
| 46 | echo "foo { margin: 21px * 2; }" > foo.scss |
| 47 | ./sample foo.scss => "foo { margin: 42px }" |
| 48 | ``` |
| 49 | |
| 50 | ## Example for `file_context` |
| 51 | |
| 52 | ```C:file.c |
| 53 | #include <stdio.h> |
| 54 | #include "sass/context.h" |
| 55 | |
| 56 | int main( int argc, const char* argv[] ) |
| 57 | { |
| 58 | |
| 59 | // get the input file from first argument or use default |
| 60 | const char* input = argc > 1 ? argv[1] : "styles.scss"; |
| 61 | |
| 62 | // create the file context and get all related structs |
| 63 | struct Sass_File_Context* file_ctx = sass_make_file_context(input); |
| 64 | struct Sass_Context* ctx = sass_file_context_get_context(file_ctx); |
| 65 | struct Sass_Options* ctx_opt = sass_context_get_options(ctx); |
| 66 | |
| 67 | // configure some options ... |
| 68 | sass_option_set_precision(ctx_opt, 10); |
| 69 | |
| 70 | // context is set up, call the compile step now |
| 71 | int status = sass_compile_file_context(file_ctx); |
| 72 | |
| 73 | // print the result or the error to the stdout |
| 74 | if (status == 0) puts(sass_context_get_output_string(ctx)); |
| 75 | else puts(sass_context_get_error_message(ctx)); |
| 76 | |
| 77 | // release allocated memory |
| 78 | sass_delete_file_context(file_ctx); |
| 79 | |
| 80 | // exit status |
| 81 | return status; |
| 82 | |
| 83 | } |
| 84 | ``` |
| 85 | |
| 86 | ### Compile file.c |
| 87 | |
| 88 | ```bash |
| 89 | gcc -c file.c -o file.o |
| 90 | gcc -o sample file.o -lsass |
| 91 | echo "foo { margin: 21px * 2; }" > foo.scss |
| 92 | ./sample foo.scss => "foo { margin: 42px }" |
| 93 | ``` |
| 94 |