Skip to content

Commit

Permalink
Merge pull request #1920 from brakhane/fix-repr-highlighter
Browse files Browse the repository at this point in the history
Fix repr highlighter
  • Loading branch information
willmcgugan committed Feb 11, 2022
2 parents 2aea852 + 099893e commit a2f6688
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -18,6 +18,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- In Jupyter mode make the link target be set to "_blank"
- Fix some issues with markup handling around "[" characters https://github.com/Textualize/rich/pull/1950

### Added

- Add support for enum.Flag in ReprHighlighter https://github.com/Textualize/rich/pull/1920

## [11.2.0] - 2022-02-08

### Added
Expand Down
4 changes: 3 additions & 1 deletion CONTRIBUTORS.md
Expand Up @@ -27,6 +27,8 @@ The following people have contributed to the development of Rich:
- [Tim Savage](https://github.com/timsavage)
- [Nicolas Simonds](https://github.com/0xDEC0DE)
- [Gabriele N. Tornetta](https://github.com/p403n1x87)
- [Patrick Arminio](https://github.com/patrick91)
- [Patrick Arminio](https://github.com/patrick9)
- [Dennis Brakhane](https://github.com/brakhane)
- [Michał Górny](https://github.com/mgorny)
- [Arian Mollik Wasi](https://github.com/wasi-master)

18 changes: 9 additions & 9 deletions rich/highlighter.py
Expand Up @@ -81,22 +81,22 @@ class ReprHighlighter(RegexHighlighter):

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

Expand Down
10 changes: 10 additions & 0 deletions tests/test_highlighter.py
Expand Up @@ -40,6 +40,16 @@ def test_wrong_type():
Span(4, 9, "repr.str"),
],
),
(
"<Permission.WRITE|READ: 3>",
[
Span(0, 1, "repr.tag_start"),
Span(1, 23, "repr.tag_name"),
Span(23, 25, "repr.tag_contents"),
Span(25, 26, "repr.tag_end"),
Span(24, 25, "repr.number"),
],
),
("( )", [Span(0, 1, "repr.brace"), Span(2, 3, "repr.brace")]),
("[ ]", [Span(0, 1, "repr.brace"), Span(2, 3, "repr.brace")]),
("{ }", [Span(0, 1, "repr.brace"), Span(2, 3, "repr.brace")]),
Expand Down

0 comments on commit a2f6688

Please sign in to comment.