aboutsummaryrefslogtreecommitdiffstats
path: root/site.go
blob: 03fdec7a6eb4f4bb6c67dcdf2a5bf62d51e492ae (plain) (blame)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main

import (
	"encoding/json"
	"fmt"
	"html/template"
	"io/fs"
	"os"
	"path/filepath"
	"strings"
)

// Site represents a static site with its configuration.
type Site struct {
	DataDir      string
	TemplatesDir string
	StaticDir    string
	OutDir       string
	Log          func(string, ...any)
}

// Build generates the complete site.
func (s *Site) Build() error {
	data, err := s.loadData()
	if err != nil {
		return err
	}
	if err := s.copyStatic(); err != nil {
		return err
	}
	return s.render(data)
}

func (s *Site) loadData() (map[string]any, error) {
	data := make(map[string]any)

	entries, err := os.ReadDir(s.DataDir)
	if err != nil {
		if os.IsNotExist(err) {
			return data, nil
		}
		return nil, fmt.Errorf("reading data directory: %w", err)
	}

	for _, entry := range entries {
		if entry.IsDir() || filepath.Ext(entry.Name()) != ".json" {
			continue
		}

		name := strings.TrimSuffix(entry.Name(), ".json")
		path := filepath.Join(s.DataDir, entry.Name())

		f, err := os.Open(path)
		if err != nil {
			return nil, fmt.Errorf("opening %s: %w", path, err)
		}

		var v any
		if err := json.NewDecoder(f).Decode(&v); err != nil {
			f.Close()
			return nil, fmt.Errorf("parsing %s: %w", path, err)
		}
		f.Close()

		data[name] = v
		s.log("data/%s.json", name)
	}

	return data, nil
}

func (s *Site) copyStatic() error {
	if _, err := os.Stat(s.StaticDir); os.IsNotExist(err) {
		return nil
	}

	err := filepath.WalkDir(s.StaticDir, func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}

		relPath, err := filepath.Rel(s.StaticDir, path)
		if err != nil {
			return err
		}

		destPath := filepath.Join(s.OutDir, relPath)
		if d.IsDir() {
			return os.MkdirAll(destPath, 0755)
		}

		if err := os.MkdirAll(filepath.Dir(destPath), 0755); err != nil {
			return err
		}

		info, err := d.Info()
		if err != nil {
			return err
		}

		src, err := os.ReadFile(path)
		if err != nil {
			return err
		}

		return os.WriteFile(destPath, src, info.Mode())
	})

	if err != nil {
		return fmt.Errorf("copying static files: %w", err)
	}

	s.log("static/")
	return nil
}

func (s *Site) render(data map[string]any) error {
	if err := os.MkdirAll(s.OutDir, 0755); err != nil {
		return fmt.Errorf("creating output directory: %w", err)
	}

	funcs := template.FuncMap{
		"filter": filterFunc,
	}

	entries, err := os.ReadDir(s.TemplatesDir)
	if err != nil {
		return fmt.Errorf("reading templates directory: %w", err)
	}

	for _, entry := range entries {
		if entry.IsDir() || filepath.Ext(entry.Name()) != ".html" {
			continue
		}

		tmplPath := filepath.Join(s.TemplatesDir, entry.Name())
		outPath := filepath.Join(s.OutDir, entry.Name())

		tmpl, err := template.New(entry.Name()).Funcs(funcs).ParseFiles(tmplPath)
		if err != nil {
			return fmt.Errorf("parsing %s: %w", tmplPath, err)
		}

		out, err := os.Create(outPath)
		if err != nil {
			return fmt.Errorf("creating %s: %w", outPath, err)
		}

		if err := tmpl.Execute(out, data); err != nil {
			out.Close()
			return fmt.Errorf("executing %s: %w", tmplPath, err)
		}
		out.Close()

		s.log("%s", entry.Name())
	}

	return nil
}

func (s *Site) log(format string, args ...any) {
	if s.Log != nil {
		s.Log(format, args...)
	}
}