diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-07-23 12:38:03 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-07-23 12:38:03 +0000 |
commit | dd1f2eabc0a3f9fb6051be3788fc37095c79f76f (patch) | |
tree | 08065523c1a4638f3110475cec9aaa65ab6feb9b | |
parent | 0d29840818793fa2443a185b031d561c6a6f3b83 (diff) | |
parent | c00198fac524814b4335bb8d6be82d0f67ccb733 (diff) | |
download | riki-dd1f2eabc0a3f9fb6051be3788fc37095c79f76f.tar.gz |
Merge branch 'noir' into 'main'
fix: handle multi-arg directives
See merge request larswirzenius/riki!22
-rw-r--r-- | src/directive/img.rs | 2 | ||||
-rw-r--r-- | src/directive/mod.rs | 2 | ||||
-rw-r--r-- | src/parser.rs | 29 | ||||
-rw-r--r-- | src/site.rs | 6 |
4 files changed, 31 insertions, 8 deletions
diff --git a/src/directive/img.rs b/src/directive/img.rs index 683db35..4a56e6b 100644 --- a/src/directive/img.rs +++ b/src/directive/img.rs @@ -15,7 +15,7 @@ impl Img { pub const ALLOWED: &'static [&'static str] = &["class"]; pub const ALLOW_ANY_UNNAMED: bool = true; - fn new(src: String) -> Self { + pub fn new(src: String) -> Self { Self { src, } diff --git a/src/directive/mod.rs b/src/directive/mod.rs index cac4a1e..efdc799 100644 --- a/src/directive/mod.rs +++ b/src/directive/mod.rs @@ -105,4 +105,4 @@ mod meta; use meta::Meta; mod img; -use img::Img; +pub use img::Img; diff --git a/src/parser.rs b/src/parser.rs index 84098f6..12a89e6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -91,7 +91,7 @@ impl WikitextParser { self.tokens.drain(..3); break; } - _ => panic!("yikes"), + _ => panic!("yikes: {:?}", self.tokens), } } if target.is_none() { @@ -113,6 +113,10 @@ impl WikitextParser { self.tokens.drain(..1); break; } + [Token::Word(word), Token::Spaces(_), ..] => { + args.insert(word.to_string(), "".to_string()); + self.tokens.drain(..2); + } [Token::Word(word), Token::CloseBrackets, ..] => { args.insert(word.to_string(), "".to_string()); self.tokens.drain(..2); @@ -126,7 +130,7 @@ impl WikitextParser { args.insert(name.to_string(), value.to_string()); self.tokens.drain(..3); } - _ => panic!("yikes"), + _ => panic!("yikes: {:?}", self.tokens), } } let p = ParsedDirective::new(&name, args)?; @@ -152,11 +156,15 @@ impl WikitextParser { self.tokens.drain(..4); break; } - _ => panic!("yikes"), + _ => panic!("yikes: {:?}", self.tokens), } } Snippet::Markdown(format!("![{}]({})", link_text, target.unwrap())) } + [Token::Equals, ..] => { + self.tokens.drain(..1); + Snippet::Markdown("=".into()) + } _ => panic!("eeek: {:?}", self.tokens), }; Ok(Some(snippet)) @@ -166,6 +174,7 @@ impl WikitextParser { #[cfg(test)] mod test { use super::{Directive, Snippet, TokenPatterns, WikiLink, WikitextParser}; + use crate::directive::Img; #[test] fn empty_input() { @@ -301,4 +310,18 @@ mod test { ); assert_eq!(p.parse().unwrap(), None); } + + #[test] + fn directive_multiple_args() { + let patterns = TokenPatterns::default(); + let mut p = WikitextParser::new( + r#"[[!img foo.jpg class=image]]"#, + &patterns + ); + assert_eq!( + p.parse().unwrap(), + Some(Snippet::Directive(Directive::Img(Img::new("foo.jpg".into())))) + ); + assert_eq!(p.parse().unwrap(), None); + } } diff --git a/src/site.rs b/src/site.rs index 3d3af3c..1a58801 100644 --- a/src/site.rs +++ b/src/site.rs @@ -3,7 +3,7 @@ use crate::page::{MarkdownPage, UnprocessedPage, WikitextPage}; use crate::parser::WikitextParser; use crate::token::TokenPatterns; use crate::util::{make_path_absolute, make_path_relative_to, make_relative_link, join_subpath}; -use log::{info, trace}; +use log::{debug, info, trace}; use std::collections::HashMap; use std::path::{Path, PathBuf}; use walkdir::WalkDir; @@ -72,7 +72,7 @@ impl Site { pub fn process_wikipage(&mut self) -> Result<bool, SiteError> { if let Some(page) = self.wikitext_pages.pop() { - trace!("processing wikitext page {}", page.meta().path().display()); + debug!("processing wikitext page {}", page.meta().path().display()); let mut parser = WikitextParser::new(page.wikitext(), &self.patterns); let page = UnprocessedPage::new(page.meta().clone(), &mut parser)?; self.unprocessed_pages.push(page); @@ -84,7 +84,7 @@ impl Site { pub fn process_unrocessed_page(&mut self) -> Result<bool, SiteError> { if let Some(page) = self.unprocessed_pages.pop() { - trace!( + debug!( "processing unprocessed page {}", page.meta().path().display() ); |