Commit 8ed1bef90f631989c0cadc326a163b874a64e02d

Parents: f37dd0dd3a8e48eb0ee59ba6fe53b6ebf92566b1

From: Samuel Lidén Borell <samuel@kodafritt.se>
Date: Sun Jan 29 17:55:29 2023 +0700

css: Support for dark mode
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>

Stats

cgit.css +113/-0
filters/html-converters/md2html +28/-2
filters/syntax-highlighting.py +11/-3
filters/syntax-highlighting.sh +62/-36

Changeset

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
diff --git a/cgit.css b/cgit.css
index 35ec45b465ec76160faa8e05f6eaba463dfd2925..76eb3f4598554f3b48e50bde1c3ee9f6cf7a6d15 100644
--- a/cgit.css
+++ b/cgit.css
@@ -930,3 +930,116 @@         margin-left: 1.5em;
         background-color: #eeeeee;
         box-shadow: 2px 2px 7px rgba(100,100,100,0.75);
 }
+
+/*
+   Color overrides for browsers running in dark mode.
+   "only all and ..." forces very old browsers to ignore the media query:
+   https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries#improving_compatibility_with_older_browsers
+*/
+:root { color-scheme: light dark; }
+@media only all and (prefers-color-scheme: dark) {
+	html, div#cgit { color: #eee; background: #171717; }
+	div#cgit a { color: #3af; }
+	div#cgit .diffstat-header a { color: #28d; }
+	div#cgit table#header td.main a { color: #eee; }
+	div#cgit table#header td.sub { color: #999; }
+	div#cgit table.tabs { border-bottom-color: #444; }
+	div#cgit table.tabs td a { color: #888; }
+	div#cgit table.tabs td a.active { color: #fff; background-color: #444; }
+	div#cgit div.path { color: #eee; background-color: #333; }
+	div#cgit div.content { border-bottom-color: #bbb; }
+	div#cgit table.list tr { background: #171717; }
+	div#cgit table.list tr.logheader { background: #171717; }
+	div#cgit table.list tr:nth-child(even) { background: #171717; }
+	div#cgit table.list tr:nth-child(odd) { background: #1f1f1f; }
+	div#cgit table.list tr:hover { background: #333; }
+	div#cgit table.list tr.nohover { background: #171717; }
+	div#cgit table.list tr.nohover:hover { background: #171717; }
+	div#cgit table.list tr.nohover-highlight:hover:nth-child(even) { background: #171717; }
+	div#cgit table.list tr.nohover-highlight:hover:nth-child(odd) { background: #1f1f1f; }
+	div#cgit table.list td.commitgraph .column1 { color: #f55; }
+	div#cgit table.list td.commitgraph .column2 { color: #5f5; }
+	div#cgit table.list td.commitgraph .column3 { color: #ff5; }
+	div#cgit table.list td.commitgraph .column4 { color: #55f; }
+	div#cgit table.list td.commitgraph .column5 { color: #f5f; }
+	div#cgit table.list td.commitgraph .column6 { color: #5ff; }
+	div#cgit table.list td a { color: #eee; }
+	div#cgit table.list td a.ls-dir { color: #28d; }
+	div#cgit table.list td a:hover { color: #3af; }
+	div#cgit table#downloads { border-color: #888; }
+	div#cgit table#downloads th { background-color: #333; }
+	div#cgit div#blob { border-color: #eee; }
+	div#cgit table.blob { border-top-color: #eee; }
+	div#cgit table.blob td.hashes,
+	div#cgit table.blob td.lines { color: #eee; }
+	div#cgit table.blob td.linenumbers { border-right-color: gray; }
+	div#cgit table.blob td.linenumbers a,
+	div#cgit table.ssdiff td.lineno a { color: gray; }
+	div#cgit table.blob td.linenumbers a:hover,
+	div#cgit table.ssdiff td.lineno a:hover { color: #eee; }
+	div#cgit table.blame div.alt:nth-child(even) { background: #171717; }
+	div#cgit table.blame div.alt:nth-child(odd) { background: #1f1f1f; }
+	div#cgit table.bin-blob { border-color: #eee; }
+	div#cgit table.bin-blob th { border-color: #888; }
+	div#cgit table.bin-blob td { border-left-color: #888; }
+	div#cgit div.cgit-panel table { border-color: #555; background-color: #282828; }
+	div#cgit div.notes { border-color: #661; background-color: #220; }
+	div#cgit table.diffstat { border-color: #555; background-color: #282828; }
+	div#cgit table.diffstat td span.modechange { color: #c66; }
+	div#cgit table.diffstat td.add a { color: #6c6; }
+	div#cgit table.diffstat td.del a { color: #c66; }
+	div#cgit table.diffstat td.upd a { color: #3af; }
+	div#cgit table.diffstat td.graph td.add { background-color: #3a3; }
+	div#cgit table.diffstat td.graph td.rem { background-color: #a33; }
+	div#cgit div.diffstat-summary { color: #777; }
+	div#cgit table.diff td div.head { color: #eee; }
+	div#cgit table.diff td div.hunk { color: #28d; }
+	div#cgit table.diff td div.add { color: #6c6; }
+	div#cgit table.diff td div.del { color: #c66; }
+	div#cgit table.list td.reposection { color: #777; }
+	div#cgit ul.pager a { color: #888; }
+	div#cgit span.age-mins { color: #7f7; }
+	div#cgit span.age-hours { color: #7f7; }
+	div#cgit span.age-days { color: #9d9; }
+	div#cgit span.age-weeks { color: #bbb; }
+	div#cgit span.age-months { color: #888; }
+	div#cgit span.age-years { color: #666; }
+	div#cgit span.insertions { color: #7f7; }
+	div#cgit span.deletions { color: #e33; }
+	div#cgit div.footer { color: #555; }
+	div#cgit div.footer a { color: #555; }
+	div#cgit a.branch-deco { color: #fff; background-color: #361; border-color: #3b2; }
+	div#cgit a.tag-deco { color: #fff; background-color: #650; border-color: #ba3; }
+	div#cgit a.tag-annotated-deco { color: #fff; background-color: #840; border-color: #b96; }
+	div#cgit a.remote-deco { color: #fff; background-color: #348; border-color: #67c; }
+	div#cgit a.deco { color: #fff; background-color: #922; border-color: #d55; }
+	div#cgit a.branch-deco:hover, div#cgit table.list td a.branch-deco:hover,
+	div#cgit a.tag-deco:hover, div#cgit table.list td a.tag-deco:hover,
+	div#cgit a.tag-annotated-deco:hover, div#cgit table.list td a.tag-annotated-deco:hover,
+	div#cgit a.remote-deco:hover, div#cgit table.list td a.remote-deco:hover,
+	div#cgit a.deco:hover, div#cgit table.list td a.deco:hover { color: #fff; }
+	div#cgit table.stats { border-color: #555; }
+	div#cgit table.stats th { background-color: #333; border-color: #555; }
+	div#cgit table.stats td { border-color: #555; }
+	div#cgit table.stats td.sum { color: #5C7E9B; }
+	div#cgit table.vgraph { border-color: #eee; }
+	div#cgit table.vgraph th { background-color: #1f1f1f; border-color: black; }
+	div#cgit table.vgraph div.bar { background-color: #1f1f1f; }
+	div#cgit table.hgraph { border-color: #eee; }
+	div#cgit table.hgraph th { background-color: #1f1f1f; border-color: #eee; }
+	div#cgit table.hgraph div.bar { background-color: #1f1f1f; }
+	div#cgit table.ssdiff td { border-left-color: #555; border-right-color: #555; }
+	div#cgit table.ssdiff td.add { color: #eee; background: #353; }
+	div#cgit table.ssdiff td.add_dark { color: #eee; background: #030; }
+	div#cgit table.ssdiff span.add { background: #030; }
+	div#cgit table.ssdiff td.del { color: #eee; background: #533; }
+	div#cgit table.ssdiff td.del_dark { color: #eee; background: #411; }
+	div#cgit table.ssdiff span.del { background: #300; }
+	div#cgit table.ssdiff td.changed { color: #eee; background: #553; }
+	div#cgit table.ssdiff td.changed_dark { color: #eee; background: #330; }
+	div#cgit table.ssdiff td.lineno { color: #eee; background: #1f1f1f; }
+	div#cgit table.ssdiff td.hunk { color: #eee; background: #0D3D64; border-top-color: #555; border-bottom-color: #555; }
+	div#cgit table.ssdiff td.head { border-top-color: #555; border-bottom-color: #555; }
+	div#cgit table.ssdiff td.head div.head { color: #eee; }
+	div#cgit table.ssdiff td.foot { border-top-color: #555; }
+}
diff --git a/filters/html-converters/md2html b/filters/html-converters/md2html
index 59f43a84167f5b6c7822958b07a4d3afc0822e16..627808a4a5df935f642f3335f5db20e2ec8e5187 100755
--- a/filters/html-converters/md2html
+++ b/filters/html-converters/md2html
@@ -4,6 +4,11 @@ import sys
 import io
 from pygments.formatters import HtmlFormatter
 from markdown.extensions.toc import TocExtension
+
+# 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')
 sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
 sys.stdout.write('''
@@ -283,10 +288,31 @@     padding: 0;
     background-color: transparent;
     border: none;
 }
+@media only all and (prefers-color-scheme: dark) {
+.markdown-body a.absent { color: #f33; }
+.markdown-body h1 .mini-icon-link, .markdown-body h2 .mini-icon-link, .markdown-body h3 .mini-icon-link, .markdown-body h4 .mini-icon-link, .markdown-body h5 .mini-icon-link, .markdown-body h6 .mini-icon-link { color: #eee; }
+div#cgit .markdown-body h1 a.toclink, div#cgit .markdown-body h2 a.toclink, div#cgit .markdown-body h3 a.toclink, div#cgit .markdown-body h4 a.toclink, div#cgit .markdown-body h5 a.toclink, div#cgit .markdown-body h6 a.toclink { color: #eee; }
+.markdown-body h1 { color: #eee; }
+.markdown-body h2 { border-bottom-color: #333; color: #eee; }
+.markdown-body h6 { color: #888; }
+.markdown-body hr { border-color: #333; }
+.markdown-body blockquote { border-left-color: #222; color: #888; }
+.markdown-body table th, .markdown-body table td { border-color: #333; }
+.markdown-body table tr { border-top-color: #333; background-color: #111; }
+.markdown-body table tr:nth-child(2n) { background-color: #070707; }
+.markdown-body span.frame span span { color: #ccc; }
+.markdown-body code, .markdown-body tt { border-color: #151515; background-color: #070707; }
+.markdown-body .highlight pre, .markdown-body pre { background-color: #070707; border-color: #333; }
 ''')
-sys.stdout.write(HtmlFormatter(style='pastie').get_style_defs('.highlight'))
+sys.stdout.write(HtmlFormatter(style=dark_style).get_style_defs('.highlight'))
 sys.stdout.write('''
-</style>   
+}
+@media (prefers-color-scheme: light) {
+''')
+sys.stdout.write(HtmlFormatter(style=light_style).get_style_defs('.highlight'))
+sys.stdout.write('''
+}
+</style>
 ''')
 sys.stdout.write("<div class='markdown-body'>")
 sys.stdout.flush()
diff --git a/filters/syntax-highlighting.py b/filters/syntax-highlighting.py
index e912594c48a71fce1baffb11149ab9b72cc223d4..f2c0fe129e5726cdbb81c0edd1b529f98859b172 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 @@
 # 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))
diff --git a/filters/syntax-highlighting.sh b/filters/syntax-highlighting.sh
index 840bc34fffd4fbbc1e9b91a7fe273ace08a315d6..0429b0fef2c61e0398f5d3ff7013cac4cf1c28d8 100755
--- a/filters/syntax-highlighting.sh
+++ b/filters/syntax-highlighting.sh
@@ -25,6 +25,20 @@ # table.blob .kwa  { color:#000000; font-weight:bold; }
 # table.blob .kwb  { color:#830000; }
 # table.blob .kwc  { color:#000000; font-weight:bold; }
 # table.blob .kwd  { color:#010181; }
+# @media only all and (prefers-color-scheme: dark) { /* cgit addition for dark mode */
+# 	table.blob .num  { color:#ff814f; }
+# 	table.blob .esc  { color:#f60; }
+# 	table.blob .str  { color:#cc0; }
+# 	table.blob .dstr { color:#ffff7e; }
+# 	table.blob .slc  { color:#7f7c7f; }
+# 	table.blob .com  { color:#7e7c7e; }
+# 	table.blob .dir  { color:#7dff7d; }
+# 	table.blob .sym  { color:#eeeeee; }
+# 	table.blob .kwa  { color:#bb0; }
+# 	table.blob .kwb  { color:#0a0; }
+# 	table.blob .kwc  { color:#eeeeee; }
+# 	table.blob .kwd  { color:#7e7efe; }
+# }
 #
 #
 # Style definition file generated by highlight 2.6.14, http://www.andre-simon.de/
@@ -45,45 +59,57 @@ # .hl.kwa  { color:#000000; font-weight:bold; }
 # .hl.kwb  { color:#830000; }
 # .hl.kwc  { color:#000000; font-weight:bold; }
 # .hl.kwd  { color:#010181; }
-#
-#
-# Style definition file generated by highlight 3.8, http://www.andre-simon.de/
-#
-# body.hl { background-color:#e0eaee; }
-# pre.hl  { color:#000000; background-color:#e0eaee; font-size:10pt; font-family:'Courier New';}
-# .hl.num { color:#b07e00; }
-# .hl.esc { color:#ff00ff; }
-# .hl.str { color:#bf0303; }
-# .hl.pps { color:#818100; }
-# .hl.slc { color:#838183; font-style:italic; }
-# .hl.com { color:#838183; font-style:italic; }
-# .hl.ppc { color:#008200; }
-# .hl.opt { color:#000000; }
-# .hl.lin { color:#555555; }
-# .hl.kwa { color:#000000; font-weight:bold; }
-# .hl.kwb { color:#0057ae; }
-# .hl.kwc { color:#000000; font-weight:bold; }
-# .hl.kwd { color:#010181; }
+# @media only all and (prefers-color-scheme: dark) { /* cgit addition for dark mode */
+# 	pre.hl  { color:#eeeeee; background-color:#111; }
+# 	.hl.num  { color:#ff814f; }
+# 	.hl.esc  { color:#f60; }
+# 	.hl.str  { color:#cc0; }
+# 	.hl.dstr { color:#ffff7e; }
+# 	.hl.slc  { color:#7f7c7f; }
+# 	.hl.com  { color:#7e7c7e; }
+# 	.hl.dir  { color:#7dff7d; }
+# 	.hl.sym  { color:#eeeeee; }
+# 	.hl.line { color:#aaaaaa; }
+# 	.hl.mark { background-color:#444400; }
+# 	.hl.kwa  { color:#bb0; }
+# 	.hl.kwb  { color:#a0a; }
+# 	.hl.kwc  { color:#eeeeee; }
+# 	.hl.kwd  { color:#7e7efe; }
+# }
 #
 #
-# Style definition file generated by highlight 3.13, http://www.andre-simon.de/
+# Style definition file generated by highlight 3.8 / 3.13 / 3.41, http://www.andre-simon.de/
 #
-# body.hl { background-color:#e0eaee; }
-# pre.hl  { color:#000000; background-color:#e0eaee; font-size:10pt; font-family:'Courier New',monospace;}
-# .hl.num { color:#b07e00; }
-# .hl.esc { color:#ff00ff; }
-# .hl.str { color:#bf0303; }
-# .hl.pps { color:#818100; }
-# .hl.slc { color:#838183; font-style:italic; }
-# .hl.com { color:#838183; font-style:italic; }
-# .hl.ppc { color:#008200; }
-# .hl.opt { color:#000000; }
-# .hl.ipl { color:#0057ae; }
-# .hl.lin { color:#555555; }
-# .hl.kwa { color:#000000; font-weight:bold; }
-# .hl.kwb { color:#0057ae; }
-# .hl.kwc { color:#000000; font-weight:bold; }
-# .hl.kwd { color:#010181; }
+# table.blob .num { color:#b07e00; }
+# table.blob .esc { color:#ff00ff; }
+# table.blob .str { color:#bf0303; }
+# table.blob .pps { color:#818100; }
+# table.blob .slc { color:#838183; font-style:italic; }
+# table.blob .com { color:#838183; font-style:italic; }
+# table.blob .ppc { color:#008200; }
+# table.blob .opt { color:#000000; }
+# table.blob .ipl { color:#0057ae; }
+# table.blob .lin { color:#555555; }
+# table.blob .kwa { color:#000000; font-weight:bold; }
+# table.blob .kwb { color:#0057ae; }
+# table.blob .kwc { color:#000000; font-weight:bold; }
+# table.blob .kwd { color:#010181; }
+# @media only all and (prefers-color-scheme: dark) { /* cgit addition for dark mode */
+# 	table.blob .num { color:#ff814f; }
+# 	table.blob .esc { color:#f60; }
+# 	table.blob .str { color:#cc0; }
+# 	table.blob .pps { color:#ffff7f; }
+# 	table.blob .slc { color:#7f7c7f; }
+# 	table.blob .com { color:#7e7c7e; }
+# 	table.blob .ppc { color:#8dd; }
+# 	table.blob .opt { color:#eeeeee; }
+# 	table.blob .ipl { color:#51a8ff; }
+# 	table.blob .lin { color:#aaaaaa; }
+# 	table.blob .kwa { color:#bb0; }
+# 	table.blob .kwb { color:#0a0; }
+# 	table.blob .kwc { color:#eeeeee; }
+# 	table.blob .kwd { color:#7e7efe; }
+# }
 #
 #
 # The following environment variables can be used to retrieve the configuration