Commit 55c191c2 by Aaron Leung

Merge pull request #28 from QuLogic/import_tweaks

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