From 35fa7c305f26b7a80b1cbb46e7bc354aea56d62c Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 24 Apr 2023 21:57:32 +0300 Subject: feat: add a silly map directive implementation It's the same as the inline one, for now. Sponsored-by: author --- src/directive/brokenlinks.rs | 2 +- src/directive/map.rs | 37 ++++++++++++++++++++++++++++++------- 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 { + let pagespec = PageSpec::new(meta.path(), &self.pages).map_err(DirectiveError::PageSpec)?; + let matches: Vec = 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 { - 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())) } } -- cgit v1.2.1 From 12cb8aa2cd186fa434b5449ca97ae193c604a897 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 24 Apr 2023 22:01:27 +0300 Subject: feat: simplistic implementation of the pagestats directive Sponsored-by: author --- src/directive/pagestats.rs | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/directive/pagestats.rs b/src/directive/pagestats.rs index b81d2d8..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 { + let pagespec = PageSpec::new(meta.path(), &self.pages).map_err(DirectiveError::PageSpec)?; + let matches: Vec = 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 { - warn!( - "page {} uses unimplemented pagestats", - meta.path().display() - ); - Err(DirectiveError::UnimplementedDirective("pagestats".into())) + fn link(container: &Path, meta: &PageMeta) -> String { + let link = make_relative_link(container, meta.path()); + format!("[{}]({})", meta.title(), link.display()) } } -- cgit v1.2.1 From b64e737671b5d251a15bfec76f57d76d691d396f Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 24 Apr 2023 22:03:15 +0300 Subject: feat: implement the brokenlinks directive Sponsored-by: author --- src/directive/brokenlinks.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/directive/brokenlinks.rs b/src/directive/brokenlinks.rs index aa135ec..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 { - warn!( - "page {} uses unimplemented brokenlinks", - meta.path().display() - ); - Err(DirectiveError::UnimplementedDirective("brokenlinks".into())) + fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result { + // We don't allow broken links, but we want to allow the + // directive, so this is very simple. + Ok(Processed::Markdown("".into())) } } -- cgit v1.2.1