hswg: store backlinks in a map
This commit is contained in:
parent
63d18d068d
commit
dd5c583e8b
31
hswg/main.go
31
hswg/main.go
|
@ -139,7 +139,7 @@ type Entry struct {
|
||||||
PathDestination string // path to destination HTML
|
PathDestination string // path to destination HTML
|
||||||
mtime time.Time // modification time
|
mtime time.Time // modification time
|
||||||
Content template.HTML // inner document with expanded LinkWords
|
Content template.HTML // inner document with expanded LinkWords
|
||||||
backlinks []string // what documents link back here
|
backlinks map[string]bool // what documents link back here
|
||||||
Backlinks []template.HTML
|
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 {
|
return linkWordRE.ReplaceAllFunc(chunk, func(match []byte) []byte {
|
||||||
if link, ok := (*m)[string(match)]; ok && string(match) != name &&
|
if link, ok := (*m)[string(match)]; ok && string(match) != name &&
|
||||||
!link.IsDraft() {
|
!link.IsDraft() {
|
||||||
link.backlinks = append(link.backlinks, name)
|
link.backlinks[name] = true
|
||||||
return []byte(makeLink(m, string(match)))
|
return []byte(makeLink(m, string(match)))
|
||||||
}
|
}
|
||||||
return match
|
return match
|
||||||
|
@ -234,6 +234,7 @@ func main() {
|
||||||
entries[name] = &Entry{
|
entries[name] = &Entry{
|
||||||
PathSource: path,
|
PathSource: path,
|
||||||
PathDestination: resultPath(path),
|
PathDestination: resultPath(path),
|
||||||
|
backlinks: map[string]bool{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -276,28 +277,22 @@ func main() {
|
||||||
e.Content = template.HTML(expanded.String())
|
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 {
|
for _, e := range entries {
|
||||||
f, err := os.Create(e.PathDestination)
|
f, err := os.Create(e.PathDestination)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
for _, name := range e.backlinks {
|
|
||||||
e.Backlinks = append(e.Backlinks,
|
backlinks := []string{}
|
||||||
template.HTML(makeLink(&entries, name)))
|
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 {
|
if err = t.Execute(f, e); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue