-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.go
141 lines (121 loc) · 2.58 KB
/
compile.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 gen
import (
"fmt"
"github.com/sdutsoftlab/softlab-generator/utils"
"html/template"
"os"
"path"
"strconv"
"time"
)
var (
mdHead = `---
title: %s
date: %s
time: %s
author:
categories:
-
tags:
-
-
---`
funcMap = template.FuncMap{
"now": utils.Now,
"unescaped": utils.Unescaped,
"format": utils.Format,
}
data = map[string]interface{}{
"title": conf.Title,
"subtitle": conf.SubTitle,
"description": conf.Description,
"keywords": conf.Keywords,
"author": conf.Author,
"avatar": conf.Avatar,
"contact": conf.Contact,
"github": conf.Github,
"mail": conf.Mail,
}
)
func Compile() {
defer func() {
if r := recover(); r != nil {
log.Errorf("panic recoverd from: %v", r)
}
}()
log.Info("开始编译")
compileArticle()
compileHome()
log.Info("编译完成")
}
func compileHome() {
data["pages"] = GetPages(conf.PageSize)
data["artlist"] = GetHomeArt(1, conf.PageSize)
data["title"] = conf.Title
err := utils.Mkdir(conf.Dist)
if err != nil {
panic("生成目录创建错误")
}
index := path.Join(conf.Dist, "index.html")
file, err := os.Create(index)
if err != nil {
panic(err)
}
t, err := template.New("main.tpl").Funcs(funcMap).ParseFiles(
conf.Theme+"layout/main.tpl",
conf.Theme+"layout/home.tpl")
if err != nil {
panic(err)
}
err = t.Execute(file, data)
if err != nil {
panic(err)
}
}
func compileArticle() {
LoadArticle()
for _, post := range articles {
data["title"] = post.Title
data["article"] = post
filepath := path.Join(conf.Dist, post.Url)
fmt.Println(filepath)
err := utils.Mkdir(filepath)
if err != nil {
panic(err)
}
file := path.Join(filepath, "index.html")
htmlFile, err := os.Create(file)
if err != nil {
panic(err)
}
t, err := template.New("posts.tpl").Funcs(funcMap).ParseFiles(
conf.Theme+"layout/posts.tpl",
conf.Theme+"layout/article.tpl")
if err != nil {
panic(err)
}
err = t.Execute(htmlFile, data)
if err != nil {
panic(err)
}
}
}
func CreateMarkdown(filename string) string {
year, month, _ := time.Now().Date()
dir := path.Join(conf.Markdown, strconv.Itoa(year),
strconv.Itoa(int(month)))
msg, err := utils.CreateFile(dir, filename+".md")
if err != nil {
log.Fatal(msg, err.Error())
}
// 创建成功
date := time.Now().Format("2006-01-02")
now := time.Now().Format("15:04:05")
// 默认以文件名字作为文章标题,可以随后修改
mdHeadStr := fmt.Sprintf(mdHead, filename, date, now)
err = utils.WriteFile(dir, filename+".md", mdHeadStr)
if err != nil {
log.Fatal(err)
}
return msg
}