added find_import to import and renamed lib method to from_paths

This commit is contained in:
Joe Ling - uni laptop 2020-07-13 18:18:25 +01:00
parent 33a2c7efbf
commit ec5ab05330
3 changed files with 85 additions and 128 deletions

View File

@ -140,6 +140,13 @@ fn raw_to_parse_error(map: &CodeMap, err: Error) -> Box<Error> {
#[cfg_attr(not(feature = "profiling"), inline)]
#[cfg(not(feature = "wasm"))]
pub fn from_path(p: &str) -> Result<String> {
from_paths(p, &Vec::new())
}
#[cfg_attr(feature = "profiling", inline(never))]
#[cfg_attr(not(feature = "profiling"), inline)]
#[cfg(not(feature = "wasm"))]
pub fn from_paths(p: &str, loadpaths: &[&Path]) -> Result<String> {
let mut map = CodeMap::new();
let file = map.add_file(p.into(), String::from_utf8(fs::read(p)?)?);
let empty_span = file.span.subspan(0, 0);
@ -161,7 +168,7 @@ pub fn from_path(p: &str) -> Result<String> {
at_root_has_selector: false,
extender: &mut Extender::new(empty_span),
content_scopes: &mut Scopes::new(),
load_paths: &Vec::new(),
load_paths: loadpaths,
}
.parse()
.map_err(|e| raw_to_parse_error(&map, *e))?;
@ -171,7 +178,6 @@ pub fn from_path(p: &str) -> Result<String> {
.pretty_print(&map)
.map_err(|e| raw_to_parse_error(&map, *e))
}
/// Compile CSS from a string
///
/// ```
@ -249,39 +255,3 @@ pub fn from_string(p: String) -> std::result::Result<String, JsValue> {
.pretty_print(&map)
.map_err(|e| raw_to_parse_error(&map, *e).to_string())?)
}
#[cfg_attr(feature = "profiling", inline(never))]
#[cfg_attr(not(feature = "profiling"), inline)]
#[cfg(not(feature = "wasm"))]
pub fn from_path_with_load_paths(p: &str, loadpaths: &[&Path]) -> Result<String> {
let mut map = CodeMap::new();
let file = map.add_file(p.into(), String::from_utf8(fs::read(p)?)?);
let empty_span = file.span.subspan(0, 0);
let stmts = Parser {
toks: &mut Lexer::new(&file)
.collect::<Vec<Token>>()
.into_iter()
.peekmore(),
map: &mut map,
path: p.as_ref(),
scopes: &mut Scopes::new(),
global_scope: &mut Scope::new(),
super_selectors: &mut NeverEmptyVec::new(Selector::new(empty_span)),
span_before: empty_span,
content: &mut Vec::new(),
flags: ContextFlags::empty(),
at_root: true,
at_root_has_selector: false,
extender: &mut Extender::new(empty_span),
content_scopes: &mut Scopes::new(),
load_paths: loadpaths,
}
.parse()
.map_err(|e| raw_to_parse_error(&map, *e))?;
Css::from_stmts(stmts, false)
.map_err(|e| raw_to_parse_error(&map, *e))?
.pretty_print(&map)
.map_err(|e| raw_to_parse_error(&map, *e))
}

View File

@ -7,7 +7,7 @@ use std::{
use clap::{arg_enum, App, AppSettings, Arg};
#[cfg(not(feature = "wasm"))]
use grass::from_path_with_load_paths;
use grass::from_paths;
arg_enum! {
#[derive(PartialEq, Debug)]
@ -193,7 +193,7 @@ fn main() -> std::io::Result<()> {
if let Some(path) = matches.value_of("OUTPUT") {
let mut buf = BufWriter::new(File::open(path).unwrap_or(File::create(path)?));
buf.write_all(
from_path_with_load_paths(name, &vals)
from_paths(name, &vals)
.unwrap_or_else(|e| {
eprintln!("{}", e);
std::process::exit(1)
@ -203,7 +203,7 @@ fn main() -> std::io::Result<()> {
} else {
let mut stdout = BufWriter::new(stdout());
stdout.write_all(
from_path_with_load_paths(name, &vals)
from_paths(name, &vals)
.unwrap_or_else(|e| {
eprintln!("{}", e);
std::process::exit(1)

View File

@ -7,6 +7,56 @@ use crate::{common::QuoteKind, error::SassResult, lexer::Lexer, value::Value, To
use super::{Parser, Stmt};
/// Searches the current directory of the file then searches in load paths directories
/// if the import has not yet been found.
/// [https://sass-lang.com/documentation/at-rules/import#finding-the-file](finding a file)
/// [https://sass-lang.com/documentation/at-rules/import#load-paths](load path)
fn find_import(file_path: &PathBuf, name: &OsStr, load_paths: &[&Path]) -> Option<PathBuf> {
let paths = [
file_path.with_file_name(name).with_extension("scss"),
file_path
.with_file_name(format!("_{}", name.to_str().unwrap()))
.with_extension("scss"),
file_path.clone(),
file_path.join("index.scss"),
file_path.join("_index.scss"),
];
for name in &paths {
if name.is_file() {
return Some(name.to_path_buf());
}
}
for path in load_paths {
let paths: Vec<PathBuf> = if path.is_dir() {
vec![
path.join(format!("{}.scss", name.to_str().unwrap())),
path.join(format!("_{}.scss", name.to_str().unwrap())),
path.join("index.scss"),
path.join("_index.scss"),
]
} else {
vec![
path.to_path_buf(),
path.with_file_name(name).with_extension("scss"),
path.with_file_name(format!("_{}", name.to_str().unwrap()))
.with_extension("scss"),
path.join("index.scss"),
path.join("_index.scss"),
]
};
for name in paths {
if name.is_file() {
return Some(name);
}
}
}
None
}
impl<'a> Parser<'a> {
pub(super) fn import(&mut self) -> SassResult<Vec<Stmt>> {
self.whitespace();
@ -57,21 +107,10 @@ impl<'a> Parser<'a> {
let name = path_buf.file_name().unwrap_or_else(|| OsStr::new(".."));
let paths = [
path_buf.with_file_name(name).with_extension("scss"),
path_buf
.with_file_name(format!("_{}", name.to_str().unwrap()))
.with_extension("scss"),
path_buf.clone(),
path_buf.join("index.scss"),
path_buf.join("_index.scss"),
];
for name in &paths {
if name.is_file() {
if let Some(name) = find_import(&path_buf, name, self.load_paths) {
let file = self.map.add_file(
name.to_string_lossy().into(),
String::from_utf8(fs::read(name)?)?,
String::from_utf8(fs::read(&name)?)?,
);
return Parser {
@ -80,7 +119,7 @@ impl<'a> Parser<'a> {
.into_iter()
.peekmore(),
map: self.map,
path: name.as_ref(),
path: &name,
scopes: self.scopes,
global_scope: self.global_scope,
super_selectors: self.super_selectors,
@ -95,58 +134,6 @@ impl<'a> Parser<'a> {
}
.parse();
}
}
for path in self.load_paths {
let paths: Vec<PathBuf> = if path.is_dir() {
vec![
path.join(format!("{}.scss", name.to_str().unwrap())),
path.join(format!("_{}.scss", name.to_str().unwrap())),
path.join("index.scss"),
path.join("_index.scss"),
]
} else {
vec![
path.to_path_buf(),
path.with_file_name(name).with_extension("scss"),
path.with_file_name(format!("_{}", name.to_str().unwrap()))
.with_extension("scss"),
path.join("index.scss"),
path.join("_index.scss"),
]
};
for name in &paths {
if name.is_file() {
println!("found file: {:?}", name);
let file = self.map.add_file(
name.to_string_lossy().into(),
String::from_utf8(fs::read(name)?)?,
);
return Parser {
toks: &mut Lexer::new(&file)
.collect::<Vec<Token>>()
.into_iter()
.peekmore(),
map: self.map,
path: name.as_ref(),
scopes: self.scopes,
global_scope: self.global_scope,
super_selectors: self.super_selectors,
span_before: file.span.subspan(0, 0),
content: self.content,
flags: self.flags,
at_root: self.at_root,
at_root_has_selector: self.at_root_has_selector,
extender: self.extender,
content_scopes: self.content_scopes,
load_paths: self.load_paths,
}
.parse();
}
}
}
Err(("Can't find stylesheet to import.", span).into())
}