-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
161 lines (138 loc) · 3.16 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
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"sort"
"strings"
"text/template"
"github.com/dinos80152/dinos80152.github.io/pkg"
)
const (
noteMdDir string = "_notes"
noteHTMLDir string = "notes"
noteURL string = "/notes/"
mdIndex string = "readme.md"
htmlIndex string = "index.html"
layout string = "tpls/layout.gtpl"
noteListLayout string = "tpls/notelist.gtpl"
logLayout string = "tpls/log.gtpl"
)
type noteList pkg.NoteList
var nl = noteList{}
func main() {
cleanFolder(noteHTMLDir)
genNotes(noteMdDir, noteHTMLDir)
genUpdateLog()
readmeToIndex("./")
genSiteMap()
changePermission()
}
func cleanFolder(path string) {
files, _ := ioutil.ReadDir(path)
root, _ := os.Getwd()
os.Chdir(path)
defer os.Chdir(root)
for _, f := range files {
if f.IsDir() {
os.RemoveAll(f.Name())
} else {
os.Remove(f.Name())
}
}
}
func genNotes(mdDir, htmlDir string) {
files, _ := ioutil.ReadDir(mdDir)
for _, f := range files {
if f.IsDir() {
nextMdDir := path.Join(mdDir, f.Name())
nextHTMLDir := path.Join(htmlDir, f.Name())
os.Mkdir(nextHTMLDir, 755)
genNotes(nextMdDir, nextHTMLDir)
continue
}
if strings.EqualFold(f.Name(), mdIndex) {
continue
}
genHTML(mdDir, htmlDir, f, "")
}
genNoteList()
}
func genNoteList() {
for _, notes := range nl {
sort.Slice(notes, func(i, j int) bool {
return notes[i].UpdatedAt > notes[j].UpdatedAt
})
}
f, _ := os.Create(path.Join(noteMdDir, "/readme.md"))
defer f.Close()
t, _ := template.ParseFiles(noteListLayout)
t.Execute(f, nl)
}
func genUpdateLog() {
log := make(noteList)
logDate := make([]string, 0)
for _, notes := range nl {
for _, note := range notes {
log[note.UpdatedDate] = append(log[note.UpdatedDate], note)
}
}
for _, notes := range log {
sort.Slice(notes, func(i, j int) bool {
if notes[i].Category == notes[j].Category {
return notes[i].Title < notes[j].Title
}
return notes[i].Category < notes[j].Category
})
}
for date := range log {
logDate = append(logDate, date)
}
sort.Sort(sort.Reverse(sort.StringSlice(logDate)))
f, _ := os.Create("README.md")
defer f.Close()
t, _ := template.ParseFiles(logLayout)
t.Execute(f, struct {
Log noteList
LogDate []string
}{
log,
logDate,
})
}
func readmeToIndex(dir string) {
paths, _ := ioutil.ReadDir(dir)
for _, path := range paths {
if path.IsDir() {
readmeToIndex(path.Name())
}
if strings.EqualFold(path.Name(), mdIndex) {
if dir == noteMdDir {
genHTML(dir, noteHTMLDir, path, htmlIndex)
} else {
genHTML(dir, dir, path, htmlIndex)
}
}
}
}
func genSiteMap() {
us := pkg.NewURLSet(pkg.NoteList(nl))
us.GenSiteMap()
}
func genHTML(sourceDir, outputDir string, fi os.FileInfo, outputBaseName string) {
note := pkg.NewNote(sourceDir, outputDir, fi, outputBaseName)
note.ToHTML()
nl[note.Category] = append(nl[note.Category], note)
}
func changePermission() {
_, err := exec.Command("sh", "-c", `find ./ -type d -exec chmod 755 {} \;`).Output()
if err != nil {
log.Fatal(err)
}
_, err = exec.Command("sh", "-c", `find ./ -type f -exec chmod 644 {} \;`).Output()
if err != nil {
log.Fatal(err)
}
}