Link templates support single-colons and inject the value at the end if no %s is found.

This commit is contained in:
Tom Alexander 2023-10-06 23:55:21 -04:00
parent aa0a0b890e
commit 65e142a215
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 9 additions and 2 deletions

View File

@ -4,3 +4,4 @@
[[cat::bat]] [[cat::bat]]
#+LINK: cat dog%s #+LINK: cat dog%s
[[cat:bat]]

View File

@ -2,6 +2,7 @@ use std::borrow::Cow;
use nom::branch::alt; use nom::branch::alt;
use nom::bytes::complete::escaped; use nom::bytes::complete::escaped;
use nom::bytes::complete::is_a;
use nom::bytes::complete::tag; use nom::bytes::complete::tag;
use nom::bytes::complete::take_till1; use nom::bytes::complete::take_till1;
use nom::bytes::complete::take_until; use nom::bytes::complete::take_until;
@ -183,8 +184,8 @@ fn apply_link_templates<'b, 'g, 'r, 's>(
) -> Option<String> { ) -> Option<String> {
let (remaining, key) = opt(map( let (remaining, key) = opt(map(
tuple(( tuple((
recognize(take_until::<_, _, nom::error::Error<_>>("::")), recognize(take_until::<_, _, nom::error::Error<_>>(":")),
tag("::"), is_a(":"),
)), )),
|(key, _)| key, |(key, _)| key,
))(input) ))(input)
@ -205,6 +206,7 @@ fn apply_link_templates<'b, 'g, 'r, 's>(
let inject_value = Into::<&str>::into(remaining); let inject_value = Into::<&str>::into(remaining);
let mut ret = String::with_capacity(replacement_template.len() + inject_value.len()); let mut ret = String::with_capacity(replacement_template.len() + inject_value.len());
let mut state = ParserState::Normal; let mut state = ParserState::Normal;
let mut injected_value = false;
for c in replacement_template.chars() { for c in replacement_template.chars() {
state = match (&state, c) { state = match (&state, c) {
(ParserState::Normal, '%') => ParserState::Percent, (ParserState::Normal, '%') => ParserState::Percent,
@ -214,6 +216,7 @@ fn apply_link_templates<'b, 'g, 'r, 's>(
} }
(ParserState::Percent, 's') => { (ParserState::Percent, 's') => {
ret.push_str(inject_value); ret.push_str(inject_value);
injected_value = true;
ParserState::Normal ParserState::Normal
} }
(ParserState::Percent, _) => { (ParserState::Percent, _) => {
@ -228,6 +231,9 @@ fn apply_link_templates<'b, 'g, 'r, 's>(
} }
_ => {} _ => {}
} }
if !injected_value {
ret.push_str(inject_value);
}
Some(ret) Some(ret)
} }