-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
157 lines (139 loc) · 3.99 KB
/
render.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package server
import (
"fmt"
"html"
"net/url"
"strings"
"github.com/gomarkdown/markdown"
mdhtml "github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"github.com/igorhub/devcard"
"github.com/igorhub/devcard/pkg/internal/project"
)
func MdToHTML(md string) string {
// create markdown parser with extensions
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock | parser.LaxHTMLBlocks
p := parser.NewWithExtensions(extensions)
doc := p.Parse([]byte(md))
// create HTML renderer with extensions
htmlFlags := mdhtml.CommonFlags
opts := mdhtml.RendererOptions{Flags: htmlFlags}
renderer := mdhtml.NewRenderer(opts)
return string(markdown.Render(doc, renderer))
}
func renderCell(project *project.Project, highlighter *highlighter, b devcard.Cell) string {
switch b := b.(type) {
case *devcard.MarkdownCell:
return renderMarkdown(b)
case *devcard.HTMLCell:
return b.HTML
case *devcard.ErrorCell:
return renderError(b.Title, b.Body)
case *devcard.MonospaceCell:
return renderMonospace(highlighter, b)
case *devcard.ValueCell:
return renderValue(highlighter, b)
case *devcard.AnnotatedValueCell:
return renderAnnotatedValue(highlighter, b)
case *devcard.SourceCell:
return renderSource(project, highlighter, b)
case *devcard.ImageCell:
return renderImage(b)
case *devcard.JumpCell:
return ""
case *devcard.CustomCell:
return renderError("CustomCell cannot be rendered", "CustomCell must be cast into one of the renderable cells.")
case nil:
return renderError("Rendering error: trying to render nil", "")
default:
return renderError(fmt.Sprintf("Rendering error: unknown type '%s'", b.Type()), "")
}
}
func renderError(title, body string) string {
if title == "" && body == "" {
return ""
}
result := fmt.Sprintf(`<div class="err">` + title + `</div>`)
if body != "" {
result += fmt.Sprintf(`<pre class="err">` + html.EscapeString(body) + `</pre>`)
}
return result
}
func renderMarkdown(b *devcard.MarkdownCell) string {
return MdToHTML(b.Text)
}
func renderMonospace(highlighter *highlighter, b *devcard.MonospaceCell) string {
if b.Text == "" {
return ""
}
result := "<pre><code>" + html.EscapeString(b.Text) + "</code></pre>"
if b.Highlighting != "" {
h, err := highlighter.Highlight(b.Text, b.Highlighting)
if err == nil {
result = h
}
}
return result
}
func renderSource(project *project.Project, highlighter *highlighter, b *devcard.SourceCell) string {
if len(b.Decls) == 0 {
return ""
}
s := strings.Builder{}
for i, decl := range b.Decls {
if i != 0 {
s.WriteString("\n\n")
}
src, err := project.Source(decl)
if err != nil {
return renderError("SourceCell error", err.Error())
}
s.WriteString(src)
}
return renderMonospace(highlighter, devcard.NewMonospaceCell(s.String(), devcard.WithHighlighting("go")))
}
func renderValue(highlighter *highlighter, b *devcard.ValueCell) string {
values := strings.Join(b.Values, "\n\n")
result, err := highlighter.Highlight(values, "go")
if err != nil {
result = "<pre><code>" + html.EscapeString(values) + "</code></pre>"
}
return result
}
func renderAnnotatedValue(highlighter *highlighter, b *devcard.AnnotatedValueCell) string {
if len(b.AnnotatedValues) == 0 {
return ""
}
s := new(strings.Builder)
for i, v := range b.AnnotatedValues {
if i > 0 {
s.WriteString("\n\n")
}
if v.Annotation != "" {
s.WriteString(fmt.Sprintf("// %s\n", v.Annotation))
}
s.WriteString(v.Value)
}
result, err := highlighter.Highlight(s.String(), "go")
if err != nil {
result = "<pre><code>" + html.EscapeString(s.String()) + "</code></pre>"
}
return result
}
func renderImage(b *devcard.ImageCell) string {
if b.Error != nil {
return renderError(b.Error.Title, b.Error.Body)
}
f := `<figure>
<img
src="/file?path=%s"
alt="%s"/>
<figcaption>%s</figcaption>
</figure>
`
s := &strings.Builder{}
for _, img := range b.Images {
fmt.Fprintf(s, f, url.QueryEscape(img.Path), img.Path, img.Annotation)
}
return s.String()
}