This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
100 lines (85 loc) · 1.93 KB
/
util.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
package main
import (
"container/list"
"net/url"
"os"
"path/filepath"
"strings"
)
var (
// markdown file's extensiton list
mdExtList = []string{
".md",
".mkd",
".mkdn",
".mdown",
".markdown",
}
)
func isMarkdown(filename string) bool {
ext := filepath.Ext(filename)
for _, mdext := range mdExtList {
if ext == mdext {
return true
}
}
return false
}
// TODO test
func removeExt(filename string) string {
dir := filepath.Dir(filename)
base := filepath.Base(filename)
if strings.Contains(base, ".") {
base = base[:strings.LastIndex(base, ".")]
}
return filepath.Join(dir, base)
}
func addExt(pathWithoutExt string) []string {
files := list.New()
for _, ext := range mdExtList {
path := pathWithoutExt + ext
_, err := os.Stat(path)
if err == nil { // file exists
files.PushBack(path)
}
}
return toStringArray(files)
}
func toStringArray(src *list.List) []string {
ret := make([]string, src.Len())
for i, v := 0, src.Front(); i < len(ret); i, v = i+1, v.Next() {
ret[i] = v.Value.(string)
}
return ret
}
// isIn returns true when dirA is in dirB
func isIn(dirA, dirB string) bool {
dirA, _ = filepath.Abs(dirA)
dirB, _ = filepath.Abs(dirB)
return strings.HasPrefix(dirA, dirB)
}
func queryEscapeWikiPath(wpath string) string {
// replace all separator to /
s := strings.Replace(wpath, string(filepath.Separator), "/", -1)
// url escape
s = url.QueryEscape(wpath)
// replace all %2F to /
s = strings.Replace(s, "%2F", "/", -1)
// replace all + to %20
s = strings.Replace(s, "+", "%20", -1)
return s
}
func toWikiPath(path string) string {
wpath, err := filepath.Rel(setting.wikiRoot, path)
if err != nil || strings.HasPrefix(wpath, "..") || wpath == "." {
return string(filepath.Separator)
}
return filepath.Join(string(filepath.Separator), wpath)
}
func splitCommma(s string) []string {
ss := strings.Split(s, ",")
for i := range ss {
ss[i] = strings.TrimSpace(ss[i])
}
return ss
}