-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglob.cpp
62 lines (53 loc) · 2.06 KB
/
glob.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <stdint.h>
#include <string>
#include "FS.hpp"
#include "sass.h"
#include <libgen.h>
// return version of libsass we are linked against
extern "C" const char* ADDCALL libsass_get_version() {
return libsass_version();
}
// create a custom importer to resolve glob-based includes
Sass_Import_List glob_importer(const char* cur_path, Sass_Importer_Entry cb, struct Sass_Compiler* comp)
{
// get the base directory from previous import
Sass_Import_Entry imp = sass_compiler_get_last_import(comp);
char* prev = strdup(sass_import_get_abs_path(imp));
std::string pattern(dirname(prev)); std::free(prev);
pattern += std::string("/") + cur_path;
// instantiate the matcher instance
FS::Match matcher(pattern);
// get vector of matches (results are cached)
const std::vector<FS::Entry*> matches = matcher.getMatches();
// propagate error back to libsass
if (matches.empty()) return NULL;
// get the cookie from importer descriptor
// void* cookie = sass_importer_get_cookie(cb);
// create a list to hold our import entries
Sass_Import_List incs = sass_make_import_list(matches.size());
// iterate over the list and print out the results
for (size_t i = 0; i < matches.size(); ++i) {
// create intermediate string object
std::string path = matches[i]->path();
// create the resolved import entries (paths to be loaded)
incs[i] = sass_make_import(path.c_str(), path.c_str(), 0, 0);
}
// return imports
return incs;
}
// entry point for libsass to request custom importers from plugin
extern "C" Sass_Importer_List ADDCALL libsass_load_importers()
{
// allocate a custom function caller
Sass_Importer_Entry c_header =
sass_make_importer(glob_importer, 3000, (void*) 0);
// create list of all custom functions
Sass_Importer_List imp_list = sass_make_importer_list(1);
// put the only function in this plugin to the list
sass_importer_set_list_entry(imp_list, 0, c_header);
// return the list
return imp_list;
}