Skip to content

Commit

Permalink
Merge pull request #1421 from Jisin0/main
Browse files Browse the repository at this point in the history
Update HTML Parser
  • Loading branch information
ernado authored Aug 16, 2024
2 parents 42b83a4 + ac6bc17 commit a590eb7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
8 changes: 6 additions & 2 deletions telegram/message/html/html_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ func sendHTML(ctx context.Context) error {
<i>italic</i>, <em>italic</em>
<u>underline</u>, <ins>underline</ins>
<s>strikethrough</s>, <strike>strikethrough</strike>, <del>strikethrough</del>
<b>bold <i>italic bold <s>italic bold strikethrough</s> <u>underline italic bold</u></i> bold</b>
<span class="tg-spoiler">spoiler</span>, <tg-spoiler>spoiler</tg-spoiler>
<b>bold <i>italic bold <s>italic bold strikethrough <span class="tg-spoiler">italic bold strikethrough spoiler</span></s> <u>underline italic bold</u></i> bold</b>
<a href="http://www.example.com/">inline URL</a>
<a href="tg://user?id=123456789">inline mention of a user</a>
<tg-emoji emoji-id="5368324170671202286">👍</tg-emoji>
<code>inline fixed-width code</code>
<pre>pre-formatted fixed-width code block</pre>
<pre><code class="language-python">pre-formatted fixed-width code block written in the Python programming language</code></pre>`))
<pre><code class="language-python">pre-formatted fixed-width code block written in the Python programming language</code></pre>
<blockquote>Block quotation started\nBlock quotation continued\nThe last line of the block quotation</blockquote>
<blockquote expandable>Expandable block quotation started\nExpandable block quotation continued\nExpandable block quotation continued\nHidden by default part of the block quotation started\nExpandable block quotation continued\nThe last line of the block quotation</blockquote>`))
return err
})
}
Expand Down
32 changes: 23 additions & 9 deletions telegram/message/html/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package html

import (
"io"
"strconv"
"strings"

"github.com/go-faster/errors"
Expand Down Expand Up @@ -36,15 +37,17 @@ func (p *htmlParser) fillAttrs() {
}

const (
pre = "pre"
code = "code"
em = "em"
ins = "ins"
strike = "strike"
del = "del"
strong = "strong"
span = "span"
tgSpoiler = "tg-spoiler"
pre = "pre"
code = "code"
em = "em"
ins = "ins"
strike = "strike"
del = "del"
strong = "strong"
span = "span"
tgSpoiler = "tg-spoiler"
tgEmoji = "tg-emoji"
blockquote = "blockquote"
)

func (p *htmlParser) tag(tn []byte) string {
Expand Down Expand Up @@ -78,6 +81,10 @@ func (p *htmlParser) tag(tn []byte) string {
return span
case tgSpoiler:
return tgSpoiler
case tgEmoji:
return tgEmoji
case blockquote:
return blockquote
default:
return string(tn)
}
Expand Down Expand Up @@ -154,6 +161,13 @@ func (p *htmlParser) startTag() error {
}
case tgSpoiler:
e.format = entity.Spoiler()
case tgEmoji:
if id, err := strconv.ParseInt(p.attr["emoji-id"], 10, 64); err == nil {
e.format = entity.CustomEmoji(id)
}
case blockquote:
_, collapsed := p.attr["expandable"]
e.format = entity.Blockquote(collapsed)
}

p.stack.push(e)
Expand Down
3 changes: 3 additions & 0 deletions telegram/message/html/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func TestHTML(t *testing.T) {
entities: getEntities(entity.Pre("python"))},
{html: "<b>&lt;</b>", msg: "<", entities: getEntities(entity.Bold())},
{html: `<span class="tg-spoiler">spoiler</span>`, msg: "spoiler", entities: getEntities(entity.Spoiler())},
{html: "<tg-emoji emoji-id=\"5368324170671202286\">👍</tg-emoji>", msg: "👍", entities: getEntities(entity.CustomEmoji(5368324170671202286))},
{html: "<blockquote expandable>quote</blockquote>", msg: "quote", entities: getEntities(entity.Blockquote(true))},
{html: "<blockquote>quote</blockquote>", msg: "quote", entities: getEntities(entity.Blockquote(false))},
}
t.Run("Common", runTests(tests, false))
}
Expand Down

0 comments on commit a590eb7

Please sign in to comment.