From 63d18d068d3b01041a4d1e73cf5eb78c6d3eac27 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Tue, 22 Jun 2021 00:59:13 +0200
Subject: hswg: actually use templates for output files
---
hswg/main.go | 74 ++++++++++++++++++++++++++++++------------------------------
1 file changed, 37 insertions(+), 37 deletions(-)
(limited to 'hswg/main.go')
diff --git a/hswg/main.go b/hswg/main.go
index 82b3014..caae1f1 100644
--- a/hswg/main.go
+++ b/hswg/main.go
@@ -134,17 +134,18 @@ func Render(r io.Reader, config configuration.Configuration) (
// Entry contains all context information about a single page.
type Entry struct {
- PathSource string // path to source AsciiDoc
- PathDestination string // path to destination HTML
- mtime time.Time // modification time
- Metadata Metadata // metadata
- document []byte // inner document with expanded LinkWords
- backlinks []string // what documents link back here
+ Metadata // metadata
+ PathSource string // path to source AsciiDoc
+ PathDestination string // path to destination HTML
+ mtime time.Time // modification time
+ Content template.HTML // inner document with expanded LinkWords
+ backlinks []string // what documents link back here
+ Backlinks []template.HTML
}
// Published returns the date when the entry was published, or nil if unknown.
func (e *Entry) Published() *time.Time {
- if d, _, err := e.Metadata.Attributes.GetAsString("date"); err != nil {
+ if d, _, err := e.Attributes.GetAsString("date"); err != nil {
return nil
} else if t, err := time.Parse(time.RFC3339, d); err == nil {
return &t
@@ -178,7 +179,7 @@ var linkWordRE = regexp.MustCompile(`\b\p{Lu}\p{L}*\b`)
func expand(m *map[string]*Entry, name string, chunk []byte) []byte {
return linkWordRE.ReplaceAllFunc(chunk, func(match []byte) []byte {
if link, ok := (*m)[string(match)]; ok && string(match) != name &&
- !link.Metadata.IsDraft() {
+ !link.IsDraft() {
link.backlinks = append(link.backlinks, name)
return []byte(makeLink(m, string(match)))
}
@@ -213,6 +214,11 @@ func main() {
log.Fatalln(err)
}
+ t, err := template.New("page").Parse(string(header))
+ if err != nil {
+ log.Fatalln(err)
+ }
+
// Create a map from document names to their page entries.
entries := map[string]*Entry{}
for _, glob := range os.Args[2:] {
@@ -253,6 +259,11 @@ func main() {
log.Fatalln(err)
}
+ // Every page needs to have a title.
+ if e.Title == "" {
+ e.Title = name
+ }
+
// Expand LinkWords anywhere between Links here: %s%s
\n", title))
-
+ for _, e := range entries {
sort.Strings(e.backlinks)
- backlinks, last := []string{}, ""
+ last, uniq := "", []string{}
for _, name := range e.backlinks {
if name != last {
- backlinks = append(backlinks, makeLink(&entries, name))
+ uniq = append(uniq, name)
}
last = name
}
+ e.backlinks = uniq
+ }
- if len(backlinks) > 0 {
- _, _ = f.WriteString(fmt.Sprintf("