forked from coderzh/gohugo.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatedoc.go
306 lines (256 loc) · 6.44 KB
/
updatedoc.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main
import (
"bytes"
"errors"
"fmt"
"github.com/spf13/cast"
"github.com/spf13/hugo/hugolib"
"github.com/spf13/hugo/parser"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
const OriginHugoDir string = "../originhugo"
const OriginDocDir string = OriginHugoDir + "/docs/content"
const TargetDocDir string = "content/doc"
const OriginImgDir string = OriginHugoDir + "/docs/static/img"
const TargetImgDir string = "static/img"
const TargetShowcaseDir string = "content/showcase"
func executeCommands(cmdList []string) error {
for _, cmdString := range cmdList {
fmt.Println("===>", cmdString)
cmdArgs := strings.Split(cmdString, " ")
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
log.Fatal(cmdString, out.String(), err)
return err
}
fmt.Println(out.String())
}
return nil
}
func updateOriginHugoRepo() error {
dir, _ := os.Getwd()
// clone if not exist
if _, err := os.Stat(OriginHugoDir); os.IsNotExist(err) {
if cmdErr := executeCommands([]string{
"git clone https://github.com/spf13/hugo.git " + OriginHugoDir,
}); cmdErr != nil {
return err
}
fmt.Println("Cloned!")
}
os.Chdir(OriginHugoDir)
if err := executeCommands([]string{
"git checkout v0.15.docs",
"git pull --rebase origin v0.15.docs",
}); err != nil {
return err
}
os.Chdir(dir)
return nil
}
func convertContentText(docSubDirs []string, content string) string {
type ReplaceItem struct {
search string
replace string
}
replaceList := []ReplaceItem{}
for _, docSubDir := range docSubDirs {
replaceList = append(replaceList, ReplaceItem{`(["\(\s])(/?)(` + docSubDir + `)/(\w+)`, "${1}${2}doc/${3}/${4}"})
}
for _, replaceItem := range replaceList {
re := regexp.MustCompile(replaceItem.search)
content = re.ReplaceAllString(content, replaceItem.replace)
}
return content
}
func isFileContentConverted(path string) bool {
if _, err := os.Stat(path); err == nil {
if contentCN, err := ioutil.ReadFile(path); err == nil {
return strings.Contains(string(contentCN), "translated: true")
}
}
return false
}
func saveMarkdownFile(metadata interface{}, content string, path string) error {
dir, filename := filepath.Split(path)
page, err := hugolib.NewPage(filename)
if err != nil {
return err
}
page.SetDir(dir)
page.SetSourceContent([]byte(content))
page.SetSourceMetaData(metadata, parser.FormatToLeadRune("yaml"))
page.SaveSourceAs(path)
return nil
}
func convertMetadata(path string, metadata map[string]interface{}) (map[string]interface{}, error) {
keys := []string{"url", "next", "prev"}
for _, key := range keys {
if url, ok := metadata[key]; ok {
metadata[key] = filepath.Join("/doc", url.(string))
}
}
return metadata, nil
}
func getMetadata(path string) (map[string]interface{}, string, error) {
contentBytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, "", err
}
psr, err := parser.ReadFrom(bytes.NewReader(contentBytes))
if err != nil {
return nil, "", err
}
m, err := psr.Metadata()
if err != nil {
return nil, "", err
}
metadata, err := cast.ToStringMapE(m)
if err != nil {
return nil, "", err
}
return metadata, string(psr.Content()), nil
}
func convertFileContent(docSubDirs []string, path string, pathEN string, pathCN string) error {
metadata, content, err := getMetadata(path)
if err != nil {
return err
}
dirname := filepath.Base(filepath.Dir(path))
newMetadata, err := convertMetadata(path, metadata)
if err != nil {
return err
}
convertedContent := convertContentText(docSubDirs, content)
if dirname == "commands" {
newMetadata["doc"] = []string{"commands_en"}
}
if err := saveMarkdownFile(newMetadata, convertedContent, pathEN); err != nil {
return err
}
if !isFileContentConverted(pathCN) {
if dirname == "commands" {
newMetadata["doc"] = []string{"commands"}
}
if err := saveMarkdownFile(newMetadata, convertedContent, pathCN); err != nil {
return err
}
}
return nil
}
func copyFile(source string, dest string) error {
sf, err := os.Open(source)
if err != nil {
return err
}
defer sf.Close()
df, err := os.Create(dest)
if err != nil {
return err
}
defer df.Close()
_, err = io.Copy(df, sf)
if err == nil {
si, err := os.Stat(source)
if err != nil {
err = os.Chmod(dest, si.Mode())
}
}
return nil
}
func copyDir(source string, dest string) error {
fi, err := os.Stat(source)
if err != nil {
return err
}
if !fi.IsDir() {
return errors.New(source + " is not a directory")
}
err = os.MkdirAll(dest, fi.Mode())
if err != nil {
return err
}
entries, err := ioutil.ReadDir(source)
for _, entry := range entries {
sfp := filepath.Join(source, entry.Name())
dfp := filepath.Join(dest, entry.Name())
if entry.IsDir() {
err = copyDir(sfp, dfp)
if err != nil {
fmt.Println(err)
}
} else {
err = copyFile(sfp, dfp)
if err != nil {
fmt.Println(err)
}
}
}
return nil
}
func onDocContentDirWalk(docSubDirs []string, path string, fi os.FileInfo, err error) error {
if fi.IsDir() {
return nil
}
rel, err := filepath.Rel(OriginDocDir, path)
if err != nil {
return err
}
dir, file := filepath.Split(rel)
dirname := filepath.Base(dir)
ext := filepath.Ext(file)
fileEn := file[:len(file)-len(ext)] + "_en"
if dirname == "showcase" {
if err := copyDir(OriginImgDir, TargetImgDir); err != nil {
return err
}
return copyFile(path, filepath.Join(TargetShowcaseDir, file))
} else {
pathEN := filepath.Join(TargetDocDir, dir, fileEn+ext)
pathCN := filepath.Join(TargetDocDir, rel)
fmt.Println(pathEN)
parentDir := filepath.Dir(pathCN)
if _, err := os.Stat(parentDir); os.IsNotExist(err) {
os.MkdirAll(parentDir, 0777)
}
return convertFileContent(docSubDirs, path, pathEN, pathCN)
}
}
func getDocSubDirs() ([]string, error) {
dirs, err := ioutil.ReadDir(OriginDocDir)
subDirs := make([]string, len(dirs))
if err != nil {
return subDirs, err
}
for i, dirInfo := range dirs {
subDirs[i] = dirInfo.Name()
}
return subDirs, nil
}
func convertAllDocs() error {
docSubDirs, err := getDocSubDirs()
if err != nil {
return err
}
callback := func(path string, fi os.FileInfo, err error) error {
return onDocContentDirWalk(docSubDirs, path, fi, err)
}
return filepath.Walk(filepath.Join(OriginDocDir), callback)
}
func main() {
if err := updateOriginHugoRepo(); err != nil {
fmt.Println(err)
}
if err := convertAllDocs(); err != nil {
fmt.Println(err)
}
}