-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.go
52 lines (42 loc) · 1.13 KB
/
paths.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
package main
import (
"fmt"
"path"
"regexp"
"strings"
"github.com/gosimple/slug"
)
// stripNumberPrefix strips the number in the format "01." from the beginning of the string.
func stripNumberPrefix(str string) string {
reg, err := regexp.Compile("^[0-9]+\\.\\s*")
if err != nil {
panic(fmt.Sprintf("cannot compile regex: %v", err))
}
str = reg.ReplaceAllString(str, "")
return str
}
type PathRewriter struct {
}
func (r *PathRewriter) ModifyPath(p string, isDir bool) string {
parts := strings.Split(p, "/")
ext := path.Ext(parts[len(parts)-1])
if ext == ".md" && !isDir {
parts[len(parts)-1] = r.rewritePageFilename(parts[len(parts)-1])
}
if isDir || len(parts) > 1 {
parts[0] = r.rewriteSectionDirname(parts[0])
}
return strings.Join(parts, "/")
}
func (r *PathRewriter) rewritePageFilename(filename string) string {
ext := path.Ext(filename)
base := strings.TrimSuffix(filename, ext)
base = stripNumberPrefix(base)
base = slug.Make(base)
return base + ".html"
}
func (r *PathRewriter) rewriteSectionDirname(dirname string) string {
dirname = stripNumberPrefix(dirname)
dirname = slug.Make(dirname)
return dirname
}