From dd5c583e8b48b2f12e7d0875fff1f8580e0a956a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Tue, 22 Jun 2021 01:29:21 +0200 Subject: hswg: store backlinks in a map --- hswg/main.go | 41 ++++++++++++++++++----------------------- 1 file 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{}, } } } @@ -276,28 +277,22 @@ func main() { e.Content = template.HTML(expanded.String()) } - 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) } -- cgit v1.2.3