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
214
215
216
217
218
|
package main
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
)
/* TODO: config.h */
const SiteTitle = "mallocd.com"
const FooterText = "made with <a href=\"https://github.com/uint23/kew\">kew</a>"
const TemplateFile = "template.html"
const NavDirSymbol = "/"
const NavFileSymbol = ": "
const NavCurrentSymbol = "@ "
type NavNode struct {
Name string
Path string
Files []NavNode
Children []NavNode
}
func title_from_name(name string) string {
name = strings.TrimSuffix(name, ".md")
name = strings.ReplaceAll(name, "-", " ")
return name
}
func build_nav(dir string, root string) (NavNode, bool) {
var node NavNode
node.Name = title_from_name(filepath.Base(dir))
entries, err := os.ReadDir(dir)
if err != nil {
return node, false
}
for _, e := range entries {
full := filepath.Join(dir, e.Name())
if e.IsDir() {
child, ok := build_nav(full, root)
if ok {
_, err := os.Stat(filepath.Join(full, "index.md"))
if err == nil {
rel_dir, _ := filepath.Rel(root, full)
child.Path = rel_dir + "/index.html"
}
node.Children = append(node.Children, child)
}
continue
}
if strings.HasSuffix(e.Name(), ".md") {
rel, _ := filepath.Rel(root, full)
html := strings.TrimSuffix(rel, ".md") + ".html"
node.Files = append(node.Files, NavNode{
Name: title_from_name(e.Name()),
Path: html,
})
}
}
if len(node.Files) == 0 && len(node.Children) == 0 {
return node, false
}
return node, true
}
func render_nav(n NavNode, b *strings.Builder, cur string) {
b.WriteString("<ul>\n")
for _, f := range n.Files {
p := f.Path
if !strings.HasPrefix(p, "/") {
p = "/" + p
}
sym := NavFileSymbol
if p == cur {
sym = NavCurrentSymbol
}
b.WriteString(`<li><a href="` + p + `">` + sym + f.Name + "</a></li>\n")
}
for _, c := range n.Children {
sym := NavDirSymbol
if c.Path == cur {
sym = NavCurrentSymbol
}
if c.Path != "" {
p := c.Path
if !strings.HasPrefix(p, "/") {
p = "/" + p
}
b.WriteString(`<li><a href="` + p + `">` + c.Name + sym + `</a>`)
} else {
b.WriteString("<li>" + c.Name + sym)
}
render_nav(c, b, cur)
b.WriteString("</li>\n")
}
b.WriteString("</ul>\n")
}
func markdown_to_html(path string) (string, error) {
cmd := exec.Command("lowdown", "-Thtml")
in, err := os.Open(path)
if err != nil {
return "", err
}
defer in.Close()
var out strings.Builder
cmd.Stdin = in
cmd.Stdout = &out
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return "", err
}
return out.String(), nil
}
func copy_file(src string, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
return err
}
func main() {
if len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "usage: kew <in> <out>\n")
os.Exit(1)
}
src := os.Args[1]
out := os.Args[2]
/* load template */
tmpl, err := os.ReadFile(filepath.Join(src, TemplateFile))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
/* build nav */
rootnav, _ := build_nav(src, src)
/* walk site */
err = filepath.WalkDir(src, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
rel, _ := filepath.Rel(src, path)
outpath := filepath.Join(out, rel)
if d.IsDir() {
return os.MkdirAll(outpath, 0755)
}
if strings.HasSuffix(path, ".md") {
html, err := markdown_to_html(path)
if err != nil {
return err
}
relhtml := strings.TrimSuffix(rel, ".md") + ".html"
cur := relhtml
if !strings.HasPrefix(cur, "/") {
cur = "/" + cur
}
var navbuf strings.Builder
render_nav(rootnav, &navbuf, cur)
page := string(tmpl)
page = strings.Replace(page, "{{TITLE}}", SiteTitle, 1)
page = strings.Replace(page, "{{NAV}}", navbuf.String(), 1)
page = strings.Replace(page, "{{CONTENT}}", html, 1)
page = strings.Replace(page, "{{FOOTER}}", FooterText, 1)
outpath = strings.TrimSuffix(outpath, ".md") + ".html"
return os.WriteFile(outpath, []byte(page), 0644)
}
return copy_file(path, outpath)
})
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
|