From 2df97a471094ddf1e20fc02d34bde942008e33fb Mon Sep 17 00:00:00 2001 From: mpl Date: Sat, 16 Jun 2012 10:20:32 -0700 Subject: [PATCH] Update camweb from old/template to html/template. Change-Id: I87294e861de4cd9e6f97db9f934fbc4effed4f32 --- website/camweb.go | 23 +++++++++++++---------- website/tmpl/error.html | 2 +- website/tmpl/page.html | 10 +++++----- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/website/camweb.go b/website/camweb.go index 16e20a652..260cefcfb 100644 --- a/website/camweb.go +++ b/website/camweb.go @@ -20,6 +20,7 @@ import ( "bytes" "flag" "fmt" + "html/template" "io" "io/ioutil" "log" @@ -27,7 +28,6 @@ import ( "net/http/cgi" "net/http/httputil" "net/url" - "old/template" "os" "os/exec" "path/filepath" @@ -55,27 +55,30 @@ var ( pageHtml, errorHtml *template.Template ) -var fmap = template.FormatterMap{ +var fmap = template.FuncMap{ "": textFmt, "html": htmlFmt, - "html-esc": htmlEscFmt, + "htmlesc": htmlEscFmt, } // Template formatter for "" (default) format. -func textFmt(w io.Writer, format string, x ...interface{}) { +func textFmt(w io.Writer, format string, x ...interface{}) string { writeAny(w, false, x[0]) + return "" } // Template formatter for "html" format. -func htmlFmt(w io.Writer, format string, x ...interface{}) { +func htmlFmt(w io.Writer, format string, x ...interface{}) string { writeAny(w, true, x[0]) + return "" } -// Template formatter for "html-esc" format. -func htmlEscFmt(w io.Writer, format string, x ...interface{}) { +// Template formatter for "htmlesc" format. +func htmlEscFmt(w io.Writer, format string, x ...interface{}) string { var buf bytes.Buffer writeAny(&buf, false, x[0]) template.HTMLEscape(w, buf.Bytes()) + return "" } // Write anything to w; optionally html-escaped. @@ -117,11 +120,11 @@ func servePage(w http.ResponseWriter, title, subtitle string, content []byte) { d := struct { Title string Subtitle string - Content []byte + Content template.HTML }{ title, subtitle, - content, + template.HTML(content), } if err := pageHtml.Execute(w, &d); err != nil { @@ -135,7 +138,7 @@ func readTemplate(name string) *template.Template { if err != nil { log.Fatalf("ReadFile %s: %v", fileName, err) } - t, err := template.Parse(string(data), fmap) + t, err := template.New(name).Funcs(fmap).Parse(string(data)) if err != nil { log.Fatalf("%s: %v", fileName, err) } diff --git a/website/tmpl/error.html b/website/tmpl/error.html index 51d0fe15b..15487a6b7 100644 --- a/website/tmpl/error.html +++ b/website/tmpl/error.html @@ -1,3 +1,3 @@

-{@|html-esc} +{{.}}

diff --git a/website/tmpl/page.html b/website/tmpl/page.html index 848fb7a08..2a52a1349 100644 --- a/website/tmpl/page.html +++ b/website/tmpl/page.html @@ -1,11 +1,11 @@ -{.section Title} - {@|html-esc} - Camlistore -{.or} +{{with $x := .Title}} + {{$x}} - Camlistore +{{else}} Camlistore -{.end} +{{end}} @@ -27,7 +27,7 @@
- {Content} + {{.Content}}