Skip to content

Commit e690924

Browse files
committed
Add configuration for supporting additional extensions as .scss
This is big step towards removing non-standard raw css imports. Using `sass_option_push_import_extension` implementors can choose to treat any extension they like as a `.scss` file. Closes #1964
1 parent 3af837c commit e690924

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

include/sass/context.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ ADDAPI size_t ADDCALL sass_compiler_get_callee_stack_size(struct Sass_Compiler*
149149
ADDAPI Sass_Callee_Entry ADDCALL sass_compiler_get_last_callee(struct Sass_Compiler* compiler);
150150
ADDAPI Sass_Callee_Entry ADDCALL sass_compiler_get_callee_entry(struct Sass_Compiler* compiler, size_t idx);
151151

152+
// Push function for import extenions
153+
ADDAPI void ADDCALL sass_option_push_import_extension (struct Sass_Options* options, const char* ext);
154+
152155
// Push function for paths (no manipulation support for now)
153156
ADDAPI void ADDCALL sass_option_push_plugin_path (struct Sass_Options* options, const char* path);
154157
ADDAPI void ADDCALL sass_option_push_include_path (struct Sass_Options* options, const char* path);

src/context.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ namespace Sass {
104104
// include_paths.push_back(CWD);
105105

106106
// collect more paths from different options
107+
collect_extensions(c_options.extension);
108+
collect_extensions(c_options.extensions);
107109
collect_include_paths(c_options.include_path);
108110
collect_include_paths(c_options.include_paths);
109111
collect_plugin_paths(c_options.plugin_path);
@@ -174,6 +176,37 @@ namespace Sass {
174176
{
175177
}
176178

179+
void Context::collect_extensions(const char* exts_str)
180+
{
181+
if (exts_str) {
182+
const char* beg = exts_str;
183+
const char* end = Prelexer::find_first<PATH_SEP>(beg);
184+
185+
while (end) {
186+
std::string ext(beg, end - beg);
187+
if (!ext.empty()) {
188+
extensions.push_back(ext);
189+
}
190+
beg = end + 1;
191+
end = Prelexer::find_first<PATH_SEP>(beg);
192+
}
193+
194+
std::string ext(beg);
195+
if (!ext.empty()) {
196+
extensions.push_back(ext);
197+
}
198+
}
199+
}
200+
201+
void Context::collect_extensions(string_list* paths_array)
202+
{
203+
while (paths_array)
204+
{
205+
collect_extensions(paths_array->string);
206+
paths_array = paths_array->next;
207+
}
208+
}
209+
177210
void Context::collect_include_paths(const char* paths_str)
178211
{
179212
if (paths_str) {
@@ -244,15 +277,20 @@ namespace Sass {
244277
// looks for alternatives and returns a list from one directory
245278
std::vector<Include> Context::find_includes(const Importer& import)
246279
{
280+
// include configured extensions
281+
std::vector<std::string> exts(File::defaultExtensions);
282+
if (extensions.size() > 0) {
283+
exts.insert(exts.end(), extensions.begin(), extensions.end());
284+
}
247285
// make sure we resolve against an absolute path
248286
std::string base_path(rel2abs(import.base_path));
249287
// first try to resolve the load path relative to the base path
250-
std::vector<Include> vec(resolve_includes(base_path, import.imp_path));
288+
std::vector<Include> vec(resolve_includes(base_path, import.imp_path, exts));
251289
// then search in every include path (but only if nothing found yet)
252290
for (size_t i = 0, S = include_paths.size(); vec.size() == 0 && i < S; ++i)
253291
{
254292
// call resolve_includes and individual base path and append all results
255-
std::vector<Include> resolved(resolve_includes(include_paths[i], import.imp_path));
293+
std::vector<Include> resolved(resolve_includes(include_paths[i], import.imp_path, exts));
256294
if (resolved.size()) vec.insert(vec.end(), resolved.begin(), resolved.end());
257295
}
258296
// return vector

src/context.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ namespace Sass {
6767

6868
std::vector<std::string> plugin_paths; // relative paths to load plugins
6969
std::vector<std::string> include_paths; // lookup paths for includes
70+
std::vector<std::string> extensions; // lookup extensions for imports`
7071

7172

7273

@@ -109,6 +110,8 @@ namespace Sass {
109110
void collect_plugin_paths(string_list* paths_array);
110111
void collect_include_paths(const char* paths_str);
111112
void collect_include_paths(string_list* paths_array);
113+
void collect_extensions(const char* extensions_str);
114+
void collect_extensions(string_list* extensions_array);
112115
std::string format_embedded_source_map();
113116
std::string format_source_mapping_url(const std::string& out_path);
114117

src/sass_context.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ namespace Sass {
7474
// move line_beg pointer to line start
7575
while (line_beg && *line_beg && lines != 0) {
7676
if (*line_beg == '\n') --lines;
77-
utf8::unchecked::next(line_beg);
77+
utf8::unchecked::next(line_beg);
7878
}
7979
const char* line_end = line_beg;
8080
// move line_end before next newline character
8181
while (line_end && *line_end && *line_end != '\n') {
8282
if (*line_end == '\n') break;
8383
if (*line_end == '\r') break;
84-
utf8::unchecked::next(line_end);
84+
utf8::unchecked::next(line_end);
8585
}
8686
if (line_end && *line_end != 0) ++ line_end;
8787
size_t line_len = line_end - line_beg;
@@ -524,6 +524,7 @@ extern "C" {
524524
options->c_headers = 0;
525525
options->plugin_paths = 0;
526526
options->include_paths = 0;
527+
options->extensions = 0;
527528
}
528529

529530
// helper function, not exported, only accessible locally
@@ -558,6 +559,18 @@ extern "C" {
558559
cur = next;
559560
}
560561
}
562+
// Deallocate extension
563+
if (options->extensions) {
564+
struct string_list* cur;
565+
struct string_list* next;
566+
cur = options->extensions;
567+
while (cur) {
568+
next = cur->next;
569+
free(cur->string);
570+
free(cur);
571+
cur = next;
572+
}
573+
}
561574
// Free options strings
562575
free(options->input_path);
563576
free(options->output_path);
@@ -577,6 +590,7 @@ extern "C" {
577590
options->c_headers = 0;
578591
options->plugin_paths = 0;
579592
options->include_paths = 0;
593+
options->extensions = 0;
580594
}
581595

582596
// helper function, not exported, only accessible locally
@@ -713,6 +727,22 @@ extern "C" {
713727
IMPLEMENT_SASS_CONTEXT_TAKER(char*, source_map_string);
714728
IMPLEMENT_SASS_CONTEXT_TAKER(char**, included_files);
715729

730+
// Push function for import extenions
731+
void ADDCALL sass_option_push_import_extension(struct Sass_Options* options, const char* ext)
732+
{
733+
struct string_list* extension = (struct string_list*) calloc(1, sizeof(struct string_list));
734+
if (extension == 0) return;
735+
extension->string = ext ? sass_copy_c_string(ext) : 0;
736+
struct string_list* last = options->extensions;
737+
if (!options->extensions) {
738+
options->extensions = extension;
739+
} else {
740+
while (last->next)
741+
last = last->next;
742+
last->next = extension;
743+
}
744+
}
745+
716746
// Push function for include paths (no manipulation support for now)
717747
void ADDCALL sass_option_push_include_path(struct Sass_Options* options, const char* path)
718748
{

src/sass_context.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ struct Sass_Options : Sass_Output_Options {
4040
// Colon-separated list of paths
4141
// Semicolon-separated on Windows
4242
// Maybe use array interface instead?
43+
char* extension;
4344
char* include_path;
4445
char* plugin_path;
4546

47+
// Extensions (linked string list)
48+
struct string_list* extensions;
4649
// Include paths (linked string list)
4750
struct string_list* include_paths;
4851
// Plugin paths (linked string list)
@@ -126,4 +129,4 @@ struct Sass_Compiler {
126129
Sass::Block_Obj root;
127130
};
128131

129-
#endif
132+
#endif

0 commit comments

Comments
 (0)