mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-23 06:51:01 +01:00
Improve namespacing
This commit is contained in:
parent
fe2e1ecaf5
commit
eb44cd8b44
3 changed files with 40 additions and 33 deletions
28
src/utils/html.rs
Normal file
28
src/utils/html.rs
Normal file
|
@ -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(" <title>Foo & Bar</title> "),
|
||||
" <title>Foo & Bar</title> "
|
||||
);
|
||||
assert_eq!(
|
||||
escape("<p>你好 & 再見</p>"),
|
||||
"<p>你好 & 再見</p>"
|
||||
);
|
||||
assert_eq!(escape("'foo\""), "'foo\"");
|
||||
}
|
||||
}
|
|
@ -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(" <title>Foo & Bar</title> "),
|
||||
" <title>Foo & Bar</title> "
|
||||
);
|
||||
assert_eq!(
|
||||
escape_html("<p>你好 & 再見</p>"),
|
||||
"<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"_*[]()~\\\`#+-=|{}.!\\"
|
||||
);
|
||||
}
|
2
src/utils/mod.rs
Normal file
2
src/utils/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub mod html;
|
||||
pub mod markdown;
|
Loading…
Reference in a new issue