From 2707a734359a8f8b6b617e37963ea30b5d67e843 Mon Sep 17 00:00:00 2001 From: Tomer Shalev Date: Thu, 26 May 2022 21:59:46 +0300 Subject: [PATCH 01/74] Add Iso8601Highlighter Add ability to highlight date and time strings according to ISO8601 [1] [1] https://en.wikipedia.org/wiki/ISO_8601 --- CHANGELOG.md | 9 ++ CONTRIBUTORS.md | 3 + docs/source/highlighting.rst | 1 + rich/default_styles.py | 3 + rich/highlighter.py | 58 ++++++++ tests/test_highlighter.py | 253 ++++++++++++++++++++++++++++++++++- 6 files changed, 326 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52db0457b..70652e146 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- Fix text wrapping edge case https://github.com/Textualize/rich/pull/2296 +- Allow exceptions that are raised while a Live is rendered to be displayed and/or processed https://github.com/Textualize/rich/pull/2305 +- Fix crashes that can happen with `inspect` when docstrings contain some special control codes https://github.com/Textualize/rich/pull/2294 +- Fix edges used in first row of tables when `show_header=False` https://github.com/Textualize/rich/pull/2330 + ## [12.4.4] - 2022-05-24 ### Changed diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index c24f21b1a..fa3de240b 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -40,3 +40,6 @@ The following people have contributed to the development of Rich: - [Gabriele N. Tornetta](https://github.com/p403n1x87) - [Arian Mollik Wasi](https://github.com/wasi-master) - [Handhika Yanuar Pratama](https://github.com/theDreamer911) +- [za](https://github.com/za) +- [Motahhar Mokfi](https://github.com/motahhar) +- [Tomer Shalev](https://github.com/tomers) \ No newline at end of file diff --git a/docs/source/highlighting.rst b/docs/source/highlighting.rst index 34274d977..0bd68292f 100644 --- a/docs/source/highlighting.rst +++ b/docs/source/highlighting.rst @@ -4,6 +4,7 @@ Highlighting ============ Rich can apply styles to patterns in text which you :meth:`~rich.console.Console.print` or :meth:`~rich.console.Console.log`. With the default settings, Rich will highlight things such as numbers, strings, collections, booleans, None, and a few more exotic patterns such as file paths, URLs and UUIDs. +Additional non-default highlighter are available, such as :class:`~rich.highlighter.ISO8601Highlighter` to highlight date and time. You can disable highlighting either by setting ``highlight=False`` on :meth:`~rich.console.Console.print` or :meth:`~rich.console.Console.log`, or by setting ``highlight=False`` on the :class:`~rich.console.Console` constructor which disables it everywhere. If you disable highlighting on the constructor, you can still selectively *enable* highlighting with ``highlight=True`` on print/log. diff --git a/rich/default_styles.py b/rich/default_styles.py index 1969bb787..f8ec672eb 100644 --- a/rich/default_styles.py +++ b/rich/default_styles.py @@ -157,6 +157,9 @@ "markdown.h7": Style(italic=True, dim=True), "markdown.link": Style(color="bright_blue"), "markdown.link_url": Style(color="blue"), + "iso8601.date": Style(color="blue"), + "iso8601.time": Style(color="magenta"), + "iso8601.timezone": Style(color="yellow"), } diff --git a/rich/highlighter.py b/rich/highlighter.py index 193261c4c..82293dffc 100644 --- a/rich/highlighter.py +++ b/rich/highlighter.py @@ -140,6 +140,64 @@ def highlight(self, text: Text) -> None: break +class ISO8601Highlighter(RegexHighlighter): + """Highlights the ISO8601 date time strings. + Regex reference: https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s07.html + """ + + base_style = "iso8601." + highlights = [ + # + # Dates + # + # Calendar month (e.g. 2008-08). The hyphen is required + r"^(?P[0-9]{4})-(?P1[0-2]|0[1-9])$", + # Calendar date w/o hyphens (e.g. 20080830) + r"^(?P(?P[0-9]{4})(?P1[0-2]|0[1-9])(?P3[01]|0[1-9]|[12][0-9]))$", + # Ordinal date (e.g. 2008-243). The hyphen is optional + r"^(?P(?P[0-9]{4})-?(?P36[0-6]|3[0-5][0-9]|[12][0-9]{2}|0[1-9][0-9]|00[1-9]))$", + # + # Weeks + # + # Week of the year (e.g., 2008-W35). The hyphen is optional + r"^(?P(?P[0-9]{4})-?W(?P5[0-3]|[1-4][0-9]|0[1-9]))$", + # Week date (e.g., 2008-W35-6). The hyphens are optional + r"^(?P(?P[0-9]{4})-?W(?P5[0-3]|[1-4][0-9]|0[1-9])-?(?P[1-7]))$", + # + # Times + # + # Hours and minutes (e.g., 17:21). The colon is optional + r"^(?P