pyrogram/docs/source/topics/text-formatting.rst

232 lines
5.4 KiB
ReStructuredText
Raw Normal View History

2018-01-06 11:18:15 +00:00
Text Formatting
===============
.. role:: strike
:class: strike
2018-07-29 01:40:04 +00:00
.. role:: underline
:class: underline
2018-07-29 01:40:04 +00:00
.. role:: bold-underline
:class: bold-underline
.. role:: strike-italic
:class: strike-italic
Pyrogram uses a custom Markdown dialect for text formatting which adds some unique features that make writing styled
texts easier in both Markdown and HTML. You can send sophisticated text messages and media captions using a great
variety of decorations that can also be nested in order to combine multiple styles together.
2020-04-01 18:08:46 +00:00
.. contents:: Contents
:backlinks: none
:local:
-----
Basic Styles
------------
When formatting your messages, you can choose between Markdown-style, HTML-style or both (default). The following is a
list of the basic styles currently supported by Pyrogram.
- **bold**
- *italic*
- :strike:`strike`
- :underline:`underline`
- `text URL <https://pyrogram.org>`_
- `user text mention <https://t.me/haskell>`_
- ``inline fixed-width code``
- .. code-block:: text
pre-formatted
fixed-width
code block
.. note::
User text mentions are only guaranteed to work if you have already met the user (in groups or private chats).
2018-01-06 11:18:15 +00:00
2018-01-23 15:14:43 +00:00
Markdown Style
--------------
To strictly use this mode, pass "markdown" to the *parse_mode* parameter when using
2019-05-28 14:41:55 +00:00
:meth:`~pyrogram.Client.send_message`. Use the following syntax in your message:
2018-01-06 11:18:15 +00:00
2018-07-29 01:37:50 +00:00
.. code-block:: text
2018-01-06 11:18:15 +00:00
**bold**
__italic__
2018-01-06 11:18:15 +00:00
--underline--
2018-01-06 11:18:15 +00:00
~~strike~~
2018-01-06 11:18:15 +00:00
[text URL](https://docs.pyrogram.org/)
[text user mention](tg://user?id=23122162)
2018-01-06 11:18:15 +00:00
`inline fixed-width code`
```
pre-formatted
fixed-width
code block
2018-01-06 11:18:15 +00:00
```
**Example**:
.. code-block:: python
app.send_message(
"haskell",
(
"**bold**, "
"__italic__, "
"--underline--, "
2019-06-30 16:51:04 +00:00
"~~strike~~, "
"[mention](tg://user?id=23122162), "
"[URL](https://pyrogram.org), "
"`code`, "
"```"
"for i in range(10):\n"
" print(i)"
"```"
),
parse_mode="markdown"
)
2018-05-12 12:15:16 +00:00
2018-01-23 15:14:43 +00:00
HTML Style
----------
To strictly use this mode, pass "html" to the *parse_mode* parameter when using :meth:`~pyrogram.Client.send_message`.
2019-05-12 17:26:55 +00:00
The following tags are currently supported:
2018-01-23 15:14:43 +00:00
2018-07-29 01:37:50 +00:00
.. code-block:: text
2018-01-23 15:14:43 +00:00
<b>bold</b>, <strong>bold</strong>
<i>italic</i>, <em>italic</em>
<u>underline</u>
2018-01-23 15:14:43 +00:00
<s>strike</s>, <del>strike</del>, <strike>strike</strike>
<a href="http://docs.pyrogram.org/">text URL</a>
<a href="tg://user?id=23122162">inline mention</a>
2018-01-23 15:14:43 +00:00
<code>inline fixed-width code</code>
<pre>
pre-formatted
fixed-width
code block
</pre>
**Example**:
.. code-block:: python
app.send_message(
"haskell",
(
"<b>bold</b>, "
"<i>italic</i>, "
"<u>underline</u>, "
2019-06-30 16:51:04 +00:00
"<s>strike</s>, "
"<a href=\"tg://user?id=23122162\">mention</a>, "
"<a href=\"https://pyrogram.org/\">URL</a>, "
"<code>code</code>\n\n"
"<pre>"
"for i in range(10):\n"
" print(i)"
"</pre>"
),
parse_mode="html"
)
.. note::
All ``<``, ``>`` and ``&`` symbols that are not a part of a tag or an HTML entity must be replaced with the
corresponding HTML entities (``<`` with ``&lt;``, ``>`` with ``&gt;`` and ``&`` with ``&amp;``). You can use this
snippet to quickly escape those characters:
2018-01-23 15:14:43 +00:00
.. code-block:: python
2018-01-23 15:14:43 +00:00
import html
2018-01-06 11:18:15 +00:00
text = "<my text>"
text = html.escape(text)
2018-01-06 11:18:15 +00:00
print(text)
2018-01-06 11:18:15 +00:00
.. code-block:: text
2018-01-23 15:14:43 +00:00
&lt;my text&gt;
Different Styles
----------------
By default, when ignoring the *parse_mode* parameter, both Markdown and HTML styles are enabled together.
This means you can combine together both syntaxes in the same text:
.. code-block:: python
app.send_message("haskell", "**bold**, <i>italic</i>")
Result:
**bold**, *italic*
If you don't like this behaviour you can always choose to only enable either Markdown or HTML in strict mode by passing
"markdown" or "html" as argument to the *parse_mode* parameter.
.. code-block::
app.send_message("haskell", "**bold**, <i>italic</i>", parse_mode="markdown")
app.send_message("haskell", "**bold**, <i>italic</i>", parse_mode="html")
Result:
**bold**, <i>italic</i>
\*\*bold**, *italic*
In case you want to completely turn off the style parser, simply pass ``None`` to *parse_mode*. The text will be sent
as-is.
.. code-block:: python
app.send_message("haskell", "**bold**, <i>italic</i>", parse_mode=None)
Result:
\*\*bold**, <i>italic</i>
Nested and Overlapping Entities
-------------------------------
You can also style texts with more than one decoration at once by nesting entities together. For example, you can send
2019-07-01 11:37:12 +00:00
a text message with both :bold-underline:`bold and underline` styles, or a text that has both :strike-italic:`italic and
strike` styles, and you can still combine both Markdown and HTML together.
Here there are some example texts you can try sending:
**Markdown**:
- ``**bold, --underline--**``
2019-06-30 16:51:04 +00:00
- ``**bold __italic --underline ~~strike~~--__**``
- ``**bold __and** italic__``
**HTML**:
- ``<b>bold, <u>underline</u></b>``
2019-06-30 16:51:04 +00:00
- ``<b>bold <i>italic <u>underline <s>strike</s></u></i></b>``
- ``<b>bold <i>and</b> italic</i>``
**Combined**:
2018-01-23 15:14:43 +00:00
- ``--you can combine <i>HTML</i> with **Markdown**--``
- ``**and also <i>overlap** --entities</i> this way--``