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 /src/edit | |
parent | b51a2c03985f09845e498ece625f5e8a309b6086 (diff) |
better text-type detection
Diffstat (limited to 'src/edit')
-rw-r--r-- | src/edit/util.go | 11 | ||||
-rw-r--r-- | src/edit/views.go | 21 |
2 files changed, 26 insertions, 6 deletions
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 { |