Skip to content

Syntax Reference

Bases: Gradient

A Rich syntax-highlighting convenience constructor with gradient rendering.

Parameters:

Name Type Description Default
code str

Source code to highlight.

required
lexer str | Lexer

Lexer name or Pygments lexer instance.

required
theme str

Pygments style theme.

'monokai'
dedent bool

Whether to strip initial indentation.

False
line_numbers bool

Whether to render line numbers.

False
start_line int

Starting line number.

1
line_range tuple[int | None, int | None] | None

Optional start/end line range.

None
highlight_lines set[int] | None

Optional line numbers to highlight.

None
code_width int | None

Optional code width.

None
tab_size int

Spaces per tab.

4
word_wrap bool

Whether to wrap long lines.

False
background_color str | None

Optional background color.

None
indent_guides bool

Whether to show indentation guides.

False
padding PaddingDimensions

Padding around syntax output.

0
colors Optional[Sequence[ColorType]]

Foreground gradient color stops.

None
bg_colors Optional[Sequence[ColorType]]

Background gradient color stops.

None
rainbow bool

Whether to generate a rainbow palette.

False
hues int

Number of auto-generated hues.

5
repeat_scale float

Scale factor controlling gradient repeats.

2.0
expand bool

Whether the gradient frame expands.

True
justify AlignMethod

Horizontal alignment.

'left'
vertical_justify VerticalAlignMethod

Vertical alignment.

'middle'
console Optional[Console]

Optional Rich console.

None
highlight_words Optional[HighlightWordsType]

Word highlight configuration.

None
highlight_regex Optional[HighlightRegexType]

Regex highlight configuration.

None
Source code in src/rich_gradient/syntax.py
class Syntax(Gradient):
    """A Rich syntax-highlighting convenience constructor with gradient rendering.

    Args:
        code: Source code to highlight.
        lexer: Lexer name or Pygments lexer instance.
        theme: Pygments style theme.
        dedent: Whether to strip initial indentation.
        line_numbers: Whether to render line numbers.
        start_line: Starting line number.
        line_range: Optional start/end line range.
        highlight_lines: Optional line numbers to highlight.
        code_width: Optional code width.
        tab_size: Spaces per tab.
        word_wrap: Whether to wrap long lines.
        background_color: Optional background color.
        indent_guides: Whether to show indentation guides.
        padding: Padding around syntax output.
        colors: Foreground gradient color stops.
        bg_colors: Background gradient color stops.
        rainbow: Whether to generate a rainbow palette.
        hues: Number of auto-generated hues.
        repeat_scale: Scale factor controlling gradient repeats.
        expand: Whether the gradient frame expands.
        justify: Horizontal alignment.
        vertical_justify: Vertical alignment.
        console: Optional Rich console.
        highlight_words: Word highlight configuration.
        highlight_regex: Regex highlight configuration.
    """

    def __init__(
        self,
        code: str,
        lexer: str | Lexer,
        *,
        theme: str = "monokai",
        dedent: bool = False,
        line_numbers: bool = False,
        start_line: int = 1,
        line_range: tuple[int | None, int | None] | None = None,
        highlight_lines: set[int] | None = None,
        code_width: int | None = None,
        tab_size: int = 4,
        word_wrap: bool = False,
        background_color: str | None = None,
        indent_guides: bool = False,
        padding: PaddingDimensions = 0,
        colors: Optional[Sequence[ColorType]] = None,
        bg_colors: Optional[Sequence[ColorType]] = None,
        rainbow: bool = False,
        hues: int = 5,
        repeat_scale: float = 2.0,
        expand: bool = True,
        justify: AlignMethod = "left",
        vertical_justify: VerticalAlignMethod = "middle",
        console: Optional[Console] = None,
        highlight_words: Optional[HighlightWordsType] = None,
        highlight_regex: Optional[HighlightRegexType] = None,
    ) -> None:
        """Initialize gradient-enabled Rich syntax highlighting."""
        syntax = RichSyntax(
            code,
            lexer,
            theme=theme,
            dedent=dedent,
            line_numbers=line_numbers,
            start_line=start_line,
            line_range=line_range,
            highlight_lines=highlight_lines,
            code_width=code_width,
            tab_size=tab_size,
            word_wrap=word_wrap,
            background_color=background_color,
            indent_guides=indent_guides,
            padding=padding,
        )
        self._syntax = syntax
        super().__init__(
            syntax,
            colors=list(colors) if colors is not None else None,
            bg_colors=list(bg_colors) if bg_colors is not None else None,
            console=console,
            hues=hues,
            rainbow=rainbow,
            expand=expand,
            justify=justify,
            vertical_justify=vertical_justify,
            repeat_scale=repeat_scale,
            highlight_words=highlight_words,
            highlight_regex=highlight_regex,
        )

    @property
    def syntax(self) -> RichSyntax:
        """Return the underlying Rich Syntax renderable."""
        return self._syntax

syntax property

Return the underlying Rich Syntax renderable.

__init__(code, lexer, *, theme='monokai', dedent=False, line_numbers=False, start_line=1, line_range=None, highlight_lines=None, code_width=None, tab_size=4, word_wrap=False, background_color=None, indent_guides=False, padding=0, colors=None, bg_colors=None, rainbow=False, hues=5, repeat_scale=2.0, expand=True, justify='left', vertical_justify='middle', console=None, highlight_words=None, highlight_regex=None)

Initialize gradient-enabled Rich syntax highlighting.

Source code in src/rich_gradient/syntax.py
def __init__(
    self,
    code: str,
    lexer: str | Lexer,
    *,
    theme: str = "monokai",
    dedent: bool = False,
    line_numbers: bool = False,
    start_line: int = 1,
    line_range: tuple[int | None, int | None] | None = None,
    highlight_lines: set[int] | None = None,
    code_width: int | None = None,
    tab_size: int = 4,
    word_wrap: bool = False,
    background_color: str | None = None,
    indent_guides: bool = False,
    padding: PaddingDimensions = 0,
    colors: Optional[Sequence[ColorType]] = None,
    bg_colors: Optional[Sequence[ColorType]] = None,
    rainbow: bool = False,
    hues: int = 5,
    repeat_scale: float = 2.0,
    expand: bool = True,
    justify: AlignMethod = "left",
    vertical_justify: VerticalAlignMethod = "middle",
    console: Optional[Console] = None,
    highlight_words: Optional[HighlightWordsType] = None,
    highlight_regex: Optional[HighlightRegexType] = None,
) -> None:
    """Initialize gradient-enabled Rich syntax highlighting."""
    syntax = RichSyntax(
        code,
        lexer,
        theme=theme,
        dedent=dedent,
        line_numbers=line_numbers,
        start_line=start_line,
        line_range=line_range,
        highlight_lines=highlight_lines,
        code_width=code_width,
        tab_size=tab_size,
        word_wrap=word_wrap,
        background_color=background_color,
        indent_guides=indent_guides,
        padding=padding,
    )
    self._syntax = syntax
    super().__init__(
        syntax,
        colors=list(colors) if colors is not None else None,
        bg_colors=list(bg_colors) if bg_colors is not None else None,
        console=console,
        hues=hues,
        rainbow=rainbow,
        expand=expand,
        justify=justify,
        vertical_justify=vertical_justify,
        repeat_scale=repeat_scale,
        highlight_words=highlight_words,
        highlight_regex=highlight_regex,
    )