diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-10-02 07:08:17 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-10-02 07:08:17 +0000 |
commit | 0583399d1a3fbe702591a673872a815dc47b8d96 (patch) | |
tree | 3d2d09f26329ff8a15067ecb2f4713c2a34f9f00 | |
parent | 5b166e37ecb15ce62f2d397f432325d57a22fbc2 (diff) | |
parent | ad3a6175b43ed553944497947e43846f24319e4e (diff) | |
download | riki-0583399d1a3fbe702591a673872a815dc47b8d96.tar.gz |
Merge branch 'pagespec-on-everything' into 'main'
feat: in the page-spec command, match also non-markdown files
See merge request larswirzenius/riki!56
-rw-r--r-- | src/bin/riki.rs | 21 | ||||
-rw-r--r-- | src/pagespec.lalrpop | 2 | ||||
-rw-r--r-- | src/pagespec.rs | 22 | ||||
-rw-r--r-- | src/site.rs | 16 |
4 files changed, 34 insertions, 27 deletions
diff --git a/src/bin/riki.rs b/src/bin/riki.rs index 954e440..6feae6c 100644 --- a/src/bin/riki.rs +++ b/src/bin/riki.rs @@ -4,6 +4,7 @@ use log::{debug, error, info}; use riki::error::SiteError; use riki::git::{git_dirty, git_whatchanged}; use riki::name::Name; +use riki::pagespec; use riki::site::Site; use riki::time::parse_timestamp; use riki::util::{canonicalize, copy_file_from_source, get_mtime, mkdir, set_mtime}; @@ -264,9 +265,25 @@ impl PageSpec { let mut site = Site::new(&srcdir, &PathBuf::from("/tmp")); site.scan()?; site.process()?; - for page in site.pagespec(&self.path, &self.pagespec)? { - println!("{}", page.meta().path().display()); + + let pagespec = pagespec::PageSpec::new(&self.path, &self.pagespec)?; + + for page in site.markdown_pages() { + let path = page.meta().path(); + debug!("consider markdown page {}", path.display()); + if pagespec.matches(path) { + println!("{}", path.display()); + } } + + for name in site.files_only() { + let path = name.page_path(); + debug!("consider file {}", path.display()); + if pagespec.matches(path) { + println!("{}", path.display()); + } + } + Ok(()) } } diff --git a/src/pagespec.lalrpop b/src/pagespec.lalrpop index 49eeb08..c7c762a 100644 --- a/src/pagespec.lalrpop +++ b/src/pagespec.lalrpop @@ -8,7 +8,7 @@ pub Expr: Box<Expr> = { } Term: Box<Expr> = { - r"(\w|[*?/-])+" => Box::new(Expr::Glob(<>.to_string())), + r"(\w|[._*?/-])+" => Box::new(Expr::Glob(<>.to_string())), "!" <t:Term> => Box::new(Expr::Negate(t)), "(" <e:Expr> ")" => e, } diff --git a/src/pagespec.rs b/src/pagespec.rs index b337097..de365a9 100644 --- a/src/pagespec.rs +++ b/src/pagespec.rs @@ -4,6 +4,7 @@ //! expression that selects pages using an expression. See the ikiwiki //! documentation for more. +use log::trace; use std::path::{Path, PathBuf}; use lalrpop_util::lalrpop_mod; @@ -11,6 +12,7 @@ use lalrpop_util::lalrpop_mod; lalrpop_mod!(pub pagespec); /// A parsed PageSpec expression, ready to be matched on page paths. +#[derive(Debug)] pub struct PageSpec { container: PathBuf, expr: Box<Expr>, @@ -19,7 +21,8 @@ pub struct PageSpec { impl PageSpec { /// Create a new PageSpec. pub fn new(containing_page: &Path, spec: &str) -> Result<Self, PageSpecError> { - let expr = pagespec::ExprParser::new().parse(spec) + let expr = pagespec::ExprParser::new() + .parse(spec) .map_err(|e| PageSpecError::Parse(format!("{}", e)))?; Ok(Self { container: containing_page.into(), @@ -29,6 +32,10 @@ impl PageSpec { /// Match a PageSpec on a page path. pub fn matches(&self, page_path: &Path) -> bool { + trace!( + "PageSpec::matches: container={} page_path={}", + self.container.display(), page_path.display() + ); assert!(page_path.is_absolute()); if let Ok(path) = page_path.strip_prefix(&self.container) { let path = format!("{}", path.display()); @@ -56,15 +63,14 @@ pub enum Expr { impl Expr { fn matches(&self, path: &str) -> bool { + trace!("Expr::matches: path={:?} self={:?}", path, self); match self { Self::Glob(glob) => glob_matches(glob, path), Self::Negate(expr) => !expr.matches(path), - Self::Op(left, op, right) => { - match op { - OpCode::And => left.matches(path) && right.matches(path), - OpCode::Or => left.matches(path) || right.matches(path), - } - } + Self::Op(left, op, right) => match op { + OpCode::And => left.matches(path) && right.matches(path), + OpCode::Or => left.matches(path) || right.matches(path), + }, } } } @@ -97,7 +103,7 @@ fn glob_matches_helper(mut glob: &[char], mut path: &[char]) -> bool { let mut path_remain = &path[..]; while !path_remain.is_empty() { if glob_matches_helper(&glob[1..], path_remain) { - return true + return true; } path_remain = &path_remain[1..]; } diff --git a/src/site.rs b/src/site.rs index 6cb539b..571bc11 100644 --- a/src/site.rs +++ b/src/site.rs @@ -2,7 +2,6 @@ use crate::error::SiteError; use crate::git::git_whatchanged; use crate::name::{Name, NameBuilder, Names}; use crate::page::{MarkdownPage, UnprocessedPage, WikitextPage}; -use crate::pagespec::PageSpec; use crate::parser::WikitextParser; use crate::token::TokenPatterns; use crate::util::make_relative_link; @@ -304,21 +303,6 @@ impl Site { pub fn shortcut(&self, name: &str) -> Option<&Shortcut> { self.shortcuts.get(name) } - - pub fn pagespec(&self, container: &Path, spec: &str) -> Result<Vec<&MarkdownPage>, SiteError> { - println!("find matches for {:?} in {}", spec, container.display()); - println!("there are {} markdown pages", self.markdown_pages.len()); - let pagespec = PageSpec::new(container, spec)?; - let mut matches = vec![]; - for page in self.markdown_pages.iter() { - println!("pagespec: consider {}", page.meta().path().display()); - if pagespec.matches(page.meta().path()) { - println!("match!"); - matches.push(page); - } - } - Ok(matches) - } } #[derive(Default)] |