-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompile.go
467 lines (443 loc) · 9.94 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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strings"
"time"
)
var compileCmd = &Command{
UsageLine: "compile config.json",
Short: "compile whole website",
Long: `
compile all markdown file in .pd/posts.
compile markdown file to html file.
`,
}
var (
Theme string
Config Mapper
Tpl *template.Template
Posts AllPost
Categories map[string]AllPost
About template.HTML
Htmls []string
)
type AllPost []Mapper
func (p AllPost) Len() int {
return len(p)
}
func (p AllPost) Less(i, j int) bool {
p1 := p[i]["date"].(string)
p2 := p[j]["date"].(string)
pt1, err := time.Parse("2006-01-02 15:04:05", p1)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
pt2, err := time.Parse("2006-01-02 15:04:05", p2)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
return pt1.After(pt2)
}
func (p AllPost) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func init() {
compileCmd.Run = compileApp
AddCommand(compileCmd)
log.SetFlags(log.Lshortfile)
Categories = make(map[string]AllPost, 0)
Config = make(Mapper)
}
func compileApp(cmd *Command, args []string) {
config_file := filepath.Join(".pd", "config.json")
err := LoadConf(config_file)
if err != nil {
log.Fatal(err)
return
}
fmt.Printf("load config :%s.\n", config_file)
err = LoadTheme()
if err != nil {
log.Fatal(err)
return
}
fmt.Printf("load theme :%s.\n", Theme)
err = LoadPosts()
if err != nil {
log.Fatal(err)
return
}
fmt.Printf("load posts.\n")
for i, v := range Posts {
p, err := CreatePost(v, i)
if err != nil {
log.Fatal(err)
continue
}
Posts[i] = p
if err := WritePostToFile(p); err != nil {
log.Fatal(err)
continue
}
}
content, err := ioutil.ReadFile("about.md")
if err == nil {
About = template.HTML(MarkdownToHtml(string(content)))
} else {
About = template.HTML("<h1>Hello</h1>")
}
for _, v := range Htmls {
if v == "index.html" || v == "post.html" {
continue
}
if err := CreateHtml(v); err != nil {
log.Fatal(err)
}
}
if err := CreateIndex(); err != nil {
log.Fatal(err)
}
CopyStatic()
if err := CreateRss(); err != nil {
log.Fatal(err)
}
fmt.Printf("create file :rss.xml.\n")
if err := CreateAtom(); err != nil {
log.Fatal(err)
}
fmt.Printf("create file :atom.xml.\n")
if err := CreateSitemap(); err != nil {
log.Fatal(err)
}
fmt.Printf("create file :sitemap.xml.\n")
fmt.Printf("compile site done.\n")
}
func LoadConf(config_file string) error {
f, err := os.Open(config_file)
if err != nil {
return err
}
defer f.Close()
err = json.NewDecoder(f).Decode(&Config)
if err != nil {
return err
}
Theme = filepath.Join(".pd", "theme", Config["theme"].(string))
return nil
}
func LoadTheme() error {
var tplfiles []string
err := filepath.Walk(filepath.Join(Theme, "base"), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
filename := filepath.Base(path)
if info.IsDir() || strings.HasPrefix(filename, ".") || !strings.HasSuffix(filename, ".html") {
return nil
}
tplfiles = append(tplfiles, path)
return nil
})
if len(tplfiles) > 0 {
Tpl = template.Must(template.ParseFiles(tplfiles...))
}
dir, err := ioutil.ReadDir(Theme)
if err != nil {
return err
}
for _, f := range dir {
filename := f.Name()
if f.IsDir() || strings.HasPrefix(filename, ".") || !strings.HasSuffix(filename, ".html") {
continue
}
Htmls = append(Htmls, filename)
}
return err
}
func LoadPosts() error {
err := filepath.Walk(filepath.Join(".pd", "posts"), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
filename := filepath.Base(path)
if info.IsDir() || strings.HasPrefix(filename, ".") || !strings.HasSuffix(filename, ".md") {
return nil
}
post, err := LoadPost(path)
if err != nil {
return err
}
post["path"] = path
category := post["category"].(string)
if Categories[category] == nil {
Categories[category] = make(AllPost, 0)
}
Categories[category] = append(Categories[category], post)
Posts = append(Posts, post)
return nil
})
sort.Sort(Posts)
return err
}
func LoadPost(path string) (post Mapper, err error) {
post = make(Mapper, 0)
content, err := ioutil.ReadFile(path)
n := strings.IndexRune(string(content), '}')
if n == -1 {
err = errors.New("error format of post header")
return
}
head := content[:n+1]
err = json.Unmarshal(head, &post)
if err != nil {
return
}
str := string(content[n+1:])
post["content"] = str
post["summary"] = MakeSummary(str)
return
}
func MakeSummary(str string) string {
r := bufio.NewReader(strings.NewReader(str))
summary := ""
readUntil := ""
lines := int(Config["summary_line"].(float64))
for lines > 0 {
line, _ := r.ReadString('\n')
if strings.Contains(line, "![") {
continue
}
summary += line
lines--
if strings.Trim(line, "\r\n\t ") == "```" {
if readUntil == "" {
readUntil = "```"
} else {
readUntil = ""
}
}
if lines == 0 {
var err error
for readUntil != strings.Trim(line, "\r\n\t ") {
line, err = r.ReadString('\n')
summary += line
if err != nil {
break
}
}
}
}
return summary
}
func CreatePost(post Mapper, i int) (Mapper, error) {
var prev, next *Mapper
if i > 0 {
next = &Posts[i-1]
} else {
next = nil
}
if i < len(Posts)-1 {
prev = &Posts[i+1]
} else {
prev = nil
}
content := post["content"].(string)
post["content"] = template.HTML(MarkdownToHtml(content))
summary := post["summary"].(string)
post["summary"] = template.HTML(MarkdownToHtml(summary))
if prev != nil {
post["previous_url"] = (*prev)["permalink"]
post["previous_title"] = (*prev)["title"]
}
if next != nil {
post["next_url"] = (*next)["permalink"]
post["next_title"] = (*next)["title"]
}
return post, nil
}
func WritePostToFile(post Mapper) error {
var buf bytes.Buffer
link := filepath.Join(".", post["permalink"].(string))
err := os.MkdirAll(filepath.Dir(link), os.ModePerm)
if err != nil {
return err
}
var t *template.Template
filename := filepath.Join(Theme, "post.html")
if Tpl != nil {
t, _ = Tpl.Clone()
t = template.Must(t.ParseFiles(filename))
} else {
t = template.Must(template.ParseFiles(filename))
}
err = t.Execute(&buf, Mapper{"categories": Categories, "post": post, "config": Config})
if err != nil {
return err
}
err = ioutil.WriteFile(link, buf.Bytes(), os.ModePerm)
if err == nil {
fmt.Printf("create file %s.\n", link)
}
return err
}
func CreateHtml(html string) error {
var buf bytes.Buffer
var t *template.Template
filename := filepath.Join(Theme, html)
if Tpl != nil {
t, _ = Tpl.Clone()
t = template.Must(t.ParseFiles(filename))
} else {
t = template.Must(template.ParseFiles(filename))
}
err := t.Execute(&buf, Mapper{"categories": Categories, "posts": Posts, "config": Config, "about": About})
if err != nil {
return err
}
err = ioutil.WriteFile(html, buf.Bytes(), os.ModePerm)
if err == nil {
fmt.Printf("create file %s.\n", html)
}
return err
}
func CreateIndex() error {
num_per_page := int(Config["blog_per_page"].(float64))
num_paginat := int(Config["pagination_show_num"].(float64))
half_num_paginat := num_paginat / 2
num := len(Posts)
total_page := num / num_per_page
if num%num_per_page != 0 {
total_page++
}
for i := 1; i <= total_page; i++ {
var buf bytes.Buffer
var t *template.Template
filename := filepath.Join(Theme, "index.html")
if Tpl != nil {
t, _ = Tpl.Clone()
t = template.Must(t.ParseFiles(filename))
} else {
t = template.Must(template.ParseFiles(filename))
}
var pages []Mapper
var s int
if i <= half_num_paginat {
s = 1
} else {
s = i - half_num_paginat
}
if i >= total_page-half_num_paginat && total_page > num_paginat {
s = total_page - num_paginat + 1
}
q := num_paginat
if q > total_page {
q = total_page
}
for p := 0; p < q; p++ {
m := make(Mapper)
m["p"] = p + s
if p+s == 1 {
m["url"] = "index.html"
} else {
m["url"] = fmt.Sprintf("index_%d.html", p+s)
}
pages = append(pages, m)
}
n := (i - 1) * num_per_page
m := i * num_per_page
if m > num {
m = num
}
var outname string
var prevurl, nexturl string
if i == 1 {
outname = "index.html"
prevurl = ""
nexturl = "/index_2.html"
} else {
outname = fmt.Sprintf("index_%d.html", i)
if i == 2 {
prevurl = "/index.html"
nexturl = "/index_3.html"
} else {
prevurl = fmt.Sprintf("/index_%d.html", i-1)
nexturl = fmt.Sprintf("/index_%d.html", i)
}
}
err := t.Execute(&buf, Mapper{"categories": Categories, "posts": Posts[n:m],
"pages": pages, "prevurl": prevurl, "nexturl": nexturl,
"page": i, "total_page": total_page, "config": Config})
if err != nil {
return err
}
err = ioutil.WriteFile(outname, buf.Bytes(), os.ModePerm)
if err != nil {
return err
}
fmt.Printf("create index :%s.\n", outname)
}
return nil
}
func CopyStatic() {
CopyDir(filepath.Join(Theme, "static"), "static")
}
func CopyDir(srcpath, dstpath string) error {
srcinfo, err := os.Stat(srcpath)
if err != nil {
return err
}
err = os.MkdirAll(dstpath, srcinfo.Mode())
if err != nil {
return err
}
dir, _ := os.Open(srcpath)
objs, err := dir.Readdir(-1)
for _, obj := range objs {
srcfile := filepath.Join(srcpath, obj.Name())
dstfile := filepath.Join(dstpath, obj.Name())
if obj.IsDir() {
err = CopyDir(srcfile, dstfile)
if err != nil {
log.Fatal(err)
}
} else {
err = CopyFile(srcfile, dstfile)
if err != nil {
log.Fatal(err)
}
}
}
return err
}
func CopyFile(srcName, dstName string) error {
src, err := os.Open(srcName)
if err != nil {
return err
}
defer src.Close()
dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
defer dst.Close()
_, err = io.Copy(dst, src)
if err == nil {
srcinfo, err := os.Stat(srcName)
if err != nil {
err = os.Chmod(dstName, srcinfo.Mode())
}
fmt.Printf("copy file :%s.\n", dstName)
}
return err
}