From ade49d8845e044292da64fcbe72a1f7ee91e66d9 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 2 Nov 2016 16:09:45 -0400 Subject: some views --- got/deleted.got | 0 got/edit.got | 16 ------------- got/login.got | 10 -------- got/modified.got | 0 got/page.html.got | 17 +++++++++++++ got/template.html.got | 31 ------------------------ got/upload.got | 11 --------- got/view_blob.got | 26 ++++++++++++++++++++ got/view_tree.got | 16 +++++++++++++ src/edit/dir.go | 10 +++++--- src/edit/git.go | 14 ++++++++--- src/edit/views.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++---- src/util/template.go | 44 ++-------------------------------- 13 files changed, 141 insertions(+), 120 deletions(-) create mode 100644 got/deleted.got delete mode 100644 got/edit.got delete mode 100644 got/login.got create mode 100644 got/modified.got create mode 100644 got/page.html.got delete mode 100644 got/template.html.got delete mode 100644 got/upload.got create mode 100644 got/view_blob.got create mode 100644 got/view_tree.got diff --git a/got/deleted.got b/got/deleted.got new file mode 100644 index 0000000..e69de29 diff --git a/got/edit.got b/got/edit.got deleted file mode 100644 index 3fcdd23..0000000 --- a/got/edit.got +++ /dev/null @@ -1,16 +0,0 @@ - -

{{.path | html}}

-

Content-Type: {{.ctype | html}}

-
- - - - -
-
- - -

Instead of editing this in your browser, you can just upload a new copy.

- - -
diff --git a/got/login.got b/got/login.got deleted file mode 100644 index 546d2e3..0000000 --- a/got/login.got +++ /dev/null @@ -1,10 +0,0 @@ - -{{if neq .url ""}} -

You must log in to do that.

-{{endif}} -
- - - - -
diff --git a/got/modified.got b/got/modified.got new file mode 100644 index 0000000..e69de29 diff --git a/got/page.html.got b/got/page.html.got new file mode 100644 index 0000000..a0f34f9 --- /dev/null +++ b/got/page.html.got @@ -0,0 +1,17 @@ + + + + + + + + + {{.title | html}} + + + {{.head}} + + + {{.body}} + + diff --git a/got/template.html.got b/got/template.html.got deleted file mode 100644 index ab9e633..0000000 --- a/got/template.html.got +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - {{.title | html}} - - - {{.head}} - - -
- {{if eq .userid ""}} -

You are not logged in

- {{else}} -

You are logged in as {{.session.userid | html}}

-
- - - -
- {{endif}} -
-
- {{.body}} -
- - diff --git a/got/upload.got b/got/upload.got deleted file mode 100644 index 069d9f8..0000000 --- a/got/upload.got +++ /dev/null @@ -1,11 +0,0 @@ - -

{{.path | html}}

-

Content-Type: {{.ctype | html}}

-
- - -

You can't edit this type of file in your browser. But you can - upload a new version.

- - -
diff --git a/got/view_blob.got b/got/view_blob.got new file mode 100644 index 0000000..a1d7e2c --- /dev/null +++ b/got/view_blob.got @@ -0,0 +1,26 @@ + +

{{.path | html}}

+

Content-Type: {{.ctype | html}}

+{{if hasprefix .ctype "text/"}} +
+ + + + +
+{{else}} +

You can't edit this type of file in your browser.

+{{end}} +
+ +

Instead of editing this in your browser, you can just upload a new copy.

+ + + +
diff --git a/got/view_tree.got b/got/view_tree.got new file mode 100644 index 0000000..46f4af7 --- /dev/null +++ b/got/view_tree.got @@ -0,0 +1,16 @@ + +

Index of {{.path | html}}

+ + + + + + + {{range $name, $file := .files}} + + + + + + {{end}} +
..tree-
{{$name | html}}{{$file.Type | html}}{{if lt $file.Size 0}}-{{else}}{{$file.Size | html}}{{end}}
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