Blame docs/api-importer-example.md

Packit Service 7770af
## Example importer.c
Packit Service 7770af
Packit Service 7770af
```C
Packit Service 7770af
#include <stdio.h>
Packit Service 7770af
#include <string.h>
Packit Service 7770af
#include "sass/context.h"
Packit Service 7770af
Packit Service 7770af
Sass_Import_List sass_importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp)
Packit Service 7770af
{
Packit Service 7770af
  // get the cookie from importer descriptor
Packit Service 7770af
  void* cookie = sass_importer_get_cookie(cb);
Packit Service 7770af
  Sass_Import_List list = sass_make_import_list(2);
Packit Service 7770af
  char* local = sass_copy_c_string("local { color: green; }");
Packit Service 7770af
  char* remote = sass_copy_c_string("remote { color: red; }");
Packit Service 7770af
  list[0] = sass_make_import_entry("/tmp/styles.scss", local, 0);
Packit Service 7770af
  list[1] = sass_make_import_entry("http://www.example.com", remote, 0);
Packit Service 7770af
  return list;
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
int main( int argc, const char* argv[] )
Packit Service 7770af
{
Packit Service 7770af
Packit Service 7770af
  // get the input file from first argument or use default
Packit Service 7770af
  const char* input = argc > 1 ? argv[1] : "styles.scss";
Packit Service 7770af
Packit Service 7770af
  // create the file context and get all related structs
Packit Service 7770af
  struct Sass_File_Context* file_ctx = sass_make_file_context(input);
Packit Service 7770af
  struct Sass_Context* ctx = sass_file_context_get_context(file_ctx);
Packit Service 7770af
  struct Sass_Options* ctx_opt = sass_context_get_options(ctx);
Packit Service 7770af
Packit Service 7770af
  // allocate custom importer
Packit Service 7770af
  Sass_Importer_Entry c_imp =
Packit Service 7770af
    sass_make_importer(sass_importer, 0, 0);
Packit Service 7770af
  // create list for all custom importers
Packit Service 7770af
  Sass_Importer_List imp_list = sass_make_importer_list(1);
Packit Service 7770af
  // put only the importer on to the list
Packit Service 7770af
  sass_importer_set_list_entry(imp_list, 0, c_imp);
Packit Service 7770af
  // register list on to the context options
Packit Service 7770af
  sass_option_set_c_importers(ctx_opt, imp_list);
Packit Service 7770af
  // context is set up, call the compile step now
Packit Service 7770af
  int status = sass_compile_file_context(file_ctx);
Packit Service 7770af
Packit Service 7770af
  // print the result or the error to the stdout
Packit Service 7770af
  if (status == 0) puts(sass_context_get_output_string(ctx));
Packit Service 7770af
  else puts(sass_context_get_error_message(ctx));
Packit Service 7770af
Packit Service 7770af
  // release allocated memory
Packit Service 7770af
  sass_delete_file_context(file_ctx);
Packit Service 7770af
Packit Service 7770af
  // exit status
Packit Service 7770af
  return status;
Packit Service 7770af
Packit Service 7770af
}
Packit Service 7770af
```
Packit Service 7770af
Packit Service 7770af
Compile importer.c
Packit Service 7770af
Packit Service 7770af
```bash
Packit Service 7770af
gcc -c importer.c -o importer.o
Packit Service 7770af
gcc -o importer importer.o -lsass
Packit Service 7770af
echo "@import 'foobar';" > importer.scss
Packit Service 7770af
./importer importer.scss
Packit Service 7770af
```
Packit Service 7770af
Packit Service 7770af
## Importer Behavior Examples
Packit Service 7770af
Packit Service 7770af
```C
Packit Service 7770af
Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
Packit Service 7770af
  // let LibSass handle the import request
Packit Service 7770af
  return NULL;
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
Packit Service 7770af
  // let LibSass handle the request
Packit Service 7770af
  // swallows »@import "http://…"« pass-through
Packit Service 7770af
  // (arguably a bug)
Packit Service 7770af
  Sass_Import_List list = sass_make_import_list(1);
Packit Service 7770af
  list[0] = sass_make_import_entry(path, 0, 0);
Packit Service 7770af
  return list;
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
Packit Service 7770af
  // return an error to halt execution
Packit Service 7770af
  Sass_Import_List list = sass_make_import_list(1);
Packit Service 7770af
  const char* message = "some error message";
Packit Service 7770af
  list[0] = sass_make_import_entry(path, 0, 0);
Packit Service 7770af
  sass_import_set_error(list[0], sass_copy_c_string(message), 0, 0);
Packit Service 7770af
  return list;
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
Packit Service 7770af
  // let LibSass load the file identifed by the importer
Packit Service 7770af
  Sass_Import_List list = sass_make_import_list(1);
Packit Service 7770af
  list[0] = sass_make_import_entry("/tmp/file.scss", 0, 0);
Packit Service 7770af
  return list;
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
Packit Service 7770af
  // completely hide the import
Packit Service 7770af
  // (arguably a bug)
Packit Service 7770af
  Sass_Import_List list = sass_make_import_list(0);
Packit Service 7770af
  return list;
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
Packit Service 7770af
  // completely hide the import
Packit Service 7770af
  // (arguably a bug)
Packit Service 7770af
  Sass_Import_List list = sass_make_import_list(1);
Packit Service 7770af
  list[0] = sass_make_import_entry(0, 0, 0);
Packit Service 7770af
  return list;
Packit Service 7770af
}
Packit Service 7770af
```