// SPDX-License-Identifier: AGPL-3.0-or-later //go:build ignore // chroma-css writes internal/adapters/web/static/css/56-chroma.css: the // chroma token classes mapped to the --syn-* custom properties of the design // tokens (the Despair hues, dark and light). Colours are not in the file, so // the theme follows the colour scheme; only bold and italic come from chroma // (its "bw" style, as in the mockup). // // go run scripts/chroma-css.go > internal/adapters/web/static/css/56-chroma.css package main import ( "fmt" "os" "sort" "strings" "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/formatters/html" "github.com/alecthomas/chroma/v2/styles" ) // synVar maps a token type to the name of a --syn-* property, "" for none. func synVar(t chroma.TokenType) string { switch { case t == chroma.KeywordType, t == chroma.NameClass, t == chroma.NameBuiltin, t == chroma.NameBuiltinPseudo, t == chroma.NameException, t == chroma.NameNamespace: return "type" case t.InCategory(chroma.Keyword): return "keyword" case t.InSubCategory(chroma.LiteralString): return "string" case t.InCategory(chroma.Comment): return "comment" case t.InSubCategory(chroma.LiteralNumber): return "number" case t.InSubCategory(chroma.NameFunction), t == chroma.NameDecorator: return "func" case t.InCategory(chroma.Punctuation), t.InCategory(chroma.Operator): return "punct" } return "" } func main() { // Learn the classes chroma emits, so that every one is covered. var learned strings.Builder if err := html.New(html.WithClasses(true)).WriteCSS(&learned, styles.Get("bw")); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } types := map[string]chroma.TokenType{} for t, c := range chroma.StandardTypes { if c != "" && t > 0 { types[c] = t } } bw := styles.Get("bw") var classes []string for _, l := range strings.Split(learned.String(), "\n") { i := strings.Index(l, ".chroma .") if i < 0 { continue } c := l[i+len(".chroma ."):] c = c[:strings.IndexAny(c, " {")] if t, ok := types[c]; ok && synVar(t) != "" { classes = append(classes, c) } } sort.Strings(classes) fmt.Println("/* Syntax colours of the code view. Generated by scripts/chroma-css.go from") fmt.Println(" chroma's token classes; the colours are the --syn-* properties of") fmt.Println(" 00-tokens.css (the Despair hues, dark and light), none is defined here. */") fmt.Println("@layer components {") fmt.Println(" .chroma { color: var(--syn-plain); }") for _, c := range classes { t := types[c] decl := "color: var(--syn-" + synVar(t) + ")" e := bw.Get(t) if e.Bold == chroma.Yes { decl += "; font-weight: bold" } if e.Italic == chroma.Yes { decl += "; font-style: italic" } fmt.Printf(" .chroma .%s { %s }\n", c, decl) } fmt.Println(" .chroma .line { display: block; }") fmt.Println(` /* an invisible character in code, shown instead of applied (Trojan Source) */ .chroma .ctl { color: var(--red); font-weight: bold; font-style: normal; font-size: var(--fs-s); outline: 1px solid var(--red); outline-offset: -1px; padding: 0 .25ch; } .img-view { margin-top: var(--lh); & img { max-width: 100%; height: auto; } } }`) }