1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
package build
import (
"fmt"
"html/template"
"os"
"path/filepath"
"sort"
"time"
"github.com/HimanshuSardana/kite/pkg/config"
"github.com/HimanshuSardana/kite/pkg/content"
)
type Page struct {
Title string
Content template.HTML
TOC []TOCItem
}
type HomePageData struct {
SiteTitle string
AuthorName string
AuthorRole string
AuthorBio string
DefaultTheme string
Year int
Posts []content.PostSummary
}
func RenderPage(tmpl *template.Template, outputPath string, data Page) error {
if err := os.MkdirAll(filepath.Dir(outputPath), 0o755); err != nil {
return fmt.Errorf("creating directories: %w", err)
}
outputFile, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("creating output file: %w", err)
}
defer outputFile.Close()
if err := tmpl.Execute(outputFile, data); err != nil {
return fmt.Errorf("executing template: %w", err)
}
return nil
}
func LoadTemplate(themePath, templateFile string) (*template.Template, error) {
tmpl, err := template.ParseFiles(filepath.Join(themePath, templateFile))
if err != nil {
return nil, fmt.Errorf("parsing template: %w", err)
}
return tmpl, nil
}
func RenderHomePage(themePath, outputDir, configPath string, summaries []content.PostSummary) error {
sort.Slice(summaries, func(i, j int) bool {
return summaries[i].Date > summaries[j].Date
})
for i, p := range summaries {
if t, err := time.Parse("2006-01-02", p.Date); err == nil {
summaries[i].Date = t.Format("Jan 2006")
}
}
cfg, err := config.Load(configPath)
if err != nil {
return fmt.Errorf("loading config: %w", err)
}
data := HomePageData{
SiteTitle: cfg.SiteTitle,
AuthorName: cfg.AuthorName,
AuthorRole: cfg.AuthorRole,
AuthorBio: cfg.AuthorBio,
DefaultTheme: cfg.DefaultTheme,
Year: time.Now().Year(),
Posts: summaries,
}
tmpl, err := LoadTemplate(themePath, "home.html")
if err != nil {
return err
}
outPath := filepath.Join(outputDir, "index.html")
if err := os.MkdirAll(filepath.Dir(outPath), 0o755); err != nil {
return fmt.Errorf("creating output dir: %w", err)
}
f, err := os.Create(outPath)
if err != nil {
return fmt.Errorf("creating index.html: %w", err)
}
defer f.Close()
if err := tmpl.Execute(f, data); err != nil {
return fmt.Errorf("rendering home page: %w", err)
}
fmt.Println("Home page written to", outPath)
return nil
}
|