Skip to content

Commit 96b6205

Browse files
committed
Initial doc update [WIP]
1 parent 5f7fcea commit 96b6205

27 files changed

+910
-1026
lines changed

docs/api-basics.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
## API documentation basics
2+
3+
LibSass as a C-API comes with multiple headers and a shared library.
4+
The complete API surface is covered by functions and by passing around
5+
anonymous structures. The implementor does not know any implementation
6+
detail about the pointers passed around.
7+
8+
### C-API headers
9+
10+
You may include specific headers directly, which have been split into
11+
groups as we saw fit, or you can simply include the main `sass.h` header.
12+
13+
### List of C-API headers
14+
15+
- sass/base.h - Includes basic headers and sets a few definitions.
16+
- sass/fwdecl.h - Forward declares all known anonymous structures.
17+
- sass/enums.h - Enums for e.g. config options exposed on the C-API.
18+
- sass/error.h - C-API functions to access `SassError` structs.
19+
- sass/import.h - C-API functions to access `SassImport` structs.
20+
- sass/importer.h - C-API functions to access `SassImporter` structs.
21+
- sass/traces.h - C-API functions to access `SassTrace` structs.
22+
- sass/values.h - C-API functions to access `SassValue` structs.
23+
- sass/variable.h - C-API functions to access scoped variables.
24+
- sass/version.h - Header including hardcoded LibSass version.
25+
26+
## Anonymous structure pointers
27+
28+
LibSass returns and consumes anonymous structure pointers on its C-API.
29+
Internally those are often directly mapped to an instance of a C++ object.
30+
Since we don't want implementors to know anything about the implementation
31+
details, we forward declare named structs (e.g. `SassCompiler`), but never
32+
provide any implementation for it. LibSass will simply cast a pointer from
33+
the c++ side to the anonymous struct and relies on the C-API to pass it
34+
back as expected to the corresponding functions. There the pointer will
35+
be statically casted back to the actual implementation. Since we provide
36+
a unique anonymous struct for every internal type, this should be safe as
37+
compilers should catch the case when arguments mismatch on the C-API side.
38+
39+
### List of anonymous structure pointers
40+
41+
These structs have no real implementation are only passed around as pointers.
42+
Internally in LibSass those pointers represent mostly different c++ objects.
43+
44+
- struct SassTrace - An single stack-trace object to get further information
45+
- struct SassError - An error object to get further information
46+
- struct SassCompiler - Main object to hold state for whole compilation phase.
47+
- struct SassFunction - Custom function object holding callback and cookie.
48+
- struct SassSource - Imported source with associated import and resolved path.
49+
- struct SassSrcSpan - Parser state for column, line and source information.
50+
- struct SassImport - Single import for entry point or returned by custom importer.
51+
- struct SassImporter - Custom importer function to be hooked into our loading.
52+
- struct SassImportList - Custom importers can return a list of imports.
53+
- struct SassMapIterator - Object to support iteration API over map objects.
54+
55+
## Why using c++11 (or gcc4.4 compatibility)
56+
57+
Since LibSass 3.0 we started to use more and more c++11 features. Some just
58+
creeped in, others were utilized deliberately. With LibSass 4.0 I took the
59+
decision to fully utilize whatever c++11 could offer. The main reason to
60+
fully embrace c++11 is the move semantics it brings. Earlier we also tried
61+
to support compilers that only had partial c++11 support (e.g. gnu++0x).
62+
63+
With LibSass 4.0 we don't really support this target anymore!
64+
65+
Note: currently the LibSass 4.0 release is on going, the target
66+
compiler should be gcc 4.8 as it should fully support c++11.
67+
68+
## Binary distributions in linkage issues
69+
70+
LibSass itself does not have any official binary distribution. The main reason for
71+
this is because it is nearly impossible to do so reliably for each and every
72+
thinkable operating system. Since LibSass is written in c++ we e.g. depend on the
73+
compilers c++ runtime library. On windows this problem is a bit less problematic,
74+
and there is a semi-official installer for it. But on Linux this e.g. is also
75+
depending on the used libc library. Since linux offers a choice here, a binary
76+
distribution made with glibc may not be compatible on a system that use musl.
77+
78+
By now LibSass is readily available on a lot of linux systems by their
79+
internal packet managers, so please try to install it that way. Alternatively
80+
try to install a recent compiler (e.g. gcc or clang) and compile it from source,
81+
preferably via the autotools way to ensure correct linkage of your external tool.

docs/api-context-example.md renamed to docs/api-compiler-example.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
```C:data.c
44
#include <stdio.h>
5-
#include "sass/context.h"
5+
#include <sass/context.h>
66

77
int main( int argc, const char* argv[] )
88
{

docs/api-compiler.md

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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

Comments
 (0)