/
tmpl.go
166 lines (149 loc) · 3.68 KB
/
tmpl.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package web
import (
"fmt"
"html/template"
"io/fs"
"path"
"reflect"
"sort"
"strings"
"gopkg.in/yaml.v3"
)
// A siteDir is a site extended with a known directory for interpreting relative paths.
type siteDir struct {
*Site
dir string
}
func toString(x interface{}) string {
switch x := x.(type) {
case string:
return x
case template.HTML:
return string(x)
case nil:
return ""
default:
panic(fmt.Sprintf("cannot toString %T", x))
}
}
// data parses the named yaml file (relative to dir) and returns its structured data.
func (site *siteDir) data(name string) (interface{}, error) {
data, err := site.readFile(site.dir, name)
if err != nil {
return nil, err
}
var d interface{}
if err := yaml.Unmarshal(data, &d); err != nil {
return nil, err
}
return d, nil
}
func first(n int, list reflect.Value) reflect.Value {
if !list.IsValid() {
return list
}
if list.Kind() == reflect.Interface {
if list.IsNil() {
return list
}
list = list.Elem()
}
if list.Len() < n {
return list
}
return list.Slice(0, n)
}
// markdown is the function provided to templates.
func markdown(data interface{}) (template.HTML, error) {
h, err := markdownToHTML(toString(data))
if err != nil {
return "", err
}
s := strings.TrimSpace(string(h))
if strings.HasPrefix(s, "<p>") && strings.HasSuffix(s, "</p>") && strings.Count(s, "<p>") == 1 {
h = template.HTML(strings.TrimSpace(s[len("<p>") : len(s)-len("</p>")]))
}
return h, nil
}
func (site *siteDir) readfile(name string) (string, error) {
data, err := site.readFile(site.dir, name)
return string(data), err
}
// page returns the page params for the page with a given url u.
// The url may or may not have its leading slash.
func (site *siteDir) page(u string) (Page, error) {
if !path.IsAbs(u) {
u = path.Join(site.dir, u)
}
p, err := site.openPage(strings.Trim(u, "/"))
if err != nil {
return nil, err
}
return p.page, nil
}
// Pages returns the pages found in files matching glob.
func (site *Site) Pages(glob string) ([]Page, error) {
return (&siteDir{site, "."}).pages(glob)
}
// pages returns the page params for pages with urls matching glob.
func (site *siteDir) pages(glob string) ([]Page, error) {
if !path.IsAbs(glob) {
glob = path.Join(site.dir, glob)
}
// TODO(rsc): Add a cache?
_, err := path.Match(glob, "")
if err != nil {
return nil, err
}
glob = strings.Trim(glob, "/")
if glob == "" {
glob = "."
}
matches, err := fs.Glob(site.fs, glob)
if err != nil {
return nil, err
}
var out []Page
for _, file := range matches {
if !strings.HasSuffix(file, ".md") && !strings.HasSuffix(file, ".html") {
f := path.Join(file, "index.md")
if _, err := fs.Stat(site.fs, f); err != nil {
f = path.Join(file, "index.html")
if _, err = fs.Stat(site.fs, f); err != nil {
continue
}
}
file = f
}
p, err := site.openPage(file)
if err != nil {
return nil, fmt.Errorf("%s: %v", file, err)
}
out = append(out, p.page)
}
sort.Slice(out, func(i, j int) bool {
return out[i]["URL"].(string) < out[j]["URL"].(string)
})
return out, nil
}
// file parses the named file (relative to dir) and returns its content as a string.
func (site *siteDir) file(name string) (string, error) {
data, err := site.readFile(site.dir, name)
if err != nil {
return "", err
}
return string(data), nil
}
func raw(s interface{}) template.HTML {
return template.HTML(toString(s))
}
func yamlFn(s string) (interface{}, error) {
var d interface{}
if err := yaml.Unmarshal([]byte(s), &d); err != nil {
return nil, err
}
return d, nil
}