/
template.go
51 lines (44 loc) · 1.19 KB
/
template.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
package main
import (
"fmt"
"io"
"os"
"reflect"
"strings"
"text/template"
"github.com/gosimple/slug"
)
// tmplFuncs contains some utility functions for use in templates.
var tmplFuncs = template.FuncMap{
"slugify": slug.Make,
"link_to_title": func(title string) string { return fmt.Sprintf("%v.html", slug.Make(title)) },
"link": func(slug string) string { return fmt.Sprintf("%v.html", slug) },
"remove_ext": RemoveExt,
"limit": func(length int, data interface{}) interface{} {
v := reflect.ValueOf(data)
if v.Len() < length {
return v
}
return v.Slice(0, length).Interface()
},
}
// loadTemplate conditionally parses a template from either def or
// path. If path is empty, def is considered to be the source and is
// parsed, otherwise the file at path is opened and the contents are
// parsed.
func loadTemplate(tmpl *template.Template, def, path string) (*template.Template, error) {
if path == "" {
return tmpl.Parse(def)
}
file, err := os.Open(path)
if err != nil {
return tmpl, err
}
defer file.Close()
var sb strings.Builder
_, err = io.Copy(&sb, file)
if err != nil {
return tmpl, fmt.Errorf("copy: %w", err)
}
return tmpl.Parse(sb.String())
}