-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.go
174 lines (154 loc) · 4.29 KB
/
utils.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
package main
import (
"fmt"
"io/ioutil"
"path"
"regexp"
"strconv"
"strings"
copy2 "github.com/otiai10/copy"
)
func GetFileInfo(filePath string) (*FileInfo, *string) {
data, err := ioutil.ReadFile(filePath)
CheckError(err)
strData := strings.TrimSpace(string(data))
metaIndex := strings.LastIndex(strData, "\n\n")
if metaIndex <= 0 {
return nil, nil
}
strMeta := strData[metaIndex:]
strMeta = fmt.Sprintf("%s\n", strMeta)
r, _ := regexp.Compile("id: *(.*)\n")
match := r.FindStringSubmatch(strMeta)
if len(match) < 2 {
return nil, nil
}
metaId := match[1]
r, _ = regexp.Compile("type_: *(.*)\n")
match = r.FindStringSubmatch(strMeta)
if len(match) < 2 {
return nil, nil
}
metaType, err := strconv.Atoi(match[1])
CheckError(err)
if 1 != metaType && 2 != metaType && 4 != metaType {
return nil, nil
}
metaParentId := ""
r, _ = regexp.Compile("parent_id: *(.*)\n")
match = r.FindStringSubmatch(strMeta)
if len(match) >= 2 {
metaParentId = match[1]
}
metaFileExt := ""
r, _ = regexp.Compile("file_extension: *(.*)\n")
match = r.FindStringSubmatch(strMeta)
if len(match) >= 2 {
metaFileExt = match[1]
}
r, _ = regexp.Compile("(.*)\n")
match = r.FindStringSubmatch(strData)
if len(match) < 2 {
return nil, nil
}
name := strings.TrimSpace(match[1])
return &FileInfo{
name: name,
metaIndex: metaIndex,
metaId: metaId,
metaType: metaType,
metaParentId: metaParentId,
metaFileExt: metaFileExt,
}, &strData
}
var StepDesc = [5]string{
"Initializing",
"Extracting Metadata", //1
"Rebuilding Folders",
"Rebuilding Articles",
"Saving Data",
}
func HandlingCoreBusiness(progress chan<- int, done chan<- bool) {
folderMap := make(map[string]*Folder)
articleMap := make(map[string]*Article)
resMap := make(map[string]*Resource)
c, err := ioutil.ReadDir(*SrcPath)
CheckError(err)
for _, entry := range c {
if entry.IsDir() ||
path.Ext(entry.Name()) != ".md" {
continue
}
filePath := path.Join(*SrcPath, entry.Name())
fi, rawData := GetFileInfo(filePath)
if fi == nil {
continue
}
if 2 == fi.metaType {
folder := Folder{FileInfo: fi}
folderMap[folder.metaId] = &folder
} else if 1 == fi.metaType {
content := (*rawData)[:fi.metaIndex]
r, _ := regexp.Compile("(.*\n)")
match := r.FindStringIndex(content)
if len(match) == 2 {
content = strings.TrimSpace(content[match[1]:])
}
article := Article{FileInfo: fi, content: content}
articleMap[article.metaId] = &article
} else if 4 == fi.metaType {
resMap[fi.metaId] = &Resource{FileInfo: fi}
}
progress <- 1
}
RebuildFoldersRelationship(&folderMap, progress)
RebuildArticlesRelationship(&articleMap, &folderMap, progress)
err = copy2.Copy(path.Join(*SrcPath, ResourcesFolder), path.Join(*DestPath, ResourcesFolder))
CheckError(err)
for _, article := range articleMap {
FixResourceRef(article, &resMap, &articleMap)
article.save()
progress <- 4
}
close(progress)
done <- true
}
func FixResourceRef(article *Article, resMap *map[string]*Resource, articleMap *map[string]*Article) {
content := article.content
r, _ := regexp.Compile(`(!?)\[(.*?)]\(:/(.*?)\)`)
matchAll := r.FindAllStringSubmatchIndex(content, -1)
for i := len(matchAll) - 1; i >= 0; i-- {
match := matchAll[i]
resId := strings.Split(content[match[6]:match[7]], " ")[0]
var resFileName string
if res, prs := (*resMap)[resId]; prs {
resFileName = res.getFileName()
} else if res, prs := (*articleMap)[resId]; prs {
resFileName = path.Join(res.folder.getRelativePath(), res.getValidName())
} else {
resFileName = path.Join("resources", resId) // help to find lost resource
}
content = fmt.Sprintf("%s[[%s]]%s", content[:match[3]], resFileName, content[match[1]:])
}
article.content = content
}
func RebuildFoldersRelationship(folderMap *map[string]*Folder, progress chan<- int) {
for _, folder := range *folderMap {
if len(folder.metaParentId) == 0 {
continue
}
parent := (*folderMap)[folder.metaParentId]
folder.parent = parent
progress <- 2
}
}
func RebuildArticlesRelationship(articleMap *map[string]*Article, folderMap *map[string]*Folder, progress chan<- int) {
for _, article := range *articleMap {
if len(article.metaParentId) == 0 {
continue
}
parent := (*folderMap)[article.metaParentId]
article.folder = parent
progress <- 3
}
}