src/libsass/docs/api-doc.md

Introduction

LibSass wouldn't be much good without a way to interface with it. These interface documentations describe the various functions and data structures available to implementers. They are split up over three major components, which have all their own source files (plus some common functionality).

Basic usage

First you will need to include the header file! This will automatically load all other headers too!

#include "sass/context.h"

Basic C Example

#include <stdio.h>
#include "sass/context.h"

int main() {
  puts(libsass_version());
  return 0;
}
gcc -Wall version.c -lsass -o version && ./version

More C Examples

Compiling your code

The most important is your sass file (or string of sass code). With this, you will want to start a LibSass compiler. Here is some pseudocode describing the process. The compiler has two different modes: direct input as a string with Sass_Data_Context or LibSass will do file reading for you by using Sass_File_Context. See the code for a list of options available Sass_Options

The general rule is if the API takes const char* it will make a copy, but where the API is char* it will take over memory ownership, so make sure to pass in memory that is allocated via sass_copy_c_string or sass_alloc_memory.

Building a file compiler

context = sass_make_file_context("file.scss")
options = sass_file_context_get_options(context)
sass_option_set_precision(options, 1)
sass_option_set_source_comments(options, true)

sass_file_context_set_options(context, options)

compiler = sass_make_file_compiler(sass_context)
sass_compiler_parse(compiler)
sass_compiler_execute(compiler)

output = sass_context_get_output_string(context)
// Retrieve errors during compilation
error_status = sass_context_get_error_status(context)
json_error = sass_context_get_error_json(context)
// Release memory dedicated to the C compiler
sass_delete_compiler(compiler)

Building a data compiler

// LibSass takes over memory owenership, make sure to allocate
// a buffer via `sass_alloc_memory` or `sass_copy_c_string`.
buffer = sass_copy_c_string("div { a { color: blue; } }")

context = sass_make_data_context(buffer)
options = sass_data_context_get_options(context)
sass_option_set_precision(options, 1)
sass_option_set_source_comments(options, true)

sass_data_context_set_options(context, options)

compiler = sass_make_data_compiler(context)
sass_compiler_parse(compiler)
sass_compiler_execute(compiler)

output = sass_context_get_output_string(context)
// div a { color: blue; }
// Retrieve errors during compilation
error_status = sass_context_get_error_status(context)
json_error = sass_context_get_error_json(context)
// Release memory dedicated to the C compiler
sass_delete_compiler(compiler)

Sass Context Internals

Everything is stored in structs:

struct Sass_Options;
struct Sass_Context : Sass_Options;
struct Sass_File_context : Sass_Context;
struct Sass_Data_context : Sass_Context;

This mirrors very well how libsass uses these structures.

Structs can be down-casted to access context or options!

Memory handling and life-cycles

We keep memory around for as long as the main context object is not destroyed (sass_delete_context). LibSass will create copies of most inputs/options beside the main sass code. You need to allocate and fill that buffer before passing it to LibSass. You may also overtake memory management from libsass for certain return values (i.e. sass_context_take_output_string). Make sure to free it via sass_free_memory.

// to allocate buffer to be filled
void* sass_alloc_memory(size_t size);
// to allocate a buffer from existing string
char* sass_copy_c_string(const char* str);
// to free overtaken memory when done
void sass_free_memory(void* ptr);

Miscellaneous API functions

// Some convenient string helper function
char* sass_string_unquote (const char* str);
char* sass_string_quote (const char* str, const char quote_mark);

// Get compiled libsass version
const char* libsass_version(void);

// Implemented sass language version
// Hardcoded version 3.4 for time being
const char* libsass_language_version(void);

Common Pitfalls

input_path

The input_path is part of Sass_Options, but it also is the main option for Sass_File_Context. It is also used to generate relative file links in source- maps. Therefore it is pretty useful to pass this information if you have a Sass_Data_Context and know the original path.

output_path

Be aware that libsass does not write the output file itself. This option merely exists to give libsass the proper information to generate links in source-maps. The file has to be written to the disk by the binding/implementation. If the output_path is omitted, libsass tries to extrapolate one from the input_path by replacing (or adding) the file ending with .css.

Error Codes

The error_code is integer value which indicates the type of error that occurred inside the LibSass process. Following is the list of error codes along with the short description:

Although for the API consumer, error codes do not offer much value except indicating whether any error occurred during the compilation, it helps debugging the LibSass internal code paths.

Real-World Implementations

The proof is in the pudding, so we have highlighted a few implementations that should be on par with the latest LibSass interface version. Some of them may not have all features implemented!

  1. Perl Example
  2. Go Example
  3. Node Example

ABI forward compatibility

We use a functional API to make dynamic linking more robust and future compatible. The API is not yet 100% stable, so we do not yet guarantee ABI forward compatibility.

Plugins (experimental)

LibSass can load plugins from directories. Just define plugin_path on context options to load all plugins from the directories. To implement plugins, please consult the following example implementations.

Internal Structs



Try the sass package in your browser

Any scripts or data that you put into this service are public.

sass documentation built on May 29, 2024, 11:51 a.m.