2020-05-07 13:20:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-05-18 20:09:48 +00:00
|
|
|
"html/template"
|
2020-05-07 13:20:36 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-05-16 19:02:19 +00:00
|
|
|
type Reference struct {
|
|
|
|
Link ParsedLink
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Refs map[string][]Reference
|
2020-05-07 13:20:36 +00:00
|
|
|
|
|
|
|
func processBackrefs(fp *FilePages) error {
|
|
|
|
pages, err := fp.AllPages()
|
|
|
|
|
|
|
|
refs := make(Refs)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, page := range pages {
|
2020-05-16 19:02:19 +00:00
|
|
|
content := page.Content
|
|
|
|
|
|
|
|
var listItems []struct {
|
|
|
|
Id int
|
|
|
|
Indented int
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(strings.NewReader(content)).Decode(&listItems)
|
|
|
|
if err == nil {
|
|
|
|
pageText := ""
|
|
|
|
for _, item := range listItems {
|
|
|
|
pageText += strings.Repeat(" ", item.Indented) + "* " + item.Text + "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
content = pageText
|
|
|
|
}
|
|
|
|
|
|
|
|
links, err := ParseLinks(content)
|
2020-05-07 13:20:36 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, link := range links {
|
2020-05-16 19:02:19 +00:00
|
|
|
refs[link.PageName] = append(refs[link.PageName], Reference{link, page.Name})
|
2020-05-07 13:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-16 20:27:48 +00:00
|
|
|
func loadBackrefs(fp *FilePages, p string) (map[string][]Backref, error) {
|
2020-05-07 13:20:36 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-16 20:27:48 +00:00
|
|
|
result := make(map[string][]Backref)
|
2020-05-08 15:21:39 +00:00
|
|
|
|
|
|
|
for _, ref := range refs[p] {
|
2020-05-16 19:02:19 +00:00
|
|
|
title := strings.Replace(ref.Name, "_", " ", -1)
|
2020-05-16 20:27:48 +00:00
|
|
|
if _, e := result[ref.Name]; !e {
|
|
|
|
result[ref.Name] = nil
|
|
|
|
}
|
|
|
|
|
2020-05-24 18:40:41 +00:00
|
|
|
links := renderLinks(strings.TrimLeft(ref.Link.Line, " *"))
|
|
|
|
pageText := renderMarkdown2(links)
|
|
|
|
|
2020-05-16 20:27:48 +00:00
|
|
|
result[ref.Name] = append(result[ref.Name], Backref{
|
2020-05-16 19:02:19 +00:00
|
|
|
Name: ref.Name,
|
2020-05-08 15:21:39 +00:00
|
|
|
Title: title,
|
2020-05-24 18:40:41 +00:00
|
|
|
Line: template.HTML(pageText),
|
2020-05-08 15:21:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2020-05-07 13:20:36 +00:00
|
|
|
}
|