| 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 | 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 | |
| 58 | **Building a file compiler** |
| 59 | |
| 60 | context = sass_make_file_context("file.scss") |
| 61 | options = sass_file_context_get_options(context) |
| 62 | sass_option_set_precision(options, 1) |
| 63 | sass_option_set_source_comments(options, true) |
| 64 | |
| 65 | sass_file_context_set_options(context, options) |
| 66 | |
| 67 | compiler = sass_make_file_compiler(sass_context) |
| 68 | sass_compiler_parse(compiler) |
| 69 | sass_compiler_execute(compiler) |
| 70 | |
| 71 | output = sass_context_get_output_string(context) |
| 72 | // Retrieve errors during compilation |
| 73 | error_status = sass_context_get_error_status(context) |
| 74 | json_error = sass_context_get_error_json(context) |
| 75 | // Release memory dedicated to the C compiler |
| 76 | sass_delete_compiler(compiler) |
| 77 | |
| 78 | **Building a data compiler** |
| 79 | |
| 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) |
| 85 | options = sass_data_context_get_options(context) |
| 86 | sass_option_set_precision(options, 1) |
| 87 | sass_option_set_source_comments(options, true) |
| 88 | |
| 89 | sass_data_context_set_options(context, options) |
| 90 | |
| 91 | compiler = sass_make_data_compiler(context) |
| 92 | sass_compiler_parse(compiler) |
| 93 | sass_compiler_execute(compiler) |
| 94 | |
| 95 | output = sass_context_get_output_string(context) |
| 96 | // div a { color: blue; } |
| 97 | // Retrieve errors during compilation |
| 98 | error_status = sass_context_get_error_status(context) |
| 99 | json_error = sass_context_get_error_json(context) |
| 100 | // Release memory dedicated to the C compiler |
| 101 | sass_delete_compiler(compiler) |
| 102 | |
| 103 | ## Sass Context Internals |
| 104 | |
| 105 | Everything is stored in structs: |
| 106 | |
| 107 | ```C |
| 108 | struct Sass_Options; |
| 109 | struct Sass_Context : Sass_Options; |
| 110 | struct Sass_File_context : Sass_Context; |
| 111 | struct Sass_Data_context : Sass_Context; |
| 112 | ``` |
| 113 | |
| 114 | This mirrors very well how `libsass` uses these structures. |
| 115 | |
| 116 | - `Sass_Options` holds everything you feed in before the compilation. It also hosts |
| 117 | `input_path` and `output_path` options, because they are used to generate/calculate |
| 118 | relative links in source-maps. The `input_path` is shared with `Sass_File_Context`. |
| 119 | - `Sass_Context` holds all the data returned by the compilation step. |
| 120 | - `Sass_File_Context` is a specific implementation that requires no additional fields |
| 121 | - `Sass_Data_Context` is a specific implementation that adds the `input_source` field |
| 122 | |
| 123 | Structs can be down-casted to access `context` or `options`! |
| 124 | |
| 125 | ## Memory handling and life-cycles |
| 126 | |
| 127 | We keep memory around for as long as the main [context](api-context.md) object |
| 128 | is not destroyed (`sass_delete_context`). LibSass will create copies of most |
| 129 | inputs/options beside the main sass code. You need to allocate and fill that |
| 130 | buffer before passing it to LibSass. You may also overtake memory management |
| 131 | from libsass for certain return values (i.e. `sass_context_take_output_string`). |
| 132 | Make sure to free it via `sass_free_memory`. |
| 133 | |
| 134 | ```C |
| 135 | // to allocate buffer to be filled |
| 136 | void* sass_alloc_memory(size_t size); |
| 137 | // to allocate a buffer from existing string |
| 138 | char* sass_copy_c_string(const char* str); |
| 139 | // to free overtaken memory when done |
| 140 | void sass_free_memory(void* ptr); |
| 141 | ``` |
| 142 | |
| 143 | ## Miscellaneous API functions |
| 144 | |
| 145 | ```C |
| 146 | // Some convenient string helper function |
| 147 | char* sass_string_unquote (const char* str); |
| 148 | char* sass_string_quote (const char* str, const char quote_mark); |
| 149 | |
| 150 | // Get compiled libsass version |
| 151 | const char* libsass_version(void); |
| 152 | |
| 153 | // Implemented sass language version |
| 154 | // Hardcoded version 3.4 for time being |
| 155 | const char* libsass_language_version(void); |
| 156 | ``` |
| 157 | |
| 158 | ## Common Pitfalls |
| 159 | |
| 160 | **input_path** |
| 161 | |
| 162 | The `input_path` is part of `Sass_Options`, but it also is the main option for |
| 163 | `Sass_File_Context`. It is also used to generate relative file links in source- |
| 164 | maps. Therefore it is pretty useful to pass this information if you have a |
| 165 | `Sass_Data_Context` and know the original path. |
| 166 | |
| 167 | **output_path** |
| 168 | |
| 169 | Be aware that `libsass` does not write the output file itself. This option |
| 170 | merely exists to give `libsass` the proper information to generate links in |
| 171 | source-maps. The file has to be written to the disk by the |
| 172 | binding/implementation. If the `output_path` is omitted, `libsass` tries to |
| 173 | extrapolate one from the `input_path` by replacing (or adding) the file ending |
| 174 | with `.css`. |
| 175 | |
| 176 | ## Error Codes |
| 177 | |
| 178 | The `error_code` is integer value which indicates the type of error that |
| 179 | occurred inside the LibSass process. Following is the list of error codes along |
| 180 | with the short description: |
| 181 | |
| 182 | * 1: normal errors like parsing or `eval` errors |
| 183 | * 2: bad allocation error (memory error) |
| 184 | * 3: "untranslated" C++ exception (`throw std::exception`) |
| 185 | * 4: legacy string exceptions ( `throw const char*` or `sass::string` ) |
| 186 | * 5: Some other unknown exception |
| 187 | |
| 188 | Although for the API consumer, error codes do not offer much value except |
| 189 | indicating whether *any* error occurred during the compilation, it helps |
| 190 | debugging the LibSass internal code paths. |
| 191 | |
| 192 | ## Real-World Implementations |
| 193 | |
| 194 | The proof is in the pudding, so we have highlighted a few implementations that |
| 195 | should be on par with the latest LibSass interface version. Some of them may not |
| 196 | have all features implemented! |
| 197 | |
| 198 | 1. [Perl Example](https://github.com/sass/perl-libsass/blob/master/Sass.xs) |
| 199 | 2. [Go Example](https://godoc.org/github.com/wellington/go-libsass#example-Compiler--Stdin) |
| 200 | 3. [Node Example](https://github.com/sass/node-sass/blob/master/src/binding.cpp) |
| 201 | |
| 202 | ## ABI forward compatibility |
| 203 | |
| 204 | We use a functional API to make dynamic linking more robust and future |
| 205 | compatible. The API is not yet 100% stable, so we do not yet guarantee |
| 206 | [ABI](https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html) forward |
| 207 | compatibility. |
| 208 | |
| 209 | ## Plugins (experimental) |
| 210 | |
| 211 | LibSass can load plugins from directories. Just define `plugin_path` on context |
| 212 | options to load all plugins from the directories. To implement plugins, please |
| 213 | consult the following example implementations. |
| 214 | |
| 215 | - https://github.com/mgreter/libsass-glob |
| 216 | - https://github.com/mgreter/libsass-math |
| 217 | - https://github.com/mgreter/libsass-digest |
| 218 | |
| 219 | ## Internal Structs |
| 220 | |
| 221 | - [Sass Context Internals](api-context-internal.md) |
| 222 | - [Sass Value Internals](api-value-internal.md) |
| 223 | - [Sass Function Internals](api-function-internal.md) |
| 224 | - [Sass Importer Internals](api-importer-internal.md) |