summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-07-24 06:23:39 +0000
committerLars Wirzenius <liw@liw.fi>2022-07-24 06:23:39 +0000
commitb08f50789bec1f9f0d7b4aa45177838b3bd2d24c (patch)
tree908f685bd7bc761c8424a079698c09d532f342dc
parente4e3c0c56857567395adf2e982292dc9b3d29e73 (diff)
parentc619847d4213504ff2fb28a814d32ea642c24287 (diff)
downloadriki-b08f50789bec1f9f0d7b4aa45177838b3bd2d24c.tar.gz
Merge branch 'unnamed-args' into 'main'
feat: allow a directive to have any number of unnamed arguments See merge request larswirzenius/riki!24
-rw-r--r--src/directive/img.rs3
-rw-r--r--src/directive/mod.rs9
-rw-r--r--src/directive/tag.rs32
-rw-r--r--src/wikitext.rs40
4 files changed, 55 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<ParsedDirective> 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/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<ParsedDirective> for Directive {
@@ -49,6 +50,10 @@ impl TryFrom<ParsedDirective> 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<String>,
+}
+
+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<String>) -> Self {
+ Self {
+ tags,
+ }
+ }
+
+ pub fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> {
+ Ok("".into())
+ }
+}
+
+impl From<ParsedDirective> for Tag {
+ fn from(p: ParsedDirective) -> Self {
+ let tags = p.unnamed_args().iter().map(|s| s.to_string()).collect();
+ Tag::new(tags)
+ }
+}
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<String, String>,
- unnamed_arg: Option<String>,
}
impl ParsedDirective {
- pub fn new(name: &str, mut args: HashMap<String, String>) -> Result<Self, SiteError> {
+ pub fn new(name: &str, args: HashMap<String, String>) -> Result<Self, SiteError> {
trace!("ParsedDirective::new: name={:?} args={:?}", name, args);
- let unnamed_args: Vec<String> = 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()
}
}