-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.go
116 lines (101 loc) · 2.97 KB
/
html.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
Copyright © 2022 Cédric L’homme <public@l-homme.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package mdrc
import (
"bytes"
"embed"
"fmt"
"io"
"log"
"strings"
"text/template"
"github.com/go-logr/logr"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/html"
)
// content holds our static web server content.
//go:embed template
var content embed.FS
const (
mdrcCodeBlockIdentifier = "mdrc"
)
type HTML struct {
logger logr.Logger
html string
}
func NewHTML(l logr.Logger, data []byte) *HTML {
logger := l.WithName("html")
h := &HTML{
logger: logger,
}
h.render(data)
return h
}
// render renders the HTML code from the Markdown text.
func (h *HTML) render(data []byte) {
opts := html.RendererOptions{
Flags: html.CommonFlags,
RenderNodeHook: h.renderShellCodeBlock(),
}
renderer := html.NewRenderer(opts)
m := string(markdown.ToHTML(data, nil, renderer))
tmpl, err := template.ParseFS(content, "template/root.html")
if err != nil {
log.Fatalf("parsing template: %v\n", err)
}
sb := &strings.Builder{}
d := struct {
Markdown string
}{
Markdown: m,
}
err = tmpl.Execute(sb, d)
if err != nil {
log.Fatalf("executing template: %v\n", err)
}
h.html = sb.String()
h.logger.Info("rendered", "size", len(h.html))
}
// renderShellCodeBlock injects the mdrc HTML code to deal with the commands
// inside CodBlock with mdrc identifier (ast.CodeBlock.Info).
func (h *HTML) renderShellCodeBlock() func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
original := html.NewRenderer(html.RendererOptions{})
var cmdCount int
return func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
if _, ok := node.(*ast.CodeBlock); !ok {
return ast.GoToNext, false
}
codeBlock := node.(*ast.CodeBlock)
original.CodeBlock(w, codeBlock)
if bytes.Equal(codeBlock.Info, []byte(mdrcCodeBlockIdentifier)) {
_, err := w.Write(
[]byte(
fmt.Sprintf(
"<button type=\"submit\" onclick=\"Run(%d)\">Run</button>\n<pre><code class=\"language-shell\" id=\"command-%d\"></code></pre>\n",
cmdCount,
cmdCount,
)))
if err != nil {
h.logger.Error(err, "codeBlock", "info", codeBlock.Info, "literal", codeBlock.Literal)
}
cmdCount++
}
return ast.GoToNext, true
}
}
// Rendered returns the final HTML rendered with mdrc injections.
func (h *HTML) Rendered() string {
return h.html
}