click_extra.capture.snippet module¶
Draw a snippet of source code as a static document.
click_extra.capture.screenshot pictures what a command printed. This module
pictures what a file says, and hands the result to the same renderer: a
README, a slide or a social post that cannot run code equally cannot syntax
highlight it, so both need a picture.
The pipeline is the screenshot module’s, with its first step swapped:
highlight_code()colors the source with Pygments, which writes the same ANSI escape sequences a terminal would.render()turns that text into a document.
That leaves nothing to reimplement. Pygments’ terminal formatter speaks the
interchange format split_ansi() already parses, so a
snippet inherits the window, the presets, the light and dark chromes, the line
numbers, the emphasis bands and the HTML export from the captures beside it.
render_snippet() chains both, and is what the click-extra snippet
command calls.
Note
A snippet answers to a Pygments style, where a capture answers to a terminal
palette. The two name colors differently: a style states every color it uses,
while a terminal names sixteen and leaves their shades to whoever draws them.
style_palette() is where the first becomes the second.
- click_extra.capture.snippet.DEFAULT_SYNTAX_STYLES: dict[TerminalBackground, str] = {TerminalBackground.DARK: 'monokai', TerminalBackground.LIGHT: 'default'}¶
Pygments style a snippet is colored with, per chrome.
Both are picked to sit beside a terminal capture without a seam showing.
monokaipaints#272822, two shades off the#292929a dark capture is drawn on, and Pygments’ owndefaultpaints#f8f8f8against a light capture’s white. A darker style renders perfectly well on its own and steps visibly when the two images share a page, which is the case this default is chosen for.
- click_extra.capture.snippet.TAB_WIDTH = 4¶
Spaces a tab is expanded to before the snippet is laid out.
A terminal expands tabs as it prints, so captured output never carries one and the renderer never had to answer for it. A file does carry them, and a tab is the one character whose width depends on where it starts: it reaches the next stop eight columns along.
grid()weighs each character on its own, so it reads every tab as the eight cellscell_width()answers for one measured alone, wherever the tab actually sits. Expanding up front is what keeps the grid arithmetic true, at the width a reader expects of source code rather than of a terminal.
- click_extra.capture.snippet.FALLBACK_LEXER = 'text'¶
Lexer a snippet is colored with when nothing identifies its language.
Emits one unstyled token per line, so an unrecognized file is drawn as the plain text it could not be proven to be, rather than miscolored as a guess.
- click_extra.capture.snippet.known_styles()[source]¶
Every Pygments style a snippet can be colored with, sorted.
Cached: the walk behind
pygments.styles.get_all_styles()reads the plugin entry points on every call, and a process never gains a style.
- click_extra.capture.snippet.resolve_lexer(code, *, language=None, filename=None)[source]¶
Pick the lexer a snippet is colored with.
Three sources answer, in falling order of how much they know: the language stated outright, the file name it was read from, and the code itself. A stated language that names no lexer is an error rather than a fallback, since silently drawing
pythnas plain text hides the typo in a picture nobody re-reads.- Parameters:
- Return type:
- Returns:
the lexer.
- Raises:
ValueError – when
languagenames no lexer Pygments knows.
- click_extra.capture.snippet.resolve_style(style, background)[source]¶
The Pygments style a snippet is colored with.
- Parameters:
style (
str|None) – the style asked for, orNonefor the chrome’s default.background (
TerminalBackground) – chrome the snippet is drawn on.
- Return type:
- Returns:
the style’s name.
- Raises:
ValueError – when
stylenames no style Pygments knows.
- click_extra.capture.snippet.style_palette(style, background, *, preset=None)[source]¶
The colors a snippet’s window is drawn with, taken from a syntax style.
A style states the background it was designed against, and a snippet drawn on any other one is a picture of that style in a window it was never meant for:
monokai’s comment gray reads on#272822and disappears on white.The chrome’s own palette is where the rest comes from. Its sixteen ANSI slots are carried through untouched, being what a preset publishes and what a run captured in the same document resolves against; a snippet consults none of them, since Pygments states every color outright.
Note
A style naming no color for plain text keeps the chrome’s foreground, which is what the twenty-five light styles shipping no
Token.Textcolor need: their code is black on near-white, and black is what a light chrome already names.- Parameters:
style (
str) – name of the Pygments style.background (
TerminalBackground) – chrome the snippet is drawn on.preset (
TerminalPreset|None) – terminal the window is dressed as, whose palette the decorations keep answering to.
- Return type:
- Returns:
the palette.
- click_extra.capture.snippet.highlight_code(code, *, language=None, filename=None, style='monokai')[source]¶
Color source code the way a terminal would print it.
Pygments’ true-color terminal formatter writes
38;2;r;g;bsequences, which is the one interchange format both capture renderers already read. So the output of this drops straight intorender(), with nothing in between.- Parameters:
- Return type:
- Returns:
the source, ANSI escape sequences included.
- Raises:
ValueError – when
languagenames no lexer Pygments knows.
- click_extra.capture.snippet.render_snippet(code, *, format=CaptureFormat.SVG, language=None, filename=None, style=None, columns='auto', head=None, tail=None, truncation='auto', line_numbers=False, emphasize=(), wrap_marker='', title='', unique_id=None, full=True, background=TerminalBackground.DARK, preset=None, font_stack=None, chrome=Chrome(border=None, border_width=1, radius=None, backdrop='none', shadow=None, margin=48, padding=8, opacity=1.0, watermark='generated with click-extra', watermark_color=None))[source]¶
Color source code and render it as a document.
Chains
highlight_code(),trim_lines()andrender().Note
The width defaults to
AUTO_COLUMNS, where a terminal capture pins eighty. A command wraps its own output to the width it was told about, so a capture at that width folds nothing; nobody wrapped a source file to any width, and a picture that soft-wrapped its code would break indentation the reader is meant to be reading.- Parameters:
code (
str) – the source to draw.format (
CaptureFormat) – which document to produce.language (
str|None) – the language, as Pygments names it. Seeresolve_lexer()for what answers when it is left out.filename (
str|None) – name the code was read from, extension included.style (
str|None) – name of the Pygments style to color with.Nonetakes the chrome’s own, seeDEFAULT_SYNTAX_STYLES.columns (
int|Literal['auto']) – width, in characters, an SVG is laid out at.truncation (
str) – line standing in for the lines cut byheadortail.line_numbers (
bool) – draw each line’s number in a gutter, seenumber_lines().emphasize (
Sequence[int]) – lines to draw a band behind, counted from1.background (
TerminalBackground) – seerender().preset (
TerminalPreset|None) – seerender().
- Return type:
- Returns:
the rendered document.
- Raises:
ValueError – when
languageorstylenames nothing Pygments knows.