forked from InkProject/ink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
executable file
·132 lines (125 loc) · 2.91 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
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"html/template"
"io/ioutil"
"os"
"path/filepath"
"strconv"
)
type Data interface{}
type RenderArticle struct {
Article
Next *Article
Prev *Article
}
// Compile html template
func CompileTpl(tplPath string, partialTpl string, name string) template.Template {
// Read template data from file
html, err := ioutil.ReadFile(tplPath)
if err != nil {
Fatal(err.Error())
}
// Append partial template
htmlStr := string(html) + partialTpl
funcMap := template.FuncMap{
"i18n": func(val string) string {
return globalConfig.I18n[val]
},
}
// Generate html content
tpl, err := template.New(name).Funcs(funcMap).Parse(htmlStr)
if err != nil {
Fatal(err.Error())
}
return *tpl
}
// Render html file by data
func RenderPage(tpl template.Template, tplData interface{}, outPath string) {
// Create file
outFile, err := os.Create(outPath)
if err != nil {
Fatal(err.Error())
}
defer func() {
outFile.Close()
}()
defer wg.Done()
// Template render
err = tpl.Execute(outFile, tplData)
if err != nil {
Fatal(err.Error())
}
}
// Generate all article page
func RenderArticles(tpl template.Template, articles Collections) {
defer wg.Done()
articleCount := len(articles)
for i, _ := range articles {
currentArticle := articles[i].(Article)
var renderArticle = RenderArticle{currentArticle, nil, nil}
if i >= 1 {
article := articles[i-1].(Article)
renderArticle.Prev = &article
}
if i <= articleCount-2 {
article := articles[i+1].(Article)
renderArticle.Next = &article
}
outPath := filepath.Join(publicPath, currentArticle.Link)
wg.Add(1)
go RenderPage(tpl, renderArticle, outPath)
}
}
// Generate article list page
func RenderArticleList(rootPath string, articles Collections, tagName string) {
defer wg.Done()
// Create path
pagePath := filepath.Join(publicPath, rootPath)
os.MkdirAll(pagePath, 0777)
// Split page
limit := globalConfig.Site.Limit
total := len(articles)
page := total / limit
rest := total % limit
if rest != 0 {
page++
}
if total < limit {
page = 1
}
for i := 0; i < page; i++ {
var prev = filepath.Join(rootPath, "page"+strconv.Itoa(i)+".html")
var next = filepath.Join(rootPath, "page"+strconv.Itoa(i+2)+".html")
outPath := filepath.Join(pagePath, "index.html")
if i != 0 {
fileName := "page" + strconv.Itoa(i+1) + ".html"
outPath = filepath.Join(pagePath, fileName)
} else {
prev = ""
}
if i == 1 {
prev = filepath.Join(rootPath, "index.html")
}
first := i * limit
count := first + limit
if i == page-1 {
if rest != 0 {
count = first + rest
}
next = ""
}
var data = map[string]interface{}{
"Articles": articles[first:count],
"Site": globalConfig.Site,
"Develop": globalConfig.Develop,
"Page": i + 1,
"Total": page,
"Prev": prev,
"Next": next,
"TagName": tagName,
"TagCount": len(articles),
}
wg.Add(1)
go RenderPage(pageTpl, data, outPath)
}
}