diff --git a/backref.go b/backref.go index af65e53..5c087d0 100644 --- a/backref.go +++ b/backref.go @@ -41,7 +41,7 @@ func processBackrefs(fp *FilePages) error { return err } -func loadBackrefs(fp *FilePages, p string) ([]string, error) { +func loadBackrefs(fp *FilePages, p string) ([]Backref, error) { refs := make(Refs) p = strings.Replace(p, " ", "_", -1) @@ -57,5 +57,15 @@ func loadBackrefs(fp *FilePages, p string) ([]string, error) { return nil, err } - return refs[p], nil + var result []Backref + + for _, ref := range refs[p] { + title := strings.Replace(ref, "_", " ", -1) + result = append(result, Backref{ + Name: ref, + Title: title, + }) + } + + return result, nil } diff --git a/file.go b/file.go index 54a4c8c..c8bb9c8 100644 --- a/file.go +++ b/file.go @@ -29,18 +29,34 @@ func NewFilePages(dirname string) PagesRepository { func (fp *FilePages) Get(title string) Page { name := strings.Replace(title, " ", "_", -1) + refs, err := loadBackrefs(fp, name) + if err != nil { + return Page{ + Title: title, + Name: name, + Content: "", + Refs: nil, + } + } + f, err := os.Open(filepath.Join(fp.dirname, name)) if err != nil { - return Page{} + return Page{ + Title: title, + Name: name, + Content: "", + Refs: refs, + } } defer f.Close() body, err := ioutil.ReadAll(f) if err != nil { - return Page{} - } - refs, err := loadBackrefs(fp, name) - if err != nil { - return Page{} + return Page{ + Title: title, + Name: name, + Content: "", + Refs: refs, + } } return Page{ Name: name, diff --git a/main.go b/main.go index 089c662..c86222f 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,11 @@ const ( RedirectURI = "https://wiki.p83.nl/auth/callback" ) +type Backref struct { + Name string + Title string +} + // Page type Page struct { // Name is the filename of the page @@ -37,7 +42,7 @@ type Page struct { Content string - Refs []string + Refs []Backref } type DiffPage struct { @@ -75,15 +80,16 @@ type indexPage struct { Title string Name string Content template.HTML - Backrefs []string + Backrefs []Backref } type editPage struct { - Session *Session - Title string - Content string - Name string - Editor template.HTML + Session *Session + Title string + Content string + Name string + Editor template.HTML + Backrefs []Backref } type historyPage struct { @@ -339,7 +345,8 @@ func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { page = "Home" } - pageText := mp.Get(page).Content + mpPage := mp.Get(page) + pageText := mpPage.Content jsonEditor := pageText != "" && pageText[0] == '{' @@ -366,11 +373,12 @@ func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } data := editPage{ - Session: sess, - Title: strings.Replace(page, "_", " ", -1), - Content: pageText, - Editor: editor, - Name: page, + Session: sess, + Title: strings.Replace(page, "_", " ", -1), + Content: pageText, + Editor: editor, + Name: page, + Backrefs: mpPage.Refs, } t, err := template.ParseFiles("templates/layout.html", "templates/edit.html") diff --git a/templates/edit.html b/templates/edit.html index 3a41ded..8191507 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -8,6 +8,17 @@ {{ .Editor }} + +{{ if .Backrefs }} +