-
Notifications
You must be signed in to change notification settings - Fork 2
/
options.go
53 lines (46 loc) · 1.09 KB
/
options.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
package capsule
import (
"io/fs"
"path/filepath"
"strings"
)
type Options struct {
AttributeHandlers []AttributeHandler
}
type AttributeHandler func(string, fs.DirEntry) Attributes
func DefaultOptions() *Options {
return &Options{
AttributeHandlers: []AttributeHandler{
filenameClassType,
filenameRole,
},
}
}
func (opt *Options) GetAttributes(path string, de fs.DirEntry) Attributes {
merged := make(Attributes)
for _, handler := range opt.AttributeHandlers {
for k, v := range handler(path, de) {
merged[k] = v
}
}
return merged
}
func filenameRole(path string, _ fs.DirEntry) Attributes {
base := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))
if base == "story" {
return Attributes{"role": "story"}
}
return nil
}
func filenameClassType(path string, _ fs.DirEntry) Attributes {
switch strings.ToLower(filepath.Ext(path)) {
case ".md":
return Attributes{"class": "document", "type": "markdown"}
case ".png":
return Attributes{"class": "image", "type": "png"}
case ".jpg":
return Attributes{"class": "image", "type": "jpeg"}
default:
return nil
}
}