From 49a685c32ed10a97d5a6996c9047d5b11a5f8e5c Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch Date: Tue, 22 Jun 2021 02:34:24 +0200 Subject: hswg: take output path for the index as argument --- hswg/main.go | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) (limited to 'hswg') 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) } } -- cgit v1.2.3-70-g09d2