This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
/
layout.go
141 lines (116 loc) · 3.45 KB
/
layout.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
// Package layout transforms content by applying Go templates to the content
// and metadata of HTML files. This plugin can be easily used with the
// "frontmatter" and "markdown" plugins to generate easy to maintain
// content-driven websites that are completely decoupled from layout details.
package layout
import (
"bytes"
"html/template"
"sync"
"github.com/FooSoft/goldsmith"
"github.com/FooSoft/goldsmith-components/filters/wildcard"
)
// Layout chainable context.
type Layout struct {
layoutKey string
contentKey string
defaultLayout *string
helpers template.FuncMap
inputFiles []*goldsmith.File
templateFiles []*goldsmith.File
mutex sync.Mutex
template *template.Template
}
// New creates a new instance of the Layout plugin.
func New() *Layout {
return &Layout{
layoutKey: "Layout",
contentKey: "Content",
helpers: nil,
}
}
// LayoutKey sets the metadata key used to access the layout identifier (default: "Layout").
func (self *Layout) LayoutKey(key string) *Layout {
self.layoutKey = key
return self
}
// DefaultLayout sets the name of the layout to use if none is specified.
func (self *Layout) DefaultLayout(name string) *Layout {
self.defaultLayout = &name
return self
}
// ContentKey sets the metadata key used to access the source content (default: "Content").
func (self *Layout) ContentKey(key string) *Layout {
self.contentKey = key
return self
}
// Helpers sets the function map used to lookup template helper functions.
func (self *Layout) Helpers(helpers template.FuncMap) *Layout {
self.helpers = helpers
return self
}
func (*Layout) Name() string {
return "layout"
}
func (self *Layout) Initialize(context *goldsmith.Context) error {
self.template = template.New("").Funcs(self.helpers)
context.Filter(wildcard.New("**/*.html", "**/*.htm", "**/*.tmpl", "**/*.gohtml"))
return nil
}
func (self *Layout) Process(context *goldsmith.Context, inputFile *goldsmith.File) error {
self.mutex.Lock()
defer self.mutex.Unlock()
switch inputFile.Ext() {
case ".html", ".htm":
if _, ok := self.getFileLayout(inputFile); ok {
var buff bytes.Buffer
if _, err := inputFile.WriteTo(&buff); err != nil {
return err
}
inputFile.SetProp(self.contentKey, template.HTML(buff.Bytes()))
self.inputFiles = append(self.inputFiles, inputFile)
} else {
context.DispatchFile(inputFile)
}
case ".tmpl", ".gohtml":
self.templateFiles = append(self.templateFiles, inputFile)
}
return nil
}
func (self *Layout) Finalize(context *goldsmith.Context) error {
for _, templateFile := range self.templateFiles {
var buff bytes.Buffer
if _, err := templateFile.WriteTo(&buff); err != nil {
return err
}
if _, err := self.template.Parse(string(buff.Bytes())); err != nil {
return err
}
}
for _, inputFile := range self.inputFiles {
if name, ok := self.getFileLayout(inputFile); ok {
var buff bytes.Buffer
if err := self.template.ExecuteTemplate(&buff, name, inputFile); err != nil {
return err
}
outputFile, err := context.CreateFileFromReader(inputFile.Path(), &buff)
if err != nil {
return err
}
outputFile.CopyProps(inputFile)
context.DispatchFile(outputFile)
} else {
context.DispatchFile(inputFile)
}
}
return nil
}
func (self *Layout) getFileLayout(file *goldsmith.File) (string, bool) {
if name, ok := file.Props()[self.layoutKey].(string); ok {
return name, true
}
if self.defaultLayout != nil {
return *self.defaultLayout, true
}
return "", false
}