...
1 package d2svg
2
3 import (
4 "strings"
5
6 "github.com/alecthomas/chroma/v2"
7 "github.com/alecthomas/chroma/v2/formatters/svg"
8 )
9
10
11
12
13
14 var svgEscaper = strings.NewReplacer(
15 `&`, "&",
16 `<`, "<",
17 `>`, ">",
18 `"`, """,
19 ` `, " ",
20 ` `, "    ",
21 )
22
23 func styleToSVG(style *chroma.Style) map[chroma.TokenType]string {
24 converted := map[chroma.TokenType]string{}
25
26
27
28 for t := range chroma.StandardTypes {
29 entry := style.Get(t)
30
31
32
33 if entry.IsZero() {
34 continue
35 }
36 converted[t] = svg.StyleEntryToSVG(entry)
37 }
38 return converted
39 }
40
41 func styleAttr(styles map[chroma.TokenType]string, tt chroma.TokenType) string {
42 if _, ok := styles[tt]; !ok {
43 tt = tt.SubCategory()
44 if _, ok := styles[tt]; !ok {
45 tt = tt.Category()
46 if _, ok := styles[tt]; !ok {
47 return ""
48 }
49 }
50 }
51
52 out := strings.Replace(styles[tt], `font-weight="bold"`, `class="text-mono-bold"`, -1)
53 return strings.Replace(out, `font-style="italic"`, `class="text-mono-italic"`, -1)
54 }
55
56
57
View as plain text