// design-export renders the reviewed Japanese handbook; it never writes the DB. package main import ( "crypto/sha256" "encoding/hex" "encoding/json" "flag" "fmt" "html" "os" "path/filepath" "strings" xhtml "golang.org/x/net/html" "pageswiki/internal/render" ) const prefix = "/lab/pageswiki-dev" var chapters = []string{ "index", "principles", "address-storage", "campus-federation", "knowledge-city", "auth-permissions", "publication", "viewer-editor", "navigation-search", "scheduler-resources", "blog-diary", "extensions", "organizer", "tasks-tickets", "calendar-contacts", "feeds", "mail", "html-editor", "operations", "i18n", "roadmap", "decisions", "sources", } type entry struct { Chapter string `json:"chapter"` Source string `json:"source"` File string `json:"file"` Path string `json:"path"` Language string `json:"language"` SourceSHA256 string `json:"source_sha256"` HTMLSHA256 string `json:"html_sha256"` } func chapterPath(id string) string { if id == "index" { return prefix } return prefix + "/" + id } func digest(b []byte) string { h := sha256.Sum256(b) return hex.EncodeToString(h[:]) } func validateLinks(body string) error { known := map[string]bool{prefix + "/": true} for _, id := range chapters { known[chapterPath(id)] = true } n, err := xhtml.Parse(strings.NewReader(body)) if err != nil { return err } var visit func(*xhtml.Node) error visit = func(n *xhtml.Node) error { if n.Type == xhtml.ElementNode && n.Data == "a" { for _, a := range n.Attr { if a.Key != "href" { continue } p := strings.SplitN(a.Val, "#", 2)[0] if strings.HasPrefix(p, "/") && !known[p] { return fmt.Errorf("unmapped local documentation link: %s", a.Val) } } } for c := n.FirstChild; c != nil; c = c.NextSibling { if err := visit(c); err != nil { return err } } return nil } return visit(n) } func export(src, dest string) error { if dest == "" { return fmt.Errorf("-out is required (use an empty staging directory)") } // Review/validate everything before producing any upload artifact. type document struct { entry entry body []byte } var docs []document for i, id := range chapters { file := filepath.Join(src, id+".md") md, err := os.ReadFile(file) if err != nil { return err } if !strings.HasPrefix(string(md), "# ") { return fmt.Errorf("missing title: %s", file) } body := render.Markdown(string(md)) if err := validateLinks(body); err != nil { return fmt.Errorf("%s: %w", file, err) } nav := `` payload := []byte(nav + "\n" + body + "\n" + nav + "\n") docs = append(docs, document{entry{id, file, id + ".html", chapterPath(id), "ja", digest(md), digest(payload)}, payload}) } if err := os.MkdirAll(dest, 0700); err != nil { return err } entries, err := os.ReadDir(dest) if err != nil { return err } if len(entries) != 0 { return fmt.Errorf("staging directory must be empty") } var manifest []entry for _, doc := range docs { if err := os.WriteFile(filepath.Join(dest, doc.entry.File), doc.body, 0600); err != nil { return err } manifest = append(manifest, doc.entry) } b, err := json.MarshalIndent(manifest, "", " ") if err != nil { return err } return os.WriteFile(filepath.Join(dest, "manifest.json"), append(b, '\n'), 0600) } func main() { src := flag.String("src", "docs/design/ja", "reviewed Markdown source directory") out := flag.String("out", "", "empty staging directory; no database or network writes") flag.Parse() if err := export(*src, *out); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } fmt.Printf("Rendered %d Japanese chapters; manifest: %s\n", len(chapters), filepath.Join(*out, "manifest.json")) }