File size: 22,378 Bytes
dae14ad | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | import os.path
import platform
import textwrap
from abc import ABC, abstractmethod
from typing import Any, Dict, Optional, Set, Tuple, Type, Union
from pygments.lexers import get_lexer_by_name, guess_lexer_for_filename
from pygments.style import Style as PygmentsStyle
from pygments.styles import get_style_by_name
from pygments.token import (
Comment,
Error,
Generic,
Keyword,
Name,
Number,
Operator,
String,
Token,
Whitespace,
)
from pygments.util import ClassNotFound
from ._loop import loop_first
from .color import Color, blend_rgb, parse_rgb_hex
from .console import Console, ConsoleOptions, JustifyMethod, RenderResult, Segment
from .jupyter import JupyterMixin
from .measure import Measurement
from .style import Style
from .text import Text
TokenType = Tuple[str, ...]
WINDOWS = platform.system() == "Windows"
DEFAULT_THEME = "monokai"
# The following styles are based on https://github.com/pygments/pygments/blob/master/pygments/formatters/terminal.py
# A few modifications were made
ANSI_LIGHT: Dict[TokenType, Style] = {
Token: Style(),
Whitespace: Style(color="white"),
Comment: Style(dim=True),
Comment.Preproc: Style(color="cyan"),
Keyword: Style(color="blue"),
Keyword.Type: Style(color="cyan"),
Operator.Word: Style(color="magenta"),
Name.Builtin: Style(color="cyan"),
Name.Function: Style(color="green"),
Name.Namespace: Style(color="cyan", underline=True),
Name.Class: Style(color="green", underline=True),
Name.Exception: Style(color="cyan"),
Name.Decorator: Style(color="magenta", bold=True),
Name.Variable: Style(color="red"),
Name.Constant: Style(color="red"),
Name.Attribute: Style(color="cyan"),
Name.Tag: Style(color="bright_blue"),
String: Style(color="yellow"),
Number: Style(color="blue"),
Generic.Deleted: Style(color="bright_red"),
Generic.Inserted: Style(color="green"),
Generic.Heading: Style(bold=True),
Generic.Subheading: Style(color="magenta", bold=True),
Generic.Prompt: Style(bold=True),
Generic.Error: Style(color="bright_red"),
Error: Style(color="red", underline=True),
}
ANSI_DARK: Dict[TokenType, Style] = {
Token: Style(),
Whitespace: Style(color="bright_black"),
Comment: Style(dim=True),
Comment.Preproc: Style(color="bright_cyan"),
Keyword: Style(color="bright_blue"),
Keyword.Type: Style(color="bright_cyan"),
Operator.Word: Style(color="bright_magenta"),
Name.Builtin: Style(color="bright_cyan"),
Name.Function: Style(color="bright_green"),
Name.Namespace: Style(color="bright_cyan", underline=True),
Name.Class: Style(color="bright_green", underline=True),
Name.Exception: Style(color="bright_cyan"),
Name.Decorator: Style(color="bright_magenta", bold=True),
Name.Variable: Style(color="bright_red"),
Name.Constant: Style(color="bright_red"),
Name.Attribute: Style(color="bright_cyan"),
Name.Tag: Style(color="bright_blue"),
String: Style(color="yellow"),
Number: Style(color="bright_blue"),
Generic.Deleted: Style(color="bright_red"),
Generic.Inserted: Style(color="bright_green"),
Generic.Heading: Style(bold=True),
Generic.Subheading: Style(color="bright_magenta", bold=True),
Generic.Prompt: Style(bold=True),
Generic.Error: Style(color="bright_red"),
Error: Style(color="red", underline=True),
}
RICH_SYNTAX_THEMES = {"ansi_light": ANSI_LIGHT, "ansi_dark": ANSI_DARK}
class SyntaxTheme(ABC):
"""Base class for a syntax theme."""
@abstractmethod
def get_style_for_token(self, token_type: TokenType) -> Style:
"""Get a style for a given Pygments token."""
raise NotImplementedError # pragma: no cover
@abstractmethod
def get_background_style(self) -> Style:
"""Get the background color."""
raise NotImplementedError # pragma: no cover
class PygmentsSyntaxTheme(SyntaxTheme):
"""Syntax theme that delagates to Pygments theme."""
def __init__(self, theme: Union[str, Type[PygmentsStyle]]) -> None:
self._style_cache: Dict[TokenType, Style] = {}
if isinstance(theme, str):
try:
self._pygments_style_class = get_style_by_name(theme)
except ClassNotFound:
self._pygments_style_class = get_style_by_name("default")
else:
self._pygments_style_class = theme
self._background_color = self._pygments_style_class.background_color
self._background_style = Style(bgcolor=self._background_color)
def get_style_for_token(self, token_type: TokenType) -> Style:
"""Get a style from a Pygments class."""
try:
return self._style_cache[token_type]
except KeyError:
try:
pygments_style = self._pygments_style_class.style_for_token(token_type)
except KeyError:
style = Style.null()
else:
color = pygments_style["color"]
bgcolor = pygments_style["bgcolor"]
style = Style(
color="#" + color if color else "#000000",
bgcolor="#" + bgcolor if bgcolor else self._background_color,
bold=pygments_style["bold"],
italic=pygments_style["italic"],
underline=pygments_style["underline"],
)
self._style_cache[token_type] = style
return style
def get_background_style(self) -> Style:
return self._background_style
class ANSISyntaxTheme(SyntaxTheme):
"""Syntax theme to use standard colors."""
def __init__(self, style_map: Dict[TokenType, Style]) -> None:
self.style_map = style_map
self._missing_style = Style.null()
self._background_style = Style.null()
self._style_cache: Dict[TokenType, Style] = {}
def get_style_for_token(self, token_type: TokenType) -> Style:
"""Look up style in the style map."""
try:
return self._style_cache[token_type]
except KeyError:
# Styles form a hierarchy
# We need to go from most to least specific
# e.g. ("foo", "bar", "baz") to ("foo", "bar") to ("foo",)
get_style = self.style_map.get
token = tuple(token_type)
style = self._missing_style
while token:
_style = get_style(token)
if _style is not None:
style = _style
break
token = token[:-1]
self._style_cache[token_type] = style
return style
def get_background_style(self) -> Style:
return self._background_style
class Syntax(JupyterMixin):
"""Construct a Syntax object to render syntax highlighted code.
Args:
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.
"""
_pygments_style_class: Type[PygmentsStyle]
_theme: SyntaxTheme
@classmethod
def get_theme(cls, name: Union[str, SyntaxTheme]) -> SyntaxTheme:
"""Get a syntax theme instance."""
if isinstance(name, SyntaxTheme):
return name
theme: SyntaxTheme
if name in RICH_SYNTAX_THEMES:
theme = ANSISyntaxTheme(RICH_SYNTAX_THEMES[name])
else:
theme = PygmentsSyntaxTheme(name)
return theme
def __init__(
self,
code: str,
lexer_name: str,
*,
theme: Union[str, SyntaxTheme] = DEFAULT_THEME,
dedent: bool = False,
line_numbers: bool = False,
start_line: int = 1,
line_range: Tuple[int, int] = None,
highlight_lines: Set[int] = None,
code_width: Optional[int] = None,
tab_size: int = 4,
word_wrap: bool = False,
background_color: str = None,
indent_guides: bool = False,
) -> None:
self.code = code
self.lexer_name = lexer_name
self.dedent = dedent
self.line_numbers = line_numbers
self.start_line = start_line
self.line_range = line_range
self.highlight_lines = highlight_lines or set()
self.code_width = code_width
self.tab_size = tab_size
self.word_wrap = word_wrap
self.background_color = background_color
self.indent_guides = indent_guides
self._theme = self.get_theme(theme)
@classmethod
def from_path(
cls,
path: str,
encoding: str = "utf-8",
theme: Union[str, SyntaxTheme] = DEFAULT_THEME,
dedent: bool = False,
line_numbers: bool = False,
line_range: Tuple[int, int] = None,
start_line: int = 1,
highlight_lines: Set[int] = None,
code_width: Optional[int] = None,
tab_size: int = 4,
word_wrap: bool = False,
background_color: str = None,
indent_guides: bool = False,
) -> "Syntax":
"""Construct a Syntax object from a file.
Args:
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:
[Syntax]: A Syntax object that may be printed to the console
"""
with open(path, "rt", encoding=encoding) as code_file:
code = code_file.read()
lexer = None
lexer_name = "default"
try:
_, ext = os.path.splitext(path)
if ext:
extension = ext.lstrip(".").lower()
lexer = get_lexer_by_name(extension)
lexer_name = lexer.name
except ClassNotFound:
pass
if lexer is None:
try:
lexer_name = guess_lexer_for_filename(path, code).name
except ClassNotFound:
pass
return cls(
code,
lexer_name,
theme=theme,
dedent=dedent,
line_numbers=line_numbers,
line_range=line_range,
start_line=start_line,
highlight_lines=highlight_lines,
code_width=code_width,
tab_size=tab_size,
word_wrap=word_wrap,
background_color=background_color,
indent_guides=indent_guides,
)
def _get_base_style(self) -> Style:
"""Get the base style."""
default_style = (
Style(bgcolor=self.background_color)
if self.background_color is not None
else self._theme.get_background_style()
)
return default_style
def _get_token_color(self, token_type: TokenType) -> Optional[Color]:
"""Get a color (if any) for the given token.
Args:
token_type (TokenType): A token type tuple from Pygments.
Returns:
Optional[Color]: Color from theme, or None for no color.
"""
style = self._theme.get_style_for_token(token_type)
return style.color
def highlight(self, code: str) -> Text:
"""Highlight code and return a Text instance.
Args:
code (str). Code to highlight.
Returns:
Text: A text instance containing syntax highlight.
"""
base_style = self._get_base_style()
justify: JustifyMethod = (
"default" if base_style.transparent_background else "left"
)
text = Text(
justify=justify,
style=base_style,
tab_size=self.tab_size,
no_wrap=not self.word_wrap,
)
try:
lexer = get_lexer_by_name(self.lexer_name)
except ClassNotFound:
text.append(code)
else:
_get_theme_style = self._theme.get_style_for_token
text.append_tokens(
(token, _get_theme_style(token_type))
for token_type, token in lexer.get_tokens(code)
)
if self.background_color is not None:
text.stylize(f"on {self.background_color}")
return text
def _get_line_numbers_color(self, blend: float = 0.3) -> Color:
background_color = self._theme.get_background_style().bgcolor
if background_color is None or background_color.is_system_defined:
return background_color or Color.default()
foreground_color = self._get_token_color(Token.Text)
if foreground_color is None or foreground_color.is_system_defined:
return foreground_color or Color.default()
new_color = blend_rgb(
background_color.get_truecolor(),
foreground_color.get_truecolor(),
cross_fade=blend,
)
return Color.from_triplet(new_color)
@property
def _numbers_column_width(self) -> int:
"""Get the number of characters used to render the numbers column."""
column_width = 0
if self.line_numbers:
column_width = len(str(self.start_line + self.code.count("\n"))) + 2
return column_width
def _get_number_styles(self, console: Console) -> Tuple[Style, Style, Style]:
"""Get background, number, and highlight styles for line numbers."""
background_style = self._get_base_style()
if background_style.transparent_background:
return Style.null(), Style(dim=True), Style.null()
if console.color_system in ("256", "truecolor"):
number_style = Style.chain(
background_style,
self._theme.get_style_for_token(Token.Text),
Style(color=self._get_line_numbers_color()),
)
highlight_number_style = Style.chain(
background_style,
self._theme.get_style_for_token(Token.Text),
Style(bold=True, color=self._get_line_numbers_color(0.9)),
)
else:
number_style = background_style + Style(dim=True)
highlight_number_style = background_style + Style(dim=False)
return background_style, number_style, highlight_number_style
def __rich_measure__(self, console: "Console", max_width: int) -> "Measurement":
if self.code_width is not None:
width = self.code_width + self._numbers_column_width
return Measurement(self._numbers_column_width, width)
return Measurement(self._numbers_column_width, max_width)
def __rich_console__(
self, console: Console, options: ConsoleOptions
) -> RenderResult:
transparent_background = self._get_base_style().transparent_background
code_width = (
(options.max_width - self._numbers_column_width - 1)
if self.code_width is None
else self.code_width
)
code = textwrap.dedent(self.code) if self.dedent else self.code
code = code.expandtabs(self.tab_size)
text = self.highlight(code)
text.remove_suffix("\n")
text.expand_tabs(self.tab_size)
(
background_style,
number_style,
highlight_number_style,
) = self._get_number_styles(console)
if self.indent_guides and not options.ascii_only:
style = (
self._get_base_style()
+ self._theme.get_style_for_token(Comment)
+ Style(dim=True)
)
text = text.with_indent_guides(self.tab_size, style=style)
if not self.line_numbers:
# Simple case of just rendering text
yield from console.render(text, options=options.update(width=code_width))
return
lines = text.split("\n")
line_offset = 0
if self.line_range:
start_line, end_line = self.line_range
line_offset = max(0, start_line - 1)
lines = lines[line_offset:end_line]
numbers_column_width = self._numbers_column_width
render_options = options.update(width=code_width)
highlight_line = self.highlight_lines.__contains__
_Segment = Segment
padding = _Segment(" " * numbers_column_width + " ", background_style)
new_line = _Segment("\n")
line_pointer = "❱ "
for line_no, line in enumerate(lines, self.start_line + line_offset):
if self.word_wrap:
wrapped_lines = console.render_lines(
line,
render_options,
style=background_style,
pad=not transparent_background,
)
else:
segments = list(line.render(console, end=""))
if options.no_wrap:
wrapped_lines = [segments]
else:
wrapped_lines = [
_Segment.adjust_line_length(
segments,
render_options.max_width,
style=background_style,
pad=not transparent_background,
)
]
for first, wrapped_line in loop_first(wrapped_lines):
if first:
line_column = str(line_no).rjust(numbers_column_width - 2) + " "
if highlight_line(line_no):
yield _Segment(line_pointer, Style(color="red"))
yield _Segment(line_column, highlight_number_style)
else:
yield _Segment(" ", highlight_number_style)
yield _Segment(line_column, number_style)
else:
yield padding
yield from wrapped_line
yield new_line
if __name__ == "__main__": # pragma: no cover
import argparse
import sys
parser = argparse.ArgumentParser(
description="Render syntax to the console with Rich"
)
parser.add_argument(
"path",
metavar="PATH",
nargs="?",
help="path to file",
)
parser.add_argument(
"-c",
"--force-color",
dest="force_color",
action="store_true",
default=None,
help="force color for non-terminals",
)
parser.add_argument(
"-i",
"--indent-guides",
dest="indent_guides",
action="store_true",
default=False,
help="display indent guides",
)
parser.add_argument(
"-l",
"--line-numbers",
dest="line_numbers",
action="store_true",
help="render line numbers",
)
parser.add_argument(
"-w",
"--width",
type=int,
dest="width",
default=None,
help="width of output (default will auto-detect)",
)
parser.add_argument(
"-r",
"--wrap",
dest="word_wrap",
action="store_true",
default=False,
help="word wrap long lines",
)
parser.add_argument(
"-s",
"--soft-wrap",
action="store_true",
dest="soft_wrap",
default=False,
help="enable soft wrapping mode",
)
parser.add_argument(
"-t", "--theme", dest="theme", default="monokai", help="pygments theme"
)
parser.add_argument(
"-b",
"--background-color",
dest="background_color",
default=None,
help="Overide background color",
)
parser.add_argument(
"-L",
"--lexer",
default="default",
dest="lexer_name",
help="Lexer name",
)
args = parser.parse_args()
from rich.console import Console
console = Console(force_terminal=args.force_color, width=args.width)
kwargs = dict(
line_numbers=args.line_numbers,
word_wrap=args.word_wrap,
theme=args.theme,
background_color=args.background_color,
indent_guides=args.indent_guides,
)
if not args.path or args.path == "-":
code = sys.stdin.read()
syntax = Syntax(code=code, lexer_name=args.lexer_name, **kwargs)
else:
syntax = Syntax.from_path(args.path, **kwargs)
console.print(syntax, soft_wrap=args.soft_wrap)
|