wiki/backref.go
Peter Stuifzand 6c04d9871f
All checks were successful
continuous-integration/drone/push Build is passing
improve backrefs for view and edit
2020-05-08 17:21:39 +02:00

72 lines
1.1 KiB
Go

package main
import (
"encoding/json"
"os"
"path/filepath"
"strings"
)
type Refs map[string][]string
func processBackrefs(fp *FilePages) error {
pages, err := fp.AllPages()
refs := make(Refs)
if err != nil {
return err
}
for _, page := range pages {
links, err := ParseLinks(page.Content)
if err != nil {
return err
}
for _, link := range links {
refs[link.PageName] = append(refs[link.PageName], page.Name)
}
}
f, err := os.Create(filepath.Join(fp.dirname, "backrefs.json"))
if err != nil {
return err
}
defer f.Close()
err = json.NewEncoder(f).Encode(&refs)
return err
}
func loadBackrefs(fp *FilePages, p string) ([]Backref, error) {
refs := make(Refs)
p = strings.Replace(p, " ", "_", -1)
f, err := os.Open(filepath.Join(fp.dirname, "backrefs.json"))
if err != nil {
return nil, err
}
defer f.Close()
err = json.NewDecoder(f).Decode(&refs)
if err != nil {
return nil, err
}
var result []Backref
for _, ref := range refs[p] {
title := strings.Replace(ref, "_", " ", -1)
result = append(result, Backref{
Name: ref,
Title: title,
})
}
return result, nil
}