From 85db279e33804c118b019c5f1e7666798df3e1f5 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 2 Nov 2016 19:57:01 -0400 Subject: better text-type detection --- got/view_blob.got | 2 +- src/edit/util.go | 11 +++++++++++ src/edit/views.go | 21 +++++++++++++++------ src/util/template.go | 16 ---------------- 4 files changed, 27 insertions(+), 23 deletions(-) delete mode 100644 src/util/template.go 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 @@

{{.path | html}}

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

-{{if hasprefix .ctype "text/"}} +{{if istext .ctype}}
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...)) -} -- cgit v1.2.3