diff options
| author | Zach DeCook <zachdecook@librem.one> | 2023-01-16 11:01:34 -0500 |
|---|---|---|
| committer | Zach DeCook <zachdecook@librem.one> | 2023-01-16 11:01:34 -0500 |
| commit | d1ceb9cf3bb505b5e1a8023930ddd21e99a05f7b (patch) | |
| tree | 36a281ccf6a835c3eecc59d8c66dfa33c3451aa5 /mime/highlightedcode.py | |
| parent | 0b8142eee1d0f0a85120f3e43a32a3cda850bf19 (diff) | |
| download | browset-d1ceb9cf3bb505b5e1a8023930ddd21e99a05f7b.tar.gz | |
HighlightedCode: Handle everything that pygments can handle
Diffstat (limited to 'mime/highlightedcode.py')
| -rw-r--r-- | mime/highlightedcode.py | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/mime/highlightedcode.py b/mime/highlightedcode.py index 63defef..49c5046 100644 --- a/mime/highlightedcode.py +++ b/mime/highlightedcode.py @@ -1,6 +1,8 @@ from textual.widgets import Static from textual.containers import Container from rich.syntax import Syntax +from pygments.lexers import get_lexer_for_mimetype +from pygments.util import ClassNotFound # mime list from gemini://geminispace.info/statistics mimetolexer = { @@ -23,6 +25,7 @@ mimetolexer = { "application/atom+xml": "xml", "text/xml": "xml", "image/svg+xml": "xml", + # Less popular mimes will call out to get_lexer_for_mimetype } class HighlightedCode(Static): @@ -41,5 +44,15 @@ class HighlightedCode(Static): if type(line) is bytes: line = line.decode("UTF-8") code += line - lexer = mimetolexer[mime] - self.mount(Static(Syntax(code,lexer=lexer))) + lexer = mimetolexer[mime] if mime in mimetolexer else get_lexer_for_mimetype(mime) + self.mount(Static(Syntax(code,lexer=lexer,background_color="default"))) + + def can_handle_mime(mime): + if mime in mimetolexer: + return True + try: + get_lexer_for_mimetype(mime) + return True + except ClassNotFound: + pass + return False |
