summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-11-02 16:09:45 -0400
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-11-02 16:09:45 -0400
commitade49d8845e044292da64fcbe72a1f7ee91e66d9 (patch)
treeb2030f99f28b1d9366989df3d74da5d874d6dbd9
parent60632ea541ac6fc57c8158bf44c625c526e1b215 (diff)
some views
-rw-r--r--got/deleted.got0
-rw-r--r--got/login.got10
-rw-r--r--got/modified.got0
-rw-r--r--got/page.html.got17
-rw-r--r--got/template.html.got31
-rw-r--r--got/upload.got11
-rw-r--r--got/view_blob.got (renamed from got/edit.got)14
-rw-r--r--got/view_tree.got16
-rw-r--r--src/edit/dir.go10
-rw-r--r--src/edit/git.go14
-rw-r--r--src/edit/views.go66
-rw-r--r--src/util/template.go44
12 files changed, 127 insertions, 106 deletions
diff --git a/got/deleted.got b/got/deleted.got
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/got/deleted.got
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 @@
-<!-- -*- Mode: HTML -*- -->
-{{if neq .url ""}}
-<p>You must log in to do that.</p>
-{{endif}}
-<form method="POST" enctype="multipart/form-data">
- <input type="hidden" name="_method" value="PUT">
- <label>Username: <input type="text" name="userid" /></label>
- <label>Password: <input type="password" name="password" /></label>
- <input type="submit" value="Save">
-</form>
diff --git a/got/modified.got b/got/modified.got
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/got/modified.got
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 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <!-- the meta tags must come first -->
+
+ <title>{{.title | html}}</title>
+
+ <link href="/style.css" rel="stylesheet">
+ {{.head}}
+ </head>
+ <body>
+ {{.body}}
+ </body>
+</html>
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 @@
-<!DOCTYPE html>
-<html lang="en">
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <!-- the meta tags must come first -->
-
- <title>{{.title | html}}</title>
-
- <link href="/style.css" rel="stylesheet">
- {{.head}}
- </head>
- <body>
- <header>
- {{if eq .userid ""}}
- <p>You are not logged in</p>
- {{else}}
- <p>You are logged in as {{.session.userid | html}}</p>
- <form action="/session" method="POST">
- <input type="hidden" name="_method" value="DELETE" />
- <input type="hidden" name="_xsrf_token" value="{{.session.id | html}}" />
- <input type="submit" value="Log out" />
- </form>
- {{endif}}
- </header>
- <article>
- {{.body}}
- </article>
- </body>
-</html>
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 @@
-<!-- -*- Mode: HTML -*- -->
-<h1>{{.path | html}}</h1>
-<p>Content-Type: {{.ctype | html}}</p>
-<form method="POST" enctype="multipart/form-data">
- <input type="hidden" name="_method" value="PUT" />
- <input type="hidden" name="_xsrf_token" value="{{.xsrf_token | html}}">
- <p>You can't edit this type of file in your browser. But you can
- upload a new version.</p>
- <input type="file" name="_body" required=required />
- <input type="submit" value="Upload" />
-</form>
diff --git a/got/edit.got b/got/view_blob.got
index 3fcdd23..a1d7e2c 100644
--- a/got/edit.got
+++ b/got/view_blob.got
@@ -1,16 +1,26 @@
<!-- -*- Mode: HTML -*- -->
<h1>{{.path | html}}</h1>
<p>Content-Type: {{.ctype | html}}</p>
+{{if hasprefix .ctype "text/"}}
<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="_method" value="PUT">
- <input type="hidden" name="_xsrf_token" value="{{.xsrf_token | html}}">
<textarea name="_body">{{.content | html }}</textarea>
+ <label>
+ Description of change:
+ <input type="text" value="_message" />
+ </label>
<input type="submit" value="Save">
</form>
+{{else}}
+<p>You can't edit this type of file in your browser.</p>
+{{end}}
<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="_method" value="PUT">
- <input type="hidden" name="_xsrf_token" value="{{.xsrf_token | html}}">
<p>Instead of editing this in your browser, you can just upload a new copy.</p>
<input type="file" name="_body" required=required>
+ <label>
+ Description of change:
+ <input type="text" value="_message" />
+ </label>
<input type="submit" value="Upload">
</form>
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 @@
+<!-- -*- Mode: HTML -*- -->
+<h1>Index of {{.path | html}}</h1>
+<table>
+ <tr>
+ <td><a href="..">..</a></td>
+ <td>tree</td>
+ <td>-</td>
+ </tr>
+ {{range $name, $file := .files}}
+ <tr>
+ <td><a href="{{$name | html}}">{{$name | html}}</a></td>
+ <td>{{$file.Type | html}}</td>
+ <td>{{if lt $file.Size 0}}-{{else}}{{$file.Size | html}}{{end}}</td>
+ </tr>
+ {{end}}
+</table>
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 "<output class=none>none</output>"
- }
- return "<output><q>" + template.HTMLEscapeString(v.(string)) + "</q></output>"
- },
- "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...))
}