improve content parser add todos

This commit is contained in:
dre 2021-07-11 00:32:21 +08:00
parent 492cf2de4b
commit 3bcb0234c2
2 changed files with 35 additions and 43 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
content
public public
config.toml config.toml
hugoext hugoext

75
main.go
View file

@ -46,37 +46,12 @@ func main() {
} }
permalinks := cfg.GetStringMapString("permalinks") permalinks := cfg.GetStringMapString("permalinks")
//fmt.Println("permalinks", permalinks) if permalinks == nil {
log.Println("no permalinks from config loaded, using default: ", defaultPermalinkFormat)
/*
fpath := "content/posts/first-post.md"
file, err := os.Open(fpath)
if err != nil {
log.Fatal("open", err)
} }
page, err := ReadFrom(file)
if err != nil {
log.Fatal("read page", err)
}
fmt.Println(string(page.FrontMatter()))
fmt.Println(string(page.Content()))
meta, err := page.Metadata()
if err != nil {
log.Fatal("read meta", err)
}
c := NewContentFromMeta(meta)
fmt.Println(c)
link, err := pathPattern(permalinks["posts"]).Expand(c)
if err != nil {
log.Fatal("permalink expand", err)
}
fmt.Println(link)
*/
// test // test
linkcfg := func(subdir string) string { linkpattern := func(subdir string) string {
format, ok := permalinks[subdir] format, ok := permalinks[subdir]
if ok { if ok {
return format return format
@ -91,7 +66,7 @@ func main() {
// for each file, get destination path, switch file extension, remove underscore for index // for each file, get destination path, switch file extension, remove underscore for index
var tree FileTree var tree FileTree
for file := range files { for file := range files {
pattern := linkcfg(file.Parent) pattern := linkpattern(file.Parent)
err := destinationPath(&file, pattern) err := destinationPath(&file, pattern)
if err != nil { if err != nil {
fmt.Println(err, file) fmt.Println(err, file)
@ -116,6 +91,7 @@ func main() {
// write to source // write to source
tree.Files[i].NewBody = procout.Bytes() tree.Files[i].NewBody = procout.Bytes()
fmt.Printf("processed %s (%dbytes)\n", file.Source, len(tree.Files[i].Body))
} }
newpath := filepath.Join(".", "public") newpath := filepath.Join(".", "public")
@ -126,7 +102,7 @@ func main() {
// write to destination // write to destination
for _, file := range tree.Files { for _, file := range tree.Files {
fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body)) //fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body))
outfile := "index." + ext outfile := "index." + ext
outdir := filepath.Join(destination, file.Destination) outdir := filepath.Join(destination, file.Destination)
@ -136,6 +112,12 @@ func main() {
log.Println(err) log.Println(err)
continue continue
} }
// TODO: change dir^
// TODO: consider links
if uglyurls {
outdir += ".gmi"
outfile = ""
}
} else { } else {
outdir = destination outdir = destination
} }
@ -146,12 +128,15 @@ func main() {
log.Println(err) log.Println(err)
continue continue
} }
fmt.Println(fullpath, string(file.NewBody))
newfile.Write(file.NewBody) n, _ := newfile.Write(file.NewBody)
newfile.Close() newfile.Close()
fmt.Printf("written %s (%dbytes)\n", fullpath, n)
} }
// replace links? // TODO
// check/replace links
// write rss? // write rss?
// write listings from template? // write listings from template?
} }
@ -251,14 +236,16 @@ func collectFiles(fullpath string, filechan chan File) error {
func NewContentFromMeta(meta map[string]interface{}) *Content { func NewContentFromMeta(meta map[string]interface{}) *Content {
return &Content{ return &Content{
Title: istring(meta["title"]), Title: stringFromInterface(meta["title"]),
Slug: istring(meta["slug"]), Slug: stringFromInterface(meta["slug"]),
Categories: istringArr(meta["categories"]), Summary: stringFromInterface(meta["summary"]),
Date: idate(meta["date"]), Categories: stringArrayFromInterface(meta["categories"]),
Tags: stringArrayFromInterface(meta["tags"]),
Date: dateFromInterface(meta["date"]),
} }
} }
func istring(input interface{}) string { func stringFromInterface(input interface{}) string {
str, ok := input.(string) str, ok := input.(string)
if ok { if ok {
return str return str
@ -266,7 +253,7 @@ func istring(input interface{}) string {
return "" return ""
} }
func idate(input interface{}) time.Time { func dateFromInterface(input interface{}) time.Time {
str, ok := input.(string) str, ok := input.(string)
if !ok { if !ok {
return time.Now() return time.Now()
@ -284,10 +271,14 @@ func idate(input interface{}) time.Time {
return t return t
} }
func istringArr(input interface{}) []string { func stringArrayFromInterface(input interface{}) []string {
str, ok := input.([]string) strarr, ok := input.([]interface{})
if ok { if ok {
return str var out []string
for _, str := range strarr {
out = append(out, stringFromInterface(str))
}
return out
} }
return nil return nil
} }