forked from go101korea/go101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
234 lines (196 loc) · 5.22 KB
/
gen.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
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
const GeneratedFolderName = "generated"
func genStaticFiles(rootURL string) {
log.SetFlags(log.Lshortfile)
wd, err := os.Getwd()
if err != nil {
log.Fatalln("Can't get current path:", err)
}
fullPath := func(relPath ...string) string {
return filepath.Join(append([]string{wd}, relPath...)...)
}
_, err = os.Stat(fullPath("web", "static", "go101"))
if err != nil {
if os.IsNotExist(err) { //errors.Is(err, os.ErrNotExist) {
log.Fatal("File web/static/go101 not found. Not run in go101 folder?")
}
log.Fatal(err)
}
// md -> html
md2htmls := func(group string) {
dir := fullPath("pages", group)
outputs, err := runShellCommand(time.Minute/2, dir, "ebooktool", "-md2htmls")
if err != nil {
log.Fatalf("ebooktool failed to execute in directory: %s.\n%s", dir, outputs)
}
}
// load from http server
loadFile := func(uri string) []byte {
fullURL := rootURL + uri
res, err := http.Get(fullURL)
if err != nil {
log.Fatalf("Load file %s error: %s", uri, err)
}
content, err := ioutil.ReadAll(res.Body)
log.Println(len(content), fullURL)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
return content
}
// read from OS file system
readFile := func(path string) []byte {
data, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("Read file %s error: %s", path, err)
}
return data
}
readFolder := func(path string) (filenames, subfolders []string) {
fds, err := ioutil.ReadDir(path)
if err != nil {
log.Fatalf("Read folder %s error: %s", path, err)
}
subfolders, filenames = make([]string, 0, len(fds)), make([]string, 0, len(fds))
for _, fd := range fds {
// ignore links, ...
if fd.IsDir() {
subfolders = append(subfolders, filepath.Join(path, fd.Name()))
continue
}
filenames = append(filenames, filepath.Join(path, fd.Name()))
}
return
}
var readFolderRecursively func(path string) (filenames []string)
readFolderRecursively = func(path string) (filenames []string) {
type Folder struct {
Path string
Next *Folder
}
var head, tail *Folder
regSubfolders := func(folders []string) {
if len(folders) == 0 {
return
}
start := 0
if head == nil {
head = &Folder{
Path: folders[0],
}
tail = head
start = 1
}
for i := start; i < len(folders); i++ {
fldr := &Folder{
Path: folders[i],
}
tail.Next = fldr
tail = fldr
}
}
var subfolders []string
filenames, subfolders = readFolder(path)
regSubfolders(subfolders)
for head != nil {
files, subfolders := readFolder(head.Path)
head = head.Next
regSubfolders(subfolders)
filenames = append(filenames, files...)
}
return
}
// collect ...
files := make(map[string][]byte, 128)
files["index.html"] = loadFile("")
{
dir := fullPath("web", "static")
filenames := readFolderRecursively(dir)
for _, f := range filenames {
name, err := filepath.Rel(dir, f)
if err != nil {
log.Fatalf("filepath.Rel(%s, %s) error: %s", dir, f, err)
}
files["static/"+name] = readFile(f)
}
}
collectPageGroupFiles := func(group, urlPrefix string, collectRes bool) {
if collectRes {
dir := fullPath("pages", group, "res")
filenames, _ := readFolder(dir)
for _, f := range filenames {
if strings.HasSuffix(f, ".png") || strings.HasSuffix(f, ".jpg") {
name, err := filepath.Rel(dir, f)
if err != nil {
log.Fatalf("filepath.Rel(%s, %s) error: %s", dir, f, err)
}
files[urlPrefix+"res/"+name] = readFile(f)
}
}
}
md2htmls(group)
{
dir := fullPath("pages", group)
filenames, _ := readFolder(dir)
for _, f := range filenames {
if strings.HasSuffix(f, ".html") {
name, err := filepath.Rel(dir, f)
if err != nil {
log.Fatalf("filepath.Rel(%s, %s) error: %s", dir, f, err)
}
files[urlPrefix+name] = loadFile(urlPrefix + name)
}
}
}
}
{
infos, err := ioutil.ReadDir(fullPath("pages"))
if err != nil {
panic("collect page groups error: " + err.Error())
}
for _, e := range infos {
if e.IsDir() {
group := e.Name()
var urlPrefix string
if group == "fundamentals" {
// For history reason, fundamentals pages use "/article/xxx" URLs.
urlPrefix = "article/"
} else if group != "website" {
urlPrefix = group + "/"
}
var collectRes bool
if _, err := os.Stat(fullPath("pages", group, "res")); err == nil {
collectRes = true
}
collectPageGroupFiles(group, urlPrefix, collectRes)
}
}
}
// write ...
err = os.RemoveAll(fullPath(GeneratedFolderName))
if err != nil {
log.Fatalf("Remove folder %s error: %s", GeneratedFolderName, err)
}
for name, data := range files {
fullFilename := fullPath(GeneratedFolderName, name)
fullFilename = strings.Replace(fullFilename, "/", string(filepath.Separator), -1)
fullFilename = strings.Replace(fullFilename, "\\", string(filepath.Separator), -1)
if err := os.MkdirAll(filepath.Dir(fullFilename), 0700); err != nil {
log.Fatalln("Mkdir error:", err)
}
if err := ioutil.WriteFile(fullFilename, data, 0644); err != nil {
log.Fatalln("Write file error:", err)
}
log.Printf("Generated %s (size: %d).", name, len(data))
}
}