diff options
| author | Himanshu Sardana <himanshusardana2005@gmail.com> | 2026-03-16 20:19:24 +0000 |
|---|---|---|
| committer | Himanshu Sardana <himanshusardana2005@gmail.com> | 2026-03-16 20:19:24 +0000 |
| commit | cc36fef8f53eaa0f76be5016616efc5aa461a6da (patch) | |
| tree | 943ab16e3b40880dc3300a77a9f2ad5b988987a9 /main.go | |
| parent | 6e5f39603b81e2e4bcae235560d12d648a7c8e30 (diff) | |
feat: add html templating
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 60 |
1 files changed, 47 insertions, 13 deletions
@@ -1,7 +1,8 @@ package main import ( - "fmt" + "html/template" + "io/fs" "log" "os" "path/filepath" @@ -10,21 +11,54 @@ import ( "github.com/gomarkdown/markdown" ) +type Page struct { + Title string + Content template.HTML +} + func main() { path := filepath.Join("./content/") - files, err := os.ReadDir(path) - if err != nil { - log.Fatalf("Error %s", err) - } - for _, f := range files { - filePath := filepath.Join(path, f.Name()) - if !f.IsDir() && strings.HasSuffix(f.Name(), ".md") { - fmt.Printf("Found content: %s\n", filePath) - htmlContent := convertToHtml(filePath) - htmlPath := strings.Replace(filePath, ".md", ".html", 1) - os.WriteFile(htmlPath, htmlContent, 0o777) - fmt.Printf("Wrote file: %s\n", htmlPath) + // outputPath := filepath.Join("./output/") + err := filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if d.IsDir() { + return nil } + + if strings.HasSuffix(d.Name(), ".md") { + if err != nil { + log.Fatalf("Error: %s", err) + } + htmlContent := convertToHtml(path) + newPage := Page{ + Title: "hello", + Content: template.HTML(htmlContent), + } + + tmpl, err := template.ParseFiles("./layout.html") + if err != nil { + log.Fatalf("Error parsing template: %s", err) + } + + outputFile, err := os.Create("./output.html") + if err != nil { + log.Fatalf("Error creating output file: %s", err) + } + + defer outputFile.Close() + + err = tmpl.Execute(outputFile, newPage) + if err != nil { + log.Fatalf("Error generating output content %s", err) + } + } + return nil + }) + if err != nil { + log.Fatalf("Error here: %s", err) } } |
