Apply the link templates.

This commit is contained in:
Tom Alexander
2023-10-06 22:08:26 -04:00
parent 2ba5156ee1
commit 4c8828b91b
8 changed files with 203 additions and 32 deletions

View File

@@ -2,8 +2,17 @@ use nom::branch::alt;
use nom::bytes::complete::is_not;
use nom::bytes::complete::tag_no_case;
use nom::bytes::complete::take_until;
use nom::character::complete::anychar;
use nom::character::complete::line_ending;
use nom::character::complete::space0;
use nom::character::complete::space1;
use nom::combinator::eof;
use nom::combinator::map;
use nom::combinator::peek;
use nom::combinator::recognize;
use nom::multi::many_till;
use nom::multi::separated_list0;
use nom::sequence::tuple;
use super::keyword::filtered_keyword;
use super::keyword_todo::todo_keywords;
@@ -75,6 +84,7 @@ fn in_buffer_settings_key<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSou
))(input)
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn apply_in_buffer_settings<'g, 's, 'sf>(
keywords: Vec<Keyword<'sf>>,
original_settings: &'g GlobalSettings<'g, 's>,
@@ -113,10 +123,22 @@ pub(crate) fn apply_in_buffer_settings<'g, 's, 'sf>(
}
}
// Link templates
for kw in keywords
.iter()
.filter(|kw| kw.key.eq_ignore_ascii_case("link"))
{
let (_, (link_key, link_value)) = link_template(kw.value).map_err(|e| e.to_string())?;
new_settings
.link_templates
.insert(link_key.to_owned(), link_value.to_owned());
}
Ok(new_settings)
}
/// Apply in-buffer settings that do not impact parsing and therefore can be applied after parsing.
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn apply_post_parse_in_buffer_settings<'g, 's, 'sf>(
document: &mut Document<'s>,
) -> Result<(), &'static str> {
@@ -135,6 +157,30 @@ pub(crate) fn apply_post_parse_in_buffer_settings<'g, 's, 'sf>(
Ok(())
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn link_template<'s>(input: &'s str) -> Res<&'s str, (&'s str, &'s str)> {
let (remaining, key) = map(
tuple((
space0,
recognize(many_till(anychar, peek(alt((space1, line_ending, eof))))),
)),
|(_, key)| key,
)(input)?;
let (remaining, replacement) = map(
tuple((
space1,
recognize(many_till(
anychar,
peek(tuple((space0, alt((line_ending, eof))))),
)),
)),
|(_, replacement)| replacement,
)(remaining)?;
Ok((remaining, (key, replacement)))
}
#[cfg(test)]
mod tests {
use super::*;