diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-11-02 19:57:01 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-11-02 19:57:01 -0400 |
commit | 85db279e33804c118b019c5f1e7666798df3e1f5 (patch) | |
tree | dfccd3ba1710fbfc68942f61690b24ca6a3fb364 | |
parent | b51a2c03985f09845e498ece625f5e8a309b6086 (diff) |
better text-type detection
-rw-r--r-- | got/view_blob.got | 2 | ||||
-rw-r--r-- | src/edit/util.go | 11 | ||||
-rw-r--r-- | src/edit/views.go | 21 | ||||
-rw-r--r-- | src/util/template.go | 16 |
4 files changed, 27 insertions, 23 deletions
diff --git a/got/view_blob.got b/got/view_blob.got index 63039f2..1a3c4cd 100644 --- a/got/view_blob.got +++ b/got/view_blob.got @@ -1,7 +1,7 @@ <!-- -*- Mode: HTML -*- --> <h1>{{.path | html}}</h1> <p>Content-Type: {{.ctype | html}}</p> -{{if hasprefix .ctype "text/"}} +{{if istext .ctype}} <form method="POST" enctype="multipart/form-data"> <input type="hidden" name="_method" value="PUT"> <textarea name="_body">{{.content | html }}</textarea> diff --git a/src/edit/util.go b/src/edit/util.go index b6dfde1..5835b10 100644 --- a/src/edit/util.go +++ b/src/edit/util.go @@ -5,6 +5,7 @@ import ( "net/http" "os/exec" "path" + "strings" ) type exitError exec.ExitError @@ -34,3 +35,13 @@ func getctype(name string, content []byte) string { } return ctype } + +func istext(ctype string) bool { + i := strings.Index(ctype, ";") + if i == -1 { + i = len(ctype) + } + ctype = strings.TrimSpace(strings.ToLower(ctype[0:i])) + + return strings.HasPrefix(ctype, "text/") || strings.HasSuffix(ctype, "+xml") +} diff --git a/src/edit/views.go b/src/edit/views.go index f9ff618..0998d4a 100644 --- a/src/edit/views.go +++ b/src/edit/views.go @@ -3,16 +3,25 @@ package main import ( "bytes" "io" + "path" "strings" - "util" + "text/template" ) +func newTemplate(filenames ...string) *template.Template { + return template.Must(template.New(path.Base(filenames[0])). + Funcs(template.FuncMap{ + "istext": istext, + }). + ParseFiles(filenames...)) +} + 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") + tmplPage = newTemplate("got/page.html.got") + tmplViewTree = newTemplate("got/view_tree.got") + tmplViewBlob = newTemplate("got/view_blob.got") + tmplModified = newTemplate("got/modified.got") + tmplDeleted = newTemplate("got/deleted.got") ) func renderPage(w io.Writer, title, head, body string) error { diff --git a/src/util/template.go b/src/util/template.go deleted file mode 100644 index ec5ac18..0000000 --- a/src/util/template.go +++ /dev/null @@ -1,16 +0,0 @@ -package util - -import ( - "path" - "strings" - "text/template" -) - -func NewTemplate(filenames ...string) *template.Template { - return template.Must(template.New(path.Base(filenames[0])). - Funcs(template.FuncMap{ - // Form input helpers - "hasprefix": strings.HasPrefix, - }). - ParseFiles(filenames...)) -} |