MCPcopy
hub / github.com/Textualize/rich / JSONHighlighter

Class JSONHighlighter

rich/highlighter.py:106–140  ·  view source on GitHub ↗

Highlights JSON

Source from the content-addressed store, hash-verified

104
105
106class JSONHighlighter(RegexHighlighter):
107 """Highlights JSON"""
108
109 # Captures the start and end of JSON strings, handling escaped quotes
110 JSON_STR = r"(?<![\\\w])(?P<str>b?\".*?(?<!\\)\")"
111 JSON_WHITESPACE = {" ", "\n", "\r", "\t"}
112
113 base_style: ClassVar[str] = "json."
114 highlights: ClassVar[Sequence[str]] = [
115 _combine_regex(
116 r"(?P<brace>[\{\[\(\)\]\}])",
117 r"\b(?P<bool_true>true)\b|\b(?P<bool_false>false)\b|\b(?P<null>null)\b",
118 r"(?P<number>(?<!\w)\-?[0-9]+\.?[0-9]*(e[\-\+]?\d+?)?\b|0x[0-9a-fA-F]*)",
119 JSON_STR,
120 ),
121 ]
122
123 def highlight(self, text: Text) -> None:
124 super().highlight(text)
125
126 # Additional work to handle highlighting JSON keys
127 plain = text.plain
128 append = text.spans.append
129 whitespace = self.JSON_WHITESPACE
130 for match in re.finditer(self.JSON_STR, plain):
131 start, end = match.span()
132 cursor = end
133 while cursor < len(plain):
134 char = plain[cursor]
135 cursor += 1
136 if char == ":":
137 append(Span(start, end, "json.key"))
138 elif char in whitespace:
139 continue
140 break
141
142
143class ISO8601Highlighter(RegexHighlighter):

Callers 6

__init__Method · 0.85
from_dataMethod · 0.85

Calls 1

_combine_regexFunction · 0.85