Backref basics
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2020-05-07 15:20:36 +02:00
parent f3f5108bbb
commit ef5e074cb0
5 changed files with 154 additions and 12 deletions

61
backref.go Normal file
View File

@ -0,0 +1,61 @@
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
}

35
file.go
View File

@ -27,8 +27,9 @@ func NewFilePages(dirname string) PagesRepository {
return fp return fp
} }
func (fp *FilePages) Get(p string) Page { func (fp *FilePages) Get(title string) Page {
f, err := os.Open(filepath.Join(fp.dirname, strings.Replace(p, " ", "_", -1))) name := strings.Replace(title, " ", "_", -1)
f, err := os.Open(filepath.Join(fp.dirname, name))
if err != nil { if err != nil {
return Page{} return Page{}
} }
@ -37,7 +38,16 @@ func (fp *FilePages) Get(p string) Page {
if err != nil { if err != nil {
return Page{} return Page{}
} }
return Page{Content: string(body)} refs, err := loadBackrefs(fp, name)
if err != nil {
return Page{}
}
return Page{
Name: name,
Title: title,
Content: string(body),
Refs: refs,
}
} }
func (fp *FilePages) Save(p string, page Page, summary, author string) error { func (fp *FilePages) Save(p string, page Page, summary, author string) error {
@ -61,6 +71,11 @@ func (fp *FilePages) Save(p string, page Page, summary, author string) error {
f.WriteString(strings.Replace(page.Content, "\r\n", "\n", -1)) f.WriteString(strings.Replace(page.Content, "\r\n", "\n", -1))
} }
err = processBackrefs(fp)
if err != nil {
return fmt.Errorf("while processing backrefs: %v", err)
}
return saveWithGit(fp, p, summary, author) return saveWithGit(fp, p, summary, author)
} }
@ -247,3 +262,17 @@ func (fp *FilePages) RecentChanges() ([]Change, error) {
return changes, nil return changes, nil
} }
func (fp *FilePages) AllPages() ([]Page, error) {
files, err := ioutil.ReadDir(fp.dirname)
if err != nil {
return nil, err
}
var pages []Page
for _, file := range files {
pages = append(pages, fp.Get(file.Name()))
}
return pages, nil
}

29
main.go
View File

@ -29,7 +29,15 @@ const (
// Page // Page
type Page struct { type Page struct {
// Name is the filename of the page
Name string
// Title is the human-readable title of the page
Title string
Content string Content string
Refs []string
} }
type DiffPage struct { type DiffPage struct {
@ -63,10 +71,11 @@ type PagesRepository interface {
} }
type indexPage struct { type indexPage struct {
Session *Session Session *Session
Title string Title string
Name string Name string
Content template.HTML Content template.HTML
Backrefs []string
} }
type editPage struct { type editPage struct {
@ -395,7 +404,8 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
page = "Home" page = "Home"
} }
pageText := mp.Get(page).Content mpPage := mp.Get(page)
pageText := mpPage.Content
if pageText == "" { if pageText == "" {
http.Redirect(w, r, "/edit/"+page, 302) http.Redirect(w, r, "/edit/"+page, 302)
return return
@ -438,10 +448,11 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
data := indexPage{ data := indexPage{
Session: sess, Session: sess,
Title: strings.Replace(page, "_", " ", -1), Title: strings.Replace(page, "_", " ", -1),
Content: template.HTML(pageText), Content: template.HTML(pageText),
Name: page, Name: page,
Backrefs: mpPage.Refs,
} }
t, err := template.ParseFiles("templates/layout.html", "templates/view.html") t, err := template.ParseFiles("templates/layout.html", "templates/view.html")

View File

@ -3,6 +3,15 @@
<div class="content"> <div class="content">
{{ .Content }} {{ .Content }}
</div> </div>
<div class="backrefs">
<h3>Backrefs</h3>
<ul>
{{ range $ref := .Backrefs }}
<li><a href="/{{ $ref }}">{{ $ref }}</a></li>
{{ end }}
</ul>
</div>
{{ end }} {{ end }}
{{ define "navbar" }} {{ define "navbar" }}

32
util.go
View File

@ -2,9 +2,16 @@ package main
import ( import (
"math/rand" "math/rand"
"regexp"
"strings"
"time" "time"
) )
type ParsedLink struct {
Name string
PageName string
}
var random *rand.Rand var random *rand.Rand
func init() { func init() {
@ -20,3 +27,28 @@ func RandStringBytes(n int) string {
} }
return string(b) return string(b)
} }
func ParseLinks(content string) ([]ParsedLink, error) {
hrefRE := regexp.MustCompile(`#?\[\[\s*([\w.\- ]+)\s*\]\]`)
links := hrefRE.FindAllStringSubmatch(content, -1)
var result []ParsedLink
for _, matches := range links {
link := matches[0]
if link[0] == '#' {
link = strings.TrimPrefix(link, "#[[")
} else {
link = strings.TrimPrefix(link, "[[")
}
link = strings.TrimSuffix(link, "]]")
link = strings.TrimSpace(link)
l := strings.Replace(link, " ", "_", -1)
result = append(result, ParsedLink{link, l})
}
return result, nil
}