22
33``` C:data.c
44#include < stdio.h>
5- #include < sass/context .h>
5+ #include < sass.h>
66
7- int main ( int argc, const char* argv[ ] )
7+ int main (int argc, const char* argv[ ] )
88{
99
1010 // LibSass will take control of data you pass in
@@ -15,22 +15,47 @@ int main( int argc, const char* argv[] )
1515 // then fill it with data you load from disk or somewhere else.
1616
1717 // 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);
18+ struct SassCompiler* compiler = sass_make_compiler();
19+ struct SassImport* data = sass_make_content_import(text, "styles");
20+ sass_compiler_set_entry_point(compiler, data);
21+ // everything you make you must delete
22+ sass_delete_import(data); // passed away
23+
24+ // Execute all three phases
25+ sass_compiler_parse(compiler);
26+ sass_compiler_compile(compiler);
27+ sass_compiler_render(compiler);
28+
29+ // Print any warning to console
30+ if (sass_compiler_get_warn_string(compiler)) {
31+ sass_print_stderr(sass_compiler_get_warn_string(compiler));
32+ }
33+
34+ // Print error message if we have an error
35+ if (sass_compiler_get_status(compiler) != 0) {
36+ const struct SassError* error = sass_compiler_get_error(compiler);
37+ sass_print_stderr(sass_error_get_string(error));
38+ }
39+
40+ // Get result code after all compilation steps
41+ int status = sass_compiler_get_status(compiler);
42+
43+ // Write to output if no errors occurred
44+ if (status == 0) {
45+
46+ // Paths where to write stuff to (might be `stream://stdout`)
47+ const char* outfile = sass_compiler_get_output_path(compiler);
48+ const char* mapfile = sass_compiler_get_srcmap_path(compiler);
49+ // Get the parts to be added to the output file (or stdout)
50+ const char* content = sass_compiler_get_output_string(compiler);
51+ const char* footer = sass_compiler_get_footer_string(compiler);
52+ const char* srcmap = sass_compiler_get_srcmap_string(compiler);
53+
54+ // Output all results
55+ if (content) puts(content);
56+ if (footer) puts(footer);
57+
58+ }
3459
3560 // exit status
3661 return status;
@@ -51,31 +76,60 @@ echo "foo { margin: 21px * 2; }" > foo.scss
5176
5277``` C:file.c
5378#include < stdio.h>
54- #include " sass/context.h "
79+ #include < sass.h >
5580
56- int main ( int argc, const char* argv[ ] )
81+ int main (int argc, const char* argv[ ] )
5782{
5883
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));
84+ // LibSass will take control of data you pass in
85+ // Therefore we need to make a copy of static data
86+ char* text = sass_copy_c_string("a{b: c ;}");
87+ // Normally you'll load data into a buffer from i.e. the disk.
88+ // Use ` sass_alloc_memory ` to get a buffer to pass to LibSass
89+ // then fill it with data you load from disk or somewhere else.
7690
77- // release allocated memory
78- sass_delete_file_context(file_ctx);
91+ // create the data context and get all related structs
92+ struct SassCompiler* compiler = sass_make_compiler();
93+ struct SassImport* data = sass_make_file_import("foo.scss");
94+ sass_compiler_set_entry_point(compiler, data);
95+ // everything you make you must delete
96+ sass_delete_import(data); // passed away
97+
98+ // Execute all three phases
99+ sass_compiler_parse(compiler);
100+ sass_compiler_compile(compiler);
101+ sass_compiler_render(compiler);
102+
103+ // Print any warning to console
104+ if (sass_compiler_get_warn_string(compiler)) {
105+ sass_print_stderr(sass_compiler_get_warn_string(compiler));
106+ }
107+
108+ // Print error message if we have an error
109+ if (sass_compiler_get_status(compiler) != 0) {
110+ const struct SassError* error = sass_compiler_get_error(compiler);
111+ sass_print_stderr(sass_error_get_string(error));
112+ }
113+
114+ // Get result code after all compilation steps
115+ int status = sass_compiler_get_status(compiler);
116+
117+ // Write to output if no errors occurred
118+ if (status == 0) {
119+
120+ // Paths where to write stuff to (might be `stream://stdout`)
121+ const char* outfile = sass_compiler_get_output_path(compiler);
122+ const char* mapfile = sass_compiler_get_srcmap_path(compiler);
123+ // Get the parts to be added to the output file (or stdout)
124+ const char* content = sass_compiler_get_output_string(compiler);
125+ const char* footer = sass_compiler_get_footer_string(compiler);
126+ const char* srcmap = sass_compiler_get_srcmap_string(compiler);
127+
128+ // Output all results
129+ if (content) puts(content);
130+ if (footer) puts(footer);
131+
132+ }
79133
80134 // exit status
81135 return status;
0 commit comments