From f23fe81456b2f5174aadbec63c333d77af21dc36 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 28 Oct 2022 16:42:11 +0300 Subject: feat: implement a simple version of the inline directive This version just produces a list of links to the matching pages. The link text is the page title, is any. Sponsored-by: author --- src/directive/inline.rs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/directive/inline.rs b/src/directive/inline.rs index 7bef13c..78872b4 100644 --- a/src/directive/inline.rs +++ b/src/directive/inline.rs @@ -1,10 +1,15 @@ use crate::error::SiteError; use crate::page::PageMeta; +use crate::pagespec::PageSpec; use crate::site::Site; use crate::wikitext::ParsedDirective; +use crate::util::make_relative_link; +use std::path::Path; #[derive(Debug, Default, Eq, PartialEq)] -pub struct Inline {} +pub struct Inline { + pages: String, +} impl Inline { pub const REQUIRED: &'static [&'static str] = &["pages"]; @@ -26,17 +31,31 @@ impl Inline { ]; pub const ALLOW_ANY_UNNAMED: bool = true; - pub fn new() -> Self { - Self::default() + pub fn new(pages: String) -> Self { + Self { pages } + } + + pub fn process(&self, site: &Site, meta: &mut PageMeta) -> Result { + let pagespec = PageSpec::new(meta.path(), &self.pages)?; + 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(matches.join("")) } - pub fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result { - Err(SiteError::UnimplementedDirective("inline".into())) + fn link(container: &Path, meta: &PageMeta) -> String { + let link = make_relative_link(container, meta.path()); + format!("[{}]({})", meta.title(), link.display()) } } impl From<&ParsedDirective> for Inline { - fn from(_p: &ParsedDirective) -> Self { - Inline::new() + fn from(p: &ParsedDirective) -> Self { + let args = p.args(); + let pages = args.get("pages").unwrap(); + Inline::new(pages.to_string()) } } -- cgit v1.2.1