-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
153 lines (121 loc) · 2.73 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
package main
import (
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
const tpl = `---
title: "%s"
tags: ["%s"]
categories: ["%s"]
date: "%s"
url: "%s"
toc: true
draft: false
---
<!--more-->
`
type MetaData struct {
Title string
Categories string
Tag string
Date string
URL string
}
const location = "./content/post"
func main() {
// AddHugoHeader(location)
CreateIndex(location)
}
// AddHugoHeader adds a header for each article
func AddHugoHeader(pos string) {
filepath.WalkDir(pos, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
fi, _ := d.Info()
// 过滤非md文件
if !strings.HasSuffix(fi.Name(), ".md") {
return nil
}
title := fi.Name()
title = title[:len(title)-3] // 删除.md
tag := strings.Split(path, "/")[0]
if tag == "go" {
tag = "golang"
}
categories := tag
date := fi.ModTime().Local().Format("2006-01-02T15:04:05+08:00")
day := fi.ModTime().Local().Format("2006-01-02")
url := day + "-" + title + ".html" // 2006-01-02-${title}.html
writeHeader(path, MetaData{
Title: title,
Categories: categories,
Tag: tag,
Date: date,
URL: url})
return nil
})
}
func writeHeader(file string, meta MetaData) {
f, err := os.OpenFile(file, os.O_RDWR, 0644)
if err != nil {
log.Fatal(err)
}
head := fmt.Sprintf(tpl, meta.Title, meta.Tag, meta.Categories, meta.Date, meta.URL)
b, _ := ioutil.ReadAll(f)
all := append([]byte(head), b...)
n, err := f.WriteAt(all, 0)
fmt.Println(n, file, meta, "done")
f.Close()
}
const readmeHeader = `# Blog
support by
* [hugo](https://gohugo.io)
* [utteranc](https://utteranc.es)
* [firebase](https://firebase.google.com)
## Index
`
// CreateIndex creates a index for all articles.
func CreateIndex(pos string) {
f, err := os.OpenFile("README.md", os.O_CREATE|os.O_TRUNC|os.O_APPEND|os.O_RDWR, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
// 写入固定的header
f.Write([]byte(readmeHeader))
filepath.WalkDir(pos, func(path string, d fs.DirEntry, err error) error {
if path == "./content/post" {
return nil
}
if d.IsDir() {
appendIndex(f, path)
}
return nil
})
}
func appendIndex(f *os.File, path string) {
urls := strings.Split(path, "/")
f.Write([]byte("### " + urls[len(urls)-1] + "\n"))
filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
fi, _ := d.Info()
// 过滤非md文件
if !strings.HasSuffix(fi.Name(), ".md") {
return nil
}
title := fi.Name()
title = title[:len(title)-3] // 删除.md
line := fmt.Sprintf("* [%s](%s)\n", title, path)
n, err := f.Write([]byte(line))
fmt.Println(line, n, "done")
return nil
})
}