all repos — vite @ 71bec3e96b175b5960f518aefae0faae433af030

a fast (this time, actually) and minimal static site generator

commands/build.go (view raw)

 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
package commands

import (
	"fmt"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"sync"
	"time"

	"git.icyphox.sh/vite/atom"
	"git.icyphox.sh/vite/config"
	"git.icyphox.sh/vite/markdown"
	"git.icyphox.sh/vite/util"
)

const (
	BUILD     = "build"
	PAGES     = "pages"
	TEMPLATES = "templates"
	STATIC    = "static"
)

type Pages struct {
	Dirs  []string
	Files []string
}

// Populates a Pages object with dirs and files
// found in 'pages/'.
func (pgs *Pages) initPages() error {
	files, err := os.ReadDir("./pages")
	if err != nil {
		return err
	}

	for _, f := range files {
		if f.IsDir() {
			pgs.Dirs = append(pgs.Dirs, f.Name())
		} else {
			pgs.Files = append(pgs.Files, f.Name())
		}
	}

	return nil
}

func (pgs *Pages) processFiles() error {
	for _, f := range pgs.Files {
		if filepath.Ext(f) == ".md" {
			// ex: pages/about.md
			mdFile := filepath.Join(PAGES, f)
			var htmlDir string
			// ex: build/index.html (root index)
			if f == "_index.md" {
				htmlDir = BUILD
			} else {
				htmlDir = filepath.Join(
					BUILD,
					strings.TrimSuffix(f, ".md"),
				)
			}
			os.Mkdir(htmlDir, 0755)
			// ex: build/about/index.html
			htmlFile := filepath.Join(htmlDir, "index.html")

			fb, err := os.ReadFile(mdFile)
			if err != nil {
				return err
			}

			out := markdown.Output{}
			if err = out.RenderMarkdown(fb); err != nil {
				return err
			}
			if err = out.RenderHTML(
				htmlFile,
				TEMPLATES,
				struct {
					Cfg  config.ConfigYaml
					Meta markdown.Matter
					Body string
				}{config.Config, out.Meta, string(out.HTML)},
			); err != nil {
				return err
			}
		} else {
			src := filepath.Join(PAGES, f)
			dst := filepath.Join(BUILD, f)
			if err := util.CopyFile(src, dst); err != nil {
				return err
			}
		}
	}
	return nil
}

func (pgs *Pages) processDirs() error {
	for _, d := range pgs.Dirs {
		// ex: build/blog
		dstDir := filepath.Join(BUILD, d)
		// ex: pages/blog
		srcDir := filepath.Join(PAGES, d)
		os.Mkdir(dstDir, 0755)

		entries, err := os.ReadDir(srcDir)
		if err != nil {
			return err
		}

		posts := []markdown.Output{}
		// Collect all posts
		for _, e := range entries {
			// foo-bar.md -> foo-bar
			slug := strings.TrimSuffix(e.Name(), filepath.Ext(e.Name()))

			// ex: build/blog/foo-bar/
			os.Mkdir(filepath.Join(dstDir, slug), 0755)
			// ex: build/blog/foo-bar/index.html
			htmlFile := filepath.Join(dstDir, slug, "index.html")

			if e.Name() != "_index.md" {
				ePath := filepath.Join(srcDir, e.Name())
				fb, err := os.ReadFile(ePath)
				if err != nil {
					return err
				}

				out := markdown.Output{}
				if err := out.RenderMarkdown(fb); err != nil {
					return err
				}
				if err = out.RenderHTML(
					htmlFile,
					TEMPLATES,
					struct {
						Cfg  config.ConfigYaml
						Meta markdown.Matter
						Body string
					}{config.Config, out.Meta, string(out.HTML)},
				); err != nil {
					return err
				}
				posts = append(posts, out)
			}

			// Sort posts slice by date
			sort.Slice(posts, func(i, j int) bool {
				dateStr1 := posts[j].Meta["date"]
				dateStr2 := posts[i].Meta["date"]
				date1, _ := time.Parse("2006-01-02", dateStr1)
				date2, _ := time.Parse("2006-01-02", dateStr2)
				return date1.Before(date2)
			})
		}

		// Render index using posts slice.
		// ex: build/blog/index.html
		indexHTML := filepath.Join(dstDir, "index.html")
		// ex: pages/blog/_index.md
		indexMd, err := os.ReadFile(filepath.Join(srcDir, "_index.md"))
		if err != nil {
			return err
		}
		out := markdown.Output{}
		if err := out.RenderMarkdown(indexMd); err != nil {
			return err
		}

		out.RenderHTML(indexHTML, TEMPLATES, struct {
			Cfg   config.ConfigYaml
			Meta  markdown.Matter
			Body  string
			Posts []markdown.Output
		}{config.Config, out.Meta, string(out.HTML), posts})

		// Create feeds
		// ex: build/blog/feed.xml
		xml, err := atom.NewAtomFeed(d, posts)
		if err != nil {
			return err
		}
		feedFile := filepath.Join(dstDir, "feed.xml")
		os.WriteFile(feedFile, xml, 0755)
	}
	return nil
}

// Core builder function. Converts markdown to html,
// copies over non .md files, etc.
func Build() error {
	fmt.Print("vite: building... ")
	pages := Pages{}
	if err := pages.initPages(); err != nil {
		return err
	}

	// Clean the build directory.
	if err := util.Clean(BUILD); err != nil {
		return err
	}

	wg := sync.WaitGroup{}
	wg.Add(2)
	wgDone := make(chan bool)

	ec := make(chan error)

	// Deal with files.
	// ex: pages/{_index,about,etc}.md
	go func() {
		err := pages.processFiles()
		if err != nil {
			ec <- err
		}
		wg.Done()
	}()

	// Deal with dirs -- i.e. dirs of markdown files.
	// ex: pages/{blog,travel}/*.md
	go func() {
		err := pages.processDirs()
		if err != nil {
			ec <- err
		}
		wg.Done()
	}()

	go func() {
		wg.Wait()
		close(wgDone)
	}()

	select {
	case <-wgDone:
		break
	case err := <-ec:
		close(ec)
		return err
	}

	// Copy the static directory into build
	// ex: build/static/
	buildStatic := filepath.Join(BUILD, STATIC)
	os.Mkdir(buildStatic, 0755)
	if err := util.CopyDir(STATIC, buildStatic); err != nil {
		return err
	}

	fmt.Print("done\n")
	return nil
}