Skip to content

Commit 0141d16

Browse files
Fix broken Chroma CSS styles (#23174)
The CSS styles in Gitea themes are out-of-sync of Chroma's styles. This PR introduces a `chroma-style-diff.go` tool to compare the diff. The missing CSS styles have been added manually. They are left as empty to reduce arguments because there was no color for them before. And this PR fixes #22348, with just 2 lines changed: `.chroma .kt & .n`, these colors are taken from GitHub. It's good enough for #22348 ![image](https://user-images.githubusercontent.com/2114189/221551941-0d27d11d-e71e-498f-8e88-92b558fe4a18.png) --------- Co-authored-by: silverwind <[email protected]>
1 parent 92d3e2a commit 0141d16

File tree

5 files changed

+105
-8
lines changed

5 files changed

+105
-8
lines changed

.stylelintrc.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
plugins:
22
- stylelint-declaration-strict-value
33

4+
ignoreFiles:
5+
- "**/*.go"
6+
47
overrides:
58
- files: ["**/*.less"]
69
customSyntax: postcss-less
710
- files: ["**/chroma/*", "**/codemirror/*", "**/standalone/*", "**/console/*"]
811
rules:
912
scale-unlimited/declaration-strict-value: null
13+
- files: ["**/chroma/*", "**/codemirror/*"]
14+
rules:
15+
block-no-empty: null
1016

1117
rules:
1218
alpha-value-notation: null
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
//go:build ignore
5+
6+
/*
7+
This tool is used to compare the CSS names in a chroma builtin styles with the Gitea theme CSS names.
8+
9+
It outputs the difference between the two sets of CSS names, eg:
10+
11+
```
12+
CSS names not in builtin:
13+
.chroma .ln
14+
----
15+
Builtin CSS names not in file:
16+
.chroma .vm
17+
```
18+
19+
Developers could use this tool to re-sync the CSS names in the Gitea theme.
20+
*/
21+
22+
package main
23+
24+
import (
25+
"os"
26+
"regexp"
27+
"strings"
28+
29+
"github.com/alecthomas/chroma/v2"
30+
)
31+
32+
func main() {
33+
if len(os.Args) != 2 {
34+
println("Usage: chroma-style-diff css-or-less-file")
35+
os.Exit(1)
36+
}
37+
38+
data, err := os.ReadFile(os.Args[1])
39+
if err != nil {
40+
println(err.Error())
41+
os.Exit(1)
42+
}
43+
44+
content := string(data)
45+
46+
// a simple CSS parser to collect CSS names
47+
content = regexp.MustCompile("//.*\r?\n").ReplaceAllString(content, "\n")
48+
content = regexp.MustCompile("/\\*.*?\\*/").ReplaceAllString(content, "")
49+
matches := regexp.MustCompile("\\s*([-.#:\\w\\s]+)\\s*\\{[^}]*}").FindAllStringSubmatch(content, -1)
50+
51+
cssNames := map[string]bool{}
52+
for _, matchGroup := range matches {
53+
cssName := strings.TrimSpace(matchGroup[1])
54+
cssNames[cssName] = true
55+
}
56+
57+
// collect Chroma builtin CSS names
58+
builtin := map[string]bool{}
59+
for tokenType, cssName := range chroma.StandardTypes {
60+
if tokenType > 0 && cssName != "" {
61+
builtin[".chroma ."+cssName] = true
62+
}
63+
}
64+
65+
// show the diff
66+
println("CSS names not in builtin:")
67+
for cssName := range cssNames {
68+
if !builtin[cssName] {
69+
println(cssName)
70+
}
71+
}
72+
println("----")
73+
println("Builtin CSS names not in file:")
74+
for cssName := range builtin {
75+
if !cssNames[cssName] {
76+
println(cssName)
77+
}
78+
}
79+
}

web_src/less/chroma/dark.less

+9-5
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,36 @@
77
.chroma .cpf { color: #649bc4; } /* CommentPreprocFile */
88
.chroma .cs { color: #9075cd; } /* CommentSpecial */
99
.chroma .dl { color: #649bc4; } /* LiteralStringDelimiter */
10+
.chroma .fm {} /* NameFunctionMagic */
11+
.chroma .g {} /* Generic */
1012
.chroma .gd { color: #ffffff; background-color: #5f3737; } /* GenericDeleted */
1113
.chroma .ge { color: #ddee30; } /* GenericEmph */
1214
.chroma .gh { color: #ffaa10; } /* GenericHeading */
1315
.chroma .gi { color: #ffffff; background-color: #3a523a; } /* GenericInserted */
16+
.chroma .gl {} /* GenericUnderline */
1417
.chroma .go { color: #777e94; } /* GenericOutput */
1518
.chroma .gp { color: #ebdbb2; } /* GenericPrompt */
1619
.chroma .gr { color: #ff4433; } /* GenericError */
1720
.chroma .gs { color: #ebdbb2; } /* GenericStrong */
1821
.chroma .gt { color: #ff7540; } /* GenericTraceback */
1922
.chroma .gu { color: #b8bb26; } /* GenericSubheading */
20-
.chroma .hl { background-color: #3f424d; } /* LineHighlight */
2123
.chroma .il { color: #649bc4; } /* LiteralNumberIntegerLong */
2224
.chroma .k { color: #ff7540; } /* Keyword */
2325
.chroma .kc { color: #649bc4; } /* KeywordConstant */
2426
.chroma .kd { color: #ff7540; } /* KeywordDeclaration */
2527
.chroma .kn { color: #ffaa10; } /* KeywordNamespace */
2628
.chroma .kp { color: #5f8700; } /* KeywordPseudo */
2729
.chroma .kr { color: #ff7540; } /* KeywordReserved */
28-
.chroma .kt { color: #fabd2f; } /* KeywordType */
29-
.chroma .ln { color: #7f8699; } /* LineNumbers */
30-
.chroma .lnt { color: #7f8699; } /* LineNumbersTable */
30+
.chroma .kt { color: #ff7b72; } /* KeywordType */
31+
.chroma .l {} /* Literal */
32+
.chroma .ld {} /* LiteralDate */
3133
.chroma .m { color: #649bc4; } /* LiteralNumber */
3234
.chroma .mb { color: #649bc4; } /* LiteralNumberBin */
3335
.chroma .mf { color: #649bc4; } /* LiteralNumberFloat */
3436
.chroma .mh { color: #649bc4; } /* LiteralNumberHex */
3537
.chroma .mi { color: #649bc4; } /* LiteralNumberInteger */
3638
.chroma .mo { color: #649bc4; } /* LiteralNumberOct */
37-
.chroma .n { color: #fabd2f; } /* Name */
39+
.chroma .n { color: #c9d1d9; } /* Name */
3840
.chroma .na { color: #b8bb26; } /* NameAttribute */
3941
.chroma .nb { color: #fabd2f; } /* NameBuiltin */
4042
.chroma .nc { color: #ffaa10; } /* NameClass */
@@ -51,6 +53,7 @@
5153
.chroma .o { color: #ff7540; } /* Operator */
5254
.chroma .ow { color: #5f8700; } /* OperatorWord */
5355
.chroma .p { color: #d2d4db; } /* Punctuation */
56+
.chroma .py {} /* NameProperty */
5457
.chroma .s { color: #b8bb26; } /* LiteralString */
5558
.chroma .s1 { color: #b8bb26; } /* LiteralStringSingle */
5659
.chroma .s2 { color: #b8bb26; } /* LiteralStringDouble */
@@ -67,4 +70,5 @@
6770
.chroma .vc { color: #ff7540; } /* NameVariableClass */
6871
.chroma .vg { color: #ffaa10; } /* NameVariableGlobal */
6972
.chroma .vi { color: #ffaa10; } /* NameVariableInstance */
73+
.chroma .vm {} /* NameVariableMagic */
7074
.chroma .w { color: #7f8699; } /* TextWhitespace */

web_src/less/chroma/light.less

+10-3
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
.chroma .cpf { color: #4c4dbc; } /* CommentPreprocFile */
88
.chroma .cs { color: #999999; } /* CommentSpecial */
99
.chroma .dl { color: #106303; } /* LiteralStringDelimiter */
10+
.chroma .fm {} /* NameFunctionMagic */
11+
.chroma .g {} /* Generic */
1012
.chroma .gd { color: #000000; background-color: #ffdddd; } /* GenericDeleted */
1113
.chroma .ge { color: #000000; } /* GenericEmph */
1214
.chroma .gh { color: #999999; } /* GenericHeading */
1315
.chroma .gi { color: #000000; background-color: #ddffdd; } /* GenericInserted */
16+
.chroma .gl {} /* GenericUnderline */
1417
.chroma .go { color: #888888; } /* GenericOutput */
1518
.chroma .gp { color: #555555; } /* GenericPrompt */
1619
.chroma .gr { color: #aa0000; } /* GenericError */
20+
.chroma .gs {} /* GenericStrong */
1721
.chroma .gt { color: #aa0000; } /* GenericTraceback */
1822
.chroma .gu { color: #aaaaaa; } /* GenericSubheading */
19-
.chroma .hl { background-color: #e5e5e5; } /* LineHighlight */
2023
.chroma .il { color: #009999; } /* LiteralNumberIntegerLong */
2124
.chroma .k { color: #d73a49; } /* Keyword */
2225
.chroma .kc { color: #d73a49; } /* KeywordConstant */
@@ -25,14 +28,15 @@
2528
.chroma .kp { color: #d73a49; } /* KeywordPseudo */
2629
.chroma .kr { color: #d73a49; } /* KeywordReserved */
2730
.chroma .kt { color: #445588; } /* KeywordType */
28-
.chroma .ln { color: #7f7f7f; } /* LineNumbers */
29-
.chroma .lnt { color: #7f7f7f; } /* LineNumbersTable */
31+
.chroma .l {} /* Literal */
32+
.chroma .ld {} /* LiteralDate */
3033
.chroma .m { color: #009999; } /* LiteralNumber */
3134
.chroma .mb { color: #009999; } /* LiteralNumberBin */
3235
.chroma .mf { color: #009999; } /* LiteralNumberFloat */
3336
.chroma .mh { color: #009999; } /* LiteralNumberHex */
3437
.chroma .mi { color: #009999; } /* LiteralNumberInteger */
3538
.chroma .mo { color: #009999; } /* LiteralNumberOct */
39+
.chroma .n {} /* Name */
3640
.chroma .na { color: #d73a49; } /* NameAttribute */
3741
.chroma .nb { color: #005cc5; } /* NameBuiltin */
3842
.chroma .nc { color: #445588; } /* NameClass */
@@ -48,6 +52,8 @@
4852
.chroma .nx { color: #24292e; } /* NameOther */
4953
.chroma .o { color: #d73a49; } /* Operator */
5054
.chroma .ow { color: #d73a49; } /* OperatorWord */
55+
.chroma .p {} /* Punctuation */
56+
.chroma .py {} /* NameProperty */
5157
.chroma .s { color: #106303; } /* LiteralString */
5258
.chroma .s1 { color: #cc7a00; } /* LiteralStringSingle */
5359
.chroma .s2 { color: #106303; } /* LiteralStringDouble */
@@ -64,4 +70,5 @@
6470
.chroma .vc { color: #008080; } /* NameVariableClass */
6571
.chroma .vg { color: #008080; } /* NameVariableGlobal */
6672
.chroma .vi { color: #008080; } /* NameVariableInstance */
73+
.chroma .vm {} /* NameVariableMagic */
6774
.chroma .w { color: #bbbbbb; } /* TextWhitespace */

web_src/less/themes/theme-arc-green.less

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@import "../chroma/base.less";
12
@import "../chroma/dark.less";
23
@import "../codemirror/dark.less";
34

0 commit comments

Comments
 (0)