improve backrefs for view and edit
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
ef5e074cb0
commit
6c04d9871f
14
backref.go
14
backref.go
|
|
@ -41,7 +41,7 @@ func processBackrefs(fp *FilePages) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadBackrefs(fp *FilePages, p string) ([]string, error) {
|
func loadBackrefs(fp *FilePages, p string) ([]Backref, error) {
|
||||||
refs := make(Refs)
|
refs := make(Refs)
|
||||||
p = strings.Replace(p, " ", "_", -1)
|
p = strings.Replace(p, " ", "_", -1)
|
||||||
|
|
||||||
|
|
@ -57,5 +57,15 @@ func loadBackrefs(fp *FilePages, p string) ([]string, error) {
|
||||||
return nil, err
|
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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
28
file.go
28
file.go
|
|
@ -29,18 +29,34 @@ func NewFilePages(dirname string) PagesRepository {
|
||||||
|
|
||||||
func (fp *FilePages) Get(title string) Page {
|
func (fp *FilePages) Get(title string) Page {
|
||||||
name := strings.Replace(title, " ", "_", -1)
|
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))
|
f, err := os.Open(filepath.Join(fp.dirname, name))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Page{}
|
return Page{
|
||||||
|
Title: title,
|
||||||
|
Name: name,
|
||||||
|
Content: "",
|
||||||
|
Refs: refs,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
body, err := ioutil.ReadAll(f)
|
body, err := ioutil.ReadAll(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Page{}
|
return Page{
|
||||||
}
|
Title: title,
|
||||||
refs, err := loadBackrefs(fp, name)
|
Name: name,
|
||||||
if err != nil {
|
Content: "",
|
||||||
return Page{}
|
Refs: refs,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Page{
|
return Page{
|
||||||
Name: name,
|
Name: name,
|
||||||
|
|
|
||||||
34
main.go
34
main.go
|
|
@ -27,6 +27,11 @@ const (
|
||||||
RedirectURI = "https://wiki.p83.nl/auth/callback"
|
RedirectURI = "https://wiki.p83.nl/auth/callback"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Backref struct {
|
||||||
|
Name string
|
||||||
|
Title string
|
||||||
|
}
|
||||||
|
|
||||||
// Page
|
// Page
|
||||||
type Page struct {
|
type Page struct {
|
||||||
// Name is the filename of the page
|
// Name is the filename of the page
|
||||||
|
|
@ -37,7 +42,7 @@ type Page struct {
|
||||||
|
|
||||||
Content string
|
Content string
|
||||||
|
|
||||||
Refs []string
|
Refs []Backref
|
||||||
}
|
}
|
||||||
|
|
||||||
type DiffPage struct {
|
type DiffPage struct {
|
||||||
|
|
@ -75,15 +80,16 @@ type indexPage struct {
|
||||||
Title string
|
Title string
|
||||||
Name string
|
Name string
|
||||||
Content template.HTML
|
Content template.HTML
|
||||||
Backrefs []string
|
Backrefs []Backref
|
||||||
}
|
}
|
||||||
|
|
||||||
type editPage struct {
|
type editPage struct {
|
||||||
Session *Session
|
Session *Session
|
||||||
Title string
|
Title string
|
||||||
Content string
|
Content string
|
||||||
Name string
|
Name string
|
||||||
Editor template.HTML
|
Editor template.HTML
|
||||||
|
Backrefs []Backref
|
||||||
}
|
}
|
||||||
|
|
||||||
type historyPage struct {
|
type historyPage struct {
|
||||||
|
|
@ -339,7 +345,8 @@ func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
page = "Home"
|
page = "Home"
|
||||||
}
|
}
|
||||||
|
|
||||||
pageText := mp.Get(page).Content
|
mpPage := mp.Get(page)
|
||||||
|
pageText := mpPage.Content
|
||||||
|
|
||||||
jsonEditor := pageText != "" && pageText[0] == '{'
|
jsonEditor := pageText != "" && pageText[0] == '{'
|
||||||
|
|
||||||
|
|
@ -366,11 +373,12 @@ func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
data := editPage{
|
data := editPage{
|
||||||
Session: sess,
|
Session: sess,
|
||||||
Title: strings.Replace(page, "_", " ", -1),
|
Title: strings.Replace(page, "_", " ", -1),
|
||||||
Content: pageText,
|
Content: pageText,
|
||||||
Editor: editor,
|
Editor: editor,
|
||||||
Name: page,
|
Name: page,
|
||||||
|
Backrefs: mpPage.Refs,
|
||||||
}
|
}
|
||||||
|
|
||||||
t, err := template.ParseFiles("templates/layout.html", "templates/edit.html")
|
t, err := template.ParseFiles("templates/layout.html", "templates/edit.html")
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,17 @@
|
||||||
<input type="hidden" name="p" value="{{ .Name }}" />
|
<input type="hidden" name="p" value="{{ .Name }}" />
|
||||||
{{ .Editor }}
|
{{ .Editor }}
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
{{ if .Backrefs }}
|
||||||
|
<div class="backrefs">
|
||||||
|
<h3>Backrefs</h3>
|
||||||
|
<ul>
|
||||||
|
{{ range $ref := .Backrefs }}
|
||||||
|
<li><a href="/{{ $ref.Name }}">{{ $ref.Title }}</a></li>
|
||||||
|
{{ end }}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
{{ define "content_head" }}
|
{{ define "content_head" }}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
<h3>Backrefs</h3>
|
<h3>Backrefs</h3>
|
||||||
<ul>
|
<ul>
|
||||||
{{ range $ref := .Backrefs }}
|
{{ range $ref := .Backrefs }}
|
||||||
<li><a href="/{{ $ref }}">{{ $ref }}</a></li>
|
<li><a href="/{{ $ref.Name }}">{{ $ref.Title }}</a></li>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user