diff options
author | Lars Wirzenius <liw@liw.fi> | 2023-04-24 21:57:32 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2023-04-24 21:57:32 +0300 |
commit | 35fa7c305f26b7a80b1cbb46e7bc354aea56d62c (patch) | |
tree | 2333edde20faecf682b6e4328bfb2a1085c4ebe8 | |
parent | db6fdb1105f3b93b2700bf1a7f9a5ed0e25f472e (diff) | |
download | riki-35fa7c305f26b7a80b1cbb46e7bc354aea56d62c.tar.gz |
feat: add a silly map directive implementation
It's the same as the inline one, for now.
Sponsored-by: author
-rw-r--r-- | src/directive/brokenlinks.rs | 2 | ||||
-rw-r--r-- | src/directive/map.rs | 37 | ||||
-rw-r--r-- | src/directive/pagestats.rs | 2 |
3 files changed, 32 insertions, 9 deletions
diff --git a/src/directive/brokenlinks.rs b/src/directive/brokenlinks.rs index b95c0aa..aa135ec 100644 --- a/src/directive/brokenlinks.rs +++ b/src/directive/brokenlinks.rs @@ -21,6 +21,6 @@ impl DirectiveImplementation for Brokenlinks { "page {} uses unimplemented brokenlinks", meta.path().display() ); - Ok(Processed::Markdown("\n".into())) + Err(DirectiveError::UnimplementedDirective("brokenlinks".into())) } } diff --git a/src/directive/map.rs b/src/directive/map.rs index b4e4c59..a2f43a6 100644 --- a/src/directive/map.rs +++ b/src/directive/map.rs @@ -1,23 +1,46 @@ use crate::directive::{DirectiveError, DirectiveImplementation, Processed}; use crate::page::PageMeta; +use crate::pagespec::PageSpec; use crate::site::Site; +use crate::util::make_relative_link; use crate::wikitext::ParsedDirective; -use log::warn; +use std::path::Path; #[derive(Debug, Default, Eq, PartialEq)] -pub struct Map {} +pub struct Map { + pages: String, +} impl DirectiveImplementation for Map { const REQUIRED: &'static [&'static str] = &["pages"]; const ALLOWED: &'static [&'static str] = &["show"]; const ALLOW_ANY_UNNAMED: bool = true; - fn from_parsed(_: &ParsedDirective) -> Self { - Self::default() + fn from_parsed(p: &ParsedDirective) -> Self { + let args = p.args(); + let pages = args.get("pages").unwrap(); + Self::new(pages.to_string()) + } + + fn process(&self, site: &Site, meta: &mut PageMeta) -> Result<Processed, DirectiveError> { + let pagespec = PageSpec::new(meta.path(), &self.pages).map_err(DirectiveError::PageSpec)?; + let matches: Vec<String> = site + .markdown_pages() + .iter() + .filter(|page| pagespec.matches(site, page.meta().path())) + .map(|page| format!("* {}\n", Self::link(meta.path(), page.meta()))) + .collect(); + Ok(Processed::Markdown(matches.join(""))) + } +} + +impl Map { + pub fn new(pages: String) -> Self { + Self { pages } } - fn process(&self, _site: &Site, meta: &mut PageMeta) -> Result<Processed, DirectiveError> { - warn!("page {} uses unimplemented map", meta.path().display()); - Ok(Processed::Markdown("\n".into())) + fn link(container: &Path, meta: &PageMeta) -> String { + let link = make_relative_link(container, meta.path()); + format!("[{}]({})", meta.title(), link.display()) } } diff --git a/src/directive/pagestats.rs b/src/directive/pagestats.rs index c2afd9b..b81d2d8 100644 --- a/src/directive/pagestats.rs +++ b/src/directive/pagestats.rs @@ -21,6 +21,6 @@ impl DirectiveImplementation for PageStats { "page {} uses unimplemented pagestats", meta.path().display() ); - Ok(Processed::Markdown("\n".into())) + Err(DirectiveError::UnimplementedDirective("pagestats".into())) } } |