This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
walk.go
113 lines (96 loc) · 2.37 KB
/
walk.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/adrg/frontmatter"
"gopkg.in/yaml.v2"
)
type Front struct {
Title string `yaml:"title"`
Draft bool `yaml:"draft"`
Tags []string `yaml:"tags"`
}
// recursively walk directory and return all files with given extension
func walk(root, ext string, index bool, ignorePaths map[string]struct{}) (res []Link, i ContentIndex) {
fmt.Printf("Scraping %s\n", root)
i = make(ContentIndex)
nPrivate := 0
formats := []*frontmatter.Format{
frontmatter.NewFormat("---", "---", yaml.Unmarshal),
}
start := time.Now()
err := filepath.WalkDir(root, func(fp string, d fs.DirEntry, e error) error {
if e != nil {
return e
}
// path normalize fp
s := filepath.ToSlash(fp)
if _, ignored := ignorePaths[s]; ignored {
fmt.Printf("[Ignored] %s\n", d.Name())
nPrivate++
} else if filepath.Ext(d.Name()) == ext {
if index {
text := getText(s)
var matter Front
raw_body, err := frontmatter.Parse(strings.NewReader(text), &matter, formats...)
body := string(raw_body)
if err != nil {
matter = Front{
Title: "Untitled Page",
Draft: false,
Tags: []string{},
}
body = text
}
// check if page is private
if !matter.Draft {
info, _ := os.Stat(s)
source := processSource(trim(s, root, ".md"))
// default title
title := matter.Title
if title == "" {
fileName := d.Name()
title = strings.TrimSuffix(filepath.Base(fileName), filepath.Ext(fileName))
}
// default tags
if matter.Tags == nil {
matter.Tags = []string{}
}
// add to content and link index
i[source] = Content{
LastModified: info.ModTime(),
Title: title,
Content: body,
Tags: matter.Tags,
}
res = append(res, parse(s, root)...)
} else {
fmt.Printf("[Ignored] %s\n", d.Name())
nPrivate++
}
}
}
return nil
})
if err != nil {
panic(err)
}
end := time.Now()
fmt.Printf("[DONE] in %s\n", end.Sub(start).Round(time.Millisecond))
fmt.Printf("Ignored %d private files \n", nPrivate)
fmt.Printf("Parsed %d total links \n", len(res))
return res, i
}
func getText(dir string) string {
// read file
fileBytes, err := ioutil.ReadFile(dir)
if err != nil {
panic(err)
}
return string(fileBytes)
}