-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.go
121 lines (111 loc) · 2.75 KB
/
render.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
package render
import (
"html/template"
"os"
"path"
"sort"
"strings"
"github.com/bigpigeon/easy-takeout/backend/logger"
)
type DirSorted []string
type PathFile struct {
Path string
File string
}
func CreateExcludeSorted(list []string) DirSorted {
new_l := []string{}
for _, l := range list {
if strings.HasSuffix(l, "/") {
new_l = append(new_l, l[:len(l)-1])
} else {
new_l = append(new_l, l)
}
}
sort.Strings(new_l)
return new_l
}
func (e DirSorted) search(s string) bool {
start, end := 0, len(e)
for start < end {
half := (end - start) / 2
if e[half] == s {
return true
} else if e[half] > s {
end = half
} else {
start = half + 1
}
}
return false
}
/*
* split file with need rending and not
*/
func SplitRenderFile(root string, keepdir DirSorted) ([]PathFile, []PathFile, error) {
return _splitRenderFile(root, "", keepdir)
}
/*
* Render render html file from source path to target path
* use html/template lib to parse template to the specified data object
* any have ".html" suffix file in source path will be load
* any without "_" prefix and have ".html" suffix file in source path will be render to target path with same path name
* all in keepdirs list path file/dir will be copy to target path
*/
func Render(source, target string, keepdirs []string, data interface{}) error {
pathfiles, keepfiles, err := SplitRenderFile(source, CreateExcludeSorted(keepdirs))
if err != nil {
return err
}
// copy all keep files to target path
for _, f := range keepfiles {
fSource := path.Join(source, f.Path, f.File)
fTarget := path.Join(target, f.Path, f.File)
logger.Debug.Println(fSource, fTarget)
dir, _ := path.Split(fTarget)
err := mkdirIfNotExist(dir)
if err != nil {
return err
}
err = copyFile(fSource, fTarget)
if err != nil {
return err
}
}
// collect *.html ,render html file that without "_" prefix to target path
commonPages := []string{}
renderPages := []string{}
for _, pf := range pathfiles {
if strings.HasSuffix(pf.File, ".html") {
if strings.HasPrefix(pf.File, "_") {
commonPages = append(commonPages, path.Join(source, pf.Path, pf.File))
} else {
renderPages = append(renderPages, path.Join(pf.Path, pf.File))
}
}
}
for _, r := range renderPages {
rSource := path.Join(source, r)
rTarget := path.Join(target, r)
temp, err := template.ParseFiles(append([]string{rSource}, commonPages...)...)
if err != nil {
return err
}
// mkdir target path if not exist
dir, _ := path.Split(rTarget)
err = mkdirIfNotExist(dir)
if err != nil {
return err
}
// write data to target file
output, err := os.Create(rTarget)
defer output.Close()
if err != nil {
return err
}
err = temp.Execute(output, data)
if err != nil {
return err
}
}
return nil
}