restructure project with wiki section and root index
sageclove
Sat, 04 Jan 2025 13:29:46 -0700
20 files changed,
67 insertions(+),
30 deletions(-)
jump to
M
build/build.go
→
build/build.go
@@ -54,34 +54,48 @@ os.Exit(1)
} } } - fmt.Println("Building...") + fmt.Println("Starting build...") os.RemoveAll(filepath.Join("out")) os.Mkdir(filepath.Join("out"), 0777) - buildPage(buildPageLinkedList()) + fmt.Println("Building wiki pages...") + buildWiki(buildWikiLinkedList()) + fmt.Println("\tDone") - cssFS := os.DirFS(filepath.Join(rootPath, "css")) - os.CopyFS(filepath.Join("out", "css"), cssFS) + fmt.Println("Copying and renaming wiki CSS...") + wikiCssFS := os.DirFS(filepath.Join(rootPath, "wiki", "css")) + wikiCssOutPath := filepath.Join("out", "wiki", "css") + os.CopyFS(wikiCssOutPath, wikiCssFS) os.Rename( - filepath.Join("out", "css", "style.css"), - filepath.Join("out", "css", "style."+strconv.FormatInt(now.Unix(), 10)+".css"), + filepath.Join(wikiCssOutPath, "style.css"), + filepath.Join(wikiCssOutPath, "style."+strconv.FormatInt(now.Unix(), 10)+".css"), ) + fmt.Println("\tDone") + fmt.Println("Copying assets...") assetsFS := os.DirFS(filepath.Join(rootPath, "assets")) os.CopyFS(filepath.Join("out", "assets"), assetsFS) + fmt.Println("\tDone") + + fmt.Println("Copying root index...") + copyFile( + filepath.Join(rootPath, "index.html"), + filepath.Join("out", "index.html"), + ) + fmt.Println("\tDone") fmt.Println("Build complete.") if serve { http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("out")))) - http.HandleFunc("GET /{page}", pageHandler) + http.HandleFunc("GET /wiki/{page}", wikiHandler) fmt.Println("Serving on http://localhost:8000") log.Fatal(http.ListenAndServe(":8000", nil)) } } -func pageHandler(responseWriter http.ResponseWriter, request *http.Request) { +func wikiHandler(responseWriter http.ResponseWriter, request *http.Request) { pageName := request.PathValue("page") - pageFile, err := os.ReadFile(filepath.Join("out", pageName+".html")) + pageFile, err := os.ReadFile(filepath.Join("out", "wiki", pageName+".html")) if err == nil { responseWriter.Write(pageFile) return@@ -89,7 +103,7 @@ }
http.NotFound(responseWriter, request) } -func buildPageLinkedList() *Page { +func buildWikiLinkedList() *Page { rootNode := &Page{ Name: "index", Parent: nil,@@ -97,11 +111,11 @@ }
parentNode := rootNode tailNode := rootNode currentDepth := 0 - tabFile, _ := os.Open(filepath.Join(rootPath, "site.tab")) + tabFile, _ := os.Open(filepath.Join(rootPath, "wiki", "wiki.tab")) scanner := bufio.NewScanner(tabFile) scanner.Split(bufio.ScanLines) for scanner.Scan() { - depth := strings.Count(scanner.Text(), " ") + depth := strings.Count(strings.TrimRight(scanner.Text(), " "), " ") pageName := strings.TrimLeft(scanner.Text(), " ") if len(pageName) > 0 { if depth == currentDepth {@@ -184,7 +198,9 @@ }
return childLinks } -func buildPage(page *Page) { +func buildWiki(page *Page) { + wikiSrcPath := filepath.Join(rootPath, "wiki") + wikiOutPath := filepath.Join("out", "wiki") parents := parentLinks(page) siblings := siblingLinks(page) children := childLinks(page)@@ -196,31 +212,39 @@ if headerLink != nil {
filteredHeaderLinks = append(filteredHeaderLinks, headerLink) } } - pageFilePath := filepath.Join(rootPath, "site", page.Name+".html") + pageFilePath := filepath.Join(wikiSrcPath, "pages", page.Name+".html") pageFileInfo, _ := os.Stat(pageFilePath) if pageFileInfo == nil { - dst, _ := os.Create(pageFilePath) - src, _ := os.Open(filepath.Join(rootPath, "templates", "page.html")) - io.Copy(dst, src) - dst.Sync() + copyFile( + filepath.Join(wikiSrcPath, "templates", "page.html"), + pageFilePath, + ) } - outfile, _ := os.Create(filepath.Join("out", page.Name+".html")) + os.Mkdir(wikiOutPath, 0777) + outfile, _ := os.Create(filepath.Join(wikiOutPath, page.Name+".html")) template, _ := template.ParseFiles( - filepath.Join(rootPath, "templates", "document.html"), - filepath.Join(rootPath, "templates", "header.html"), - filepath.Join(rootPath, "site", page.Name+".html"), + filepath.Join(wikiSrcPath, "templates", "document.html"), + filepath.Join(wikiSrcPath, "templates", "header.html"), + filepath.Join(wikiSrcPath, "pages", page.Name+".html"), ) template.Execute( outfile, &PageProperties{ Title: page.Name, - StylePath: "/css/style." + strconv.FormatInt(now.Unix(), 10) + ".css", + StylePath: "/wiki/css/style." + strconv.FormatInt(now.Unix(), 10) + ".css", HeaderLinks: filteredHeaderLinks, }, ) for _, child := range page.Children { - buildPage(child) + buildWiki(child) } +} + +func copyFile(srcPath string, dstPath string) { + dst, _ := os.Create(dstPath) + src, _ := os.Open(srcPath) + io.Copy(dst, src) + dst.Sync() } func __(foo any) {}
M
build/readme.md
→
build/readme.md
@@ -7,14 +7,15 @@
`./build <ROOT>` `<ROOT>` should be a path to the site's source directory. The source directory must contain the following subdirectories and files: -- `css/`: for CSS stylesheets -- `assets/`: for other assets (e.g. images, files...) -- `site/`: for site pages. The structure of this subdirectory determines the structure of the site itself. -- `templates/`: for reused markup. -- `site.tab`: this defines the structure of the site shown in the header nav. Each line should be a name of a file in the `site` directory (without the `.html` extension). Hierarchy is denoted by spaces preceding the page name. If a corresponding file does not exist in `site` for a given name, a new file will be created in `site`. Therefore the best approach for adding a new page is to add its name to `site.tab`, then rebuild: this way the new page file will be created automatically with the proper template tags. +- `wiki/` +- `assets` +- `index.html` If `<ROOT>` is omitted, the current directory will be used as the source directory. ### Flags -- `--serve`, `-s`: Builds site, then serves built site on port 8000. For previewing site, not for use in a live environment.+- `--serve`, `-s`: Builds site, then serves built site on port 8000. For previewing site, not for use in a live environment. + +### Creating a new wiki page +Add a new name to `wiki.tab` at its proper place in the hierarchy and rerun the build. A new file will be created in the `/wiki/pages` directory with the proper template tags.
A
index.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html> +<html> + <head> + <meta charset="UTF-8"> + <meta http-equiv="X-UA-Compatible" content="IE=edge"> + <meta name="viewport" content="width=device-width,initial-scale=1.0"> + <title>deserthorns</title> + </head> + <body> + <h1>deserthorns</h1> + </body> +</html>