|
| 1 | +## Sass Compiler API |
| 2 | + |
| 3 | +Sass Compiler is the main object for the Sass compilation process. |
| 4 | +It keeps the state between different stages of the process, which is |
| 5 | +split in three main stages: parsing, compiling and rendering. |
| 6 | + |
| 7 | +The regular life-time of a compiler will mostly look like this: |
| 8 | +- Create new compiler object |
| 9 | +- Set various configuration options |
| 10 | +- Set one compilation entry point |
| 11 | +- Call the parse function |
| 12 | +- Call the compile function |
| 13 | +- Call the render function |
| 14 | +- Process the results |
| 15 | + |
| 16 | +The split between parse, compile and render is done because there |
| 17 | +are certain cases where we might want to omit certain phases. One |
| 18 | +example would be the init phase for a watcher to get all includes. |
| 19 | + |
| 20 | +### Sass Compiler API |
| 21 | + |
| 22 | +```C |
| 23 | +///////////////////////////////////////////////////////////////////////// |
| 24 | +///////////////////////////////////////////////////////////////////////// |
| 25 | + |
| 26 | +// Create a new LibSass compiler context |
| 27 | +struct SassCompiler* sass_make_compiler(); |
| 28 | + |
| 29 | +// Release all memory allocated with the compiler |
| 30 | +void sass_delete_compiler(struct SassCompiler* compiler); |
| 31 | + |
| 32 | +///////////////////////////////////////////////////////////////////////// |
| 33 | +///////////////////////////////////////////////////////////////////////// |
| 34 | + |
| 35 | +// Parse the entry point and potentially all imports within. |
| 36 | +void sass_compiler_parse(struct SassCompiler* compiler); |
| 37 | + |
| 38 | +// Evaluate the parsed entry point and store resulting ast-tree. |
| 39 | +void sass_compiler_compile(struct SassCompiler* compiler); |
| 40 | + |
| 41 | +// Render the evaluated ast-tree to get the final output string. |
| 42 | +void sass_compiler_render(struct SassCompiler* compiler); |
| 43 | + |
| 44 | +///////////////////////////////////////////////////////////////////////// |
| 45 | +///////////////////////////////////////////////////////////////////////// |
| 46 | + |
| 47 | +// Add additional include paths where LibSass will look for includes. |
| 48 | +// Note: the passed in `paths` can be path separated (`;` on windows, `:` otherwise). |
| 49 | +void sass_compiler_add_include_paths(struct SassCompiler* compiler, const char* paths); |
| 50 | + |
| 51 | +// Load dynamic loadable plugins from `paths`. Plugins are only supported on certain OSs and |
| 52 | +// are still in experimental state. This will look for `*.dll`, `*.so` or `*.dynlib` files. |
| 53 | +// It then tries to load the found libraries and does a few checks to see if the library |
| 54 | +// is actually a LibSass plugin. We then call its init hook if the library is compatible. |
| 55 | +// Note: the passed in `paths` can be path separated (`;` on windows, `:` otherwise). |
| 56 | +void sass_compiler_load_plugins(struct SassCompiler* compiler, const char* paths); |
| 57 | + |
| 58 | +// Add a custom header importer that will always be executed before any other |
| 59 | +// compilations takes place. Useful to prepend a shared copyright header or to |
| 60 | +// provide global variables or functions. This feature is still in experimental state. |
| 61 | +// Note: With the adaption of Sass Modules this might be completely replaced in the future. |
| 62 | +void sass_compiler_add_custom_header(struct SassCompiler* compiler, struct SassImporter* header); |
| 63 | + |
| 64 | +// Add a custom importer that will be executed when a sass `@import` rule is found. |
| 65 | +// This is useful to e.g. rewrite import locations or to load content from remote. |
| 66 | +// For more please check https://github.com/sass/libsass/blob/master/docs/api-importer.md |
| 67 | +// Note: The importer will not be called for regular css `@import url()` rules. |
| 68 | +void sass_compiler_add_custom_importer(struct SassCompiler* compiler, struct SassImporter* importer); |
| 69 | + |
| 70 | +// Add a custom function that will be executed when the corresponding function call is |
| 71 | +// requested from any sass code. This is useful to provide custom functions in your code. |
| 72 | +// For more please check https://github.com/sass/libsass/blob/master/docs/api-function.md |
| 73 | +void sass_compiler_add_custom_function(struct SassCompiler* compiler, struct SassFunction* function); |
| 74 | + |
| 75 | +///////////////////////////////////////////////////////////////////////// |
| 76 | +///////////////////////////////////////////////////////////////////////// |
| 77 | + |
| 78 | +// Setter for output style (see `enum SassOutputStyle` for possible options). |
| 79 | +void sass_compiler_set_output_style(struct SassCompiler* compiler, enum SassOutputStyle output_style); |
| 80 | + |
| 81 | +// Setter for logging style (see `enum SassLoggerStyle` for possible options). |
| 82 | +void sass_compiler_set_logger_style(struct SassCompiler* compiler, enum SassLoggerStyle log_style); |
| 83 | + |
| 84 | +// Getter for compiler precision (how floating point numbers are truncated). |
| 85 | +int sass_compiler_get_precision(struct SassCompiler* compiler); |
| 86 | + |
| 87 | +// Setter for compiler precision (how floating point numbers are truncated). |
| 88 | +void sass_compiler_set_precision(struct SassCompiler* compiler, int precision); |
| 89 | + |
| 90 | +// Getter for compiler entry point (which file or data to parse first). |
| 91 | +struct SassImport* sass_compiler_get_entry_point(struct SassCompiler* compiler); |
| 92 | + |
| 93 | +// Setter for compiler entry point (which file or data to parse first). |
| 94 | +void sass_compiler_set_entry_point(struct SassCompiler* compiler, struct SassImport* import); |
| 95 | + |
| 96 | +// Getter for compiler output path (where to store the result) |
| 97 | +// Note: LibSass does not write the file, implementers should write to this path. |
| 98 | +const char* sass_compiler_get_output_path(struct SassCompiler* compiler); |
| 99 | + |
| 100 | +// Setter for compiler output path (where to store the result) |
| 101 | +// Note: LibSass does not write the file, implementers should write to this path. |
| 102 | +void sass_compiler_set_output_path(struct SassCompiler* compiler, const char* output_path); |
| 103 | + |
| 104 | +///////////////////////////////////////////////////////////////////////// |
| 105 | +///////////////////////////////////////////////////////////////////////// |
| 106 | + |
| 107 | +// Getter for warnings that occurred during any step. |
| 108 | +const char* sass_compiler_get_warn_string(struct SassCompiler* compiler); |
| 109 | + |
| 110 | +// Getter for output after parsing, compilation and rendering. |
| 111 | +const char* sass_compiler_get_output_string(struct SassCompiler* compiler); |
| 112 | + |
| 113 | +// Getter for footer string containing optional source-map (embedded or link). |
| 114 | +const char* sass_compiler_get_footer_string(struct SassCompiler* compiler); |
| 115 | + |
| 116 | +// Getter for string containing the optional source-mapping. |
| 117 | +const char* sass_compiler_get_srcmap_string(struct SassCompiler* compiler); |
| 118 | + |
| 119 | +///////////////////////////////////////////////////////////////////////// |
| 120 | +///////////////////////////////////////////////////////////////////////// |
| 121 | + |
| 122 | +// Setter for source-map mode (how to embed or not embed the source-map). |
| 123 | +void sass_compiler_set_srcmap_mode(struct SassCompiler* compiler, enum SassSrcMapMode mode); |
| 124 | + |
| 125 | +// Setter for source-map path (where to store the source-mapping). |
| 126 | +// Note: if path is not explicitly given, we will deduct one from input path. |
| 127 | +// Note: LibSass does not write the file, implementers should write to this path. |
| 128 | +void sass_compiler_set_srcmap_path(struct SassCompiler* compiler, const char* path); |
| 129 | + |
| 130 | +// Setter for source-map root (simply passed to the resulting srcmap info). |
| 131 | +// Note: if not given, no root attribute will be added to the srcmap info object. |
| 132 | +void sass_compiler_set_srcmap_root(struct SassCompiler* compiler, const char* root); |
| 133 | + |
| 134 | +// Setter for source-map file-url option (renders urls in srcmap as `file://` urls) |
| 135 | +void sass_compiler_set_srcmap_file_urls(struct SassCompiler* compiler, bool enable); |
| 136 | + |
| 137 | +// Setter for source-map embed-contents option (includes full sources in the srcmap info) |
| 138 | +void sass_compiler_set_srcmap_embed_contents(struct SassCompiler* compiler, bool enable); |
| 139 | + |
| 140 | +///////////////////////////////////////////////////////////////////////// |
| 141 | +///////////////////////////////////////////////////////////////////////// |
| 142 | + |
| 143 | +// Getter to return the number of all included files. |
| 144 | +size_t sass_compiler_get_included_files_count(struct SassCompiler* compiler); |
| 145 | + |
| 146 | +// Getter to return path to the included file at position `n`. |
| 147 | +const char* sass_compiler_get_included_file_path(struct SassCompiler* compiler, size_t n); |
| 148 | + |
| 149 | +///////////////////////////////////////////////////////////////////////// |
| 150 | +///////////////////////////////////////////////////////////////////////// |
| 151 | + |
| 152 | +// Getter for current import context. Use `SassImport` functions to query the state. |
| 153 | +const struct SassImport* sass_compiler_get_last_import(struct SassCompiler* compiler); |
| 154 | + |
| 155 | +// Returns pointer to error object associated with compiler. |
| 156 | +// Will be valid until the associated compiler is destroyed. |
| 157 | +const struct SassError* sass_compiler_get_error(struct SassCompiler* compiler); |
| 158 | + |
| 159 | +///////////////////////////////////////////////////////////////////////// |
| 160 | +///////////////////////////////////////////////////////////////////////// |
| 161 | + |
| 162 | +// Resolve a file relative to last import or include paths in the sass option struct. |
| 163 | +char* sass_compiler_find_file(const char* path, struct SassCompiler* compiler); |
| 164 | + |
| 165 | +// Resolve an include relative to last import or include paths in the sass option struct. |
| 166 | +// This will do a lookup as LibSass would do internally (partials, different extensions). |
| 167 | +// ToDo: Check if we should add `includeIndex` option to check for directory index files!? |
| 168 | +char* sass_compiler_find_include(const char* path, struct SassCompiler* compiler); |
| 169 | + |
| 170 | +///////////////////////////////////////////////////////////////////////// |
| 171 | +///////////////////////////////////////////////////////////////////////// |
| 172 | +``` |
0 commit comments