aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2021-06-22 02:34:24 +0200
committerPřemysl Eric Janouch <p@janouch.name>2021-06-22 23:25:02 +0200
commit49a685c32ed10a97d5a6996c9047d5b11a5f8e5c (patch)
tree2e3e191b3be125d7b279a9cadb8bf9d6db7851bb
parentd763ce619d9833ee4d3b5adc88fca29deaad0b52 (diff)
downloadhaven-49a685c32ed10a97d5a6996c9047d5b11a5f8e5c.tar.gz
haven-49a685c32ed10a97d5a6996c9047d5b11a5f8e5c.tar.xz
haven-49a685c32ed10a97d5a6996c9047d5b11a5f8e5c.zip
hswg: take output path for the index as argument
-rw-r--r--hswg/main.go48
1 files changed, 29 insertions, 19 deletions
diff --git a/hswg/main.go b/hswg/main.go
index 9972817..4626472 100644
--- a/hswg/main.go
+++ b/hswg/main.go
@@ -284,7 +284,8 @@ func finalizeEntries(entries *map[string]*Entry) {
}
}
-func writeIndex(t *template.Template, entries *map[string]*Entry) error {
+func writeIndex(path string, t *template.Template,
+ entries *map[string]*Entry) error {
// Reorder entries reversely, primarily by date, secondarily by filename.
ordered := []*Entry{}
for _, e := range *entries {
@@ -309,8 +310,13 @@ func writeIndex(t *template.Template, entries *map[string]*Entry) error {
return p2.Before(*p1)
})
+ f, err := os.Create(path)
+ if err != nil {
+ return err
+ }
+
// TODO(p): Splitting content to categories would be nice.
- return t.Execute(os.Stdout, ordered)
+ return t.Execute(f, ordered)
}
func singleFile() {
@@ -330,43 +336,47 @@ func main() {
singleFile()
return
}
- if len(os.Args) < 3 {
- log.Fatalf("usage: %s TEMPLATE GLOB...\n", os.Args[0])
+ if len(os.Args) < 4 {
+ log.Fatalf("usage: %s TEMPLATE INDEX GLOB...\n", os.Args[0])
}
- // Read the common page header.
- header, err := ioutil.ReadFile(os.Args[1])
+ argTemplate, argIndex, argGlobs := os.Args[1], os.Args[2], os.Args[3:]
+
+ // Read a template for entries.
+ header, err := ioutil.ReadFile(argTemplate)
if err != nil {
log.Fatalln(err)
}
- t, err := template.New("page").Parse(string(header))
+ tmplEntry, err := template.New("entry").Parse(string(header))
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ // Read a template for the index from the standard input.
+ index, err := ioutil.ReadAll(os.Stdin)
+ if err != nil {
+ log.Fatalln(err)
+ }
+ tmplIndex, err := template.New("index").Parse(string(index))
if err != nil {
log.Fatalln(err)
}
// Process all entries.
- entries, err := loadEntries(os.Args[2:])
+ entries, err := loadEntries(argGlobs)
if err != nil {
log.Fatalln(err)
}
finalizeEntries(&entries)
for _, e := range entries {
- if err := writeEntry(e, t, &entries); err != nil {
+ if err := writeEntry(e, tmplEntry, &entries); err != nil {
log.Fatalln(err)
}
}
- // Read a template from the standard input, write an index.
- var input []byte
- if input, err = ioutil.ReadAll(os.Stdin); err != nil {
- log.Fatalln(err)
- }
- t, err = template.New("-").Parse(string(input))
- if err != nil {
- log.Fatalln(err)
- }
- if err := writeIndex(t, &entries); err != nil {
+ // Write an index.
+ if err := writeIndex(argIndex, tmplIndex, &entries); err != nil {
log.Fatalln(err)
}
}