diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-12-04 09:56:46 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-12-04 09:56:46 +0200 |
commit | a2bb3de4db8b4cb7ab325511e9cbf0450f843295 (patch) | |
tree | 23bf3d50fba1d70220b670782c1e6fe6ca71d03a | |
parent | 2b978041bd59f539a2990778e8e18015a63341fe (diff) | |
download | riki-a2bb3de4db8b4cb7ab325511e9cbf0450f843295.tar.gz |
fix: handle optional title in image links
Sponsored-by: author
-rw-r--r-- | riki.md | 2 | ||||
-rw-r--r-- | src/directive/graph.rs | 1 | ||||
-rw-r--r-- | src/directive/inline.rs | 2 | ||||
-rw-r--r-- | src/parser.rs | 19 | ||||
-rw-r--r-- | src/token.rs | 45 | ||||
-rw-r--r-- | subplot/riki.rs | 2 |
6 files changed, 55 insertions, 16 deletions
@@ -207,7 +207,7 @@ then AST of site/page.mdwn matches that of output/page/index.html ~~~{#image-link .file} -![my kitten](cat.jpg) +![my kitten](cat.jpg "image-title") ~~~ ### Emphasized text diff --git a/src/directive/graph.rs b/src/directive/graph.rs index ae90050..56a6ac8 100644 --- a/src/directive/graph.rs +++ b/src/directive/graph.rs @@ -19,5 +19,4 @@ impl DirectiveImplementation for Graph { fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<Processed, SiteError> { Err(SiteError::UnimplementedDirective("graph".into())) } - } diff --git a/src/directive/inline.rs b/src/directive/inline.rs index 9ddeef4..f4e18d7 100644 --- a/src/directive/inline.rs +++ b/src/directive/inline.rs @@ -3,8 +3,8 @@ 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 crate::wikitext::ParsedDirective; use std::path::Path; #[derive(Debug, Default, Eq, PartialEq)] diff --git a/src/parser.rs b/src/parser.rs index 559ee3a..26a60ec 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -172,6 +172,7 @@ impl WikitextParser { let mut link_text = String::new(); #[allow(unused_assignments)] let mut target = None; + let mut title = None; self.drain(2); loop { let (line, col) = self.position(); @@ -190,10 +191,26 @@ impl WikitextParser { self.drain(4); break; } + [TokenKind::ClosedBracket, TokenKind::OpenParens, TokenKind::Word(word), TokenKind::Spaces(_), TokenKind::QuotedValue(t), TokenKind::ClosedParens, ..] => + { + target = Some(word.to_string()); + title = Some(t.to_string()); + self.drain(6); + break; + } _ => return Err(SiteError::wikitext_syntax(line, col, &self.tokens[..5])), } } - Snippet::Markdown(format!("![{}]({})", link_text, target.unwrap())) + if let Some(title) = title { + Snippet::Markdown(format!( + "![{}]({} \"{}\")", + link_text, + target.unwrap(), + title + )) + } else { + Snippet::Markdown(format!("![{}]({})", link_text, target.unwrap())) + } } [TokenKind::Markdown(text), ..] => { let snippet = Snippet::Markdown(text.to_string()); diff --git a/src/token.rs b/src/token.rs index 0f2daaa..c1d2abc 100644 --- a/src/token.rs +++ b/src/token.rs @@ -10,10 +10,7 @@ pub struct Token { impl Token { fn new(token: TokenKind, pos: usize) -> Self { debug!("Token: token={:?} pos={}", token, pos); - Self { - token, - pos, - } + Self { token, pos } } } @@ -170,7 +167,10 @@ mod test { fn plain_markdown() { let patterns = TokenPatterns::default(); let mut p = parser("** hello, world", &patterns); - assert_eq!(p.parse().token, TokenKind::Markdown("** hello, world".into())); + assert_eq!( + p.parse().token, + TokenKind::Markdown("** hello, world".into()) + ); assert_eq!(p.parse().token, TokenKind::End); } @@ -287,7 +287,10 @@ mod test { fn single_quoted() { let patterns = TokenPatterns::default(); let mut p = parser(r#""hello there""#, &patterns); - assert_eq!(p.parse().token, TokenKind::QuotedValue(r#"hello there"#.into())); + assert_eq!( + p.parse().token, + TokenKind::QuotedValue(r#"hello there"#.into()) + ); assert_eq!(p.parse().token, TokenKind::End); } @@ -295,7 +298,10 @@ mod test { fn triple_quoted() { let patterns = TokenPatterns::default(); let mut p = parser(r#""""hello\nthere""""#, &patterns); - assert_eq!(p.parse().token, TokenKind::QuotedValue(r#"hello\nthere"#.into())); + assert_eq!( + p.parse().token, + TokenKind::QuotedValue(r#"hello\nthere"#.into()) + ); assert_eq!(p.parse().token, TokenKind::End); } @@ -310,7 +316,10 @@ mod test { assert_eq!(p.parse().token, TokenKind::Word("test".into())); assert_eq!(p.parse().token, TokenKind::Equals); - assert_eq!(p.parse().token, TokenKind::QuotedValue(r#"enabled(sidebar)"#.into())); + assert_eq!( + p.parse().token, + TokenKind::QuotedValue(r#"enabled(sidebar)"#.into()) + ); assert_eq!(p.parse().token, TokenKind::CloseBrackets); assert_eq!(p.parse().token, TokenKind::End); @@ -334,12 +343,18 @@ mod test { assert_eq!(p.parse().token, TokenKind::Spaces(" ".into())); assert_eq!(p.parse().token, TokenKind::Word("test".into())); assert_eq!(p.parse().token, TokenKind::Equals); - assert_eq!(p.parse().token, TokenKind::QuotedValue(r#"enabled(sidebar)"#.into())); + assert_eq!( + p.parse().token, + TokenKind::QuotedValue(r#"enabled(sidebar)"#.into()) + ); assert_eq!(p.parse().token, TokenKind::Spaces(" ".into())); assert_eq!(p.parse().token, TokenKind::Word("then".into())); assert_eq!(p.parse().token, TokenKind::Equals); - assert_eq!(p.parse().token, TokenKind::QuotedValue("\n[[!sidebar]]\n".into())); + assert_eq!( + p.parse().token, + TokenKind::QuotedValue("\n[[!sidebar]]\n".into()) + ); assert_eq!(p.parse().token, TokenKind::Spaces(" ".into())); assert_eq!(p.parse().token, TokenKind::Word("else".into())); @@ -372,12 +387,18 @@ mod test { assert_eq!(p.parse().token, TokenKind::Spaces(" ".into())); assert_eq!(p.parse().token, TokenKind::Word("test".into())); assert_eq!(p.parse().token, TokenKind::Equals); - assert_eq!(p.parse().token, TokenKind::QuotedValue(r#"enabled(sidebar)"#.into())); + assert_eq!( + p.parse().token, + TokenKind::QuotedValue(r#"enabled(sidebar)"#.into()) + ); assert_eq!(p.parse().token, TokenKind::Spaces(" ".into())); assert_eq!(p.parse().token, TokenKind::Word("then".into())); assert_eq!(p.parse().token, TokenKind::Equals); - assert_eq!(p.parse().token, TokenKind::QuotedValue("\n[[!sidebar]]\n".into())); + assert_eq!( + p.parse().token, + TokenKind::QuotedValue("\n[[!sidebar]]\n".into()) + ); assert_eq!(p.parse().token, TokenKind::Spaces(" ".into())); assert_eq!(p.parse().token, TokenKind::Word("else".into())); diff --git a/subplot/riki.rs b/subplot/riki.rs index 722b597..9575b55 100644 --- a/subplot/riki.rs +++ b/subplot/riki.rs @@ -81,6 +81,8 @@ impl MutVisitor for DropImageFigTitle { if let Inline::Image(attr, alt, target) = inline { if target.1 == "fig:" { *inline = Inline::Image(attr.clone(), alt.to_vec(), (target.0.clone(), "".into())); + } else if let Some(rest) = target.1.strip_prefix("fig:") { + *inline = Inline::Image(attr.clone(), alt.to_vec(), (target.0.clone(), rest.into())); } } } |