Exemple de code pour l'importateur Sass
Exemple d'importer.c :
- #include <stdio.h>
- #include <string.h>
- #include "sass/context.h"
-
- Sass_Import_List sass_importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
-
- // obtenir le cookie à partir du descripteur de l'importateur
- void* cookie = sass_importer_get_cookie(cb);
- Sass_Import_List list = sass_make_import_list(2);
- char* local = sass_copy_c_string("local { color: green; }");
- char* remote = sass_copy_c_string("remote { color: red; }");
- list[0] = sass_make_import_entry("/tmp/styles.scss", local, 0);
- list[1] = sass_make_import_entry("http://www.example.com", remote, 0);
- return list;
- }
-
- int main( int argc, const char* argv[] ) {
-
- // récupérer le fichier d'entrée à partir du premier argument ou utiliser la valeur par défaut
- const char* input = argc > 1 ? argv[1] : "styles.scss";
-
- // créer le contexte du fichier et obtenir toutes les structures associées
- struct Sass_File_Context* file_ctx = sass_make_file_context(input);
- struct Sass_Context* ctx = sass_file_context_get_context(file_ctx);
- struct Sass_Options* ctx_opt = sass_context_get_options(ctx);
-
- // attribuer un importateur personnalisé
- Sass_Importer_Entry c_imp = sass_make_importer(sass_importer, 0, 0);
- // créer une liste pour tous les importateurs personnalisés
- Sass_Importer_List imp_list = sass_make_importer_list(1);
- // mettre uniquement l'importateur sur la liste
- sass_importer_set_list_entry(imp_list, 0, c_imp);
- // liste d'inscription sur les options de contexte
- sass_option_set_c_importers(ctx_opt, imp_list);
- // le contexte est configuré, appelez l'étape de compilation maintenant
- int status = sass_compile_file_context(file_ctx);
-
- // afficher le résultat ou l'erreur sur la sortie standard
- if (status == 0) puts(sass_context_get_output_string(ctx));
- else puts(sass_context_get_error_message(ctx));
-
- // libérer la mémoire allouée
- sass_delete_file_context(file_ctx);
-
- // état de sortie
- return status;
-
- }
Compiler importer.c :
gcc -c importer.c -o importer.o gcc -o importer importer.o -lsass echo "@import 'foobar';" > importer.scss ./importer importer.scss |
Exemples de comportement des importateurs :
- Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
- // laissez LibSass gérer la demande d'importation
- return NULL;
- }
-
- Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
- // laisser LibSass gérer la requête
- // avaler le pass-through «@import "http://..."»
- // (probablement un bogue)
-
- Sass_Import_List list = sass_make_import_list(1);
- list[0] = sass_make_import_entry(path, 0, 0);
- return list;
- }
-
- Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
- // renvoyer une erreur pour arrêter l'exécution
- Sass_Import_List list = sass_make_import_list(1);
- const char* message = "un message d'erreur";
- list[0] = sass_make_import_entry(path, 0, 0);
- sass_import_set_error(list[0], sass_copy_c_string(message), 0, 0);
- return list;
- }
-
- Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
- // laissez LibSass charger le fichier identifié par l'importateur
- Sass_Import_List list = sass_make_import_list(1);
- list[0] = sass_make_import_entry("/tmp/file.scss", 0, 0);
- return list;
- }
-
- Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
- // masquer complètement l'importation
- // (probablement un bogue)
- Sass_Import_List list = sass_make_import_list(0);
- return list;
- }
-
- Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
- // masquer complètement l'importation
- // (probablement un bogue)
- Sass_Import_List list = sass_make_import_list(1);
- list[0] = sass_make_import_entry(0, 0, 0);
- return list;
- }
Dernière mise à jour : Mardi, le 8 octobre 2024