-
Notifications
You must be signed in to change notification settings - Fork 357
/
main.go
160 lines (137 loc) · 3.51 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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
)
var (
_ = fmt.Printf
dir = `D:\guotie\code\oauth2\RFC6749.zh-cn\`
LF = []byte("\r\n")
)
type ConvFile struct {
ConvName string
FileName string
}
type ConvFiles []ConvFile
func (cf ConvFiles) Len() int { return len(cf) }
func (cf ConvFiles) Swap(i, j int) { cf[i], cf[j] = cf[j], cf[i] }
func (cf ConvFiles) Less(i, j int) bool { return cf[i].ConvName < cf[j].ConvName }
// 把文件名转换为正确的排序格式
// 例如按照普通字母排序的话, 结果是这样的: 10.1 10.10 10.11 10.12 10.2 10.3
// 这个函数把10.1 10.10 10.11 10.12 10.2 10.3先转换为010.001 010.010 010.011 010.012 010.002 010.003
// 然后再按字母排序, 得到正确的顺序
func conv(files []string) []ConvFile {
var cf []ConvFile = make([]ConvFile, len(files))
for idx, file := range files {
segs := strings.Split(strings.ToLower(file), ".md")
if len(segs) != 2 {
panic(fmt.Sprintf("file name format error: %s!\n", file))
}
var conv string
nums := strings.Split(segs[0], ".")
for _, num := range nums {
no, err := strconv.Atoi(num)
if err != nil {
panic(fmt.Sprintf("file name %s format error: chapter is not number\n", segs[0]))
}
conv += fmt.Sprintf("%03d.", no)
}
cf[idx] = ConvFile{conv, file}
}
return cf
}
func sortFiles(files []string) []string {
var (
conv_files []ConvFile
res []string = make([]string, len(files))
)
conv_files = conv(files)
sort.Sort(ConvFiles(conv_files))
for i, f := range conv_files {
res[i] = f.FileName
}
return res
}
func catSectionFiles(buf *bytes.Buffer, dir string) error {
var files []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("error found: path: %s error: %s\n", path, err.Error())
return err
}
if info.IsDir() && path != dir {
return filepath.SkipDir
}
name := strings.ToLower(info.Name())
if strings.HasSuffix(name, ".md") {
files = append(files, info.Name())
}
return nil
})
if err != nil && err != filepath.SkipDir {
panic(err)
}
//fmt.Printf("Section Dir: %s files: %v\n", dir, files)
// 文件排序
files = sortFiles(files)
//fmt.Printf("Section Dir: %s sorted files: %v\n", dir, files)
// 逐个读出文件的内容, 合并
for _, fn := range files {
file := path.Join(dir, fn)
content, err := ioutil.ReadFile(file)
if err != nil {
panic(fmt.Sprintf("read file %s failed: %s\n", file, err.Error()))
}
_, err = buf.Write(content)
if err != nil {
panic("write file content to buffer failed: " + err.Error())
}
_, err = buf.Write(LF)
if err != nil {
panic("write LF to buffer failed: " + err.Error())
}
}
return nil
}
func catSectionDir(dir string) {
var (
contents []byte
buf *bytes.Buffer
sections []string
)
buf = bytes.NewBuffer(contents)
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("error found: path: %s error: %s\n", path, err.Error())
return err
}
if info.IsDir() && path != dir {
if strings.HasPrefix(info.Name(), "Section") {
sections = append(sections, info.Name())
}
return filepath.SkipDir
}
return nil
})
if err != nil {
panic(err)
}
//fmt.Println(sections)
for _, sec := range sections {
catSectionFiles(buf, dir+sec)
}
err = ioutil.WriteFile(path.Join(dir, "all-in-one.md"), buf.Bytes(), os.ModePerm)
if err != nil {
panic(err)
}
}
func main() {
catSectionDir(dir)
}