Skip to content

Commit

Permalink
Split compilation step into parse and execute steps
Browse files Browse the repository at this point in the history
This allows to query include files before the compile step!
  • Loading branch information
mgreter committed Nov 12, 2014
1 parent b0ea135 commit c96db76
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 130 deletions.
97 changes: 59 additions & 38 deletions context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace Sass {
source_map (resolve_relative_path(initializers.output_path(), initializers.source_map_file(), get_cwd())),
c_functions (vector<Sass_C_Function_Callback>()),
image_path (initializers.image_path()),
input_path (make_canonical_path(initializers.input_path())),
output_path (make_canonical_path(initializers.output_path())),
source_comments (initializers.source_comments()),
output_style (initializers.output_style()),
Expand All @@ -71,6 +72,11 @@ namespace Sass {
{
cwd = get_cwd();

// enforce some safe defaults
// used to create relative file links
if (input_path == "") input_path = "stdin";
if (output_path == "") output_path = "stdout";

include_paths.push_back(cwd);
collect_include_paths(initializers.include_paths_c_str());
collect_include_paths(initializers.include_paths_array());
Expand Down Expand Up @@ -207,7 +213,36 @@ namespace Sass {
void register_c_functions(Context&, Env* env, Sass_C_Function_List);
void register_c_function(Context&, Env* env, Sass_C_Function_Callback);

char* Context::compile_file()
char* Context::compile_block(Block* root)
{
char* result = 0;
if (!root) return 0;
switch (output_style) {
case COMPRESSED: {
Output_Compressed output_compressed(this);
root->perform(&output_compressed);
string output = output_compressed.get_buffer();
if (source_map_file != "" && !omit_source_map_url) {
output += format_source_mapping_url(source_map_file);
}
result = copy_c_str(output.c_str());
} break;

default: {
Output_Nested output_nested(source_comments, this);
root->perform(&output_nested);
string output = output_nested.get_buffer();
if (source_map_file != "" && !omit_source_map_url) {
output += "\n" + format_source_mapping_url(source_map_file);
}
result = copy_c_str(output.c_str());

} break;
}
return result;
}

Block* Context::parse_file()
{
Block* root = 0;
for (size_t i = 0; i < queue.size(); ++i) {
Expand Down Expand Up @@ -237,31 +272,32 @@ namespace Sass {
Remove_Placeholders remove_placeholders(*this);
root->perform(&remove_placeholders);

char* result = 0;
switch (output_style) {
case COMPRESSED: {
Output_Compressed output_compressed(this);
root->perform(&output_compressed);
string output = output_compressed.get_buffer();
if (source_map_file != "" && !omit_source_map_url) {
output += format_source_mapping_url(source_map_file);
}
result = copy_c_str(output.c_str());
} break;

default: {
Output_Nested output_nested(source_comments, this);
root->perform(&output_nested);
string output = output_nested.get_buffer();
if (source_map_file != "" && !omit_source_map_url) {
output += "\n" + format_source_mapping_url(source_map_file);
}
result = copy_c_str(output.c_str());
return root;
}

} break;
Block* Context::parse_string()
{
if (!source_c_str) return 0;
queue.clear();
if(is_indented_syntax_src) {
char * contents = sass2scss(source_c_str, SASS2SCSS_PRETTIFY_1);
add_source(input_path, input_path, contents);
return parse_file();
}
add_source(input_path, input_path, strdup(source_c_str));
return parse_file();
}

return result;
char* Context::compile_file()
{
// returns NULL if something fails
return compile_block(parse_file());
}

char* Context::compile_string()
{
// returns NULL if something fails
return compile_block(parse_string());
}

string Context::format_source_mapping_url(const string& file)
Expand All @@ -288,21 +324,6 @@ namespace Sass {
return result;
}

// allow to optionally overwrite the input path
// default argument for input_path is string("stdin")
// usefull to influence the source-map generating etc.
char* Context::compile_string(const string& input_path)
{
if (!source_c_str) return 0;
queue.clear();
if(is_indented_syntax_src) {
char * contents = sass2scss(source_c_str, SASS2SCSS_PRETTIFY_1);
add_source(input_path, input_path, contents);
return compile_file();
}
add_source(input_path, input_path, strdup(source_c_str));
return compile_file();
}

std::vector<std::string> Context::get_included_files(size_t skip)
{
Expand Down
7 changes: 6 additions & 1 deletion context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ namespace Sass {
vector<Sass_C_Function_Callback> c_functions;

string image_path; // for the image-url Sass function
string input_path; // for relative paths in src-map
string output_path; // for relative paths to the output
bool source_comments; // for inline debug comments in css output
Output_Style output_style; // output style for the generated css code
Expand All @@ -84,6 +85,7 @@ namespace Sass {
KWD_ARG_SET(Data) {
KWD_ARG(Data, const char*, source_c_str);
KWD_ARG(Data, string, entry_point);
KWD_ARG(Data, string, input_path);
KWD_ARG(Data, string, output_path);
KWD_ARG(Data, string, image_path);
KWD_ARG(Data, const char*, include_paths_c_str);
Expand All @@ -105,13 +107,16 @@ namespace Sass {
~Context();
void setup_color_map();
string add_file(string);
Block* parse_file();
string add_file(string, string);
Block* parse_string();
void add_source(string, string, const char*);
// allow to optionally overwrite the input path
// default argument for input_path is string("stdin")
// usefull to influence the source-map generating etc.
char* compile_string(const string& input_path = "stdin");
char* compile_file();
char* compile_string();
char* compile_block(Block* root);
char* generate_source_map();

vector<string> get_included_files(size_t skip = 0);
Expand Down
Loading

0 comments on commit c96db76

Please sign in to comment.