-
Notifications
You must be signed in to change notification settings - Fork 43
/
ext.go
42 lines (34 loc) · 793 Bytes
/
ext.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
package processors
import (
"errors"
gostatic "github.com/piranha/gostatic/lib"
"path/filepath"
)
type ExtProcessor struct {
}
func NewExtProcessor() *ExtProcessor {
return &ExtProcessor{}
}
func (p *ExtProcessor) Process(page *gostatic.Page, args []string) error {
return ProcessExt(page, args)
}
func (p *ExtProcessor) Description() string {
return "change extension"
}
func (p *ExtProcessor) Mode() int {
return gostatic.Pre
}
func ProcessExt(page *gostatic.Page, args []string) error {
if len(args) < 1 {
return errors.New(
"'ext' rule requires an extension prefixed with dot")
}
newExt := args[0]
ext := filepath.Ext(page.Path)
if ext == "" {
page.Path = page.Path + newExt
} else {
page.Path = page.Path[0:len(page.Path)-len(ext)] + newExt
}
return nil
}