/
pages.go
143 lines (125 loc) · 4.12 KB
/
pages.go
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
package main
import (
"bytes"
"os"
"strings"
"io/fs"
"io/ioutil"
"path/filepath"
"github.com/adrg/frontmatter"
"gopkg.in/yaml.v3"
"github.com/noirbizarre/gonja"
"github.com/brothertoad/btu"
)
type pageInfo struct {
outputPath string
dataPath string
perPageDataPath string
}
// Default format for page modification date.
const TEXTDATE = "January 2, 2006"
// key for page-specific properties
const DATE_MODIFIED_KEY = "modTime"
const PATH_KEY = "path"
var numPageDirParts int
func processPages(pageDir string, outputDir string, globalData map[string]interface{}) {
btu.DirMustExist(pageDir)
err := filepath.Walk(pageDir, func(path string, fileInfo fs.FileInfo, err error) error {
// Check the extension and filename to see if we should ignore this file.
if !validExtension(fileInfo.Name()) || !validFilename(fileInfo.Name()) {
return nil
}
info := buildPageInfo(path, outputDir, fileInfo)
// Copy the global data, then read and merge the frontmatter.
pageData := make(map[string]interface{})
for k, v := range globalData {
pageData[k] = v
}
b, ferr := ioutil.ReadFile(path)
btu.CheckError(ferr)
fm := make(map[string]interface{})
rest, fmerr := frontmatter.Parse(bytes.NewReader(b), &fm)
btu.CheckError(fmerr)
for k, v := range fm {
pageData[k] = v
}
// Read in page-specific data, if any.
readPageData(info.dataPath, pageData)
readPageData(info.perPageDataPath, pageData)
// Get the last-modified date of the file, and add it as a page-specific property.
modTimeFormat := TEXTDATE
if config.ModTimeFormat != "" {
modTimeFormat = config.ModTimeFormat
}
pageData[DATE_MODIFIED_KEY] = fileInfo.ModTime().Format(modTimeFormat)
// Add other generic page-specific properties.
pageData[PATH_KEY] = strings.TrimPrefix(path, config.PageDir)[1:]
tpl := gonja.Must(gonja.FromBytes(rest))
out, err := tpl.Execute(pageData)
btu.CheckError(err)
btu.CreateDirForFile(info.outputPath)
err = os.WriteFile(info.outputPath, []byte(out + "\n"), 0644)
btu.CheckError(err)
return nil
})
btu.CheckError(err)
}
func readPageData(path string, pageData map[string]interface{}) {
b, ferr := ioutil.ReadFile(path)
if ferr == nil {
pageMap := make(map[string]interface{})
yerr := yaml.Unmarshal(b, pageMap)
btu.CheckError(yerr)
for k, v := range pageMap {
pageData[k] = v
}
}
}
func validExtension(filename string) bool {
if strings.HasSuffix(filename, ".html") {
return true
}
// Note that this case also covers .html.j2
if strings.HasSuffix(filename, ".j2") {
return true
}
return false
}
func validFilename(filename string) bool {
return !strings.HasPrefix(filename, "__")
}
func buildPageInfo(path string, outputDir string, fileInfo fs.FileInfo) pageInfo {
var info pageInfo
relativePath := strings.TrimPrefix(path, config.PageDir)
outputPath := filepath.Join(outputDir, relativePath)
dir, base := filepath.Split(outputPath)
suffix := getSuffix(base)
// If the filename starts with an underscore, remove the underscore and the suffix and add .html.
// Otherwise, remove the suffix and add a separator followed by index.html.
if strings.HasPrefix(base, "_") {
outputPath = filepath.Join(dir, strings.TrimSuffix(base[1:], suffix) + ".html")
} else {
outputPath = strings.TrimSuffix(outputPath, suffix)
outputPath = filepath.Join(outputPath, "index.html")
}
info.outputPath = outputPath
info.dataPath = strings.TrimSuffix(path, suffix) + ".yaml"
if config.PerPageDataDir != "" {
info.perPageDataPath = filepath.Join(config.PerPageDataDir, strings.TrimSuffix(relativePath, suffix) + ".yaml")
}
btu.Log(10, "path is %s, relativePath is %s, dataPath is %s, perPageDataPath is %s\n", path, relativePath, info.dataPath, info.perPageDataPath)
return info
}
func getSuffix(base string) string {
if strings.HasSuffix(base, ".html") {
return ".html"
}
if strings.HasSuffix(base, ".html.j2") {
return ".html.j2"
}
if strings.HasSuffix(base, ".j2") {
return ".j2"
}
btu.Fatal("Can't get suffix of %s\n", base)
return ""
}