Show more information in back references
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
191db91095
commit
86f4d68d9c
36
backref.go
36
backref.go
|
@ -2,12 +2,18 @@ package main
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Refs map[string][]string
|
||||
type Reference struct {
|
||||
Link ParsedLink
|
||||
Name string
|
||||
}
|
||||
|
||||
type Refs map[string][]Reference
|
||||
|
||||
func processBackrefs(fp *FilePages) error {
|
||||
pages, err := fp.AllPages()
|
||||
|
@ -19,14 +25,33 @@ func processBackrefs(fp *FilePages) error {
|
|||
}
|
||||
|
||||
for _, page := range pages {
|
||||
links, err := ParseLinks(page.Content)
|
||||
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)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, link := range links {
|
||||
refs[link.PageName] = append(refs[link.PageName], page.Name)
|
||||
fmt.Println(link.Line)
|
||||
refs[link.PageName] = append(refs[link.PageName], Reference{link, page.Name})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -60,10 +85,11 @@ func loadBackrefs(fp *FilePages, p string) ([]Backref, error) {
|
|||
var result []Backref
|
||||
|
||||
for _, ref := range refs[p] {
|
||||
title := strings.Replace(ref, "_", " ", -1)
|
||||
title := strings.Replace(ref.Name, "_", " ", -1)
|
||||
result = append(result, Backref{
|
||||
Name: ref,
|
||||
Name: ref.Name,
|
||||
Title: title,
|
||||
Line: strings.TrimLeft(ref.Link.Line, " *"),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
22
file.go
22
file.go
|
@ -31,12 +31,7 @@ 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,
|
||||
}
|
||||
refs = nil
|
||||
}
|
||||
|
||||
f, err := os.Open(filepath.Join(fp.dirname, name))
|
||||
|
@ -89,7 +84,7 @@ func (fp *FilePages) Save(p string, page Page, summary, author string) error {
|
|||
|
||||
err = processBackrefs(fp)
|
||||
if err != nil {
|
||||
return fmt.Errorf("while processing backrefs: %v", err)
|
||||
return fmt.Errorf("while processing backrefs: %s", err)
|
||||
}
|
||||
|
||||
return saveWithGit(fp, p, summary, author)
|
||||
|
@ -100,14 +95,14 @@ func saveWithGit(fp *FilePages, p string, summary, author string) error {
|
|||
cmd.Dir = fp.dirname
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return fmt.Errorf("while adding page %s: %w", p, err)
|
||||
return fmt.Errorf("while adding page %s: %s", p, err)
|
||||
}
|
||||
|
||||
cmd = exec.Command("git", "commit", "-m", "Changes to "+p+" by "+author+"\n\n"+summary)
|
||||
cmd.Dir = fp.dirname
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return fmt.Errorf("while commiting page %s: %w", p, err)
|
||||
return fmt.Errorf("while commiting page %s: %s", p, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -156,8 +151,7 @@ func (fp *FilePages) PageHistory(p string) ([]Revision, error) {
|
|||
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
log.Println("Start")
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("while starting", err)
|
||||
}
|
||||
|
||||
buf := bufio.NewScanner(output)
|
||||
|
@ -187,8 +181,7 @@ func (fp *FilePages) PageHistory(p string) ([]Revision, error) {
|
|||
}
|
||||
|
||||
if err := cmd.Wait(); err != nil {
|
||||
log.Println("wait")
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("while waiting", err)
|
||||
}
|
||||
|
||||
return revisions, nil
|
||||
|
@ -287,6 +280,9 @@ func (fp *FilePages) AllPages() ([]Page, error) {
|
|||
|
||||
var pages []Page
|
||||
for _, file := range files {
|
||||
if file.Name() == "backrefs.json" {
|
||||
continue
|
||||
}
|
||||
pages = append(pages, fp.Get(file.Name()))
|
||||
}
|
||||
|
||||
|
|
1
main.go
1
main.go
|
@ -34,6 +34,7 @@ const (
|
|||
type Backref struct {
|
||||
Name string
|
||||
Title string
|
||||
Line string
|
||||
}
|
||||
|
||||
// Page
|
||||
|
|
|
@ -10,14 +10,18 @@
|
|||
</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>
|
||||
<div class="backrefs content">
|
||||
<h3>Linked references</h3>
|
||||
<ul>
|
||||
{{ range $ref := .Backrefs }}
|
||||
<li><a href="/{{ $ref.Name }}">{{ $ref.Title }}</a>
|
||||
<ul>
|
||||
<li>{{ $ref.Line }}</li>
|
||||
</ul>
|
||||
</li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
</div>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
|
|
|
@ -115,6 +115,12 @@
|
|||
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=20)";
|
||||
filter: alpha(opacity=20);
|
||||
}
|
||||
|
||||
.backrefs {
|
||||
padding: 24px;
|
||||
background: #deeeee;
|
||||
border-top: 3px solid #acc;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
@ -4,11 +4,15 @@
|
|||
{{ .Content }}
|
||||
</div>
|
||||
|
||||
<div class="backrefs">
|
||||
<div class="backrefs content">
|
||||
<h3>Backrefs</h3>
|
||||
<ul>
|
||||
{{ range $ref := .Backrefs }}
|
||||
<li><a href="/{{ $ref.Name }}">{{ $ref.Title }}</a></li>
|
||||
<li><a href="/{{ $ref.Name }}">{{ $ref.Title }}</a>
|
||||
<ul>
|
||||
<li>{{ $ref.Line }}</li>
|
||||
</ul>
|
||||
</li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
</div>
|
||||
|
|
8
util.go
8
util.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
@ -10,6 +11,7 @@ import (
|
|||
type ParsedLink struct {
|
||||
Name string
|
||||
PageName string
|
||||
Line string
|
||||
}
|
||||
|
||||
var random *rand.Rand
|
||||
|
@ -29,14 +31,14 @@ func RandStringBytes(n int) string {
|
|||
}
|
||||
|
||||
func ParseLinks(content string) ([]ParsedLink, error) {
|
||||
hrefRE := regexp.MustCompile(`#?\[\[\s*([^\]]+)\s*\]\]`)
|
||||
hrefRE := regexp.MustCompile(`(?m)^(.*(#?\[\[\s*([^\]]+)\s*\]\]).*)$`)
|
||||
|
||||
links := hrefRE.FindAllStringSubmatch(content, -1)
|
||||
|
||||
var result []ParsedLink
|
||||
|
||||
for _, matches := range links {
|
||||
link := matches[0]
|
||||
link := matches[2]
|
||||
|
||||
if link[0] == '#' {
|
||||
link = strings.TrimPrefix(link, "#[[")
|
||||
|
@ -47,7 +49,7 @@ func ParseLinks(content string) ([]ParsedLink, error) {
|
|||
link = strings.TrimSuffix(link, "]]")
|
||||
link = strings.TrimSpace(link)
|
||||
l := cleanNameURL(link)
|
||||
result = append(result, ParsedLink{link, l})
|
||||
result = append(result, ParsedLink{link, l, matches[0]})
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
|
Loading…
Reference in New Issue
Block a user