From ade49d8845e044292da64fcbe72a1f7ee91e66d9 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 2 Nov 2016 16:09:45 -0400 Subject: some views --- src/edit/dir.go | 10 +++++--- src/edit/git.go | 14 ++++++++--- src/edit/views.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++---- src/util/template.go | 44 ++--------------------------------- 4 files changed, 82 insertions(+), 52 deletions(-) (limited to 'src') diff --git a/src/edit/dir.go b/src/edit/dir.go index cc99774..b9fa170 100644 --- a/src/edit/dir.go +++ b/src/edit/dir.go @@ -17,7 +17,6 @@ func ServeGit(out http.ResponseWriter, in *http.Request) { upath = "/" + upath } upath = path.Clean(upath) - upath = upath[1:] errcheck(GitPull()) tree, err := GitLsTree() @@ -28,10 +27,15 @@ func ServeGit(out http.ResponseWriter, in *http.Request) { http.NotFound(out, in) return } - if file.Type == "tree" && !strings.HasSuffix(in.URL.Path, "/") { + if file.Type == "tree" { + if !strings.HasSuffix(in.URL.Path, "/") { out.Header().Set("Location", path.Base(upath)+"/") out.WriteHeader(http.StatusMovedPermanently) - return + return + } + if !strings.HasSuffix(upath, "/") { + upath = upath + "/" + } } } diff --git a/src/edit/git.go b/src/edit/git.go index 5679e49..edeb111 100644 --- a/src/edit/git.go +++ b/src/edit/git.go @@ -91,6 +91,10 @@ type GitFile struct { Size int64 } +func (f GitFile) Cat() ([]byte, error) { + return exec.Command("git", "cat-file", "blob", f.Hash).Output() +} + type GitTree map[string]GitFile var ( @@ -162,19 +166,23 @@ func GitLsTree() (GitTree, error) { // the root tree, we have to +1. ret := make(GitTree, len(lines)) // Add the root tree - ret[""] = GitFile{ + ret["/"] = GitFile{ Mode: int32(40000), Type: "tree", Hash: treeish, Size: -1, } // Parse the lines from git ls-tree - for _, line := range lines[:len(ret)] { + for _, line := range lines { + if len(line) == 0 { + continue + } name, file, err := parseGitTreeLine(line) if err != nil { + panic(err) return nil, err } - ret[name] = file + ret["/"+name] = file } return ret, nil } diff --git a/src/edit/views.go b/src/edit/views.go index 90b6f1b..74d8006 100644 --- a/src/edit/views.go +++ b/src/edit/views.go @@ -2,16 +2,74 @@ package main import ( "io" + "mime" + "util" + "bytes" + "path" + "strings" ) +var ( + tmplPage = util.NewTemplate("got/page.html.got") + tmplViewTree = util.NewTemplate("got/view_tree.got") + tmplViewBlob = util.NewTemplate("got/view_blob.got") + tmplModified = util.NewTemplate("got/modified.got") + tmplDeleted = util.NewTemplate("got/deleted.got") +) + +func renderPage(w io.Writer, title, head, body string) error { + return tmplPage.Execute(w, map[string]string{ + "title": title, + "head": head, + "body": body, + }) +} + func renderViewTree(w io.Writer, upath string, tree GitTree) error { - // TODO - return nil + // Pre-processing + files := make(GitTree) + for name, file := range tree { + if !strings.HasPrefix(name, upath) { + continue + } + name = strings.TrimPrefix(name, upath) + if len(name) == 0 || strings.Contains(name, "/") { + continue + } + files[name] = file + + } + // Component Render + var buf bytes.Buffer + err := tmplViewTree.Execute(&buf, map[string]interface{}{ + "path": upath, + "files": files, + }) + if err != nil { + return err + } + // Page render + return renderPage(w, upath, "", buf.String()) } func renderViewBlob(w io.Writer, upath string, file GitFile) error { - // TODO - return nil + // Pre-processing + content, err := file.Cat() + if err != nil { + return err + } + // Component render + var buf bytes.Buffer + err = tmplViewBlob.Execute(&buf, map[string]string{ + "path": upath, + "ctype": mime.TypeByExtension(path.Ext(upath)), + "content": string(content), + }) + if err != nil { + return err + } + // Page render + return renderPage(w, upath, "", buf.String()) } func renderModified(w io.Writer, upath string) error { diff --git a/src/util/template.go b/src/util/template.go index f42875a..3c5bd40 100644 --- a/src/util/template.go +++ b/src/util/template.go @@ -3,54 +3,14 @@ package util import ( "path" "text/template" - "time" + "strings" ) func NewTemplate(filenames ...string) *template.Template { return template.Must(template.New(path.Base(filenames[0])). Funcs(template.FuncMap{ // Form input helpers - "value": func(v interface{}) string { - if v == nil { - return "" - } - return "value=\"" + template.HTMLEscapeString(v.(string)) + "\"" - }, - "checked": func(v1 interface{}, v2 interface{}) string { - if v1 == nil || v2 == nil { - return "" - } - if v1 == v2 { - return "checked" - } - return "" - }, - "selected": func(v1 interface{}, v2 interface{}) string { - if v1 == nil || v2 == nil { - return "" - } - if v1 == v2 { - return "selected" - } - return "" - }, - // Form result helpers - "q": func(v interface{}) string { - if v == nil || v.(string) == "" { - return "none" - } - return "" + template.HTMLEscapeString(v.(string)) + "" - }, - "have": func(v interface{}) bool { - return v != nil && v.(string) == "on" - }, - "date": func(v string) string { - t, err := time.Parse("2006-01-02", v) - if err != nil { - panic(err) - } - return t.Format("January 2, 2006") - }, + "hasprefix": strings.HasPrefix, }). ParseFiles(filenames...)) } -- cgit v1.2.3