Skip to content

Commit 931d381

Browse files
authored
Merge pull request #2968 from mgreter/docs/data-context
Improve data context documentation
2 parents eb54de6 + 62187f0 commit 931d381

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

docs/api-context-example.md

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,55 @@
1-
## Example main.c
1+
## Example for `data_context`
22

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
453
#include <stdio.h>
554
#include "sass/context.h"
655

@@ -34,11 +83,11 @@ int main( int argc, const char* argv[] )
3483
}
3584
```
3685
37-
### Compile main.c
86+
### Compile file.c
3887
3988
```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
4291
echo "foo { margin: 21px * 2; }" > foo.scss
4392
./sample foo.scss => "foo { margin: 42px }"
4493
```

docs/api-doc.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ process. The compiler has two different modes: direct input as a string with
5151
`Sass_File_Context`. See the code for a list of options available
5252
[Sass_Options](https://github.com/sass/libsass/blob/36feef0/include/sass/interface.h#L18)
5353

54+
The general rule is if the API takes const char* it will make a copy,
55+
but where the API is char* it will take over memory ownership, so make sure to pass
56+
in memory that is allocated via sass_copy_c_string or sass_alloc_memory.
57+
5458
**Building a file compiler**
5559

5660
context = sass_make_file_context("file.scss")
@@ -73,7 +77,11 @@ process. The compiler has two different modes: direct input as a string with
7377

7478
**Building a data compiler**
7579

76-
context = sass_make_data_context("div { a { color: blue; } }")
80+
// LibSass takes over memory owenership, make sure to allocate
81+
// a buffer via `sass_alloc_memory` or `sass_copy_c_string`.
82+
buffer = sass_copy_c_string("div { a { color: blue; } }")
83+
84+
context = sass_make_data_context(buffer)
7785
options = sass_data_context_get_options(context)
7886
sass_option_set_precision(options, 1)
7987
sass_option_set_source_comments(options, true)

0 commit comments

Comments
 (0)