summaryrefslogtreecommitdiff
path: root/kew.go
blob: 5874d50486f86dbaa10fe4af742068c65c58b56b (plain)
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
package main

import (
	"fmt"
	"io"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

/* TODO: config.h */

const SiteTitle = "mallocd.com"
const FooterText = "made with kew"
const TemplateFile = "template.html"

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 {
				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) {
	b.WriteString("<ul>\n")

	for _, f := range n.Files {
		b.WriteString(`<li><a href="` + f.Path + `">` + f.Name + "</a></li>\n")
	}

	for _, c := range n.Children {
		b.WriteString("<li>" + c.Name)
		render_nav(c, b)
		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)
	var navbuf strings.Builder
	render_nav(rootnav, &navbuf)

	/* 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
			}

			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)

			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)
	}
}