|
1 |
| -## Example main.c |
| 1 | +## Example for `data_context` |
2 | 2 |
|
3 |
| -```C |
| 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 somwhere 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 |
4 | 53 | #include <stdio.h>
|
5 | 54 | #include "sass/context.h"
|
6 | 55 |
|
@@ -34,11 +83,11 @@ int main( int argc, const char* argv[] )
|
34 | 83 | }
|
35 | 84 | ```
|
36 | 85 |
|
37 |
| -### Compile main.c |
| 86 | +### Compile file.c |
38 | 87 |
|
39 | 88 | ```bash
|
40 |
| -gcc -c main.c -o main.o |
41 |
| -gcc -o sample main.o -lsass |
| 89 | +gcc -c file.c -o file.o |
| 90 | +gcc -o sample file.o -lsass |
42 | 91 | echo "foo { margin: 21px * 2; }" > foo.scss
|
43 | 92 | ./sample foo.scss => "foo { margin: 42px }"
|
44 | 93 | ```
|
|
0 commit comments