Initial commit
[yaffs-website] / node_modules / node-sass / src / libsass / docs / api-doc.md
1 ## Introduction
2
3 LibSass wouldn't be much good without a way to interface with it. These
4 interface documentations describe the various functions and data structures
5 available to implementers. They are split up over three major components, which
6 have all their own source files (plus some common functionality).
7
8 - [Sass Context](api-context.md) - Trigger and handle the main Sass compilation
9 - [Sass Value](api-value.md) - Exchange values and its format with LibSass
10 - [Sass Function](api-function.md) - Get invoked by LibSass for function statments
11 - [Sass Importer](api-importer.md) - Get invoked by LibSass for @import statments
12
13 ### Basic usage
14
15 First you will need to include the header file!
16 This will automatically load all other headers too!
17
18 ```C
19 #include "sass/context.h"
20 ```
21
22 ## Basic C Example
23
24 ```C
25 #include <stdio.h>
26 #include "sass/context.h"
27
28 int main() {
29   puts(libsass_version());
30   return 0;
31 }
32 ```
33
34 ```bash
35 gcc -Wall version.c -lsass -o version && ./version
36 ```
37
38 ## More C Examples
39
40 - [Sample code for Sass Context](api-context-example.md)
41 - [Sample code for Sass Value](api-value-example.md)
42 - [Sample code for Sass Function](api-function-example.md)
43 - [Sample code for Sass Importer](api-importer-example.md)
44
45 ## Compiling your code
46
47 The most important is your sass file (or string of sass code). With this, you
48 will want to start a LibSass compiler. Here is some pseudocode describing the
49 process. The compiler has two different modes: direct input as a string with
50 `Sass_Data_Context` or LibSass will do file reading for you by using
51 `Sass_File_Context`. See the code for a list of options available
52 [Sass_Options](https://github.com/sass/libsass/blob/36feef0/include/sass/interface.h#L18)
53
54 **Building a file compiler**
55
56     context = sass_make_file_context("file.scss")
57     options = sass_file_context_get_options(context)
58     sass_option_set_precision(options, 1)
59     sass_option_set_source_comments(options, true)
60
61     sass_file_context_set_options(context, options)
62
63     compiler = sass_make_file_compiler(sass_context)
64     sass_compiler_parse(compiler)
65     sass_compiler_execute(compiler)
66
67     output = sass_context_get_output_string(context)
68     // Retrieve errors during compilation
69     error_status = sass_context_get_error_status(context)
70     json_error = sass_context_get_error_json(context)
71     // Release memory dedicated to the C compiler
72     sass_delete_compiler(compiler)
73
74 **Building a data compiler**
75
76     context = sass_make_data_context("div { a { color: blue; } }")
77     options = sass_data_context_get_options(context)
78     sass_option_set_precision(options, 1)
79     sass_option_set_source_comments(options, true)
80
81     sass_data_context_set_options(context, options)
82
83     compiler = sass_make_data_compiler(context)
84     sass_compiler_parse(compiler)
85     sass_compiler_execute(compiler)
86
87     output = sass_context_get_output_string(context)
88     // div a { color: blue; }
89     // Retrieve errors during compilation
90     error_status = sass_context_get_error_status(context)
91     json_error = sass_context_get_error_json(context)
92     // Release memory dedicated to the C compiler
93     sass_delete_compiler(compiler)
94
95 ## Sass Context Internals
96
97 Everything is stored in structs:
98
99 ```C
100 struct Sass_Options;
101 struct Sass_Context : Sass_Options;
102 struct Sass_File_context : Sass_Context;
103 struct Sass_Data_context : Sass_Context;
104 ```
105
106 This mirrors very well how `libsass` uses these structures.
107
108 - `Sass_Options` holds everything you feed in before the compilation. It also hosts
109 `input_path` and `output_path` options, because they are used to generate/calculate
110 relative links in source-maps. The `input_path` is shared with `Sass_File_Context`.
111 - `Sass_Context` holds all the data returned by the compilation step.
112 - `Sass_File_Context` is a specific implementation that requires no additional fields
113 - `Sass_Data_Context` is a specific implementation that adds the `input_source` field
114
115 Structs can be down-casted to access `context` or `options`!
116
117 ## Memory handling and life-cycles
118
119 We keep memory around for as long as the main [context](api-context.md) object
120 is not destroyed (`sass_delete_context`). LibSass will create copies of most
121 inputs/options beside the main sass code. You need to allocate and fill that
122 buffer before passing it to LibSass. You may also overtake memory management
123 from libsass for certain return values (i.e. `sass_context_take_output_string`).
124
125 ```C
126 // to allocate buffer to be filled
127 void* sass_alloc_memory(size_t size);
128 // to allocate a buffer from existing string
129 char* sass_copy_c_string(const char* str);
130 // to free overtaken memory when done
131 void sass_free_memory(void* ptr);
132 ```
133
134 ## Miscellaneous API functions
135
136 ```C
137 // Some convenient string helper function
138 char* sass_string_unquote (const char* str);
139 char* sass_string_quote (const char* str, const char quote_mark);
140
141 // Get compiled libsass version
142 const char* libsass_version(void);
143
144 // Implemented sass language version
145 // Hardcoded version 3.4 for time being
146 const char* libsass_language_version(void);
147 ```
148
149 ## Common Pitfalls
150
151 **input_path**
152
153 The `input_path` is part of `Sass_Options`, but it also is the main option for
154 `Sass_File_Context`. It is also used to generate relative file links in source-
155 maps. Therefore it is pretty usefull to pass this information if you have a
156 `Sass_Data_Context` and know the original path.
157
158 **output_path**
159
160 Be aware that `libsass` does not write the output file itself. This option
161 merely exists to give `libsass` the proper information to generate links in
162 source-maps. The file has to be written to the disk by the
163 binding/implementation. If the `output_path` is omitted, `libsass` tries to
164 extrapolate one from the `input_path` by replacing (or adding) the file ending
165 with `.css`.
166
167 ## Error Codes
168
169 The `error_code` is integer value which indicates the type of error that
170 occurred inside the LibSass process. Following is the list of error codes along
171 with the short description:
172
173 * 1: normal errors like parsing or `eval` errors
174 * 2: bad allocation error (memory error)
175 * 3: "untranslated" C++ exception (`throw std::exception`)
176 * 4: legacy string exceptions ( `throw const char*` or `std::string` )
177 * 5: Some other unknown exception
178
179 Although for the API consumer, error codes do not offer much value except
180 indicating whether *any* error occurred during the compilation, it helps
181 debugging the LibSass internal code paths.
182
183 ## Real-World Implementations
184
185 The proof is in the pudding, so we have highlighted a few implementations that
186 should be on par with the latest LibSass interface version. Some of them may not
187 have all features implemented!
188
189 1. [Perl Example](https://github.com/sass/perl-libsass/blob/master/lib/CSS/Sass.xs)
190 2. [Go Example](http://godoc.org/github.com/wellington/go-libsass#example-Context-Compile)
191 3. [Node Example](https://github.com/sass/node-sass/blob/master/src/binding.cpp)
192
193 ## ABI forward compatibility
194
195 We use a functional API to make dynamic linking more robust and future
196 compatible. The API is not yet 100% stable, so we do not yet guarantee
197 [ABI](https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html) forward
198 compatibility.
199
200 ## Plugins (experimental)
201
202 LibSass can load plugins from directories. Just define `plugin_path` on context
203 options to load all plugins from the directories. To implement plugins, please
204 consult the following example implementations.
205
206 - https://github.com/mgreter/libsass-glob
207 - https://github.com/mgreter/libsass-math
208 - https://github.com/mgreter/libsass-digest
209
210 ## Internal Structs
211
212 - [Sass Context Internals](api-context-internal.md)
213 - [Sass Value Internals](api-value-internal.md)
214 - [Sass Function Internals](api-function-internal.md)
215 - [Sass Importer Internals](api-importer-internal.md)