Initial commit
This commit is contained in:
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user