rich.syntax

class rich.syntax.Syntax(code, lexer, *, theme='monokai', dedent=False, line_numbers=False, start_line=1, line_range=None, highlight_lines=None, code_width=None, tab_size=4, word_wrap=False, background_color=None, indent_guides=False, padding=0)[source]

Construct a Syntax object to render syntax highlighted code.

Parameters
  • code (str) – Code to highlight.

  • lexer (Lexer | str) – Lexer to use (see https://pygments.org/docs/lexers/)

  • theme (str, optional) – Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to “monokai”.

  • dedent (bool, optional) – Enable stripping of initial whitespace. Defaults to False.

  • line_numbers (bool, optional) – Enable rendering of line numbers. Defaults to False.

  • start_line (int, optional) – Starting number for line numbers. Defaults to 1.

  • line_range (Tuple[int | None, int | None], optional) – If given should be a tuple of the start and end line to render. A value of None in the tuple indicates the range is open in that direction.

  • highlight_lines (Set[int]) – A set of line numbers to highlight.

  • code_width (Optional[int]) – Width of code to render (not including line numbers), or None to use all available width.

  • tab_size (int, optional) – Size of tabs. Defaults to 4.

  • word_wrap (bool, optional) – Enable word wrapping.

  • background_color (str, optional) – Optional background color, or None to use theme color. Defaults to None.

  • indent_guides (bool, optional) – Show indent guides. Defaults to False.

  • padding (PaddingDimensions) – Padding to apply around the syntax. Defaults to 0 (no padding).

property default_lexer: Lexer

A Pygments Lexer to use if one is not specified or invalid.

classmethod from_path(path, encoding='utf-8', lexer=None, theme='monokai', dedent=False, line_numbers=False, line_range=None, start_line=1, highlight_lines=None, code_width=None, tab_size=4, word_wrap=False, background_color=None, indent_guides=False, padding=0)[source]

Construct a Syntax object from a file.

Parameters
  • path (str) – Path to file to highlight.

  • encoding (str) – Encoding of file.

  • lexer (str | Lexer, optional) – Lexer to use. If None, lexer will be auto-detected from path/file content.

  • theme (str, optional) – Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to “emacs”.

  • dedent (bool, optional) – Enable stripping of initial whitespace. Defaults to True.

  • line_numbers (bool, optional) – Enable rendering of line numbers. Defaults to False.

  • start_line (int, optional) – Starting number for line numbers. Defaults to 1.

  • line_range (Tuple[int, int], optional) – If given should be a tuple of the start and end line to render.

  • highlight_lines (Set[int]) – A set of line numbers to highlight.

  • code_width (Optional[int]) – Width of code to render (not including line numbers), or None to use all available width.

  • tab_size (int, optional) – Size of tabs. Defaults to 4.

  • word_wrap (bool, optional) – Enable word wrapping of code.

  • background_color (str, optional) – Optional background color, or None to use theme color. Defaults to None.

  • indent_guides (bool, optional) – Show indent guides. Defaults to False.

  • padding (PaddingDimensions) – Padding to apply around the syntax. Defaults to 0 (no padding).

Returns

A Syntax object that may be printed to the console

Return type

[Syntax]

classmethod get_theme(name)[source]

Get a syntax theme instance.

Parameters

name (Union[str, SyntaxTheme]) –

Return type

SyntaxTheme

classmethod guess_lexer(path, code=None)[source]

Guess the alias of the Pygments lexer to use based on a path and an optional string of code. If code is supplied, it will use a combination of the code and the filename to determine the best lexer to use. For example, if the file is index.html and the file contains Django templating syntax, then “html+django” will be returned. If the file is index.html, and no templating language is used, the “html” lexer will be used. If no string of code is supplied, the lexer will be chosen based on the file extension..

Parameters
  • path (AnyStr) – The path to the file containing the code you wish to know the lexer for.

  • code (str, optional) – Optional string of code that will be used as a fallback if no lexer is found for the supplied path.

Returns

The name of the Pygments lexer that best matches the supplied path/code.

Return type

str

highlight(code, line_range=None)[source]

Highlight code and return a Text instance.

Parameters
  • code (str) – Code to highlight.

  • line_range (Tuple[int, int], optional) – Optional line range to highlight.

Returns

A text instance containing highlighted syntax.

Return type

Text

property lexer: Optional[Lexer]

The lexer for this syntax, or None if no lexer was found.

Tries to find the lexer by name if a string was passed to the constructor.

stylize_range(style, start, end)[source]

Adds a custom style on a part of the code, that will be applied to the syntax display when it’s rendered. Line numbers are 1-based, while column indexes are 0-based.

Parameters
  • style (StyleType) – The style to apply.

  • start (Tuple[int, int]) – The start of the range, in the form [line number, column index].

  • end (Tuple[int, int]) – The end of the range, in the form [line number, column index].

Return type

None