aboutsummaryrefslogtreecommitdiffstats
path: root/filters/syntax-highlighting.py
diff options
context:
space:
mode:
authorSamuel Lidén Borell <samuel@kodafritt.se>2023-01-29 17:55:29 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2023-02-08 13:50:05 -0300
commit8ed1bef90f631989c0cadc326a163b874a64e02d (patch)
treea5fa7dbbba44450a08c5f91f36774dc8c4016059 /filters/syntax-highlighting.py
parentf37dd0dd3a8e48eb0ee59ba6fe53b6ebf92566b1 (diff)
downloadcgit-8ed1bef90f631989c0cadc326a163b874a64e02d.tar.gz
cgit-8ed1bef90f631989c0cadc326a163b874a64e02d.tar.bz2
cgit-8ed1bef90f631989c0cadc326a163b874a64e02d.zip
css: Support for dark modejd/zx2c4-deployment
Modern browsers have a "dark mode" preference, which enables alternate styles on web sites that support this. This patch adds a dark color scheme, that is automatically activated via a CSS @media query. Older browsers that do not support color schemes will simply show the light scheme, but possibly without syntax highlighting. Note that filters that use color (such as source highlighters) and logotypes may need to be updated to work with a black background! See the updated files in the filters/ directory. Signed-off-by: Samuel Lidén Borell <samuel@kodafritt.se> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'filters/syntax-highlighting.py')
-rwxr-xr-xfilters/syntax-highlighting.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/filters/syntax-highlighting.py b/filters/syntax-highlighting.py
index e912594..f2c0fe1 100755
--- a/filters/syntax-highlighting.py
+++ b/filters/syntax-highlighting.py
@@ -29,12 +29,16 @@ from pygments.lexers import guess_lexer
from pygments.lexers import guess_lexer_for_filename
from pygments.formatters import HtmlFormatter
+# The dark style is automatically selected if the browser is in dark mode
+light_style='pastie'
+dark_style='monokai'
sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='replace')
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
data = sys.stdin.read()
filename = sys.argv[1]
-formatter = HtmlFormatter(style='pastie', nobackground=True)
+light_formatter = HtmlFormatter(style=light_style, nobackground=True)
+dark_formatter = HtmlFormatter(style=dark_style, nobackground=True)
try:
lexer = guess_lexer_for_filename(filename, data)
@@ -50,6 +54,10 @@ except TypeError:
# highlight! :-)
# printout pygments' css definitions as well
sys.stdout.write('<style>')
-sys.stdout.write(formatter.get_style_defs('.highlight'))
+sys.stdout.write('\n@media only all and (prefers-color-scheme: dark) {\n')
+sys.stdout.write(dark_formatter.get_style_defs('.highlight'))
+sys.stdout.write('\n}\n@media (prefers-color-scheme: light) {\n')
+sys.stdout.write(light_formatter.get_style_defs('.highlight'))
+sys.stdout.write('\n}\n')
sys.stdout.write('</style>')
-sys.stdout.write(highlight(data, lexer, formatter, outfile=None))
+sys.stdout.write(highlight(data, lexer, light_formatter, outfile=None))