rich/docs/source/syntax.rst

25 lines
813 B
ReStructuredText
Raw Normal View History

2020-01-12 11:43:23 +00:00
Syntax
======
Rich can syntax highlight various programming languages with line numbers.
To syntax highlight code, construct a :class:`~rich.syntax.Syntax` object and print it to the console. Here's an example::
from rich.console import console
from rich.syntax import Syntax
console = Console()
syntax = Syntax.from_path("syntax.py", line_numbers=True)
console.print(syntax)
2020-05-08 12:36:12 +00:00
2020-05-08 14:14:18 +00:00
The Syntax constructor (and :meth:`~rich.syntax.Syntax.from_path`) accept a ``theme`` attribute which should be the name of a `Pygments theme <https://pygments.org/demo/>`_.
You can use this class from the command line. Here's how you would syntax highlight a file called "syntax.py"::
python -m rich.syntax syntax.py
For the full list of arguments, run the following::
python -m rich.syntax -h