-
Notifications
You must be signed in to change notification settings - Fork 2
/
renderers.go
213 lines (175 loc) · 5.09 KB
/
renderers.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"bytes"
"embed"
"github.com/flosch/pongo2/v5"
chroma "github.com/alecthomas/chroma/formatters/html"
"github.com/dustin/go-humanize"
mathjax "github.com/litao91/goldmark-mathjax"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer/html"
)
// We use Goldmark as the Markdown converter. Configure it here.
var markdown = goldmark.New(
goldmark.WithRendererOptions(
html.WithXHTML(),
html.WithUnsafe(),
html.WithHardWraps(),
),
goldmark.WithExtensions(
extension.Footnote,
extension.Linkify,
extension.Strikethrough,
extension.Table,
extension.Typographer,
extension.GFM,
highlighting.NewHighlighting(
highlighting.WithFormatOptions(
chroma.WithClasses(true),
),
),
mathjax.MathJax,
),
)
//go:embed template
var templatesContent embed.FS
var pongoLoader = pongo2.NewFSLoader(templatesContent)
var templateSet = pongo2.NewSet("template", pongoLoader)
// Register some Pongo filters
// TODO: How do I prevent this assignment?
var _ = pongo2.RegisterFilter(
"humanizeNumber",
func(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
return pongo2.AsValue(humanize.Comma(int64(in.Integer()))), nil
})
var t_archive, _ = templateSet.FromCache("template/archive.njk")
var t_article_raw, _ = templateSet.FromCache("template/article-raw.njk")
var t_article, _ = templateSet.FromCache("template/article.njk")
var t_folder, _ = templateSet.FromCache("template/folder.njk")
var t_index, _ = templateSet.FromCache("template/index.njk")
var t_not_found, _ = templateSet.FromCache("template/not-found.njk")
var t_random, _ = templateSet.FromCache("template/random.njk")
var t_revision_raw, _ = templateSet.FromCache("template/revision-raw.njk")
var t_revision, _ = templateSet.FromCache("template/revision.njk")
var t_revisionList, _ = templateSet.FromCache("template/revision-list.njk")
func renderIndex(config *BockConfig) string {
html, _ := t_index.Execute(pongo2.Context{
"type": "index",
"version": VERSION,
})
return html
}
func renderNotFound(config *BockConfig) string {
html, _ := t_not_found.Execute(pongo2.Context{
"type": "not-found",
"version": VERSION,
})
return html
}
func renderRandom(config *BockConfig) string {
html, _ := t_random.Execute(pongo2.Context{
"list": config.listOfArticles,
"type": "random",
"version": VERSION,
})
return html
}
func renderArticle(
source []byte,
article Article,
entityType string,
config *BockConfig,
) (string, string) {
var conversionBuffer bytes.Buffer
if err := markdown.Convert(source, &conversionBuffer); err != nil {
panic(err)
}
baseContext := pongo2.Context{
"created": article.Created,
"hierarchy": article.Hierarchy,
"html": conversionBuffer.String(),
"id": article.ID,
"modified": article.Modified,
"revisions": article.Revisions,
"sizeInBytes": article.Size,
"source": article.Source,
"title": article.Title,
"untracked": article.Untracked,
"uri": article.URI,
"relativePath": article.RelativePath,
"meta": config.meta,
"type": entityType,
"version": VERSION,
}
html, _ := t_article.Execute(baseContext)
baseContext.Update(pongo2.Context{
"type": "raw",
})
raw, _ := t_article_raw.Execute(baseContext)
conversionBuffer.Reset()
return html, raw
}
func renderFolder(folder Folder) string {
var conversionBuffer bytes.Buffer
if err := markdown.Convert([]byte(folder.README), &conversionBuffer); err != nil {
panic(err)
}
html, _ := t_folder.Execute(pongo2.Context{
"children": folder.Children,
"hierarchy": folder.Hierarchy,
"readme": conversionBuffer.String(),
"title": folder.Title,
"uri": folder.URI,
"type": "folder",
"version": VERSION,
})
conversionBuffer.Reset()
return html
}
func renderArchive(config *BockConfig) string {
html, _ := t_archive.Execute(pongo2.Context{
"title": "Archive",
"tree": config.entityTree,
"uri": "/archive",
"meta": config.meta,
"type": "archive",
"version": VERSION,
})
return html
}
func renderRevisionList(article Article, revisions []Revision) string {
html, _ := t_revisionList.Execute(pongo2.Context{
"revisions": revisions,
"hierarchy": article.Hierarchy,
"title": article.Title,
"uri": article.URI,
"type": "revision-list",
"version": VERSION,
})
return html
}
func renderRevision(article Article, revision Revision) (string, string) {
var conversionBuffer bytes.Buffer
if err := markdown.Convert([]byte(revision.Content), &conversionBuffer); err != nil {
panic(err)
}
baseContext := pongo2.Context{
"html": conversionBuffer.String(),
"hierarchy": article.Hierarchy,
"revision": revision,
"source": revision.Content,
"title": article.Title,
"uri": article.URI,
"type": "revision",
"version": VERSION,
}
html, _ := t_revision.Execute(baseContext)
baseContext.Update(pongo2.Context{
"type": "revision-raw",
})
raw, _ := t_revision_raw.Execute(baseContext)
conversionBuffer.Reset()
return html, raw
}