hswg: store backlinks in a map

This commit is contained in:
Přemysl Eric Janouch 2021-06-22 01:29:21 +02:00
parent 63d18d068d
commit dd5c583e8b
Signed by: p
GPG Key ID: A0420B94F92B9493

View File

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