diff --git a/src/utils/html.rs b/src/utils/html.rs new file mode 100644 index 00000000..4d0bf5ed --- /dev/null +++ b/src/utils/html.rs @@ -0,0 +1,28 @@ +use std::string::String; + +// Escapes the string to be shown "as is" within the Telegram HTML message style. +// Does not escape ' and " characters (as should be for usual HTML). +// Because they shoudn't be escaped by the spec: https://core.telegram.org/bots/api#html-style +pub fn escape(s: &str) -> String { + s.replace("&", "&") + .replace("<", "<") + .replace(">", ">") +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_escape() { + assert_eq!( + escape(" Foo & Bar "), + " <title>Foo & Bar</title> " + ); + assert_eq!( + escape("

你好 & 再見

"), + "<p>你好 & 再見</p>" + ); + assert_eq!(escape("'foo\""), "'foo\""); + } +} diff --git a/src/utils.rs b/src/utils/markdown.rs similarity index 51% rename from src/utils.rs rename to src/utils/markdown.rs index afdda8dd..54f84090 100644 --- a/src/utils.rs +++ b/src/utils/markdown.rs @@ -1,18 +1,8 @@ use std::string::String; -// Escapes the string to be shown "as is" within the Telegram HTML message style. -// Does not escape ' and " characters (as should be for usual HTML). -// Because they shoudn't be escaped by the spec: https://core.telegram.org/bots/api#html-style -pub fn escape_html(s: &str) -> String { - s.replace("&", "&") - .replace("<", "<") - .replace(">", ">") - .to_string() -} - // Escapes all markdown special characters in the string // https://core.telegram.org/bots/api#markdownv2-style -pub fn escape_markdown(s: &str) -> String { +pub fn escape(s: &str) -> String { s.replace("_", r"\_") .replace("*", r"\*") .replace("[", r"\[") @@ -33,12 +23,12 @@ pub fn escape_markdown(s: &str) -> String { } // Escapes all markdown special characters in the link URL (...) -pub fn escape_markdown_link_url(s: &str) -> String { +pub fn escape_link_url(s: &str) -> String { s.replace("`", r"\`").replace(")", r"\)") } // Escapes all markdown special characters in the code block -pub fn escape_markdown_code_block(s: &str) -> String { +pub fn escape_code_block(s: &str) -> String { s.replace(r"\", r"\\").replace("`", r"\`") } @@ -47,44 +37,31 @@ mod tests { use super::*; #[test] - fn test_escape_html() { + fn test_escape_link_url() { assert_eq!( - escape_html(" Foo & Bar "), - " <title>Foo & Bar</title> " - ); - assert_eq!( - escape_html("

你好 & 再見

"), - "<p>你好 & 再見</p>" - ); - assert_eq!(escape_html("'foo\""), "'foo\""); - } - - #[test] - fn test_escape_markdown_link_url() { - assert_eq!( - escape_markdown_link_url( + escape_link_url( r"https://en.wikipedia.org/wiki/Development+(Software)" ), r"https://en.wikipedia.org/wiki/Development+(Software\)" ); assert_eq!( - escape_markdown_link_url(r"https://en.wikipedia.org/wiki/`"), + escape_link_url(r"https://en.wikipedia.org/wiki/`"), r"https://en.wikipedia.org/wiki/\`" ); assert_eq!( - escape_markdown_link_url(r"_*[]()~`#+-=|{}.!\"), + escape_link_url(r"_*[]()~`#+-=|{}.!\"), r"_*[](\)~\`#+-=|{}.!\" ); } #[test] - fn test_escape_markdown_code_block() { + fn test_escapecode_block() { assert_eq!( - escape_markdown_code_block(r"` \code inside the code\ `"), + escape_code_block(r"` \code inside the code\ `"), r"\` \\code inside the code\\ \`" ); assert_eq!( - escape_markdown_code_block(r"_*[]()~\`#+-=|{}.!\"), + escape_code_block(r"_*[]()~\`#+-=|{}.!\"), r"_*[]()~\\\`#+-=|{}.!\\" ); } diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 00000000..06eba848 --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1,2 @@ +pub mod html; +pub mod markdown;