summaryrefslogtreecommitdiff
path: root/src/directive/map.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/directive/map.rs')
-rw-r--r--src/directive/map.rs37
1 files changed, 30 insertions, 7 deletions
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())
}
}