Add recent changes

This commit is contained in:
Peter Stuifzand 2019-02-18 20:47:31 +01:00
parent d83dca2424
commit 53bb6bdf23
5 changed files with 143 additions and 0 deletions

62
file.go
View File

@ -12,6 +12,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
) )
type FilePages struct { type FilePages struct {
@ -158,3 +159,64 @@ func gitRevision(dirname, page, version string) string {
cmd.Wait() cmd.Wait()
return buf.String() return buf.String()
} }
func (fp *FilePages) RecentChanges() ([]Change, error) {
cmd := exec.Command("git", "log", "--format=--1--%nDate: %aI%n--2--", "--name-only")
cmd.Dir = fp.dirname
buf := bytes.Buffer{}
cmd.Stdout = &buf
err := cmd.Start()
if err != nil {
return nil, err
}
err = cmd.Wait()
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(&buf)
first := false
second := false
var changes []Change
var change Change
for scanner.Scan() {
line := scanner.Text()
if line == "--1--" {
second = false
first = true
continue
}
if line == "--2--" {
first = false
second = true
continue
}
if first && strings.HasPrefix(line, "Date: ") {
line = line[6:]
changeTime, err := time.Parse(time.RFC3339, line)
if err != nil {
return changes, err
}
change.Date = changeTime
continue
}
if second {
if line == "" {
continue
}
change.Page = line
}
changes = append(changes, change)
}
return changes, nil
}

57
main.go
View File

@ -11,6 +11,7 @@ import (
"p83.nl/go/indieauth" "p83.nl/go/indieauth"
"regexp" "regexp"
"strings" "strings"
"time"
) )
var ( var (
@ -38,11 +39,17 @@ type Revision struct {
Summary string Summary string
} }
type Change struct {
Page string
Date time.Time
}
type PagesRepository interface { type PagesRepository interface {
Get(p string) Page Get(p string) Page
Save(p string, page Page, summary, author string) Save(p string, page Page, summary, author string)
Exist(p string) bool Exist(p string) bool
PageHistory(p string) ([]Revision, error) PageHistory(p string) ([]Revision, error)
RecentChanges() ([]Change, error)
} }
type indexPage struct { type indexPage struct {
@ -66,10 +73,18 @@ type historyPage struct {
History []Revision History []Revision
} }
type recentPage struct {
Session *Session
Title string
Name string
Recent []Change
}
type indexHandler struct{} type indexHandler struct{}
type saveHandler struct{} type saveHandler struct{}
type editHandler struct{} type editHandler struct{}
type historyHandler struct{} type historyHandler struct{}
type recentHandler struct{}
type authHandler struct{} type authHandler struct{}
func (*authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (*authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -352,6 +367,47 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
func (h *recentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
sess, err := NewSession(w, r)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer sess.Flush()
if !sess.LoggedIn {
http.Redirect(w, r, "/", http.StatusFound)
return
}
r.ParseForm()
changes, err := mp.RecentChanges()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
t, err := template.New("layout.html").ParseFiles("templates/layout.html", "templates/recent.html")
if err != nil {
http.Error(w, err.Error(), 500)
return
}
err = t.Execute(w, recentPage{
Session: sess,
Title: "Recent changes",
Name: "Recent changes",
Recent: changes,
})
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}
func main() { func main() {
mp = NewFilePages("data") mp = NewFilePages("data")
@ -360,6 +416,7 @@ func main() {
http.Handle("/save/", &saveHandler{}) http.Handle("/save/", &saveHandler{})
http.Handle("/edit/", &editHandler{}) http.Handle("/edit/", &editHandler{})
http.Handle("/history/", &historyHandler{}) http.Handle("/history/", &historyHandler{})
http.Handle("/recent/", &recentHandler{})
http.Handle("/", &indexHandler{}) http.Handle("/", &indexHandler{})
fmt.Printf("Running on port 8080") fmt.Printf("Running on port 8080")

View File

@ -35,3 +35,11 @@ func (mp *MemoryPages) Exist(p string) bool {
_, e := mp.pages[p] _, e := mp.pages[p]
return e return e
} }
func (mp *MemoryPages) PageHistory(p string) ([]Revision, error) {
panic("implement me")
}
func (mp *MemoryPages) RecentChanges() ([]Change, error) {
panic("implement me")
}

15
templates/recent.html Normal file
View File

@ -0,0 +1,15 @@
{{ define "navbar" }}
{{ end }}
{{ define "content" }}
<h1 class="title">{{ .Title }}</h1>
<table class="table">
{{ range $index, $change := .Recent }}
<tr>
<td>{{ $change.Date }}</td>
<td><a href="/{{ $change.Page }}">{{ $change.Page }}</a></td>
</tr>
{{ end }}
</table>
{{ end }}

View File

@ -9,6 +9,7 @@
{{ if $.Session.LoggedIn }} {{ if $.Session.LoggedIn }}
<a href="/edit/{{ .Name }}" class="navbar-item">Edit</a> <a href="/edit/{{ .Name }}" class="navbar-item">Edit</a>
<a href="/history/{{ .Name }}" class="navbar-item">History</a> <a href="/history/{{ .Name }}" class="navbar-item">History</a>
<a href="/recent/" class="navbar-item">Recent Changes</a>
<a href="/auth/logout" class="navbar-item">Logout</a> <a href="/auth/logout" class="navbar-item">Logout</a>
<span class="navbar-item"><b>{{ $.Session.Me }}</b></span> <span class="navbar-item"><b>{{ $.Session.Me }}</b></span>
{{ else }} {{ else }}