Construct a Syntax object from a file. Args: 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, optiona
(
cls,
path: str,
encoding: str = "utf-8",
lexer: Optional[Union[Lexer, str]] = None,
theme: Union[str, SyntaxTheme] = DEFAULT_THEME,
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,
padding: PaddingDimensions = 0,
)
| 318 | |
| 319 | @classmethod |
| 320 | def from_path( |
| 321 | cls, |
| 322 | path: str, |
| 323 | encoding: str = "utf-8", |
| 324 | lexer: Optional[Union[Lexer, str]] = None, |
| 325 | theme: Union[str, SyntaxTheme] = DEFAULT_THEME, |
| 326 | dedent: bool = False, |
| 327 | line_numbers: bool = False, |
| 328 | line_range: Optional[Tuple[int, int]] = None, |
| 329 | start_line: int = 1, |
| 330 | highlight_lines: Optional[Set[int]] = None, |
| 331 | code_width: Optional[int] = None, |
| 332 | tab_size: int = 4, |
| 333 | word_wrap: bool = False, |
| 334 | background_color: Optional[str] = None, |
| 335 | indent_guides: bool = False, |
| 336 | padding: PaddingDimensions = 0, |
| 337 | ) -> "Syntax": |
| 338 | """Construct a Syntax object from a file. |
| 339 | |
| 340 | Args: |
| 341 | path (str): Path to file to highlight. |
| 342 | encoding (str): Encoding of file. |
| 343 | lexer (str | Lexer, optional): Lexer to use. If None, lexer will be auto-detected from path/file content. |
| 344 | theme (str, optional): Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to "emacs". |
| 345 | dedent (bool, optional): Enable stripping of initial whitespace. Defaults to True. |
| 346 | line_numbers (bool, optional): Enable rendering of line numbers. Defaults to False. |
| 347 | start_line (int, optional): Starting number for line numbers. Defaults to 1. |
| 348 | line_range (Tuple[int, int], optional): If given should be a tuple of the start and end line to render. |
| 349 | highlight_lines (Set[int]): A set of line numbers to highlight. |
| 350 | code_width: Width of code to render (not including line numbers), or ``None`` to use all available width. |
| 351 | tab_size (int, optional): Size of tabs. Defaults to 4. |
| 352 | word_wrap (bool, optional): Enable word wrapping of code. |
| 353 | background_color (str, optional): Optional background color, or None to use theme color. Defaults to None. |
| 354 | indent_guides (bool, optional): Show indent guides. Defaults to False. |
| 355 | padding (PaddingDimensions): Padding to apply around the syntax. Defaults to 0 (no padding). |
| 356 | |
| 357 | Returns: |
| 358 | [Syntax]: A Syntax object that may be printed to the console |
| 359 | """ |
| 360 | code = Path(path).read_text(encoding=encoding) |
| 361 | |
| 362 | if not lexer: |
| 363 | lexer = cls.guess_lexer(path, code=code) |
| 364 | |
| 365 | return cls( |
| 366 | code, |
| 367 | lexer, |
| 368 | theme=theme, |
| 369 | dedent=dedent, |
| 370 | line_numbers=line_numbers, |
| 371 | line_range=line_range, |
| 372 | start_line=start_line, |
| 373 | highlight_lines=highlight_lines, |
| 374 | code_width=code_width, |
| 375 | tab_size=tab_size, |
| 376 | word_wrap=word_wrap, |
| 377 | background_color=background_color, |