-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
146 lines (119 loc) · 2.68 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"strings"
"path/filepath"
"path"
"io/ioutil"
"github.com/pkg/errors"
"github.com/unders/tmplgen/html"
"github.com/unders/tmplgen/toml"
)
var from = ""
var to = ""
var dataFile = ""
var indexFile = "index.html"
const usage = `Usage of tmplgen:
tmplgen -from=dir -to=dir files
Example:
tmplgen -from=templates -to=websites/public page/blog/a-very-long-name.html`
func init() {
flag.StringVar(&from, "from", "", "from directory")
flag.StringVar(&to, "to", "", "to directory")
flag.Parse()
if from == "" {
fmt.Println(usage)
os.Exit(1)
}
if to == "" {
fmt.Println(usage)
os.Exit(1)
}
dataFile = filepath.Join(from, "data.toml")
indexFile = filepath.Join(to, indexFile)
}
func main() {
pages := flag.Args()
l := len(pages)
if l == 0 {
fmt.Println(usage)
return
}
p := pages[0]
if l > 1 {
p = dataFile
}
p = path.Clean(p)
data, err := toml.ReadFile(dataFile)
if err != nil {
fmt.Println(err)
os.Exit(2)
}
template := html.NewTemplate(from)
pager := Pager{template: template, data: data, destDir: path.Clean(to)}
if p == "all" {
pager.renderAll()
return
}
if p == dataFile {
pager.renderAll()
return
}
if strings.HasPrefix(p, template.LayoutDir) {
pager.renderAll()
return
}
if strings.HasPrefix(p, template.PartDir) {
pager.renderAll()
return
}
pager.render(p)
}
// Pager creates pages
type Pager struct {
template *html.Template
data *toml.Data
destDir string
}
func (p Pager) renderAll() {
fmt.Println("render all pages")
pages := []string{}
walkparts := func(path string, f os.FileInfo, err error) error {
if err != nil {
return errors.Wrap(err, "page file walk failed")
}
if f.IsDir() {
return nil
}
pages = append(pages, path)
return nil
}
if err := filepath.Walk(p.template.PageDir, walkparts); err != nil {
fmt.Println(err)
return
}
for _, file := range pages {
p.render(file)
}
}
func (p Pager) render(file string) {
filename := strings.TrimPrefix(file, p.template.PageDir+"/")
layout := p.data.Layout(filename)
b, err := p.template.Execute(layout, filename, p.data)
if err != nil {
fmt.Printf("template error: %s [ layout: %s, file: %s ]\n", err, layout, filename)
return
}
destFile := path.Join(p.destDir, filename)
if err := os.MkdirAll(path.Dir(destFile), os.ModePerm); err != nil {
fmt.Printf("could not create dir %s, error: %s\n", path.Dir(destFile), err)
return
}
if err := ioutil.WriteFile(destFile, b, os.ModePerm); err != nil {
fmt.Printf("write %s error: %s [ layout %s, file: %s ]\n", destFile, err, layout, filename)
return
}
fmt.Printf("write file: %s [ layout: %s, file: %s ]\n", destFile, layout, filename)
}