Skip to content

Commit

Permalink
Merge pull request #27 from ilya-nikolaev/develop
Browse files Browse the repository at this point in the history
Added wbr tag support
  • Loading branch information
Tishka17 authored Jan 14, 2024
2 parents 45d4e90 + 16822d1 commit 1fe0b97
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
__pycache__

.idea/*
*.egg-info
*.egg-info

build/*
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ But this is how it is rendered in Telegram with `parse_mode="html"`:

![](https://github.com/tishka17/sulguk/blob/master/images/problem_telegram.png?raw=True)

T osolve this we can convert HTML to telegram entites with `sulguk`. So that's how it looks now:
To solve this we can convert HTML to telegram entities with `sulguk`. So that's how it looks now:

![](https://github.com/tishka17/sulguk/blob/master/images/problem_sulguk.png?raw=True)

Expand Down Expand Up @@ -131,6 +131,7 @@ The same behavior is supported in sulguk. Otherwise, you can set the language on
#### Additional tags:
* `<br/>` - new line
* `<hr/>` - horizontal line
* `<wbr/>` - word break opportunity
* `<ul>` - unordered list
* `<ol>` - ordered list with optional attributes
* `reversed` - to reverse numbers order
Expand Down
5 changes: 4 additions & 1 deletion example.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ <h3>Lists and formatting are supported</h3>
<hr />
<h3>Other features</h3>
<Blockquote>This is block quote</Blockquote>
<p>This is paragraph!</p><p>And one more with <q>quote</q>.</p>
<p>This is paragraph!</p><p>And one more with <q>quote</q>.</p>
<p>The longest word in the German language: Donaudampfschifffahrtsgesellschaftskapitaenswitwe</p>
<p>And again, the longest word in the German language:
Donau<wbr />dampf<wbr />schiff<wbr />fahrts<wbr />gesellschafts<wbr />kapitaens<wbr />witwe</p>
3 changes: 2 additions & 1 deletion src/sulguk/entities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"ListItem",
"NewLine",
"HorizontalLine",
"ZeroWidthSpace",
"Progress",
"Stub",
"Text",
Expand All @@ -41,7 +42,7 @@
)
from .emoji import Emoji
from .list import ListGroup, ListItem
from .no_contents import HorizontalLine, NewLine
from .no_contents import HorizontalLine, NewLine, ZeroWidthSpace
from .progress import Progress
from .stub import Stub
from .text import Text
5 changes: 5 additions & 0 deletions src/sulguk/entities/no_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ def render(self, state: State) -> None:
state.canvas.add_new_line_soft()
state.canvas.add_text("⎯" * 10)
state.canvas.add_new_line_soft()


class ZeroWidthSpace(NoContents):
def render(self, state: State) -> None:
state.canvas.add_text("\u200b")
5 changes: 4 additions & 1 deletion src/sulguk/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Text,
Underline,
Uppercase,
ZeroWidthSpace,
)

Attrs = List[Tuple[str, Optional[str]]]
Expand All @@ -39,7 +40,7 @@

LANG_CLASS_PREFIX = "language-"

NO_CLOSING_TAGS = ("br", "hr", "meta", "link", "img")
NO_CLOSING_TAGS = ("br", "wbr", "hr", "meta", "link", "img")


class Transformer(HTMLParser):
Expand Down Expand Up @@ -196,6 +197,8 @@ def _get_tg_emoji(self, attrs: Attrs) -> Entity:
def handle_startendtag(self, tag: str, attrs: Attrs) -> None:
if tag == "br":
entity = NewLine()
elif tag == "wbr":
entity = ZeroWidthSpace()
elif tag == "hr":
entity = HorizontalLine()
elif tag in ("img",):
Expand Down

0 comments on commit 1fe0b97

Please sign in to comment.