62 lines
965 B
Go
62 lines
965 B
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) ([]string, 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
|
||
|
}
|
||
|
|
||
|
return refs[p], nil
|
||
|
}
|