Debian Codegraph @

render/engine.go

raw symbols SCIP
1 package render
2
3 import (
4 "fmt"
5 "html/template"
6 "io"
7 "regexp"
8 "strings"
9
10 "github.com/go-echarts/go-echarts/v2/types"
11 )
12
13 const (
14 ModChart = "chart"
15 ModPage = "page"
16 // EchartsInstancePrefix the default prefix for each echarts instance
17 EchartsInstancePrefix = "goecharts_"
18 // EchartsInstancePlaceholder a placeholder for types.FuncStr inject echarts instance
19 EchartsInstancePlaceholder = "%MY_ECHARTS%"
20 )
21
22 var pat = regexp.MustCompile(`(__f__")|("__f__)|(__f__)`)
23
24 type ChartSnippet struct {
25 Element string
26 Script string
27 Option string
28 }
29
30 // Renderer
31 // Any kinds of charts have their render implementation and
32 // you can define your own render logic easily.
33 type Renderer interface {
34 Render(w io.Writer) error
35 RenderContent() []byte
36 RenderSnippet() ChartSnippet
37 }
38
39 // MustTemplate creates a new template with the given name and parsed contents.
40 func MustTemplate(name string, contents []string) *template.Template {
41 tpl := template.New(name).Funcs(template.FuncMap{
42 "safeHTML": func(s interface{}) template.HTML {
43 return template.HTML(fmt.Sprint(s))
44 },
45 "safeJS": func(s interface{}) template.JS {
46 return template.JS(fmt.Sprint(s))
47 },
48 "injectInstance": func(funcStr types.FuncStr, echartsInstancePlaceholder string, chartID string) string {
49 instance := EchartsInstancePrefix + chartID
50 return strings.Replace(string(funcStr), echartsInstancePlaceholder, instance, -1)
51 },
52 })
53 tpl = template.Must(tpl.Parse(contents[0]))
54
55 for _, cont := range contents[1:] {
56 tpl = template.Must(tpl.Parse(cont))
57 }
58 return tpl
59 }