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" const NavDirSymbol = "/" const NavFileSymbol = ": " const NavCurrentSymbol = "@ " type NavNode struct { Name string Path string Files []NavNode Children []NavNode } 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") { if e.Name() == "index.md" { continue } 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 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 fix_md_references(s string) string { r := strings.NewReplacer( /* common cases */ ".md)", ".html)", ".md\"", ".html\"", ".md'", ".html'", ".md)", ".html)", ".md#", ".html#", ".md>", ".html>", ".md ", ".html ", ".md,", ".html,", ) return r.Replace(s) } 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 render_nav(n NavNode, b *strings.Builder, cur string) { b.WriteString("