-
Notifications
You must be signed in to change notification settings - Fork 100
/
files.go
54 lines (45 loc) · 1.06 KB
/
files.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
package kwebsite
import (
"os"
"path/filepath"
"strings"
"text/template"
"github.com/stoewer/go-strcase"
)
type PartIndex struct {
Title string
Weight int
}
func (o *KWebsite) addPartIndex(subdir string, name string, weight int) error {
dirname := filepath.Join(o.Directory, subdir)
if err := os.Mkdir(dirname, 0755); err != nil {
return err
}
t := template.Must(template.ParseFiles(filepath.Join(o.TemplatesDir, "part-index.tmpl")))
data := PartIndex{
Title: name,
Weight: weight,
}
filename := filepath.Join(o.Directory, subdir, "_index.md")
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
return t.Execute(f, data)
}
// escapeName returns a name usable as file name
func escapeName(parts ...string) string {
result := []string{}
for _, s := range parts {
if s != "" {
result = append(result, strcase.KebabCase(s))
}
}
return strings.Join(result, "-")
}
// headingID returns the ID built by hugo for a given header
func headingID(s string) string {
result := strings.ReplaceAll(s, " ", "-")
return result
}