-
Notifications
You must be signed in to change notification settings - Fork 54
/
parts.go
167 lines (148 loc) · 3.66 KB
/
parts.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
package parts
import (
"bytes"
_ "embed"
"fmt"
"strings"
"text/template"
"github.com/heyvito/docuowl/fs"
"github.com/heyvito/docuowl/fts"
"github.com/heyvito/docuowl/markdown"
"github.com/heyvito/docuowl/static"
)
//go:embed head.html
var headTemplate string
//go:embed section.html
var sectionTemplate string
//go:embed sidebar.html
var sidebarTemplate string
//go:embed group.html
var groupTemplate string
func MakeHead(index string, noFTS bool) string {
tmpl, err := template.New("header").Parse(headTemplate)
if err != nil {
panic(err)
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, struct {
CSS string
Index string
FTSSource string
NoFTS bool
}{static.CSS, index, static.FTSExecutor, noFTS})
if err != nil {
panic(err)
}
return buf.String()
}
func makeTOC(tree []fs.Entity) string {
var list []string
for _, i := range tree {
switch v := i.(type) {
case *fs.Group:
list = append(list, fmt.Sprintf(`<details id="anchor-%s"><summary>%s</summary><div class="sidebar-group-wrapper">%s</div></details>`, v.CompoundID(), v.Meta().Title, makeTOC(v.Children)))
case *fs.Section:
compID := v.CompoundID()
list = append(list, fmt.Sprintf(`<a href="#%s" id="anchor-%s">%s</a>`, compID, compID, v.Meta().Title))
}
}
return strings.Join(list, "")
}
func MakeSidebar(tree []fs.Entity, version string, noFTS bool) string {
TOC := makeTOC(tree)
tmpl, err := template.New("sidebar").Parse(sidebarTemplate)
if err != nil {
panic(err)
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, struct {
TOC string
Version string
ThemeSelector string
ToggleMenu string
NoFTS bool
}{TOC, version, static.ThemeSelector, static.ToggleMenu, noFTS})
if err != nil {
panic(err)
}
return buf.String()
}
func MakeContent(section *fs.Section) string {
tmpl, err := template.New("content").Parse(sectionTemplate)
if err != nil {
panic(err)
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, struct {
HasTitle, HasSideNote bool
Title, SideNote, Content, ID string
}{
HasTitle: section.Meta().Title != "",
HasSideNote: section.HasSideNotes,
ID: section.CompoundID(),
Title: section.Meta().Title,
SideNote: markdown.ProcessSideNotes(section.SideNotes),
Content: markdown.ProcessContent(section.Content),
})
if err != nil {
panic(err)
}
return buf.String()
}
func MakeGroup(group *fs.Group, fts *fts.FullTextSearchEngine) string {
tmpl, err := template.New("content").Parse(groupTemplate)
if err != nil {
panic(err)
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, struct {
Title, ID, Child, Content string
}{
Title: group.Meta().Title,
ID: group.CompoundID(),
Content: RenderSectionNoMeta(group.Content),
Child: RenderItems(group.Children, fts),
})
if err != nil {
panic(err)
}
return buf.String()
}
func RenderItems(items []fs.Entity, fts *fts.FullTextSearchEngine) string {
var ret []string
for _, e := range items {
switch v := e.(type) {
case *fs.Section:
fts.AddSection(v)
ret = append(ret, MakeContent(v))
case *fs.Group:
ret = append(ret, MakeGroup(v, fts))
}
}
return strings.Join(ret, "")
}
func RenderSectionNoMeta(section *fs.Section) string {
if section == nil {
return ""
}
tmpl, err := template.New("content").Parse(sectionTemplate)
if err != nil {
panic(err)
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, struct {
HasTitle, HasSideNote bool
Title, SideNote, Content, ID string
}{
HasTitle: false,
HasSideNote: false,
ID: "",
Title: "",
SideNote: "",
Content: markdown.ProcessContent(section.Content),
})
if err != nil {
panic(err)
}
return buf.String()
}