From 9654e9965c93d79fe472302bdf11fba386921124 Mon Sep 17 00:00:00 2001 From: Keith Solomon Date: Sat, 19 Apr 2025 14:33:01 -0500 Subject: [PATCH] Initial commit --- .vscode/settings.json | 8 ++++ content/about.md | 3 ++ content/index.md | 3 ++ go.mod | 5 +++ go.sum | 2 + main.go | 98 +++++++++++++++++++++++++++++++++++++++++++ public/about.html | 35 ++++++++++++++++ public/index.html | 35 ++++++++++++++++ templates/layout.html | 29 +++++++++++++ 9 files changed, 218 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 content/about.md create mode 100644 content/index.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 public/about.html create mode 100644 public/index.html create mode 100644 templates/layout.html diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..214e70e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "workbench.colorCustomizations": { + "tree.indentGuidesStroke": "#3d92ec", + "activityBar.background": "#0C188E", + "titleBar.activeBackground": "#1121C7", + "titleBar.activeForeground": "#F9F9FE" + } +} diff --git a/content/about.md b/content/about.md new file mode 100644 index 0000000..57c402b --- /dev/null +++ b/content/about.md @@ -0,0 +1,3 @@ +# About + +Experimenting with Go. Seems pretty cool so far! diff --git a/content/index.md b/content/index.md new file mode 100644 index 0000000..def2499 --- /dev/null +++ b/content/index.md @@ -0,0 +1,3 @@ +# Welcome + +This is your first static site built with Go! diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..08fe808 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module go-ssg + +go 1.24.2 + +require github.com/russross/blackfriday/v2 v2.1.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..502a072 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..b1c07cc --- /dev/null +++ b/main.go @@ -0,0 +1,98 @@ +package main + +import ( + "fmt" + "html/template" + "io/ioutil" + "os" + "path/filepath" + "strings" + + "github.com/russross/blackfriday/v2" +) + +// NavItem represents a link in the navigation menu +type NavItem struct { + Title string + URL string +} + +func main() { + contentDir := "./content" + outputDir := "./public" + templateFile := "./templates/layout.html" + + tpl, err := parseTemplate(templateFile) + if err != nil { + fmt.Printf("Template parsing error: %v\n", err) + return + } + + files, err := readMarkdownFiles(contentDir) + if err != nil { + fmt.Printf("Failed to read content directory: %v\n", err) + return + } + + nav := buildNavigation(files) + + generatePages(files, contentDir, outputDir, tpl, nav) + + fmt.Println("✅ Site generation complete.") +} + +func parseTemplate(templateFile string) (*template.Template, error) { + return template.ParseFiles(templateFile) +} + +func readMarkdownFiles(contentDir string) ([]os.FileInfo, error) { + return ioutil.ReadDir(contentDir) +} + +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}) + } + } + 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 + } + + 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, + } + + 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) + } + } +} diff --git a/public/about.html b/public/about.html new file mode 100644 index 0000000..c0d0ee1 --- /dev/null +++ b/public/about.html @@ -0,0 +1,35 @@ + + + + + + About + + + +
+ +
+ +
+

About

+ +

+Experimenting with Go. Seems pretty cool so far!

+ +
+ + + + + diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..51441cc --- /dev/null +++ b/public/index.html @@ -0,0 +1,35 @@ + + + + + + Index + + + +
+ +
+ +
+

Welcome

+ +

+This is your first static site built with Go!

+ +
+ + + + + diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000..8d904bc --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,29 @@ + + + + + + {{ .Title }} + + + +
+ +
+ +
+ {{ .Content }} +
+ + + + +