Skip to content

Commit

Permalink
refactor: File gathering
Browse files Browse the repository at this point in the history
I thought it'd be common to only look at the relative paths
but over time that wasn't the case and made things messy, so
I went and simplified all of this

BREAKING CHANGE: `page.source` for drafts will no longer be the literal
source path but will be the path as-if the page wasn't a draft.
  • Loading branch information
Ed Page authored and epage committed Oct 26, 2017
1 parent 8676a3a commit 40c2dd3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 52 deletions.
10 changes: 7 additions & 3 deletions src/bin/cobalt/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ pub fn watch_command(matches: &clap::ArgMatches) -> Result<()> {

build::build(&config)?;

let source = path::Path::new(&config.source);
// canonicalize is to ensure there is no question that `watcher`s paths come back safe for
// Files::includes_file
let source = path::Path::new(&config.source)
.canonicalize()
.chain_err(|| "Failed in processing source")?;
let dest = path::Path::new(&config.destination).to_owned();

// Be as broad as possible in what can cause a rebuild to
// ensure we don't miss anything (normal file walks will miss
// `_layouts`, etc).
let mut site_files = files::FilesBuilder::new(source)?;
let mut site_files = files::FilesBuilder::new(&source)?;
site_files.ignore_hidden(false)?;
for line in &config.ignore {
site_files.add_ignore(line.as_str())?;
Expand All @@ -56,7 +60,7 @@ pub fn watch_command(matches: &clap::ArgMatches) -> Result<()> {
let (tx, rx) = channel();
let mut watcher = raw_watcher(tx).chain_err(|| "Notify error")?;
watcher
.watch(&config.source, RecursiveMode::Recursive)
.watch(&source, RecursiveMode::Recursive)
.chain_err(|| "Notify error")?;
info!("Watching {:?} for changes", &config.source);

Expand Down
42 changes: 23 additions & 19 deletions src/cobalt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,20 @@ pub fn build(config: &Config) -> Result<()> {
for file_path in page_files.files().filter(|p| {
template_extensions.contains(&p.extension().unwrap_or_else(|| OsStr::new("")))
}) {
// if the document is in the posts folder it's considered a post
let src_path = source.join(file_path.as_path());
let is_post = src_path.starts_with(posts_path.as_path());
let rel_src = file_path
.strip_prefix(source)
.expect("file was found under the root");

// if the document is in the posts folder it's considered a post
let is_post = file_path.starts_with(posts_path.as_path());
let default_front = if is_post {
config.posts.default.clone()
} else {
config.pages.default.clone()
};

let doc = Document::parse(source, &file_path, &file_path, default_front)
.chain_err(|| format!("Failed to parse {:?}", src_path))?;
let doc = Document::parse(&file_path, rel_src, default_front)
.chain_err(|| format!("Failed to parse {:?}", rel_src))?;
if !doc.front.is_draft || config.include_drafts {
documents.push(doc);
}
Expand All @@ -81,18 +83,16 @@ pub fn build(config: &Config) -> Result<()> {
for file_path in draft_files.files().filter(|p| {
template_extensions.contains(&p.extension().unwrap_or_else(|| OsStr::new("")))
}) {
let new_path = posts_path.join(&file_path);
let new_path = new_path
.strip_prefix(source)
.expect("Entry not in source folder");
// Provide a fake path as if it was not a draft
let rel_src = file_path
.strip_prefix(&drafts_root)
.expect("file was found under the root");
let new_path = Path::new(&config.posts.dir).join(rel_src);

let default_front = config.posts.default.clone().set_draft(true);

let doc = Document::parse(&drafts_root, &file_path, new_path, default_front)
.chain_err(|| {
let src_path = drafts_root.join(file_path);
format!("Failed to parse {:?}", src_path)
})?;
let doc = Document::parse(&file_path, &new_path, default_front)
.chain_err(|| format!("Failed to parse {:?}", rel_src))?;
documents.push(doc);
}
}
Expand Down Expand Up @@ -233,8 +233,10 @@ pub fn build(config: &Config) -> Result<()> {
if file_path.extension() == Some(OsStr::new("scss")) {
compile_sass(config, source, dest, file_path)?;
} else {
let src_file = source.join(&file_path);
copy_file(src_file.as_path(), dest.join(file_path).as_path())?;
let rel_src = file_path
.strip_prefix(source)
.expect("file was found under the root");
copy_file(&file_path, dest.join(rel_src).as_path())?;
}
}
}
Expand Down Expand Up @@ -349,10 +351,12 @@ fn compile_sass_internal(config: &Config,
SassOutputStyle::Compact => sass_rs::OutputStyle::Compact,
SassOutputStyle::Compressed => sass_rs::OutputStyle::Compressed,
};
let content = sass_rs::compile_file(file_path, sass_opts)?;

let src_file = source.join(file_path);
let content = sass_rs::compile_file(src_file.as_path(), sass_opts.clone())?;
let mut dest_file = dest.join(file_path);
let rel_src = file_path
.strip_prefix(source)
.expect("file was found under the root");
let mut dest_file = dest.join(rel_src);
dest_file.set_extension("css");

create_document_file(content, dest_file)
Expand Down
26 changes: 8 additions & 18 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ fn read_file<P: AsRef<Path>>(path: P) -> Result<String> {
Ok(text)
}

fn read_document<PB: Into<PathBuf>, P: AsRef<Path>>(root: PB, relpath: P) -> Result<String> {
let path = root.into().join(relpath);
let mut file = File::open(path)?;
let mut text = String::new();
file.read_to_string(&mut text)?;
Ok(text)
}

fn split_document(content: &str) -> Result<(Option<&str>, &str)> {
if FRONT_MATTER_DIVIDE.is_match(content) {
let mut splits = FRONT_MATTER_DIVIDE.splitn(content, 2);
Expand Down Expand Up @@ -226,13 +218,12 @@ impl Document {
}
}

pub fn parse(root_path: &Path,
source_file: &Path,
dest_file: &Path,
pub fn parse(src_path: &Path,
rel_path: &Path,
default_front: frontmatter::FrontmatterBuilder)
-> Result<Document> {
trace!("Parsing {:?}", source_file);
let content = read_document(root_path, source_file)?;
trace!("Parsing {:?}", rel_path);
let content = read_file(src_path)?;
let (front, content) = split_document(&content)?;
let legacy_front: wildwest::FrontmatterBuilder =
front
Expand All @@ -241,21 +232,20 @@ impl Document {
.unwrap_or_else(wildwest::FrontmatterBuilder::new);

let front: frontmatter::FrontmatterBuilder = legacy_front.into();
let front = front.merge_path(dest_file).merge(default_front);
let front = front.merge_path(rel_path).merge(default_front);

let front = front.build()?;

let perma_attributes = permalink_attributes(&front, dest_file);
let perma_attributes = permalink_attributes(&front, rel_path);
let (file_path, url_path) = {
let permalink = front.path.as_ref();
let url_path = explode_permalink(permalink, perma_attributes);
let file_path = format_url_as_file(&url_path);
(file_path, url_path)
};

let doc_attributes = document_attributes(&front,
source_file.to_str().unwrap_or(""),
url_path.as_ref());
let doc_attributes =
document_attributes(&front, rel_path.to_str().unwrap_or(""), url_path.as_ref());

Ok(Document::new(url_path,
file_path,
Expand Down
12 changes: 5 additions & 7 deletions src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,7 @@ impl<'a> FilesIterator<'a> {
.filter_entry(move |e| files.includes_entry(e))
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.filter_map(move |e| {
e.path()
.strip_prefix(files.root_dir.as_path())
.ok()
.map(|p| p.to_path_buf())
});
.map(move |e| e.path().to_path_buf());
FilesIterator { inner: Box::new(walker) }
}
}
Expand Down Expand Up @@ -431,7 +426,10 @@ mod tests {
fn files_iter_matches_include() {
let root_dir = Path::new("tests/fixtures/hidden_files");
let files = FilesBuilder::new(root_dir).unwrap().build().unwrap();
let mut actual: Vec<_> = files.files().collect();
let mut actual: Vec<_> = files
.files()
.map(|f| f.strip_prefix(root_dir).unwrap().to_owned())
.collect();
actual.sort();

let expected = vec![Path::new("child/child.txt").to_path_buf(),
Expand Down
13 changes: 8 additions & 5 deletions src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,19 @@ fn load_data(data_path: &path::Path) -> Result<liquid::Value> {
fn insert_data_dir(data: &mut liquid::Object, data_root: &path::Path) -> Result<()> {
let data_files_builder = files::FilesBuilder::new(data_root)?;
let data_files = data_files_builder.build()?;
for df in data_files.files() {
let file_stem = df.file_stem()
for full_path in data_files.files() {
let rel_path = full_path
.strip_prefix(data_root)
.expect("file was found under the root");

let file_stem = full_path
.file_stem()
.expect("Files will always return with a stem");
let file_stem = String::from(file_stem.to_str().unwrap());

let full_path = data_root.join(df.clone());

let data_fragment = load_data(&full_path)?;

deep_insert(data, &df, file_stem, data_fragment)?;
deep_insert(data, rel_path, file_stem, data_fragment)?;
}

Ok(())
Expand Down

0 comments on commit 40c2dd3

Please sign in to comment.