Browse Source

Add recent changes

master
Peter Stuifzand 2 years ago
parent
commit
53bb6bdf23
  1. 62
      file.go
  2. 57
      main.go
  3. 8
      memory.go
  4. 15
      templates/recent.html
  5. 1
      templates/view.html

62
file.go

@ -12,6 +12,7 @@ import (
"os/exec"
"path/filepath"
"strings"
"time"
)
type FilePages struct {
@ -158,3 +159,64 @@ func gitRevision(dirname, page, version string) string {
cmd.Wait()
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

@ -11,6 +11,7 @@ import (
"p83.nl/go/indieauth"
"regexp"
"strings"
"time"
)
var (
@ -38,11 +39,17 @@ type Revision struct {
Summary string
}
type Change struct {
Page string
Date time.Time
}
type PagesRepository interface {
Get(p string) Page
Save(p string, page Page, summary, author string)
Exist(p string) bool
PageHistory(p string) ([]Revision, error)
RecentChanges() ([]Change, error)
}
type indexPage struct {
@ -66,10 +73,18 @@ type historyPage struct {
History []Revision
}
type recentPage struct {
Session *Session
Title string
Name string
Recent []Change
}
type indexHandler struct{}
type saveHandler struct{}
type editHandler struct{}
type historyHandler struct{}
type recentHandler struct{}
type authHandler struct{}
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() {
mp = NewFilePages("data")
@ -360,6 +416,7 @@ func main() {
http.Handle("/save/", &saveHandler{})
http.Handle("/edit/", &editHandler{})
http.Handle("/history/", &historyHandler{})
http.Handle("/recent/", &recentHandler{})
http.Handle("/", &indexHandler{})
fmt.Printf("Running on port 8080")

8
memory.go

@ -35,3 +35,11 @@ func (mp *MemoryPages) Exist(p string) bool {
_, e := mp.pages[p]
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

@ -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 }}

1
templates/view.html

@ -9,6 +9,7 @@
{{ if $.Session.LoggedIn }}
<a href="/edit/{{ .Name }}" class="navbar-item">Edit</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>
<span class="navbar-item"><b>{{ $.Session.Me }}</b></span>
{{ else }}

Loading…
Cancel
Save