upstream
  1package main
  2
  3import (
  4	"html/template"
  5	"net"
  6	"net/http"
  7	"sort"
  8	"strings"
  9
 10	"github.com/alecthomas/chroma/v2/formatters/html"
 11	"github.com/alecthomas/chroma/v2/lexers"
 12	"github.com/alecthomas/chroma/v2/styles"
 13
 14	"github.com/antonmedv/gitmal/pkg/templates"
 15)
 16
 17var themeStyles = map[string]string{
 18	"abap":                 "light",
 19	"algol":                "light",
 20	"arduino":              "light",
 21	"autumn":               "light",
 22	"average":              "dark",
 23	"base16-snazzy":        "dark",
 24	"borland":              "light",
 25	"bw":                   "light",
 26	"catppuccin-frappe":    "dark",
 27	"catppuccin-latte":     "light",
 28	"catppuccin-macchiato": "dark",
 29	"catppuccin-mocha":     "dark",
 30	"colorful":             "light",
 31	"doom-one":             "dark",
 32	"doom-one2":            "dark",
 33	"dracula":              "dark",
 34	"emacs":                "light",
 35	"evergarden":           "dark",
 36	"friendly":             "light",
 37	"fruity":               "dark",
 38	"github-dark":          "dark",
 39	"github":               "light",
 40	"gruvbox-light":        "light",
 41	"gruvbox":              "dark",
 42	"hrdark":               "dark",
 43	"igor":                 "light",
 44	"lovelace":             "light",
 45	"manni":                "light",
 46	"modus-operandi":       "light",
 47	"modus-vivendi":        "dark",
 48	"monokai":              "dark",
 49	"monokailight":         "light",
 50	"murphy":               "light",
 51	"native":               "dark",
 52	"nord":                 "dark",
 53	"nordic":               "dark",
 54	"onedark":              "dark",
 55	"onesenterprise":       "dark",
 56	"paraiso-dark":         "dark",
 57	"paraiso-light":        "light",
 58	"pastie":               "light",
 59	"perldoc":              "light",
 60	"pygments":             "light",
 61	"rainbow_dash":         "light",
 62	"rose-pine-dawn":       "light",
 63	"rose-pine-moon":       "dark",
 64	"rose-pine":            "dark",
 65	"rpgle":                "dark",
 66	"rrt":                  "dark",
 67	"solarized-dark":       "dark",
 68	"solarized-dark256":    "dark",
 69	"solarized-light":      "light",
 70	"swapoff":              "dark",
 71	"tango":                "light",
 72	"tokyonight-day":       "light",
 73	"tokyonight-moon":      "dark",
 74	"tokyonight-night":     "dark",
 75	"tokyonight-storm":     "dark",
 76	"trac":                 "light",
 77	"vim":                  "dark",
 78	"vs":                   "light",
 79	"vulcan":               "dark",
 80	"witchhazel":           "dark",
 81	"xcode-dark":           "dark",
 82	"xcode":                "light",
 83}
 84
 85func previewThemes() {
 86	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 87		names := make([]string, 0, len(themeStyles))
 88		for name := range themeStyles {
 89			names = append(names, name)
 90		}
 91		sort.Strings(names)
 92
 93		sampleLang := "javascript"
 94		sampleCode := `function fib(n) {
 95    if (n <= 1) {
 96        return n;
 97    }
 98    return fib(n - 1) + fib(n - 2);
 99}
100
101// Print n Fibonacci numbers.
102const n = 10;
103
104for (let i = 0; i < n; i++) {
105    console.log(fib(i));
106}`
107
108		formatter := html.New(
109			html.WithClasses(false),
110		)
111
112		// Generate cards
113		cards := make([]templates.PreviewCard, 0, len(names))
114		for _, theme := range names {
115			style := styles.Get(theme)
116			if style == nil {
117				continue
118			}
119			lexer := lexers.Get(sampleLang)
120			if lexer == nil {
121				continue
122			}
123			it, err := lexer.Tokenise(nil, sampleCode)
124			if err != nil {
125				continue
126			}
127			var sb strings.Builder
128			if err := formatter.Format(&sb, style, it); err != nil {
129				continue
130			}
131			cards = append(cards, templates.PreviewCard{
132				Name: theme,
133				Tone: themeStyles[theme],
134				HTML: template.HTML(sb.String()),
135			})
136		}
137
138		w.Header().Set("Content-Type", "text/html; charset=utf-8")
139		_ = templates.PreviewTemplate.Execute(w, templates.PreviewParams{
140			Count:  len(cards),
141			Themes: cards,
142		})
143	})
144
145	ln, err := net.Listen("tcp", "127.0.0.1:0")
146	if err != nil {
147		panic(err)
148	}
149
150	addr := ln.Addr().String()
151	echo("Preview themes at http://" + addr)
152
153	if err := http.Serve(ln, handler); err != nil && err != http.ErrServerClosed {
154		panic(err)
155	}
156}