2021-12-28 00:19:05 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gernest/front"
|
|
|
|
"io/fs"
|
|
|
|
"io/ioutil"
|
2021-12-28 00:58:21 +03:00
|
|
|
"os"
|
2021-12-28 00:19:05 +03:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// recursively walk directory and return all files with given extension
|
2021-12-28 00:51:36 +03:00
|
|
|
func walk(root, ext string, index bool, ignorePaths map[string]struct{}) (res []Link, i ContentIndex) {
|
2021-12-28 00:19:05 +03:00
|
|
|
fmt.Printf("Scraping %s\n", root)
|
|
|
|
i = make(ContentIndex)
|
|
|
|
|
|
|
|
m := front.NewMatter()
|
|
|
|
m.Handle("---", front.YAMLHandler)
|
|
|
|
nPrivate := 0
|
|
|
|
|
|
|
|
err := filepath.WalkDir(root, func(s string, d fs.DirEntry, e error) error {
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
2021-12-28 00:51:36 +03:00
|
|
|
if _, ignored := ignorePaths[s]; ignored {
|
|
|
|
fmt.Printf("[Ignored] %s\n", d.Name())
|
2021-12-28 00:58:21 +03:00
|
|
|
nPrivate++
|
2021-12-28 00:51:36 +03:00
|
|
|
} else if filepath.Ext(d.Name()) == ext {
|
2021-12-28 00:19:05 +03:00
|
|
|
res = append(res, parse(s, root)...)
|
|
|
|
if index {
|
|
|
|
text := getText(s)
|
|
|
|
|
|
|
|
frontmatter, body, err := m.Parse(strings.NewReader(text))
|
|
|
|
if err != nil {
|
|
|
|
frontmatter = map[string]interface{}{}
|
|
|
|
body = text
|
|
|
|
}
|
|
|
|
|
|
|
|
var title string
|
|
|
|
if parsedTitle, ok := frontmatter["title"]; ok {
|
|
|
|
title = parsedTitle.(string)
|
|
|
|
} else {
|
|
|
|
title = "Untitled Page"
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if page is private
|
|
|
|
if parsedPrivate, ok := frontmatter["draft"]; !ok || !parsedPrivate.(bool) {
|
2021-12-28 00:58:21 +03:00
|
|
|
info, _ := os.Stat(s)
|
2021-12-28 00:19:05 +03:00
|
|
|
adjustedPath := strings.Replace(hugoPathTrim(trim(s, root, ".md")), " ", "-", -1)
|
|
|
|
i[adjustedPath] = Content{
|
2021-12-28 00:58:21 +03:00
|
|
|
LastModified: info.ModTime(),
|
2021-12-28 00:19:05 +03:00
|
|
|
Title: title,
|
|
|
|
Content: body,
|
|
|
|
}
|
|
|
|
} else {
|
2021-12-28 00:58:21 +03:00
|
|
|
fmt.Printf("[Ignored] %s\n", d.Name())
|
2021-12-28 00:19:05 +03:00
|
|
|
nPrivate++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|