-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathmain.go
180 lines (152 loc) · 3.67 KB
/
main.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
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
)
func main() {
// 1. 遍历所有子目录,获取最新的文件复制为index.html
err := updateIndexHtml("docs")
if err != nil {
fmt.Println(err)
return
}
// 2. 生成more.html页面,列出所有html文件
err = generateMoreHtml("docs")
if err != nil {
fmt.Println(err)
return
}
}
// 更新index.html
func updateIndexHtml(root string) error {
var latestFile string
var latestTime int64
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
fileName := info.Name()
if !strings.HasSuffix(fileName, ".html") {
return nil
}
timestamp := extractTimestamp(fileName)
if timestamp > latestTime {
latestTime = timestamp
latestFile = path
}
return nil
})
if err != nil {
return err
}
// 复制最新文件为index.html
copyFile(latestFile, filepath.Join(root, "index.html"))
fmt.Println("update index.html successfully!")
return nil
}
// 从文件名中提取时间戳
func extractTimestamp(name string) int64 {
parts := strings.Split(name, "-")
if len(parts) < 2 {
return 0
}
timestamp, err := strconv.ParseInt(parts[1][:8], 10, 64)
if err != nil {
return 0
}
return timestamp
}
// 复制文件
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}
func generateMoreHtml(docsDir string) error {
// 读取docs目录下的所有文件/目录,除了index.html和more.html
var files []string
err := filepath.Walk(docsDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if info.Name() == "index.html" || info.Name() == "more.html" {
return nil
}
files = append(files, path)
return nil
})
if err != nil {
return err
}
// 对文件路径按月份分类
fileMap := make(map[string][]string)
for _, file := range files {
month := filepath.Base(filepath.Dir(file))
fileMap[month] = append(fileMap[month], file)
}
// 对月份进行排序
months := make([]string, 0, len(fileMap))
for month := range fileMap {
months = append(months, month)
}
sort.Slice(months, func(i, j int) bool {
return months[i] > months[j]
})
if len(months) > 12 {
months = months[:12] // 取最近12个月
}
// 生成more.html内容
rootURL := `https://bigwhite.github.io/gopherdaily`
var content string
content += "<html>\n"
content += "<body>\n"
content += "<h1>GopherDaily归档</h1>\n"
content += "<p>此页面列出GopherDaily最近12个月的所有html文件,以月份分类,从新到旧排序,点击文件超链接查看对应日期的GopherDaily内容。</p>\n"
content += fmt.Sprintf("<p><a href=\"%s\">回到首页</a></p>\n", rootURL)
for _, month := range months {
content += fmt.Sprintf("<h2>%s</h2>\n", month)
files := fileMap[month]
// 对月份下文件进行排序
sort.Slice(files, func(i, j int) bool {
return files[i] > files[j]
})
for _, file := range files {
fileName := filepath.Base(file)
fileLink := fmt.Sprintf("<a href=\"%s\">%s</a>", rootURL+"/"+month+"/"+fileName, fileName)
content += fmt.Sprintf("<p>%s</p>\n", fileLink)
}
}
content += "</body>\n"
content += "</html>"
// 写入more.html文件
err = ioutil.WriteFile(filepath.Join(docsDir, "/more/index.html"), []byte(content), 0644)
if err != nil {
return err
}
fmt.Println("more/index.html generated successfully!")
return nil
}