Commit 55c191c2 by Aaron Leung

Merge pull request #28 from QuLogic/import_tweaks

Import tweaks
parents cdb98139 fff6118b
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "error.hpp" #include "error.hpp"
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <sys/stat.h>
namespace Sass { namespace Sass {
...@@ -30,28 +31,28 @@ namespace Sass { ...@@ -30,28 +31,28 @@ namespace Sass {
{ {
std::FILE *f; std::FILE *f;
const char* path_str = path.c_str(); const char* path_str = path.c_str();
f = std::fopen(path_str, "rb"); struct stat st;
if (!f) { string tmp;
string path_with_extension(path + ".scss"); if (stat(path_str, &st) == -1 || S_ISDIR(st.st_mode)) {
f = std::fopen(path_with_extension.c_str(), "rb"); tmp = path + ".scss";
if (!f) { path_str = tmp.c_str();
const char* file_name_str = Prelexer::folders(path_str); if (stat(path_str, &st) == -1 || S_ISDIR(st.st_mode)) {
string path_with_underscore(Token::make(path_str, file_name_str).to_string() + const char *full_path_str = path.c_str();
const char *file_name_str = Prelexer::folders(full_path_str);
tmp = Token::make(full_path_str, file_name_str).to_string() +
"_" + "_" +
Token::make(file_name_str).to_string()); string(file_name_str);
f = std::fopen(path_with_underscore.c_str(), "rb"); path_str = tmp.c_str();
if (!f) { if (stat(path_str, &st) == -1 || S_ISDIR(st.st_mode)) {
string path_with_underscore_and_extension(path_with_underscore + ".scss"); tmp = tmp + ".scss";
f = std::fopen(path_with_underscore_and_extension.c_str(), "rb"); path_str = tmp.c_str();
if (!f) throw path; if (stat(path_str, &st) == -1 || S_ISDIR(st.st_mode))
throw path;
} }
} }
} }
if (std::fseek(f, 0, SEEK_END)) throw path; f = std::fopen(path_str, "rb");
int status = std::ftell(f); size_t len = st.st_size;
if (status < 0) throw path;
size_t len = status;
std::rewind(f);
char* source = new char[len + 1]; char* source = new char[len + 1];
size_t bytes_read = std::fread(source, sizeof(char), len, f); size_t bytes_read = std::fread(source, sizeof(char), len, f);
if (bytes_read != len) { if (bytes_read != len) {
...@@ -61,6 +62,8 @@ namespace Sass { ...@@ -61,6 +62,8 @@ namespace Sass {
source[len] = '\0'; source[len] = '\0';
char* end = source + len; char* end = source + len;
if (std::fclose(f)) throw path; if (std::fclose(f)) throw path;
const char *file_name_str = Prelexer::folders(path_str);
string include_path(path_str, file_name_str - path_str);
Document doc(ctx); Document doc(ctx);
doc.path = path; doc.path = path;
...@@ -72,6 +75,9 @@ namespace Sass { ...@@ -72,6 +75,9 @@ namespace Sass {
doc.end = end; doc.end = end;
doc.position = source; doc.position = source;
doc.context.source_refs.push_back(source); doc.context.source_refs.push_back(source);
if (!include_path.empty()) {
doc.context.include_paths.push_back(include_path);
}
return doc; return doc;
} }
......
...@@ -101,17 +101,16 @@ namespace Sass { ...@@ -101,17 +101,16 @@ namespace Sass {
if (!lex< string_constant >()) throw_syntax_error("@import directive requires a url or quoted path"); if (!lex< string_constant >()) throw_syntax_error("@import directive requires a url or quoted path");
// TO DO: BETTER PATH HANDLING // TO DO: BETTER PATH HANDLING
string import_path(lexed.unquote()); string import_path(lexed.unquote());
const char* curr_path_start = path.c_str(); for (vector<string>::iterator path = context.include_paths.begin(); path < context.include_paths.end(); ++path) {
const char* curr_path_end = folders(curr_path_start);
string current_path(curr_path_start, curr_path_end - curr_path_start);
try { try {
Document importee(Document::make_from_file(context, current_path + import_path)); Document importee(Document::make_from_file(context, *path + import_path));
importee.parse_scss(); importee.parse_scss();
return importee.root; return importee.root;
} }
catch (string& path) { catch (string& path) {
throw_read_error("error reading file \"" + path + "\"");
} }
}
throw_read_error("error reading file \"" + import_path + "\"");
// unreached statement // unreached statement
return Node(); return Node();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment