rich/docs/source/markup.rst

61 lines
2.5 KiB
ReStructuredText
Raw Normal View History

2019-12-30 22:08:02 +00:00
.. _console_markup:
2020-04-29 14:21:10 +00:00
2019-12-29 18:03:42 +00:00
Console Markup
==============
Rich supports a simple markup which you can use to insert color and styles virtually everywhere Rich would accept a string (e.g. :meth:`~rich.console.Console.print` and :meth:`~rich.console.Console.log`).
Syntax
------
2019-12-29 18:03:42 +00:00
2020-05-21 16:15:08 +00:00
Console markup uses a syntax inspired by `bbcode <https://en.wikipedia.org/wiki/BBCode>`_. If you write the style (see :ref:`styles`) in square brackets, e.g. ``[bold red]``, that style will apply until it is *closed* with a corresponding ``[/bold red]``.
2019-12-29 18:03:42 +00:00
Here's a simple example::
from rich import print
2020-05-08 12:36:12 +00:00
print("[bold red]alert![/bold red] Something happened")
2019-12-30 13:29:25 +00:00
If you don't close a style, it will apply until the end of the string. Which is sometimes convenient if you want to style a single line. For example::
print("[bold italic yellow on red blink]This text is impossible to read")
There is a shorthand for closing a style. If you omit the style name from the closing tag, Rich will close the last style. For example::
print("[bold red]Bold and red[/] not bold or red")
2020-03-10 22:09:13 +00:00
2020-05-10 13:12:20 +00:00
Links
~~~~~
Console markup can output hyperlinks with the following syntax: ``[link=URL]text[/link]``. Here's an example::
print("Visit my [link=https://www.willmcgugan.com]blog[/link]!")
If your terminal software supports hyperlinks, you will be able to click the word "blog" which will typically open a browser. If your terminal doesn't support hyperlinks, you will see the text but it won't be clickable.
2020-04-13 11:24:13 +00:00
Escaping
~~~~~~~~
2020-09-12 13:57:34 +00:00
Occasionally you may want to print something that Rich would interpret as markup. You can *escape* a tag by preceding it with backslash. Here's an example::
2020-04-13 11:24:13 +00:00
2020-05-08 12:36:12 +00:00
>>> from rich import print
2020-08-01 20:23:51 +00:00
>>> print("foo\[bar]")
2020-05-08 12:36:12 +00:00
foo[bar]
2020-04-13 11:24:13 +00:00
2020-08-02 10:55:40 +00:00
The function :func:`~rich.markup.escape` will handle escaping of text for you.
2020-04-13 11:24:13 +00:00
2020-03-10 22:09:13 +00:00
Rendering Markup
----------------
By default, Rich will render console markup when you explicitly pass a string to :meth:`~rich.console.Print.print` or implicitly when you embed a string in another renderable object such as :class:`~rich.table.Table` or :class:`~rich.panel.Panel`.
Console markup is convenient, but you may wish to disable it if the syntax clashes with the string you want to print. You can do this by setting ``markup=False`` on the :meth:`~rich.console.Print.print` method or on the :class:`~rich.console.Console` constructor.
Markup API
----------
You can convert a string to styled text by calling :meth:`~rich.text.Text.from_markup`, which returns a :class:`~rich.text.Text` instance you can print or add more styles to.