write section listings

This commit is contained in:
dre 2021-07-11 16:49:46 +08:00
parent 77d963a3a9
commit f01d86a0d3
2 changed files with 55 additions and 17 deletions

View file

@ -9,12 +9,12 @@ via the Gemini protocol.
**NOTE**: this is rather minimal and only has one use case for now, many edge cases may not be covered. **NOTE**: this is rather minimal and only has one use case for now, many edge cases may not be covered.
Features **Features**
- reads hugo `.toml` file for section output formats - reads hugo `.toml` file for section output formats
- supports an arbitrary document processor, any program that supports UNIX pipes - supports an arbitrary document processor, any program that supports UNIX pipes
- ugly urls - ugly urls, note that I have not tested this much with links, nice urls recommended
- section listings - append section listings to section pages, optionally on root
- supports drafts - supports with and without drafts from config
- composable with other tools - composable with other tools
To illustrate what this program does, run the following in the hugo directory. To illustrate what this program does, run the following in the hugo directory.

64
main.go
View file

@ -15,17 +15,18 @@ import (
) )
const ( const (
defaultExt = "md" defaultExt = "md"
defaultProcessor = "" defaultProcessor = ""
defaultSource = "content" defaultSource = "content"
defaultDestination = "public" defaultDestination = "public"
defaultConfigPath = "config.toml" defaultConfigPath = "config.toml"
defaultSectionOnRoot = "posts"
defaultPermalinkFormat = "/:year/:month/:title/" defaultPermalinkFormat = "/:year/:month/:title/"
) )
func main() { func main() {
var ext, pipecmd, source, destination, cfgPath string var ext, pipecmd, source, destination, cfgPath, seconOnRoot string
var noSectionList bool var noSectionList bool
flag.StringVar(&ext, "ext", defaultExt, "ext to look for templates in ./layout") flag.StringVar(&ext, "ext", defaultExt, "ext to look for templates in ./layout")
@ -34,6 +35,7 @@ func main() {
flag.StringVar(&destination, "destination", defaultDestination, "output directory") flag.StringVar(&destination, "destination", defaultDestination, "output directory")
flag.StringVar(&cfgPath, "config", defaultConfigPath, "hugo config path") flag.StringVar(&cfgPath, "config", defaultConfigPath, "hugo config path")
flag.BoolVar(&noSectionList, "no-section-list", false, "disable auto append of section content lists") flag.BoolVar(&noSectionList, "no-section-list", false, "disable auto append of section content lists")
flag.StringVar(&seconOnRoot, "section-on-root", defaultSectionOnRoot, "if append sections, add this one on the root")
flag.Parse() flag.Parse()
// what are we doing // what are we doing
@ -136,15 +138,51 @@ func main() {
fmt.Printf("written %s (%dbytes)\n", fullpath, n) fmt.Printf("written %s (%dbytes)\n", fullpath, n)
} }
for _, file := range tree.Files { // we're done if we
fmt.Printf("section %v\n", file.Parent) if noSectionList {
return
} }
// TODO // aggregate sections and section entries
// sections := make(map[string]*Section)
// append section listings for _, file := range tree.Files {
// => link title line // not a section
// date - summary block if file.Parent == "." {
continue
}
name := file.Parent
sectionFile := filepath.Join(destination, file.Parent, "index."+ext)
//fmt.Printf("section %v\n", sectionFile)
link := file.Destination
if uglyURLs {
link += "." + ext
}
//fmt.Printf("link %v\n", link)
if _, ok := sections[name]; !ok {
sections[name] = &Section{File: sectionFile}
}
sections[name].List = append(sections[name].List, SectionEntry{
Date: file.Metadata.Date,
Title: file.Metadata.Title,
Summary: file.Metadata.Summary,
Link: link,
})
}
for name, section := range sections {
section.Write(section.File)
fmt.Printf("written section listing %s to %s\n", name, section.File)
}
section, ok := sections[seconOnRoot]
if ok {
sectionFile := filepath.Join(destination, "index."+ext)
section.Write(sectionFile)
fmt.Printf("written section listing for root to %s\n", section.File)
}
// TODO // TODO
// in watch mode, compare timestamps of files before replacement, keep index? // in watch mode, compare timestamps of files before replacement, keep index?