diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-10-15 09:48:51 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-10-15 09:48:51 +0300 |
commit | de906b71f75bcf34f824f238479403bfa0cfbecb (patch) | |
tree | 4c61746ede66b41ac118a34383c6cf92d0de4d47 | |
parent | c2c8e50248a27ca99318b584dda10b87646beaff (diff) | |
download | riki-de906b71f75bcf34f824f238479403bfa0cfbecb.tar.gz |
refactor: use PathFilter in Site::all_files
Sponsored-by: author
-rw-r--r-- | src/site.rs | 26 | ||||
-rw-r--r-- | src/srcdir.rs | 7 |
2 files changed, 14 insertions, 19 deletions
diff --git a/src/site.rs b/src/site.rs index a9d6123..b314627 100644 --- a/src/site.rs +++ b/src/site.rs @@ -3,7 +3,7 @@ use crate::git::git_whatchanged; use crate::name::{Name, NameBuilder, Names}; use crate::page::{MarkdownPage, UnprocessedPage, WikitextPage}; use crate::parser::WikitextParser; -use crate::srcdir::SouceDir; +use crate::srcdir::{PathFilter, SouceDir}; use crate::token::TokenPatterns; use crate::util::make_relative_link; use log::{debug, info, trace}; @@ -142,14 +142,15 @@ impl Site { fn all_files(&self) -> Result<Vec<Name>, SiteError> { let mut srcdir = SouceDir::new(self.builder.srcdir()); srcdir.scan()?; + + let filter = PathFilter::new(Self::EXCLUDE_SUBSTRINGS, Self::EXCLUDE_ENDS); + let mut names = vec![]; let root = self.builder.srcdir(); trace!("all_files: root={}", root.display()); for path in srcdir.files() { trace!("all_files: path={}", path.display()); - if Self::is_excluded(path) { - debug!("exclude {}", path.display()); - } else { + if filter.is_included(path) { debug!("include {}", path.display()); if Self::is_markdown(path) { trace!("it's markdown"); @@ -160,26 +161,13 @@ impl Site { } else { trace!("it's not a file"); } + } else { + debug!("exclude {}", path.display()); } } Ok(names) } - 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" diff --git a/src/srcdir.rs b/src/srcdir.rs index 8d19a62..7958c60 100644 --- a/src/srcdir.rs +++ b/src/srcdir.rs @@ -44,6 +44,13 @@ pub struct PathFilter { } impl PathFilter { + pub fn new(subs: &[&'static str], suffixes: &[&'static str]) -> Self { + Self { + excluded_substrings: subs.to_vec(), + excluded_suffixes: suffixes.to_vec(), + } + } + pub fn exclude_substring(&mut self, s: &'static str) { self.excluded_substrings.push(s); } |