Initial commit
This commit is contained in:
8
.vscode/settings.json
vendored
Normal file
8
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"workbench.colorCustomizations": {
|
||||
"tree.indentGuidesStroke": "#3d92ec",
|
||||
"activityBar.background": "#0C188E",
|
||||
"titleBar.activeBackground": "#1121C7",
|
||||
"titleBar.activeForeground": "#F9F9FE"
|
||||
}
|
||||
}
|
||||
3
content/about.md
Normal file
3
content/about.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# About
|
||||
|
||||
Experimenting with Go. Seems pretty cool so far!
|
||||
3
content/index.md
Normal file
3
content/index.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# Welcome
|
||||
|
||||
This is your first static site built with Go!
|
||||
5
go.mod
Normal file
5
go.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module go-ssg
|
||||
|
||||
go 1.24.2
|
||||
|
||||
require github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
2
go.sum
Normal file
2
go.sum
Normal file
@@ -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=
|
||||
98
main.go
Normal file
98
main.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
35
public/about.html
Normal file
35
public/about.html
Normal file
@@ -0,0 +1,35 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>About</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<nav>
|
||||
<ul>
|
||||
|
||||
<li><a href="/about.html">About</a></li>
|
||||
|
||||
<li><a href="/index.html">Index</a></li>
|
||||
|
||||
</ul>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<h1>About
|
||||
</h1>
|
||||
|
||||
<p>
|
||||
Experimenting with Go. Seems pretty cool so far!
|
||||
</p>
|
||||
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
<p>© Keith Solomon</p>
|
||||
</footer>
|
||||
</body>
|
||||
35
public/index.html
Normal file
35
public/index.html
Normal file
@@ -0,0 +1,35 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Index</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<nav>
|
||||
<ul>
|
||||
|
||||
<li><a href="/about.html">About</a></li>
|
||||
|
||||
<li><a href="/index.html">Index</a></li>
|
||||
|
||||
</ul>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<h1>Welcome
|
||||
</h1>
|
||||
|
||||
<p>
|
||||
This is your first static site built with Go!
|
||||
</p>
|
||||
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
<p>© Keith Solomon</p>
|
||||
</footer>
|
||||
</body>
|
||||
29
templates/layout.html
Normal file
29
templates/layout.html
Normal file
@@ -0,0 +1,29 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>{{ .Title }}</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<nav>
|
||||
<ul>
|
||||
{{ range .Nav }}
|
||||
<li><a href="{{ .URL }}">{{ .Title }}</a></li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
{{ .Content }}
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
<p>© {{ .Year }} Keith Solomon</p>
|
||||
</footer>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user