Construct a Syntax object to render syntax highlighted code. Args: 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/#getti
| 241 | |
| 242 | |
| 243 | class Syntax(JupyterMixin): |
| 244 | """Construct a Syntax object to render syntax highlighted code. |
| 245 | |
| 246 | Args: |
| 247 | code (str): Code to highlight. |
| 248 | lexer (Lexer | str): Lexer to use (see https://pygments.org/docs/lexers/) |
| 249 | theme (str, optional): Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to "monokai". |
| 250 | dedent (bool, optional): Enable stripping of initial whitespace. Defaults to False. |
| 251 | line_numbers (bool, optional): Enable rendering of line numbers. Defaults to False. |
| 252 | start_line (int, optional): Starting number for line numbers. Defaults to 1. |
| 253 | line_range (Tuple[int | None, int | None], optional): If given should be a tuple of the start and end line to render. |
| 254 | A value of None in the tuple indicates the range is open in that direction. |
| 255 | highlight_lines (Set[int]): A set of line numbers to highlight. |
| 256 | code_width: Width of code to render (not including line numbers), or ``None`` to use all available width. |
| 257 | tab_size (int, optional): Size of tabs. Defaults to 4. |
| 258 | word_wrap (bool, optional): Enable word wrapping. |
| 259 | background_color (str, optional): Optional background color, or None to use theme color. Defaults to None. |
| 260 | indent_guides (bool, optional): Show indent guides. Defaults to False. |
| 261 | padding (PaddingDimensions): Padding to apply around the syntax. Defaults to 0 (no padding). |
| 262 | """ |
| 263 | |
| 264 | _pygments_style_class: Type[PygmentsStyle] |
| 265 | _theme: SyntaxTheme |
| 266 | |
| 267 | @classmethod |
| 268 | def get_theme(cls, name: Union[str, SyntaxTheme]) -> SyntaxTheme: |
| 269 | """Get a syntax theme instance.""" |
| 270 | if isinstance(name, SyntaxTheme): |
| 271 | return name |
| 272 | theme: SyntaxTheme |
| 273 | if name in RICH_SYNTAX_THEMES: |
| 274 | theme = ANSISyntaxTheme(RICH_SYNTAX_THEMES[name]) |
| 275 | else: |
| 276 | theme = PygmentsSyntaxTheme(name) |
| 277 | return theme |
| 278 | |
| 279 | def __init__( |
| 280 | self, |
| 281 | code: str, |
| 282 | lexer: Union[Lexer, str], |
| 283 | *, |
| 284 | theme: Union[str, SyntaxTheme] = DEFAULT_THEME, |
| 285 | dedent: bool = False, |
| 286 | line_numbers: bool = False, |
| 287 | start_line: int = 1, |
| 288 | line_range: Optional[Tuple[Optional[int], Optional[int]]] = None, |
| 289 | highlight_lines: Optional[Set[int]] = None, |
| 290 | code_width: Optional[int] = None, |
| 291 | tab_size: int = 4, |
| 292 | word_wrap: bool = False, |
| 293 | background_color: Optional[str] = None, |
| 294 | indent_guides: bool = False, |
| 295 | padding: PaddingDimensions = 0, |
| 296 | ) -> None: |
| 297 | self.code = code |
| 298 | self._lexer = lexer |
| 299 | self.dedent = dedent |
| 300 | self.line_numbers = line_numbers |