diff options
Diffstat (limited to 'src/site.rs')
-rw-r--r-- | src/site.rs | 68 |
1 files changed, 45 insertions, 23 deletions
diff --git a/src/site.rs b/src/site.rs index 1a58801..48de58d 100644 --- a/src/site.rs +++ b/src/site.rs @@ -2,7 +2,7 @@ use crate::error::SiteError; use crate::page::{MarkdownPage, UnprocessedPage, WikitextPage}; use crate::parser::WikitextParser; use crate::token::TokenPatterns; -use crate::util::{make_path_absolute, make_path_relative_to, make_relative_link, join_subpath}; +use crate::util::{join_subpath, make_path_absolute, make_path_relative_to, make_relative_link}; use log::{debug, info, trace}; use std::collections::HashMap; use std::path::{Path, PathBuf}; @@ -13,6 +13,7 @@ pub struct Site { unprocessed_pages: Vec<UnprocessedPage>, markdown_pages: Vec<MarkdownPage>, files: Vec<PathBuf>, + included_files: Vec<PathBuf>, patterns: TokenPatterns, srcdir: PathBuf, destdir: PathBuf, @@ -20,6 +21,9 @@ pub struct Site { } impl Site { + const EXCLUDE_SUBSTRINGS: &'static [&'static str] = &[".git"]; + const EXCLUDE_ENDS: &'static [&'static str] = &[".git", "~", "#"]; + pub fn new<P>(srcdir: P, destdir: P) -> Self where P: AsRef<Path>, @@ -29,6 +33,7 @@ impl Site { unprocessed_pages: vec![], markdown_pages: vec![], files: vec![], + included_files: vec![], patterns: TokenPatterns::default(), srcdir: srcdir.as_ref().into(), destdir: destdir.as_ref().into(), @@ -38,6 +43,8 @@ impl Site { pub fn scan(&mut self) -> Result<(), SiteError> { for filename in Self::all_files(&self.srcdir)? { + self.included_files + .push(make_path_relative_to(&self.srcdir, &filename)); if Self::is_markdown(&filename) { let page = WikitextPage::read(&self.srcdir, &filename)?; self.add_wikitextpage(page); @@ -104,6 +111,10 @@ impl Site { &self.files } + pub fn included_files(&self) -> &[PathBuf] { + &self.included_files + } + pub fn input_filename(&self, filename: &Path) -> Result<PathBuf, SiteError> { Ok(join_subpath(&self.srcdir, filename)) } @@ -114,14 +125,35 @@ impl Site { fn all_files(root: &Path) -> Result<Vec<PathBuf>, SiteError> { let mut files = vec![]; - for e in WalkDir::new(root) { + for e in WalkDir::new(root) + { let e = e.map_err(|err| SiteError::WalkDir(root.to_path_buf(), err))?; let path = e.path(); - files.push(path.to_path_buf()); + if Self::is_excluded(path) { + debug!("exclude {}", path.display()); + } else { + debug!("include {}", path.display()); + files.push(path.to_path_buf()); + } } Ok(files) } + fn is_excluded(path: &Path) -> bool { + let path = path.to_string_lossy(); + for pat in Self::EXCLUDE_ENDS { + if path.ends_with(pat) { + return true; + } + } + for pat in Self::EXCLUDE_SUBSTRINGS { + if path.contains(pat) { + return true; + } + } + false + } + fn is_markdown(path: &Path) -> bool { if let Some(ext) = path.extension() { ext == "mdwn" @@ -171,7 +203,11 @@ impl Site { // Does a sibling page or file exist? if let Some(parent) = page.parent() { let path = parent.join(target); - trace!("checking for sibling in {}: {}", parent.display(), path.display()); + trace!( + "checking for sibling in {}: {}", + parent.display(), + path.display() + ); if let Some(path) = self.pages.get(path.as_path()) { trace!("sibling page exists: {}", path.display()); return Ok(path.into()); @@ -215,9 +251,7 @@ impl PageSet { } fn normalize(path: &Path) -> String { - path.to_str() - .expect("path is UTF8") - .to_lowercase() + path.to_str().expect("path is UTF8").to_lowercase() } } @@ -265,10 +299,7 @@ mod test { let mut site = Site::new(".", "."); site.add_wikitextpage(page("/foo/yo")); site.process().unwrap(); - assert_eq!( - site.resolve("/foo/bar", "yo").unwrap(), - Path::new("yo") - ); + assert_eq!(site.resolve("/foo/bar", "yo").unwrap(), Path::new("yo")); } #[test] @@ -276,10 +307,7 @@ mod test { let mut site = Site::new(".", "."); site.add_wikitextpage(page("/foo/yo")); site.process().unwrap(); - assert_eq!( - site.resolve("/foo/bar", "YO").unwrap(), - Path::new("yo") - ); + assert_eq!(site.resolve("/foo/bar", "YO").unwrap(), Path::new("yo")); } #[test] @@ -287,10 +315,7 @@ mod test { let mut site = Site::new(".", "."); site.add_wikitextpage(page("/foo/bar/yo")); site.process().unwrap(); - assert_eq!( - site.resolve("/foo/bar", "yo").unwrap(), - Path::new("bar/yo") - ); + assert_eq!(site.resolve("/foo/bar", "yo").unwrap(), Path::new("bar/yo")); } #[test] @@ -299,10 +324,7 @@ mod test { site.add_wikitextpage(page("/foo/bar/yo")); site.add_wikitextpage(page("/foo/yo")); site.process().unwrap(); - assert_eq!( - site.resolve("/foo/bar", "yo").unwrap(), - Path::new("bar/yo") - ); + assert_eq!(site.resolve("/foo/bar", "yo").unwrap(), Path::new("bar/yo")); } #[test] |