Skip to content

Commit

Permalink
Add expandable blockquote
Browse files Browse the repository at this point in the history
  • Loading branch information
Desiders committed Jun 9, 2024
1 parent af1d3af commit ae0a3cd
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
35 changes: 33 additions & 2 deletions telers/src/utils/text/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,31 @@ where
.expect("Failed to add spoiler. Report this issue to the developers")
}

/// # Warning
/// If the given text length is greater than [`u16::MAX`], then the text will be truncated.
#[must_use]
pub fn blockquote(self, text: impl AsRef<str>) -> Self {
let text = text.as_ref();
let entity = MessageEntity::new_blockquote(self.text.len() as u16, text.len() as u16);

self.text(text)
.entity(&entity)
.expect("Failed to add blockquote. Report this issue to the developers")
}

/// # Warning
/// If the given text length is greater than [`u16::MAX`], then the text will be truncated.
#[must_use]
pub fn expandable_blockquote(self, text: impl AsRef<str>) -> Self {
let text = text.as_ref();
let entity =
MessageEntity::new_expandable_blockquote(self.text.len() as u16, text.len() as u16);

self.text(text)
.entity(&entity)
.expect("Failed to add expandable blockquote. Report this issue to the developers")
}

/// Add code as monowidth string.
/// # Arguments
/// * `code` - Code that will be added as monowidth string.
Expand Down Expand Up @@ -432,7 +457,11 @@ mod tests {
.text(" ")
.text_mention("text_mention", User::default())
.text(" ")
.custom_emoji("custom_emoji", "emoji_id");
.custom_emoji("custom_emoji", "emoji_id")
.text(" ")
.blockquote("blockquote")
.text(" ")
.expandable_blockquote("expandable_blockquote");

assert_eq!(
builder.get_text(),
Expand All @@ -444,7 +473,9 @@ mod tests {
<pre><code class=\"language-python\">pre_language</code></pre> \
<a href=\"https://example.com\">text_link</a> \
<a href=\"tg://user?id=0\">text_mention</a> \
<tg-emoji data-emoji-id=\"emoji_id\">custom_emoji</tg-emoji>\
<tg-emoji data-emoji-id=\"emoji_id\">custom_emoji</tg-emoji> \
<blockquote>blockquote</blockquote> \
<blockquote expandable>expandable_blockquote</blockquote>\
"
);
}
Expand Down
12 changes: 12 additions & 0 deletions telers/src/utils/text/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ pub trait Formatter {
where
T: AsRef<str>;

#[must_use]
fn expandable_blockquote<T>(&self, text: T) -> String
where
T: AsRef<str>;

#[must_use]
fn text_link<T, U>(&self, text: T, url: U) -> String
where
Expand Down Expand Up @@ -223,6 +228,13 @@ mod tests {
todo!()
}

fn expandable_blockquote<T>(&self, _text: T) -> String
where
T: AsRef<str>,
{
todo!()
}

fn text_link<T, U>(&self, _text: T, _url: U) -> String
where
T: AsRef<str>,
Expand Down
25 changes: 24 additions & 1 deletion telers/src/utils/text/html_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ impl TextFormatter for Formatter {
format!("<blockquote>{text}</blockquote>", text = text.as_ref())
}

fn expandable_blockquote<T>(&self, text: T) -> String
where
T: AsRef<str>,
{
format!(
"<blockquote expandable>{text}</blockquote>",
text = text.as_ref()
)
}

fn text_link<T, U>(&self, text: T, url: U) -> String
where
T: AsRef<str>,
Expand Down Expand Up @@ -235,7 +245,7 @@ impl TextFormatter for Formatter {
MessageEntityKind::Strikethrough => self.strikethrough(editable_text),
MessageEntityKind::Spoiler => self.spoiler(editable_text),
MessageEntityKind::Blockquote => self.blockquote(editable_text),
MessageEntityKind::ExpandableBlockquote => self.blockquote(editable_text),
MessageEntityKind::ExpandableBlockquote => self.expandable_blockquote(editable_text),
MessageEntityKind::Code => self.code(editable_text),
MessageEntityKind::Pre(PreMessageEntity { language }) => match language {
Some(language) => self.pre_language(editable_text, language),
Expand Down Expand Up @@ -282,6 +292,10 @@ pub fn blockquote(text: impl AsRef<str>) -> String {
FORMATTER.blockquote(text)
}

pub fn expandable_blockquote(text: impl AsRef<str>) -> String {
FORMATTER.expandable_blockquote(text)
}

pub fn text_link(text: impl AsRef<str>, url: impl AsRef<str>) -> String {
FORMATTER.text_link(text, url)
}
Expand Down Expand Up @@ -353,6 +367,15 @@ mod tests {
);
}

#[test]
fn test_expandable_blockquote() {
let formatter = Formatter::default();
assert_eq!(
formatter.expandable_blockquote("text"),
"<blockquote expandable>text</blockquote>"
);
}

#[test]
fn test_text_link() {
let formatter = Formatter::default();
Expand Down
26 changes: 25 additions & 1 deletion telers/src/utils/text/markdown_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ impl TextFormatter for Formatter {
.join("\n")
}

fn expandable_blockquote<T>(&self, text: T) -> String
where
T: AsRef<str>,
{
let mut text = self.blockquote(text);
text.push_str("||");

text
}

fn text_link<T, U>(&self, text: T, url: U) -> String
where
T: AsRef<str>,
Expand Down Expand Up @@ -180,7 +190,7 @@ impl TextFormatter for Formatter {
MessageEntityKind::Strikethrough => self.strikethrough(editable_text),
MessageEntityKind::Spoiler => self.spoiler(editable_text),
MessageEntityKind::Blockquote => self.blockquote(editable_text),
MessageEntityKind::ExpandableBlockquote => self.blockquote(editable_text),
MessageEntityKind::ExpandableBlockquote => self.expandable_blockquote(editable_text),
MessageEntityKind::Code => self.code(editable_text),
MessageEntityKind::Pre(PreMessageEntity { language }) => match language {
Some(language) => self.pre_language(editable_text, language),
Expand Down Expand Up @@ -227,6 +237,10 @@ pub fn blockquote(text: impl AsRef<str>) -> String {
FORMATTER.blockquote(text)
}

pub fn expandable_blockquote(text: impl AsRef<str>) -> String {
FORMATTER.expandable_blockquote(text)
}

pub fn text_link(text: impl AsRef<str>, url: &str) -> String {
FORMATTER.text_link(text, url)
}
Expand Down Expand Up @@ -296,6 +310,16 @@ mod tests {
assert_eq!(formatter.blockquote("text\ntext"), ">text\n>text");
}

#[test]
fn expandable_blockquote() {
let formatter = Formatter::default();
assert_eq!(formatter.expandable_blockquote("text"), ">text||");
assert_eq!(
formatter.expandable_blockquote("text\ntext"),
">text\n>text||"
);
}

#[test]
fn test_text_link() {
let formatter = Formatter::default();
Expand Down

0 comments on commit ae0a3cd

Please sign in to comment.