-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
405 lines (353 loc) · 12 KB
/
build.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//just主要入口函数,负责编译日志
package justExpress
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
var siteInfo SiteInfo //全局变量:站点信息
func Build(siteDirPath string, onlyRebuildHtml bool) {
var logList LogList //全部日志列表
var buildedLogList map[string]int64 //已生成的日志列表
var updatedLogList []string //有修改变动的日志列表
//路径参数配置列表
var compliedDirPath = siteDirPath + "\\complied"
var themeDirPath = compliedDirPath + "\\style"
var postDirPath = compliedDirPath + "\\posts"
var archiveDirPath = compliedDirPath + "\\archives"
var tagDirPath = compliedDirPath + "\\tags"
// var indexDirPath = compliedDirPath
siteCfg := Configure(compliedDirPath + "\\setting")
themeCfg := Configure(themeDirPath + "\\meta")
siteInfo.Site = siteCfg.GetStr("site")
siteInfo.Domain = siteCfg.GetStr("domain")
siteInfo.Author = siteCfg.GetStr("author")
siteInfo.PageSize = siteCfg.GetInt("pageSize")
siteInfo.Categorys = GetCategorys(siteCfg.GetArray("categorys"))
siteInfo.Tags = GetTags(siteCfg.GetArray("tags"))
siteInfo.Links = GetLinks(siteCfg.GetArray("links"))
siteInfo.Socials = GetSocials(siteCfg.GetArray("socials"))
siteInfo.ThemeName = themeCfg.GetStr("ThemeName")
siteInfo.ImgWidth = themeCfg.GetInt("ImgWidth")
siteInfo.BigImgWidth = themeCfg.GetInt("BigImgWidth")
siteInfo.SitePath = siteDirPath
buildedLogList = getBuildedLogList(postDirPath)
//解析日志、统计站点信息============================
//解析log数据
logDirList, _ := filepath.Glob(siteDirPath + "\\*")
os.Mkdir(postDirPath, os.ModePerm)
for k := range logDirList {
if filepath.Base(logDirList[k]) == "complied" {
continue
}
logInfo := Decode_log(logDirList[k], siteInfo)
if logList.Contain(logInfo.Permalink) {
log.Fatal("《" + logInfo.Title + "》文章转拼音后跟其他文章冲突,请指定alias别名")
}
logList = append(logList, logInfo)
}
//检查变动
for k := range logList {
//如果没有创建过 或者 如果有更新
_, ok := buildedLogList[logList[k].Permalink]
if !ok || int64(logList[k].LastModTime) > buildedLogList[logList[k].Permalink] || onlyRebuildHtml {
updatedLogList = append(updatedLogList, logList[k].Title)
}
}
// deletedLogList := buildedLogList
//检查是否有删除的日志,获取被删除日志列表
for _, logInfo := range logList {
delete(buildedLogList, logInfo.Permalink)
}
var deletedLogList []map[string]string
if len(buildedLogList) > 0 {
fi, _ := ioutil.ReadFile(compliedDirPath + "\\loglist.json")
var lastLogList []LogInfo
json.Unmarshal(fi, &lastLogList)
for permalink := range buildedLogList {
for k := range lastLogList {
if lastLogList[k].Permalink == permalink {
deletedLogList = append(deletedLogList, lastLogList[k].MetaData)
break
}
}
}
/* if err != nil {
fmt.Println("error in translating,", err.Error())
return
}*/
/* for buildedLogPermalink, _ := range buildedLogList {
fi, err := ioutil.ReadFile(postDirPath + "\\" + buildedLogPermalink + "\\article.md")
if err != nil {
fi, err = ioutil.ReadFile(postDirPath + "\\" + buildedLogPermalink + "\\meta")
}
metaData, _ := decode_meta(string(fi))
deletedLogList = append(deletedLogList, metaData)
}*/
}
//同步主题
SyncTheme(themeDirPath, siteInfo.ThemeName)
//若无改动则直接返回
if len(updatedLogList) == 0 && len(deletedLogList) == 0 {
//清理生成日志目录(删除未预料到的文件或者已经删除、改名的日志)
// cleanDestPostDir(postDirPath, oldLogList)
//复制主题
return
}
//日志列表排序
logList = LogSort(logList)
//=======================统计===========================
//统计tag数据
for k := range siteInfo.Tags {
for kk := range logList {
if strings.Contains(logList[kk].MetaData["tag"], siteInfo.Tags[k].Name) {
siteInfo.Tags[k].Count += 1
}
}
}
//统计category数据
for k := range siteInfo.Categorys {
//统计每个分类底下的日志个数
for kk := range logList {
if strings.Contains(logList[kk].MetaData["category"], siteInfo.Categorys[k].Name) || logList[kk].Type == siteInfo.Categorys[k].Alias {
siteInfo.Categorys[k].Count += 1
}
}
}
//统计archive数据
for _, log := range logList {
year_month := log.Date.Format("2006-1")
length := len(siteInfo.Archives)
if length == 0 || siteInfo.Archives[length-1].YearMonth != year_month {
archive := Archive{YearMonth: year_month, Count: 1}
siteInfo.Archives = append(siteInfo.Archives, archive)
} else {
siteInfo.Archives[length-1].Count += 1
}
}
//生成全局引用模版
siteInfo.GlobalTpl = make(map[string]string)
tplList, _ := filepath.Glob(themeDirPath + "\\*")
for _, tplFilePath := range tplList {
fileName := filepath.Base(tplFilePath)
if strings.HasPrefix(fileName, "_") && strings.HasSuffix(fileName, ".html") {
key := strings.TrimPrefix(strings.TrimSuffix(fileName, ".html"), "_")
template_byte, err := ioutil.ReadFile(tplFilePath)
if err != nil {
log.Fatal(tplFilePath + "模版文件读取失败,请检查该模版是否存在?")
}
siteInfo.GlobalTpl[key] = string(renderTpl(siteInfo, string(template_byte), strings.TrimSuffix(filepath.Base(tplFilePath), filepath.Ext(tplFilePath))))
}
}
//========================生成==============================
//日志生成
last := len(logList) - 1
for k, logInfo := range logList {
if In_array(logInfo.Title, updatedLogList) {
var logPage = LogPage{LogInfo: logList[k], SiteInfo: siteInfo, RelPath: "../../"}
if (k - 1) < 0 {
logPage.PrevLog = logList[last]
} else {
logPage.PrevLog = logList[k-1]
}
if (k + 1) > last {
logPage.NextLog = logList[0]
} else {
logPage.NextLog = logList[k+1]
}
Build_log(logPage, themeDirPath, postDirPath, onlyRebuildHtml)
} else {
//TODO
update_log(postDirPath+"\\"+logInfo.Permalink+"\\index.html", "../../")
}
}
//总索引生成============================
// os.Mkdir(indexDirPath, os.ModePerm)
indexPage := IndexPage{Category: Category{Name: "首页", Alias: "index"}, SiteInfo: siteInfo, PageSize: siteInfo.PageSize, LogList: logList, RelPath: "./"}
// if len(updatedLogList) > 0 || !Exist(compliedDirPath+"\\index.html") {
Build_index(indexPage, themeDirPath, compliedDirPath)
// }
//按分类生成索引
for _, category := range siteInfo.Categorys {
haveUpdated := false
_logList := LogList{}
categoryDirPath := compliedDirPath + "\\" + category.Alias
if onlyRebuildHtml {
haveUpdated = true
}
if !haveUpdated && len(deletedLogList) > 0 {
for _, logMeta := range deletedLogList {
if strings.Contains(logMeta["category"], category.Name) {
haveUpdated = true
break
}
}
}
//判断该分类下的日志是否有变动(添加日志或修改日志)
if category.Count == 0 {
if !haveUpdated {
if !Exist(categoryDirPath + "\\index.html") {
haveUpdated = true
}
}
} else if category.Count > 0 {
//剔出该分类下的日志列表
for _, logInfo := range logList {
if strings.Contains(logInfo.MetaData["category"], category.Name) || logInfo.Type == category.Alias {
_logList = append(_logList, logInfo)
}
}
if !haveUpdated {
for _, logInfo := range _logList {
if In_array(logInfo.Title, updatedLogList) /* || !Exist(categoryDirPath) */ {
haveUpdated = true
break
}
}
}
} else {
continue
}
indexPage.LogList = _logList
indexPage.Category = category
indexPage.RelPath = "../"
if haveUpdated {
Remkdir(categoryDirPath)
Build_index(indexPage, themeDirPath, categoryDirPath)
} else {
//TODO
update_index(indexPage, categoryDirPath)
}
}
//标签页生成============================
os.Mkdir(tagDirPath, os.ModePerm)
tagPage := TagPage{SiteInfo: siteInfo, RelPath: "../"}
//按标签生成索引
for _, tag := range siteInfo.Tags {
haveUpdated := false
_logList := LogList{}
tagPagePath := tagDirPath + "\\" + tag.Alias + ".html"
if onlyRebuildHtml {
haveUpdated = true
}
if !haveUpdated && len(deletedLogList) > 0 {
for _, logMeta := range deletedLogList {
if strings.Contains(logMeta["tag"], tag.Name) {
haveUpdated = true
break
}
}
}
//如果没有日志关联到该分类下
if tag.Count == 0 {
if !haveUpdated {
if !Exist(tagPagePath) {
haveUpdated = true
}
}
} else if tag.Count > 0 {
//选出关联至该标签的日志列表
for _, logInfo := range logList {
//如果关联到该标签并且有修改或是新创建日志
if strings.Contains(logInfo.MetaData["tag"], tag.Name) {
_logList = append(_logList, logInfo)
}
}
if !haveUpdated {
//判断该分类下的日志是否有变动(添加日志或修改日志)
for _, logInfo := range _logList {
if In_array(logInfo.Title, updatedLogList) /* || !Exist(tagPagePath)*/ {
haveUpdated = true
break
}
}
}
} else {
continue
}
tagPage.LogList = _logList
tagPage.Tag = tag
if haveUpdated {
Build_tagpage(tagPage, themeDirPath, tagDirPath)
} else {
//TODO
update_tag(tagDirPath+"\\"+tag.Alias+".html", "../")
}
}
//归档生成============================
os.Mkdir(archiveDirPath, os.ModePerm)
archivePage := ArchivePage{SiteInfo: siteInfo, RelPath: "../"}
archives := []Archive{}
for _, log := range logList {
year_month := log.Date.Format("2006-1")
length := len(archives)
if length == 0 || siteInfo.Archives[length-1].YearMonth != year_month {
var logList LogList
logList = append(logList, log)
archive := Archive{YearMonth: year_month, LogList: logList}
archives = append(archives, archive)
} else {
archives[length-1].LogList = append(archives[length-1].LogList, log)
}
}
// if len(archives) > 0 {
archivePage.Archives = archives
Build_archive(archivePage, themeDirPath, archiveDirPath)
// }
//清理生成日志目录(删除未预料到的文件或者已经删除、改名的日志)
cleanDestPostDir(postDirPath, buildedLogList)
build_loglistdata(compliedDirPath, logList)
}
func getBuildedLogList(postDirPath string) map[string]int64 {
oldLogList := make(map[string]int64)
dirList, _ := filepath.Glob(postDirPath + "\\*")
for _, v := range dirList {
fi, err := os.Stat(v)
if err != nil {
log.Println("读取" + v + "文件出现未预料的问题")
continue
} else {
oldLogList[filepath.Base(v)] = GetCreationTime(fi)
}
}
return oldLogList
}
func cleanDestPostDir(postDirPath string, deletedLogList map[string]int64) {
for k := range deletedLogList {
_postDirPath := postDirPath + "\\" + k
err := os.RemoveAll(_postDirPath)
if err != nil {
panic(err)
log.Println("清理" + ConvertPath(_postDirPath) + "目录出现未预料到的问题")
} else {
log.Println(ConvertPath(_postDirPath) + "目录清理成功")
}
}
}
//同步主题
func SyncTheme(siteThemeDirPath string, themeName string) {
if !Exist(siteThemeDirPath) {
log.Fatal("模版文件缺失!可能是误删除!请使用tool添加模版文件或者直接手动复制。")
} else /*if Exist(siteThemeDirPath + "\\meta")*/ {
var localThemeList []string
fileList, _ := filepath.Glob(".\\themes\\*")
for k := range fileList {
if len(themeName) > 0 {
localThemeList = append(localThemeList, filepath.Base(fileList[k]))
}
}
if !In_array(themeName, localThemeList) {
CopyDir(siteThemeDirPath, ".\\themes\\"+themeName)
}
} /* else {
log.Fatal("站点模板文件包不完整,缺失描述元数据文件!")
}*/
}
func ConvertPath(path string) string {
path = strings.TrimPrefix(path, siteInfo.SitePath)
path = siteInfo.Site + path
return path
}