Blame docs/api-doc.md

Packit bfcc33
## Introduction
Packit bfcc33
Packit bfcc33
LibSass wouldn't be much good without a way to interface with it. These
Packit bfcc33
interface documentations describe the various functions and data structures
Packit bfcc33
available to implementers. They are split up over three major components, which
Packit bfcc33
have all their own source files (plus some common functionality).
Packit bfcc33
Packit bfcc33
- [Sass Context](api-context.md) - Trigger and handle the main Sass compilation
Packit bfcc33
- [Sass Value](api-value.md) - Exchange values and its format with LibSass
Packit bfcc33
- [Sass Function](api-function.md) - Get invoked by LibSass for function statments
Packit bfcc33
- [Sass Importer](api-importer.md) - Get invoked by LibSass for @import statments
Packit bfcc33
Packit bfcc33
### Basic usage
Packit bfcc33
Packit bfcc33
First you will need to include the header file!
Packit bfcc33
This will automatically load all other headers too!
Packit bfcc33
Packit bfcc33
```C
Packit bfcc33
#include "sass/context.h"
Packit bfcc33
```
Packit bfcc33
Packit bfcc33
## Basic C Example
Packit bfcc33
Packit bfcc33
```C
Packit bfcc33
#include <stdio.h>
Packit bfcc33
#include "sass/context.h"
Packit bfcc33
Packit bfcc33
int main() {
Packit bfcc33
  puts(libsass_version());
Packit bfcc33
  return 0;
Packit bfcc33
}
Packit bfcc33
```
Packit bfcc33
Packit bfcc33
```bash
Packit bfcc33
gcc -Wall version.c -lsass -o version && ./version
Packit bfcc33
```
Packit bfcc33
Packit bfcc33
## More C Examples
Packit bfcc33
Packit bfcc33
- [Sample code for Sass Context](api-context-example.md)
Packit bfcc33
- [Sample code for Sass Value](api-value-example.md)
Packit bfcc33
- [Sample code for Sass Function](api-function-example.md)
Packit bfcc33
- [Sample code for Sass Importer](api-importer-example.md)
Packit bfcc33
Packit bfcc33
## Compiling your code
Packit bfcc33
Packit bfcc33
The most important is your sass file (or string of sass code). With this, you
Packit bfcc33
will want to start a LibSass compiler. Here is some pseudocode describing the
Packit bfcc33
process. The compiler has two different modes: direct input as a string with
Packit bfcc33
`Sass_Data_Context` or LibSass will do file reading for you by using
Packit bfcc33
`Sass_File_Context`. See the code for a list of options available
Packit bfcc33
[Sass_Options](https://github.com/sass/libsass/blob/36feef0/include/sass/interface.h#L18)
Packit bfcc33
Packit bfcc33
**Building a file compiler**
Packit bfcc33
Packit bfcc33
    context = sass_make_file_context("file.scss")
Packit bfcc33
    options = sass_file_context_get_options(context)
Packit bfcc33
    sass_option_set_precision(options, 1)
Packit bfcc33
    sass_option_set_source_comments(options, true)
Packit bfcc33
Packit bfcc33
    sass_file_context_set_options(context, options)
Packit bfcc33
Packit bfcc33
    compiler = sass_make_file_compiler(sass_context)
Packit bfcc33
    sass_compiler_parse(compiler)
Packit bfcc33
    sass_compiler_execute(compiler)
Packit bfcc33
Packit bfcc33
    output = sass_context_get_output_string(context)
Packit bfcc33
    // Retrieve errors during compilation
Packit bfcc33
    error_status = sass_context_get_error_status(context)
Packit bfcc33
    json_error = sass_context_get_error_json(context)
Packit bfcc33
    // Release memory dedicated to the C compiler
Packit bfcc33
    sass_delete_compiler(compiler)
Packit bfcc33
Packit bfcc33
**Building a data compiler**
Packit bfcc33
Packit bfcc33
    context = sass_make_data_context("div { a { color: blue; } }")
Packit bfcc33
    options = sass_data_context_get_options(context)
Packit bfcc33
    sass_option_set_precision(options, 1)
Packit bfcc33
    sass_option_set_source_comments(options, true)
Packit bfcc33
Packit bfcc33
    sass_data_context_set_options(context, options)
Packit bfcc33
Packit bfcc33
    compiler = sass_make_data_compiler(context)
Packit bfcc33
    sass_compiler_parse(compiler)
Packit bfcc33
    sass_compiler_execute(compiler)
Packit bfcc33
Packit bfcc33
    output = sass_context_get_output_string(context)
Packit bfcc33
    // div a { color: blue; }
Packit bfcc33
    // Retrieve errors during compilation
Packit bfcc33
    error_status = sass_context_get_error_status(context)
Packit bfcc33
    json_error = sass_context_get_error_json(context)
Packit bfcc33
    // Release memory dedicated to the C compiler
Packit bfcc33
    sass_delete_compiler(compiler)
Packit bfcc33
Packit bfcc33
## Sass Context Internals
Packit bfcc33
Packit bfcc33
Everything is stored in structs:
Packit bfcc33
Packit bfcc33
```C
Packit bfcc33
struct Sass_Options;
Packit bfcc33
struct Sass_Context : Sass_Options;
Packit bfcc33
struct Sass_File_context : Sass_Context;
Packit bfcc33
struct Sass_Data_context : Sass_Context;
Packit bfcc33
```
Packit bfcc33
Packit bfcc33
This mirrors very well how `libsass` uses these structures.
Packit bfcc33
Packit bfcc33
- `Sass_Options` holds everything you feed in before the compilation. It also hosts
Packit bfcc33
`input_path` and `output_path` options, because they are used to generate/calculate
Packit bfcc33
relative links in source-maps. The `input_path` is shared with `Sass_File_Context`.
Packit bfcc33
- `Sass_Context` holds all the data returned by the compilation step.
Packit bfcc33
- `Sass_File_Context` is a specific implementation that requires no additional fields
Packit bfcc33
- `Sass_Data_Context` is a specific implementation that adds the `input_source` field
Packit bfcc33
Packit bfcc33
Structs can be down-casted to access `context` or `options`!
Packit bfcc33
Packit bfcc33
## Memory handling and life-cycles
Packit bfcc33
Packit bfcc33
We keep memory around for as long as the main [context](api-context.md) object
Packit bfcc33
is not destroyed (`sass_delete_context`). LibSass will create copies of most
Packit bfcc33
inputs/options beside the main sass code. You need to allocate and fill that
Packit bfcc33
buffer before passing it to LibSass. You may also overtake memory management
Packit bfcc33
from libsass for certain return values (i.e. `sass_context_take_output_string`).
Packit bfcc33
Packit bfcc33
```C
Packit bfcc33
// to allocate buffer to be filled
Packit bfcc33
void* sass_alloc_memory(size_t size);
Packit bfcc33
// to allocate a buffer from existing string
Packit bfcc33
char* sass_copy_c_string(const char* str);
Packit bfcc33
// to free overtaken memory when done
Packit bfcc33
void sass_free_memory(void* ptr);
Packit bfcc33
```
Packit bfcc33
Packit bfcc33
## Miscellaneous API functions
Packit bfcc33
Packit bfcc33
```C
Packit bfcc33
// Some convenient string helper function
Packit bfcc33
char* sass_string_unquote (const char* str);
Packit bfcc33
char* sass_string_quote (const char* str, const char quote_mark);
Packit bfcc33
Packit bfcc33
// Get compiled libsass version
Packit bfcc33
const char* libsass_version(void);
Packit bfcc33
Packit bfcc33
// Implemented sass language version
Packit bfcc33
// Hardcoded version 3.4 for time being
Packit bfcc33
const char* libsass_language_version(void);
Packit bfcc33
```
Packit bfcc33
Packit bfcc33
## Common Pitfalls
Packit bfcc33
Packit bfcc33
**input_path**
Packit bfcc33
Packit bfcc33
The `input_path` is part of `Sass_Options`, but it also is the main option for
Packit bfcc33
`Sass_File_Context`. It is also used to generate relative file links in source-
Packit bfcc33
maps. Therefore it is pretty usefull to pass this information if you have a
Packit bfcc33
`Sass_Data_Context` and know the original path.
Packit bfcc33
Packit bfcc33
**output_path**
Packit bfcc33
Packit bfcc33
Be aware that `libsass` does not write the output file itself. This option
Packit bfcc33
merely exists to give `libsass` the proper information to generate links in
Packit bfcc33
source-maps. The file has to be written to the disk by the
Packit bfcc33
binding/implementation. If the `output_path` is omitted, `libsass` tries to
Packit bfcc33
extrapolate one from the `input_path` by replacing (or adding) the file ending
Packit bfcc33
with `.css`.
Packit bfcc33
Packit bfcc33
## Error Codes
Packit bfcc33
Packit bfcc33
The `error_code` is integer value which indicates the type of error that
Packit bfcc33
occurred inside the LibSass process. Following is the list of error codes along
Packit bfcc33
with the short description:
Packit bfcc33
Packit bfcc33
* 1: normal errors like parsing or `eval` errors
Packit bfcc33
* 2: bad allocation error (memory error)
Packit bfcc33
* 3: "untranslated" C++ exception (`throw std::exception`)
Packit bfcc33
* 4: legacy string exceptions ( `throw const char*` or `std::string` )
Packit bfcc33
* 5: Some other unknown exception
Packit bfcc33
Packit bfcc33
Although for the API consumer, error codes do not offer much value except
Packit bfcc33
indicating whether *any* error occurred during the compilation, it helps
Packit bfcc33
debugging the LibSass internal code paths.
Packit bfcc33
Packit bfcc33
## Real-World Implementations
Packit bfcc33
Packit bfcc33
The proof is in the pudding, so we have highlighted a few implementations that
Packit bfcc33
should be on par with the latest LibSass interface version. Some of them may not
Packit bfcc33
have all features implemented!
Packit bfcc33
Packit bfcc33
1. [Perl Example](https://github.com/sass/perl-libsass/blob/master/lib/CSS/Sass.xs)
Packit bfcc33
2. [Go Example](http://godoc.org/github.com/wellington/go-libsass#example-Context-Compile)
Packit bfcc33
3. [Node Example](https://github.com/sass/node-sass/blob/master/src/binding.cpp)
Packit bfcc33
Packit bfcc33
## ABI forward compatibility
Packit bfcc33
Packit bfcc33
We use a functional API to make dynamic linking more robust and future
Packit bfcc33
compatible. The API is not yet 100% stable, so we do not yet guarantee
Packit bfcc33
[ABI](https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html) forward
Packit bfcc33
compatibility.
Packit bfcc33
Packit bfcc33
## Plugins (experimental)
Packit bfcc33
Packit bfcc33
LibSass can load plugins from directories. Just define `plugin_path` on context
Packit bfcc33
options to load all plugins from the directories. To implement plugins, please
Packit bfcc33
consult the following example implementations.
Packit bfcc33
Packit bfcc33
- https://github.com/mgreter/libsass-glob
Packit bfcc33
- https://github.com/mgreter/libsass-math
Packit bfcc33
- https://github.com/mgreter/libsass-digest
Packit bfcc33
Packit bfcc33
## Internal Structs
Packit bfcc33
Packit bfcc33
- [Sass Context Internals](api-context-internal.md)
Packit bfcc33
- [Sass Value Internals](api-value-internal.md)
Packit bfcc33
- [Sass Function Internals](api-function-internal.md)
Packit bfcc33
- [Sass Importer Internals](api-importer-internal.md)