From 65e142a215ea759fbdd9ee9296f3c24163644078 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 6 Oct 2023 23:55:21 -0400 Subject: [PATCH] Link templates support single-colons and inject the value at the end if no %s is found. --- org_mode_samples/object/regular_link/template.org | 1 + src/parser/regular_link.rs | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/org_mode_samples/object/regular_link/template.org b/org_mode_samples/object/regular_link/template.org index d98a77b..23ac386 100644 --- a/org_mode_samples/object/regular_link/template.org +++ b/org_mode_samples/object/regular_link/template.org @@ -4,3 +4,4 @@ [[cat::bat]] #+LINK: cat dog%s +[[cat:bat]] diff --git a/src/parser/regular_link.rs b/src/parser/regular_link.rs index 1c983d9..5dbd8b9 100644 --- a/src/parser/regular_link.rs +++ b/src/parser/regular_link.rs @@ -2,6 +2,7 @@ use std::borrow::Cow; use nom::branch::alt; use nom::bytes::complete::escaped; +use nom::bytes::complete::is_a; use nom::bytes::complete::tag; use nom::bytes::complete::take_till1; use nom::bytes::complete::take_until; @@ -183,8 +184,8 @@ fn apply_link_templates<'b, 'g, 'r, 's>( ) -> Option { let (remaining, key) = opt(map( tuple(( - recognize(take_until::<_, _, nom::error::Error<_>>("::")), - tag("::"), + recognize(take_until::<_, _, nom::error::Error<_>>(":")), + is_a(":"), )), |(key, _)| key, ))(input) @@ -205,6 +206,7 @@ fn apply_link_templates<'b, 'g, 'r, 's>( let inject_value = Into::<&str>::into(remaining); let mut ret = String::with_capacity(replacement_template.len() + inject_value.len()); let mut state = ParserState::Normal; + let mut injected_value = false; for c in replacement_template.chars() { state = match (&state, c) { (ParserState::Normal, '%') => ParserState::Percent, @@ -214,6 +216,7 @@ fn apply_link_templates<'b, 'g, 'r, 's>( } (ParserState::Percent, 's') => { ret.push_str(inject_value); + injected_value = true; ParserState::Normal } (ParserState::Percent, _) => { @@ -228,6 +231,9 @@ fn apply_link_templates<'b, 'g, 'r, 's>( } _ => {} } + if !injected_value { + ret.push_str(inject_value); + } Some(ret) }