Blame docs/api-context-example.md
|
Packit |
bfcc33 |
## Example main.c
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
```C
|
|
Packit |
bfcc33 |
#include <stdio.h>
|
|
Packit |
bfcc33 |
#include "sass/context.h"
|
|
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 |
// configure some options ...
|
|
Packit |
bfcc33 |
sass_option_set_precision(ctx_opt, 10);
|
|
Packit |
bfcc33 |
|
|
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 main.c
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
```bash
|
|
Packit |
bfcc33 |
gcc -c main.c -o main.o
|
|
Packit |
bfcc33 |
gcc -o sample main.o -lsass
|
|
Packit |
bfcc33 |
echo "foo { margin: 21px * 2; }" > foo.scss
|
|
Packit |
bfcc33 |
./sample foo.scss => "foo { margin: 42px }"
|
|
Packit |
bfcc33 |
```
|
|
Packit |
bfcc33 |
|