diff options
author | Přemysl Eric Janouch <p@janouch.name> | 2021-06-22 01:29:21 +0200 |
---|---|---|
committer | Přemysl Eric Janouch <p@janouch.name> | 2021-06-22 23:25:01 +0200 |
commit | dd5c583e8b48b2f12e7d0875fff1f8580e0a956a (patch) | |
tree | 3c5d27305879980e16d63309499de62d4b1270d8 /hswg | |
parent | 63d18d068d3b01041a4d1e73cf5eb78c6d3eac27 (diff) | |
download | haven-dd5c583e8b48b2f12e7d0875fff1f8580e0a956a.tar.gz haven-dd5c583e8b48b2f12e7d0875fff1f8580e0a956a.tar.xz haven-dd5c583e8b48b2f12e7d0875fff1f8580e0a956a.zip |
hswg: store backlinks in a map
Diffstat (limited to 'hswg')
-rw-r--r-- | hswg/main.go | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/hswg/main.go b/hswg/main.go index caae1f1..5076bd1 100644 --- a/hswg/main.go +++ b/hswg/main.go @@ -134,12 +134,12 @@ func Render(r io.Reader, config configuration.Configuration) ( // Entry contains all context information about a single page. type Entry struct { - 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 + 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 map[string]bool // what documents link back here Backlinks []template.HTML } @@ -180,7 +180,7 @@ 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.IsDraft() { - link.backlinks = append(link.backlinks, name) + link.backlinks[name] = true return []byte(makeLink(m, string(match))) } return match @@ -234,6 +234,7 @@ func main() { entries[name] = &Entry{ PathSource: path, PathDestination: resultPath(path), + backlinks: map[string]bool{}, } } } @@ -277,27 +278,21 @@ func main() { } for _, e := range entries { - sort.Strings(e.backlinks) - - last, uniq := "", []string{} - for _, name := range e.backlinks { - if name != last { - uniq = append(uniq, name) - } - last = name - } - e.backlinks = uniq - } - - for _, e := range entries { f, err := os.Create(e.PathDestination) if err != nil { log.Fatalln(err) } - for _, name := range e.backlinks { - e.Backlinks = append(e.Backlinks, - template.HTML(makeLink(&entries, name))) + + backlinks := []string{} + for name := range e.backlinks { + backlinks = append(backlinks, name) + } + sort.Strings(backlinks) + for _, name := range backlinks { + e.Backlinks = + append(e.Backlinks, template.HTML(makeLink(&entries, name))) } + if err = t.Execute(f, e); err != nil { log.Fatalln(err) } |