-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_render_test.go
96 lines (77 loc) · 2.04 KB
/
html_render_test.go
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
package highlight
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tree-sitter/go-tree-sitter"
"github.com/tree-sitter/tree-sitter-go/bindings/go"
)
var cssTheme = map[string]string{
"variable": "color: #FEFEF8;",
"function": "color: #73FBF1;",
"string": "color: #B8E466;",
"keyword": "color: #A578EA;",
"comment": "color: #8A8A8A;",
}
func attributeCallback(captureNames []string) AttributeCallback {
return func(h Highlight, languageName string) []byte {
if h == DefaultHighlight {
return nil
}
return []byte(`class="hl-` + captureNames[h] + `"`)
}
}
func TestHTMLRender_Render(t *testing.T) {
// Get the capture names from the theme
captureNames := make([]string, 0, len(theme))
for name := range theme {
captureNames = append(captureNames, name)
}
source, err := os.ReadFile("testdata/test.go")
require.NoError(t, err)
language := tree_sitter.NewLanguage(tree_sitter_go.Language())
highlightsQuery, err := os.ReadFile("testdata/highlights.scm")
require.NoError(t, err)
cfg, err := NewConfiguration(language, "go", highlightsQuery, nil, nil)
require.NoError(t, err)
cfg.Configure(captureNames)
highlighter := New()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
events := highlighter.Highlight(ctx, *cfg, source, func(name string) *Configuration {
return nil
})
f, err := os.Create("out.html")
require.NoError(t, err)
defer func() {
err = f.Close()
require.NoError(t, err)
}()
htmlRender := NewHTMLRender()
_, err = fmt.Fprintf(f, `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<style>`)
require.NoError(t, err)
err = htmlRender.RenderCSS(f, cssTheme)
require.NoError(t, err)
_, err = fmt.Fprintf(f, `</style>
</head>
<body>
<pre><code>
`)
require.NoError(t, err)
err = htmlRender.Render(f, events, source, attributeCallback(captureNames))
assert.NoError(t, err)
_, err = fmt.Fprintf(f, `</code></pre>
</body>
</html>
`)
require.NoError(t, err)
}