1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package driver
16
17 import (
18 "embed"
19 "fmt"
20 "html/template"
21 "os"
22 "sync"
23
24 "github.com/google/pprof/internal/report"
25 )
26
27 var (
28 htmlTemplates *template.Template
29 htmlTemplateInit sync.Once
30 )
31
32
33
34 func getHTMLTemplates() *template.Template {
35 htmlTemplateInit.Do(func() {
36 htmlTemplates = template.New("templategroup")
37 addTemplates(htmlTemplates)
38 report.AddSourceTemplates(htmlTemplates)
39 })
40 return htmlTemplates
41 }
42
43
44 var embeddedFiles embed.FS
45
46
47 func addTemplates(templates *template.Template) {
48
49 loadFile := func(fname string) string {
50 data, err := embeddedFiles.ReadFile(fname)
51 if err != nil {
52 fmt.Fprintf(os.Stderr, "internal/driver: embedded file %q not found\n",
53 fname)
54 os.Exit(1)
55 }
56 return string(data)
57 }
58 loadCSS := func(fname string) string {
59 return `<style type="text/css">` + "\n" + loadFile(fname) + `</style>` + "\n"
60 }
61 loadJS := func(fname string) string {
62 return `<script>` + "\n" + loadFile(fname) + `</script>` + "\n"
63 }
64
65
66 def := func(name, contents string) {
67 sub := template.New(name)
68 template.Must(sub.Parse(contents))
69 template.Must(templates.AddParseTree(name, sub.Tree))
70 }
71
72
73 def("css", loadCSS("html/common.css"))
74 def("header", loadFile("html/header.html"))
75 def("graph", loadFile("html/graph.html"))
76 def("graph_css", loadCSS("html/graph.css"))
77 def("script", loadJS("html/common.js"))
78 def("top", loadFile("html/top.html"))
79 def("sourcelisting", loadFile("html/source.html"))
80 def("plaintext", loadFile("html/plaintext.html"))
81
82 def("stacks", loadFile("html/stacks.html"))
83 def("stacks_css", loadCSS("html/stacks.css"))
84 def("stacks_js", loadJS("html/stacks.js"))
85 }
86
View as plain text