diff options
author | Lars Wirzenius <liw@liw.fi> | 2023-04-24 19:06:46 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2023-04-24 19:06:46 +0000 |
commit | 51c086c3c7091364842a93ba3e8d2171a7ad89dc (patch) | |
tree | a4bebd5c6653d5cc9ada2a9033dec853f7f0f457 | |
parent | db6fdb1105f3b93b2700bf1a7f9a5ed0e25f472e (diff) | |
parent | b64e737671b5d251a15bfec76f57d76d691d396f (diff) | |
download | riki-51c086c3c7091364842a93ba3e8d2171a7ad89dc.tar.gz |
Merge branch 'liw/directives' into 'main'
add just enough directive implementations to build my journal
See merge request larswirzenius/riki!82
-rw-r--r-- | src/directive/brokenlinks.rs | 11 | ||||
-rw-r--r-- | src/directive/map.rs | 37 | ||||
-rw-r--r-- | src/directive/pagestats.rs | 40 |
3 files changed, 64 insertions, 24 deletions
diff --git a/src/directive/brokenlinks.rs b/src/directive/brokenlinks.rs index b95c0aa..11fdb80 100644 --- a/src/directive/brokenlinks.rs +++ b/src/directive/brokenlinks.rs @@ -2,7 +2,6 @@ use crate::directive::{DirectiveError, DirectiveImplementation, Processed}; use crate::page::PageMeta; use crate::site::Site; use crate::wikitext::ParsedDirective; -use log::warn; #[derive(Debug, Default, Eq, PartialEq)] pub struct Brokenlinks {} @@ -16,11 +15,9 @@ impl DirectiveImplementation for Brokenlinks { Self::default() } - fn process(&self, _site: &Site, meta: &mut PageMeta) -> Result<Processed, DirectiveError> { - warn!( - "page {} uses unimplemented brokenlinks", - meta.path().display() - ); - Ok(Processed::Markdown("\n".into())) + fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<Processed, DirectiveError> { + // We don't allow broken links, but we want to allow the + // directive, so this is very simple. + Ok(Processed::Markdown("".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..8c7feec 100644 --- a/src/directive/pagestats.rs +++ b/src/directive/pagestats.rs @@ -1,26 +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 PageStats {} +pub struct PageStats { + pages: String, +} impl DirectiveImplementation for PageStats { const REQUIRED: &'static [&'static str] = &["pages"]; const ALLOWED: &'static [&'static str] = &["among", "style"]; 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 PageStats { + pub fn new(pages: String) -> Self { + Self { pages } } - fn process(&self, _site: &Site, meta: &mut PageMeta) -> Result<Processed, DirectiveError> { - warn!( - "page {} uses unimplemented pagestats", - 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()) } } |