From 71af009bbf014e2ae52e87af2a9de6521bb2ca0c Mon Sep 17 00:00:00 2001 From: Keith Solomon Date: Sat, 19 Apr 2025 14:55:03 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feature:=20Make=20"pretty"=20URLs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 90 ++++++++++++++----------- public/{about.html => about/index.html} | 4 +- public/index.html | 4 +- 3 files changed, 55 insertions(+), 43 deletions(-) rename public/{about.html => about/index.html} (74%) diff --git a/main.go b/main.go index ae0d978..f74ec5a 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,7 @@ func main() { return } - files, err := readMarkdownFiles(contentDir) + files, err := readContentFiles(contentDir) if err != nil { fmt.Printf("Failed to read content directory: %v\n", err) return @@ -37,7 +37,14 @@ func main() { nav := buildNavigation(files) - generatePages(files, contentDir, outputDir, tpl, nav) + for _, file := range files { + if filepath.Ext(file.Name()) == ".md" { + err := generateHTML(file, contentDir, outputDir, tpl, nav) + if err != nil { + fmt.Printf("Error generating HTML for %s: %v\n", file.Name(), err) + } + } + } fmt.Println("✅ Site generation complete.") } @@ -46,7 +53,7 @@ func parseTemplate(templateFile string) (*template.Template, error) { return template.ParseFiles(templateFile) } -func readMarkdownFiles(contentDir string) ([]os.FileInfo, error) { +func readContentFiles(contentDir string) ([]os.FileInfo, error) { return ioutil.ReadDir(contentDir) } @@ -54,47 +61,52 @@ func buildNavigation(files []os.FileInfo) []NavItem { var nav []NavItem for _, file := range files { if filepath.Ext(file.Name()) == ".md" { - title := strings.Title(strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))) - filename := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())) + ".html" - nav = append(nav, NavItem{Title: title, URL: "/" + filename}) + name := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())) + title := strings.Title(name) + url := "/" + if name != "index" { + url = "/" + name + "/" + } + nav = append(nav, NavItem{Title: title, URL: url}) } } return nav } -func generatePages(files []os.FileInfo, contentDir, outputDir string, tpl *template.Template, nav []NavItem) { - for _, file := range files { - if filepath.Ext(file.Name()) == ".md" { - mdPath := filepath.Join(contentDir, file.Name()) - mdContent, err := ioutil.ReadFile(mdPath) - if err != nil { - fmt.Printf("Failed to read %s: %v\n", file.Name(), err) - continue - } +func generateHTML(file os.FileInfo, contentDir, outputDir string, tpl *template.Template, nav []NavItem) error { + name := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())) + title := strings.Title(name) - htmlContent := blackfriday.Run(mdContent) - outFile := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())) + ".html" - outPath := filepath.Join(outputDir, outFile) - out, err := os.Create(outPath) - if err != nil { - fmt.Printf("Failed to create %s: %v\n", outPath, err) - continue - } - - data := map[string]interface{}{ - "Content": template.HTML(htmlContent), - "Title": strings.Title(strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))), - "Nav": nav, - "Year": time.Now().Year(), - } - - err = tpl.Execute(out, data) - if err != nil { - fmt.Printf("Template execution failed for %s: %v\n", file.Name(), err) - continue - } - - fmt.Printf("Generated: %s\n", outPath) - } + mdPath := filepath.Join(contentDir, file.Name()) + mdContent, err := ioutil.ReadFile(mdPath) + if err != nil { + return fmt.Errorf("failed to read %s: %w", file.Name(), err) } + + htmlContent := blackfriday.Run(mdContent) + + var outPath string + if name == "index" { + outPath = filepath.Join(outputDir, "index.html") + } else { + subDir := filepath.Join(outputDir, name) + os.MkdirAll(subDir, os.ModePerm) + outPath = filepath.Join(subDir, "index.html") + } + + outFile, err := os.Create(outPath) + if err != nil { + return fmt.Errorf("failed to create %s: %w", outPath, err) + } + defer outFile.Close() + + data := map[string]interface{}{ + "Content": template.HTML(htmlContent), + "Title": title, + "Nav": nav, + "Year": time.Now().Year(), + "Date": time.Now().Format("January 2, 2006"), + } + + return tpl.Execute(outFile, data) } diff --git a/public/about.html b/public/about/index.html similarity index 74% rename from public/about.html rename to public/about/index.html index 82e4747..55cb9ad 100644 --- a/public/about.html +++ b/public/about/index.html @@ -11,9 +11,9 @@ diff --git a/public/index.html b/public/index.html index dbedbff..e173b53 100644 --- a/public/index.html +++ b/public/index.html @@ -11,9 +11,9 @@