-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinclude.go
More file actions
104 lines (95 loc) · 2.69 KB
/
include.go
File metadata and controls
104 lines (95 loc) · 2.69 KB
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
package main
import (
"bytes"
"encoding/csv"
"fmt"
"path/filepath"
"strings"
"github.com/alecthomas/chroma/v2"
chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
"golang.org/x/net/html"
)
func csvToTable(data []byte, src string) string {
reader := csv.NewReader(bytes.NewReader(data))
records, err := reader.ReadAll()
if err != nil {
warn("<include src=%q> failed to parse CSV: %v", src, err)
return ""
}
if len(records) == 0 {
return ""
}
var sb strings.Builder
sb.WriteString("<table>\n<thead>\n<tr>")
for _, cell := range records[0] {
sb.WriteString("<th>")
sb.WriteString(html.EscapeString(cell))
sb.WriteString("</th>")
}
sb.WriteString("</tr>\n</thead>\n<tbody>\n")
for _, row := range records[1:] {
sb.WriteString("<tr>")
for _, cell := range row {
sb.WriteString("<td>")
sb.WriteString(html.EscapeString(cell))
sb.WriteString("</td>")
}
sb.WriteString("</tr>\n")
}
sb.WriteString("</tbody>\n</table>")
return sb.String()
}
func highlightCode(data []byte, src string) string {
ext := filepath.Ext(src)
lexer := lexers.Match(src)
if lexer == nil {
lexer = lexers.Fallback
}
lexer = chroma.Coalesce(lexer)
formatter := chromahtml.New(
chromahtml.WithClasses(true),
chromahtml.PreventSurroundingPre(false),
)
iterator, err := lexer.Tokenise(nil, string(data))
if err != nil {
lang := strings.TrimPrefix(ext, ".")
return fmt.Sprintf("<pre><code class=\"language-%s\">%s</code></pre>", lang, html.EscapeString(string(data)))
}
var buf bytes.Buffer
if err := formatter.Format(&buf, styles.Fallback, iterator); err != nil {
lang := strings.TrimPrefix(ext, ".")
return fmt.Sprintf("<pre><code class=\"language-%s\">%s</code></pre>", lang, html.EscapeString(string(data)))
}
return buf.String()
}
// syntaxThemeCSS returns the chroma CSS for the given theme name, or "" if not found.
func syntaxThemeCSS(theme string) string {
style := styles.Get(theme)
if style == styles.Fallback && theme != "fallback" {
return ""
}
formatter := chromahtml.New(chromahtml.WithClasses(true))
var buf bytes.Buffer
if err := formatter.WriteCSS(&buf, style); err != nil {
return ""
}
return buf.String()
}
// syntaxThemeDarkCSS returns the chroma CSS for the given theme wrapped in
// both a prefers-color-scheme media query and a .dark class selector.
func syntaxThemeDarkCSS(theme string) string {
css := syntaxThemeCSS(theme)
if css == "" {
return ""
}
var sb strings.Builder
sb.WriteString("@media (prefers-color-scheme: dark) {\n")
sb.WriteString(css)
sb.WriteString("}\n")
sb.WriteString(".dark {\n")
sb.WriteString(css)
sb.WriteString("}\n")
return sb.String()
}