diff --git a/file.go b/file.go index 2f47c30..d7f63bf 100644 --- a/file.go +++ b/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 +} diff --git a/main.go b/main.go index 60320e1..b09e341 100644 --- a/main.go +++ b/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") diff --git a/memory.go b/memory.go index 81c93b9..cf3dd5b 100644 --- a/memory.go +++ b/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") +} diff --git a/templates/recent.html b/templates/recent.html new file mode 100644 index 0000000..5d8fac0 --- /dev/null +++ b/templates/recent.html @@ -0,0 +1,15 @@ +{{ define "navbar" }} +{{ end }} + +{{ define "content" }} +
{{ $change.Date }} | +{{ $change.Page }} | +