summaryrefslogtreecommitdiff
path: root/internal/build/build.go
blob: 45117458b8afee9ce1f6ec59e6e45cfcd0d713eb (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package build

import (
	"fmt"
	"html/template"
	"io/fs"
	"log"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"time"

	"gopkg.in/yaml.v2"

	"github.com/adrg/frontmatter"
	"github.com/gomarkdown/markdown"
	"github.com/gomarkdown/markdown/ast"
	"github.com/gomarkdown/markdown/html"
	"github.com/gomarkdown/markdown/parser"
)

var (
	themeName  = "gruvbox"
	contentDir = "./content"
	outputDir  = "./output"
)

type Frontmatter struct {
	Title string   `yaml:"title"`
	Date  string   `yaml:"date"`
	Tags  []string `yaml:"tags"`
}

type Page struct {
	Title   string
	Content template.HTML
	TOC     []TOCItem
}

type TOCItem struct {
	Level int
	Text  string
	ID    string
}

type PostSummary struct {
	Title string
	Slug  string
	Date  string
	Tags  []string
}

type HomePage struct {
	SiteTitle  string `yaml:"siteTitle"`
	AuthorName string `yaml:"authorName"`
	AuthorRole string `yaml:"authorRole"`
	AuthorBio  string `yaml:"authorBio"`
	Year       int
	Posts      []PostSummary
}

var posts = make([]Post, 0)

type Post struct {
	Title string
}

func Build(themeName string) {
	if themeName == "" {
		themeName = "modern-light"
	}

	summaries := make([]PostSummary, 0)

	err := filepath.WalkDir(contentDir, 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") {
			fmt.Println("Processing:", path)

			matter, htmlContent, toc := convertToHtml(path)
			slug := strings.TrimSuffix(d.Name(), ".md")
			summaries = append(summaries, PostSummary{
				Title: matter.Title,
				Slug:  slug,
				Date:  matter.Date,
				Tags:  matter.Tags,
			})

			posts = append(posts, Post{Title: matter.Title})
			// fmt.Println("Appended post", posts)
			newPage := Page{
				Title:   matter.Title,
				Content: template.HTML(htmlContent),
				TOC:     toc,
			}

			tmpl, err := template.ParseFiles("./themes/" + themeName + "/layout.html")
			if err != nil {
				log.Fatalf("Error parsing template: %s", err)
			}

			relPath, err := filepath.Rel(contentDir, path)
			if err != nil {
				log.Fatalf("Error computing relative path: %s", err)
			}
			// test.md -> test.html
			// test.md -> test/index.html
			outputFilePath := filepath.Join(outputDir, strings.Replace(relPath, ".md", "/index.html", 1))

			err = os.MkdirAll(filepath.Dir(outputFilePath), 0o755)
			if err != nil {
				log.Fatalf("Error creating directories: %s", err)
			}

			outputFile, err := os.Create(outputFilePath)
			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 walking directory: %s", err)
	}

	fmt.Println("All files processed!")

	fmt.Println(posts)
	fmt.Println(themeName)

	renderHomePage(themeName, summaries, outputDir)
}

func convertToHtml(path string) (Frontmatter, []byte, []TOCItem) {
	md, err := os.ReadFile(path)
	if err != nil {
		log.Fatalf("Error reading %s: %s", path, err)
	}

	var matter Frontmatter
	rest, err := frontmatter.Parse(strings.NewReader(string(md)), &matter)
	if err != nil {
		log.Fatalf("Error parsing frontmatter: %s", err)
	}

	extensions := parser.CommonExtensions | parser.AutoHeadingIDs
	p := parser.NewWithExtensions(extensions)

	doc := p.Parse(rest)

	var toc []TOCItem

	ast.WalkFunc(doc, func(node ast.Node, entering bool) ast.WalkStatus {
		if heading, ok := node.(*ast.Heading); ok && entering {
			text := extractText(heading)
			id := string(heading.HeadingID)

			toc = append(toc, TOCItem{
				Level: heading.Level,
				Text:  text,
				ID:    id,
			})
		}
		return ast.GoToNext
	})

	renderer := html.NewRenderer(html.RendererOptions{
		Flags: html.CommonFlags,
	})

	output := markdown.Render(doc, renderer)

	return matter, output, toc
}

func extractText(h *ast.Heading) string {
	var text string
	ast.WalkFunc(h, func(node ast.Node, entering bool) ast.WalkStatus {
		if leaf, ok := node.(*ast.Text); ok && entering {
			text += string(leaf.Literal)
		}
		return ast.GoToNext
	})
	return text
}

func renderHomePage(themeName string, summaries []PostSummary, outputDir string) {
	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")
		}
	}

	config, err := os.ReadFile("config.yaml")
	if err != nil {
		panic(err)
	}
	var data HomePage
	err = yaml.Unmarshal(config, &data)
	data.Posts = summaries
	data.Year = time.Now().Year()

	if err != nil {
		panic(err)
	}

	tmpl, err := template.ParseFiles("./themes/" + themeName + "/home.html")
	if err != nil {
		log.Fatalf("Error parsing home template: %s", err)
	}

	outPath := filepath.Join(outputDir, "index.html")
	if err := os.MkdirAll(filepath.Dir(outPath), 0o755); err != nil {
		log.Fatalf("Error creating output dir: %s", err)
	}
	f, err := os.Create(outPath)
	if err != nil {
		log.Fatalf("Error creating index.html: %s", err)
	}
	defer f.Close()

	if err := tmpl.Execute(f, data); err != nil {
		log.Fatalf("Error rendering home page: %s", err)
	}
	fmt.Println("Home page written to", outPath)
}

func ListThemes() []string {
	themeList := make([]string, 0)
	themes, err := os.ReadDir("./themes")
	if err != nil {
		log.Fatal("Error:", err)
	}
	for _, theme := range themes {
		if theme.IsDir() {
			themeList = append(themeList, string(theme.Name()))
		}
	}

	return themeList
}

func ShowHelpMessage() {
	fmt.Println(`
Kite — A lightweight static site generator

USAGE:
  kite <command> [options]

COMMANDS:
  build         Build the static site into the output directory
  serve         Start a local development server with live reload
  list-themes   List all available themes

OPTIONS:
  -h, --help    Show this help message

EXAMPLES:
  kite build
  kite serve
  kite serve --port 8080
  kite list-themes

DESCRIPTION:
  Kite converts your content into a static website using themes and templates.
  Use 'build' for production output and 'serve' for local development.
`)
}