Section courante

A propos

Section administrative du site

Exemple de code pour l'importateur Sass

Exemple d'importer.c :

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "sass/context.h"
  4.  
  5. Sass_Import_List sass_importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
  6.  
  7.   // obtenir le cookie à partir du descripteur de l'importateur
  8.   void* cookie = sass_importer_get_cookie(cb);
  9.   Sass_Import_List list = sass_make_import_list(2);
  10.   char* local = sass_copy_c_string("local { color: green; }");
  11.   char* remote = sass_copy_c_string("remote { color: red; }");
  12.   list[0] = sass_make_import_entry("/tmp/styles.scss", local, 0);
  13.   list[1] = sass_make_import_entry("http://www.example.com", remote, 0);
  14.   return list;
  15. }
  16.  
  17. int main( int argc, const char* argv[] ) {
  18.  
  19.   // récupérer le fichier d'entrée à partir du premier argument ou utiliser la valeur par défaut
  20.   const char* input = argc > 1 ? argv[1] : "styles.scss";
  21.  
  22.   // créer le contexte du fichier et obtenir toutes les structures associées
  23.   struct Sass_File_Context* file_ctx = sass_make_file_context(input);
  24.   struct Sass_Context* ctx = sass_file_context_get_context(file_ctx);
  25.   struct Sass_Options* ctx_opt = sass_context_get_options(ctx);
  26.  
  27.   // attribuer un importateur personnalisé
  28.   Sass_Importer_Entry c_imp = sass_make_importer(sass_importer, 0, 0);
  29.   // créer une liste pour tous les importateurs personnalisés
  30.   Sass_Importer_List imp_list = sass_make_importer_list(1);
  31.   // mettre uniquement l'importateur sur la liste
  32.   sass_importer_set_list_entry(imp_list, 0, c_imp);
  33.   // liste d'inscription sur les options de contexte
  34.   sass_option_set_c_importers(ctx_opt, imp_list);
  35.   // le contexte est configuré, appelez l'étape de compilation maintenant
  36.   int status = sass_compile_file_context(file_ctx);
  37.  
  38.   // afficher le résultat ou l'erreur sur la sortie standard
  39.   if (status == 0) puts(sass_context_get_output_string(ctx));
  40.   else puts(sass_context_get_error_message(ctx));
  41.  
  42.   // libérer la mémoire allouée
  43.   sass_delete_file_context(file_ctx);
  44.  
  45.   // état de sortie
  46.   return status;
  47.  
  48. }

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 :

  1. Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
  2.   // laissez LibSass gérer la demande d'importation
  3.   return NULL;
  4. }
  5.  
  6. Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
  7.   // laisser LibSass gérer la requête
  8.   // avaler le pass-through «@import "http://..."»
  9.   // (probablement un bogue)
  10.  
  11.   Sass_Import_List list = sass_make_import_list(1);
  12.   list[0] = sass_make_import_entry(path, 0, 0);
  13.   return list;
  14. }
  15.  
  16. Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
  17.   // renvoyer une erreur pour arrêter l'exécution
  18.   Sass_Import_List list = sass_make_import_list(1);
  19.   const char* message = "un message d'erreur";
  20.   list[0] = sass_make_import_entry(path, 0, 0);
  21.   sass_import_set_error(list[0], sass_copy_c_string(message), 0, 0);
  22.   return list;
  23. }
  24.  
  25. Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
  26.   // laissez LibSass charger le fichier identifié par l'importateur
  27.   Sass_Import_List list = sass_make_import_list(1);
  28.   list[0] = sass_make_import_entry("/tmp/file.scss", 0, 0);
  29.   return list;
  30. }
  31.  
  32. Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
  33.   // masquer complètement l'importation
  34.   // (probablement un bogue)
  35.   Sass_Import_List list = sass_make_import_list(0);
  36.   return list;
  37. }
  38.  
  39. Sass_Import_List importer(const char* path, Sass_Importer_Entry cb, struct Sass_Compiler* comp) {
  40.   // masquer complètement l'importation
  41.   // (probablement un bogue)
  42.   Sass_Import_List list = sass_make_import_list(1);
  43.   list[0] = sass_make_import_entry(0, 0, 0);
  44.   return list;
  45. }


Dernière mise à jour : Mardi, le 8 octobre 2024