-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdef.go
105 lines (92 loc) · 1.83 KB
/
def.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
)
var SrcPath *string
var DestPath *string
const ResourcesFolder string = "resources"
func CheckError(e error) {
if e != nil {
panic(e)
}
}
type FileInfo struct {
name string
metaIndex int
metaId string
metaParentId string
metaType int //1:Article 2:Folder 4:Resource 5:Tag
metaFileExt string
}
func (fi FileInfo) getValidName() string {
r := strings.NewReplacer(
"*", ".",
"\"", "''",
"\\", "-",
"/", "_",
"<", ",",
">", ".",
":", ";",
"|", "-",
"?", "!")
return r.Replace(fi.name)
}
type Folder struct {
*FileInfo
parent *Folder
}
func (f Folder) getPath() string {
return path.Join(*DestPath, f.getRelativePath())
}
func (f Folder) getRelativePath() string {
if f.parent == nil {
return f.getValidName()
} else {
return path.Join(f.parent.getRelativePath(), f.getValidName())
}
}
type Article struct {
*FileInfo
folder *Folder
content string
}
func (a Article) getPath() string {
return fmt.Sprintf("%s.md", path.Join(a.folder.getPath(), a.getValidName()))
}
func (a Article) save() {
filePath := a.getPath()
dirName := path.Dir(filePath)
if _, err := os.Stat(dirName); os.IsNotExist(err) {
err := os.MkdirAll(dirName, 0755)
CheckError(err)
}
err := ioutil.WriteFile(filePath, []byte(a.content), 0644)
CheckError(err)
}
type Resource struct {
*FileInfo
}
func (r Resource) getFileName() string {
var fileName string
if /*len(r.metaFileExt) > 0*/ false {
fileName = fmt.Sprintf("%s.%s", r.metaId, r.metaFileExt)
} else {
resPath := path.Join(*SrcPath, "resources")
c, err := ioutil.ReadDir(resPath)
CheckError(err)
for _, entry := range c {
if entry.IsDir() {
continue
}
if strings.Index(entry.Name(), r.metaId) >= 0 {
fileName = entry.Name()
break
}
}
}
return fileName
}