From 6869df7dedd8b434cb676fb25e9631b7723f0f28 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 24 Jul 2022 09:17:13 +0300 Subject: feat: allow a directive to have any number of unnamed arguments Sponsored-by: author --- src/directive/img.rs | 3 ++- src/wikitext.rs | 40 ++++++++++++---------------------------- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/src/directive/img.rs b/src/directive/img.rs index 4a56e6b..f81f8a9 100644 --- a/src/directive/img.rs +++ b/src/directive/img.rs @@ -31,6 +31,7 @@ impl Img { impl From for Img { fn from(p: ParsedDirective) -> Self { - Img::new(p.unnamed_arg().expect("img must have filename argument").into()) + let unnamed = p.unnamed_args().pop().unwrap(); + Img::new(unnamed.into()) } } diff --git a/src/wikitext.rs b/src/wikitext.rs index 77d6b58..39fbd32 100644 --- a/src/wikitext.rs +++ b/src/wikitext.rs @@ -201,39 +201,14 @@ impl WikiLink { pub struct ParsedDirective { name: String, args: HashMap, - unnamed_arg: Option, } impl ParsedDirective { - pub fn new(name: &str, mut args: HashMap) -> Result { + pub fn new(name: &str, args: HashMap) -> Result { trace!("ParsedDirective::new: name={:?} args={:?}", name, args); - let unnamed_args: Vec = args - .iter() - .filter_map(|(k, v)| { - if v.is_empty() { - Some(k.to_string()) - } else { - None - } - }) - .collect(); - trace!("ParsedDirective::new: unnamed_args={:?}", unnamed_args); - - let unnamed_arg = match unnamed_args.len() { - 0 => None, - 1 => { - let key = unnamed_args.get(0).unwrap(); - args.remove(key); - Some(key.to_string()) - } - _ => return Err(SiteError::TooManyUnnamedArgs(name.into())), - }; - trace!("ParsedDirective::new: unnamed_arg={:?}", unnamed_arg); - Ok(Self { name: name.into(), args, - unnamed_arg, }) } @@ -248,8 +223,17 @@ impl ParsedDirective { .collect() } - pub fn unnamed_arg(&self) -> Option<&str> { - self.unnamed_arg.as_deref() + pub fn unnamed_args(&self) -> Vec<&str> { + self.args + .iter() + .filter_map(|(k, v)| { + if v.is_empty() { + Some(k.as_str()) + } else { + None + } + }) + .collect() } } -- cgit v1.2.1 From c619847d4213504ff2fb28a814d32ea642c24287 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 24 Jul 2022 09:22:04 +0300 Subject: feat: add placeholder for tag directive Sponsored-by: author --- src/directive/mod.rs | 9 +++++++++ src/directive/tag.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/directive/tag.rs diff --git a/src/directive/mod.rs b/src/directive/mod.rs index efdc799..38fb0d3 100644 --- a/src/directive/mod.rs +++ b/src/directive/mod.rs @@ -14,6 +14,7 @@ pub enum Directive { Meta(Meta), Img(Img), + Tag(Tag), } impl TryFrom for Directive { @@ -49,6 +50,10 @@ impl TryFrom for Directive { Self::check_args(&p, Meta::REQUIRED, Meta::ALLOWED, Meta::ALLOW_ANY_UNNAMED)?; Directive::Meta(Meta::from(p)) } + "tag" => { + Self::check_args(&p, Tag::REQUIRED, Tag::ALLOWED, Tag::ALLOW_ANY_UNNAMED)?; + Directive::Tag(Tag::from(p)) + } _ => return Err(SiteError::UnknownDirective(p.name().into())), }; Ok(d) @@ -97,6 +102,7 @@ impl Directive { } Self::Img(x) => x.process(site, meta), Self::Meta(x) => x.process(site, meta), + Self::Tag(x) => x.process(site, meta), } } } @@ -106,3 +112,6 @@ use meta::Meta; mod img; pub use img::Img; + +mod tag; +use tag::Tag; diff --git a/src/directive/tag.rs b/src/directive/tag.rs new file mode 100644 index 0000000..9486a7b --- /dev/null +++ b/src/directive/tag.rs @@ -0,0 +1,32 @@ +use crate::error::SiteError; +use crate::page::PageMeta; +use crate::site::Site; +use crate::wikitext::ParsedDirective; + +#[derive(Debug, Eq, PartialEq)] +pub struct Tag { + tags: Vec, +} + +impl Tag { + pub const REQUIRED: &'static [&'static str] = &[]; + pub const ALLOWED: &'static [&'static str] = &["class"]; + pub const ALLOW_ANY_UNNAMED: bool = true; + + pub fn new(tags: Vec) -> Self { + Self { + tags, + } + } + + pub fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result { + Ok("".into()) + } +} + +impl From for Tag { + fn from(p: ParsedDirective) -> Self { + let tags = p.unnamed_args().iter().map(|s| s.to_string()).collect(); + Tag::new(tags) + } +} -- cgit v1.2.1