-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerator.go
107 lines (79 loc) · 1.73 KB
/
generator.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
)
func main() {
fs, err := ioutil.ReadDir("csdn/list")
if err != nil {
log.Fatal(err)
}
var buf bytes.Buffer
buf.WriteString(`---
layout: default
---
# blog list
* * *
## 2018
`)
index := 72 // 共72篇博客
year := "2018" // 最新为2018年写成
for _, v := range fs {
if v.IsDir() {
continue
}
f, err := ioutil.ReadFile(fmt.Sprintf("csdn/list/%s", v.Name()))
if err != nil {
log.Fatal(err)
}
for {
n1 := bytes.Index(f, []byte(`title="编辑">`))
if n1 == -1 {
break
}
n2 := bytes.IndexByte(f[n1:], '<')
if n2 == -1 {
break
}
n3 := bytes.Index(f[n1+n2:], []byte(`class="">201`))
if n3 == -1 {
break
}
n4 := bytes.IndexByte(f[n1+n2+n3:], '<')
if n4 == -1 {
break
}
t := f[n1+n2+n3+9 : n1+n2+n3+n4]
if y := string(t[:4]); year != y {
buf.WriteString(fmt.Sprintf("## %s\n\n", y))
year = y
}
if err := ioutil.WriteFile(fmt.Sprintf("blog/%03d.md", index), []byte(fmt.Sprintf(`---
layout: default
---
# %s
_%s_
* * *
content
`, f[n1+15:n1+n2], fmt.Sprintf("%s-%s-%s %s:%s:%s", t[:4], t[7:9], t[12:14], t[18:20], t[21:23], t[24:]))), 0644); err != nil {
log.Fatal(err)
}
buf.WriteString(fmt.Sprintf("[%s](./blog/%03d.html).\n\n", f[n1+15:n1+n2], index))
f = f[n1+n2+n3+n4:]
if l := bytes.Index(f, []byte(`<a href="`)); l != -1 {
if r := bytes.IndexByte(f[l+9:], '"'); r != -1 {
fmt.Printf("\"https://panshiqu.github.io/blog/%03d.html\": \"%s\",\n", index, f[l+9:l+9+r])
}
}
index--
if index == 65 {
index = 64 // 回收站占位
}
}
}
if err := ioutil.WriteFile("index.md", buf.Bytes()[:buf.Len()-1], 0644); err != nil {
log.Fatal(err)
}
}