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) }