rich.syntax¶
-
class
rich.syntax.
Syntax
(code: str, lexer_name: str, *, theme: Union[str, rich.syntax.SyntaxTheme] = 'monokai', dedent: bool = False, line_numbers: bool = False, start_line: int = 1, line_range: Optional[Tuple[int, int]] = None, highlight_lines: Optional[Set[int]] = None, code_width: Optional[int] = None, tab_size: int = 4, word_wrap: bool = False, background_color: Optional[str] = None, indent_guides: bool = False)[source]¶ Construct a Syntax object to render syntax highlighted code.
- Parameters
code (str) – Code to highlight.
lexer_name (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, 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 – 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.
-
classmethod
from_path
(path: str, encoding: str = 'utf-8', theme: Union[str, rich.syntax.SyntaxTheme] = 'monokai', dedent: bool = False, line_numbers: bool = False, line_range: Optional[Tuple[int, int]] = None, start_line: int = 1, highlight_lines: Optional[Set[int]] = None, code_width: Optional[int] = None, tab_size: int = 4, word_wrap: bool = False, background_color: Optional[str] = None, indent_guides: bool = False) → rich.syntax.Syntax[source]¶ Construct a Syntax object from a file.
- Parameters
path (str) – Path to file to highlight.
encoding (str) – Encoding of file.
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 – 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.
- Returns
A Syntax object that may be printed to the console
- Return type
[Syntax]
-
classmethod
get_theme
(name: Union[str, rich.syntax.SyntaxTheme]) → rich.syntax.SyntaxTheme[source]¶ Get a syntax theme instance.
-
highlight
(code: str, line_range: Optional[Tuple[int, int]] = None) → rich.text.Text[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