summaryrefslogtreecommitdiff
path: root/src
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 /src
parent60632ea541ac6fc57c8158bf44c625c526e1b215 (diff)
some views
Diffstat (limited to 'src')
-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
4 files changed, 82 insertions, 52 deletions
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...))
}