-
Notifications
You must be signed in to change notification settings - Fork 1
/
doc.go
43 lines (36 loc) · 1.05 KB
/
doc.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
package foswiki
import (
"fmt"
"path/filepath"
"regexp"
"strings"
)
type Doc struct {
Path string
}
// PageName returns the page name of the current Foswiki document.
func (d Doc) PageName() string {
// pagename = filename - extension
return strings.Replace(filepath.Base(d.Path), filepath.Ext(d.Path), "", 1)
}
// WebName returns the web name of the current Foswiki document.
func (d Doc) WebName() string {
var pathReg = regexp.MustCompile(`.*/(.+?)/.+?\.txt`)
unixPath := filepath.ToSlash(d.Path)
matchedParts := pathReg.FindStringSubmatch(unixPath)
if matchedParts == nil {
return ""
}
return matchedParts[1]
}
// PubDir returns the path to the Foswiki pub folder for the current Foswiki document.
func (d Doc) PubDir() string {
var pathReg = regexp.MustCompile(`(.*)/data/(.+?)/(.+?)\.txt`)
unixPath := filepath.ToSlash(d.Path)
matchedParts := pathReg.FindStringSubmatch(unixPath)
if matchedParts == nil {
return ""
}
pubDir := fmt.Sprintf("%v/pub/%v/%v/", matchedParts[1], matchedParts[2], matchedParts[3])
return filepath.Clean(pubDir)
}