<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">from abc import ABC, abstractmethod
from typing import List, Union

from .text import Text


def _combine_regex(*regexes: str) -&gt; str:
    """Combine a number of regexes in to a single regex.

    Returns:
        str: New regex with all regexes ORed together.
    """
    return "|".join(regexes)


class Highlighter(ABC):
    """Abstract base class for highlighters."""

    def __call__(self, text: Union[str, Text]) -&gt; Text:
        """Highlight a str or Text instance.

        Args:
            text (Union[str, ~Text]): Text to highlight.

        Raises:
            TypeError: If not called with text or str.

        Returns:
            Text: A test instance with highlighting applied.
        """
        if isinstance(text, str):
            highlight_text = Text(text)
        elif isinstance(text, Text):
            highlight_text = text.copy()
        else:
            raise TypeError(f"str or Text instance required, not {text!r}")
        self.highlight(highlight_text)
        return highlight_text

    @abstractmethod
    def highlight(self, text: Text) -&gt; None:
        """Apply highlighting in place to text.

        Args:
            text (~Text): A text object highlight.
        """


class NullHighlighter(Highlighter):
    """A highlighter object that doesn't highlight.

    May be used to disable highlighting entirely.

    """

    def highlight(self, text: Text) -&gt; None:
        """Nothing to do"""


class RegexHighlighter(Highlighter):
    """Applies highlighting from a list of regular expressions."""

    highlights: List[str] = []
    base_style: str = ""

    def highlight(self, text: Text) -&gt; None:
        """Highlight :class:`rich.text.Text` using regular expressions.

        Args:
            text (~Text): Text to highlighted.

        """

        highlight_regex = text.highlight_regex
        for re_highlight in self.highlights:
            highlight_regex(re_highlight, style_prefix=self.base_style)


class ReprHighlighter(RegexHighlighter):
    """Highlights the text typically produced from ``__repr__`` methods."""

    base_style = "repr."
    highlights = [
        r"(?P&lt;tag_start&gt;\&lt;)(?P&lt;tag_name&gt;[\w\-\.\:]*)(?P&lt;tag_contents&gt;[\w\W]*?)(?P&lt;tag_end&gt;\&gt;)",
        r"(?P&lt;attrib_name&gt;[\w_]{1,50})=(?P&lt;attrib_value&gt;\"?[\w_]+\"?)?",
        r"(?P&lt;brace&gt;[\{\[\(\)\]\}])",
        _combine_regex(
            r"(?P&lt;ipv4&gt;[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})",
            r"(?P&lt;ipv6&gt;([A-Fa-f0-9]{1,4}::?){1,7}[A-Fa-f0-9]{1,4})",
            r"(?P&lt;eui64&gt;(?:[0-9A-Fa-f]{1,2}-){7}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{1,2}:){7}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{4}\.){3}[0-9A-Fa-f]{4})",
            r"(?P&lt;eui48&gt;(?:[0-9A-Fa-f]{1,2}-){5}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{1,2}:){5}[0-9A-Fa-f]{1,2}|(?:[0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})",
            r"(?P&lt;call&gt;[\w\.]*?)\(",
            r"\b(?P&lt;bool_true&gt;True)\b|\b(?P&lt;bool_false&gt;False)\b|\b(?P&lt;none&gt;None)\b",
            r"(?P&lt;ellipsis&gt;\.\.\.)",
            r"(?P&lt;number&gt;(?&lt;!\w)\-?[0-9]+\.?[0-9]*(e[\-\+]?\d+?)?\b|0x[0-9a-fA-F]*)",
            r"(?P&lt;path&gt;\B(\/[\w\.\-\_\+]+)*\/)(?P&lt;filename&gt;[\w\.\-\_\+]*)?",
            r"(?&lt;![\\\w])(?P&lt;str&gt;b?\'\'\'.*?(?&lt;!\\)\'\'\'|b?\'.*?(?&lt;!\\)\'|b?\"\"\".*?(?&lt;!\\)\"\"\"|b?\".*?(?&lt;!\\)\")",
            r"(?P&lt;uuid&gt;[a-fA-F0-9]{8}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{12})",
            r"(?P&lt;url&gt;(file|https|http|ws|wss):\/\/[0-9a-zA-Z\$\-\_\+\!`\(\)\,\.\?\/\;\:\&amp;\=\%\#]*)",
        ),
    ]


class JSONHighlighter(RegexHighlighter):
    """Highlights JSON"""

    base_style = "json."
    highlights = [
        _combine_regex(
            r"(?P&lt;brace&gt;[\{\[\(\)\]\}])",
            r"\b(?P&lt;bool_true&gt;true)\b|\b(?P&lt;bool_false&gt;false)\b|\b(?P&lt;null&gt;null)\b",
            r"(?P&lt;number&gt;(?&lt;!\w)\-?[0-9]+\.?[0-9]*(e[\-\+]?\d+?)?\b|0x[0-9a-fA-F]*)",
            r"(?&lt;![\\\w])(?P&lt;str&gt;b?\".*?(?&lt;!\\)\")",
        ),
        r"(?&lt;![\\\w])(?P&lt;key&gt;b?\".*?(?&lt;!\\)\")\:",
    ]


if __name__ == "__main__":  # pragma: no cover
    from .console import Console

    console = Console()
    console.print("[bold green]hello world![/bold green]")
    console.print("'[bold green]hello world![/bold green]'")

    console.print(" /foo")
    console.print("/foo/")
    console.print("/foo/bar")
    console.print("foo/bar/baz")

    console.print("/foo/bar/baz?foo=bar+egg&amp;egg=baz")
    console.print("/foo/bar/baz/")
    console.print("/foo/bar/baz/egg")
    console.print("/foo/bar/baz/egg.py")
    console.print("/foo/bar/baz/egg.py word")
    console.print(" /foo/bar/baz/egg.py word")
    console.print("foo /foo/bar/baz/egg.py word")
    console.print("foo /foo/bar/ba._++z/egg+.py word")
    console.print("https://example.org?foo=bar#header")

    console.print(1234567.34)
    console.print(1 / 2)
    console.print(-1 / 123123123123)

    console.print(
        "127.0.1.1 bar 192.168.1.4 2001:0db8:85a3:0000:0000:8a2e:0370:7334 foo"
    )
</pre></body></html>