From 3cd71ce30252053aa82c95dfe504c76389bc9a49 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sat, 26 Sep 2020 15:22:39 -0600 Subject: [PATCH] escape special prefixes in plaintext nodes --- gemtext/src/lib.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/gemtext/src/lib.rs b/gemtext/src/lib.rs index 3c6a499..ca56b0c 100644 --- a/gemtext/src/lib.rs +++ b/gemtext/src/lib.rs @@ -60,7 +60,13 @@ pub fn render(nodes: Vec, out: &mut impl Write) -> io::Result<()> { for node in nodes { match node { - Text(body) => write!(out, "{}\n", body)?, + Text(body) => { + let special_prefixes = ["=>", "```", "#", "*", ">"]; + if special_prefixes.iter().any(|prefix| body.starts_with(prefix)) { + write!(out, " ")?; + } + write!(out, "{}\n", body)? + }, Link { to, name } => match name { Some(name) => write!(out, "=> {} {}\n", to, name)?, None => write!(out, "=> {}\n", to)?, @@ -337,4 +343,15 @@ mod tests { ]; assert_eq!(expected, parse(msg)); } + + #[test] + fn ambiguous_text() { + let _ = pretty_env_logger::try_init(); + let original = Node::Text("#1 World's Best Coder".to_string()); + let expected = " #1 World's Best Coder\n"; + let mut rendered: Vec = vec![]; + render(vec![original], &mut rendered).unwrap(); + let rendered = String::from_utf8(rendered).unwrap(); + assert_eq!(expected, rendered) + } }