Update camweb from old/template to html/template.

Change-Id: I87294e861de4cd9e6f97db9f934fbc4effed4f32
This commit is contained in:
mpl 2012-06-16 10:20:32 -07:00 committed by Brad Fitzpatrick
parent ed4e781075
commit 2df97a4710
3 changed files with 19 additions and 16 deletions

View File

@ -20,6 +20,7 @@ import (
"bytes" "bytes"
"flag" "flag"
"fmt" "fmt"
"html/template"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -27,7 +28,6 @@ import (
"net/http/cgi" "net/http/cgi"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
"old/template"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -55,27 +55,30 @@ var (
pageHtml, errorHtml *template.Template pageHtml, errorHtml *template.Template
) )
var fmap = template.FormatterMap{ var fmap = template.FuncMap{
"": textFmt, "": textFmt,
"html": htmlFmt, "html": htmlFmt,
"html-esc": htmlEscFmt, "htmlesc": htmlEscFmt,
} }
// Template formatter for "" (default) format. // 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]) writeAny(w, false, x[0])
return ""
} }
// Template formatter for "html" format. // 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]) writeAny(w, true, x[0])
return ""
} }
// Template formatter for "html-esc" format. // Template formatter for "htmlesc" format.
func htmlEscFmt(w io.Writer, format string, x ...interface{}) { func htmlEscFmt(w io.Writer, format string, x ...interface{}) string {
var buf bytes.Buffer var buf bytes.Buffer
writeAny(&buf, false, x[0]) writeAny(&buf, false, x[0])
template.HTMLEscape(w, buf.Bytes()) template.HTMLEscape(w, buf.Bytes())
return ""
} }
// Write anything to w; optionally html-escaped. // Write anything to w; optionally html-escaped.
@ -117,11 +120,11 @@ func servePage(w http.ResponseWriter, title, subtitle string, content []byte) {
d := struct { d := struct {
Title string Title string
Subtitle string Subtitle string
Content []byte Content template.HTML
}{ }{
title, title,
subtitle, subtitle,
content, template.HTML(content),
} }
if err := pageHtml.Execute(w, &d); err != nil { if err := pageHtml.Execute(w, &d); err != nil {
@ -135,7 +138,7 @@ func readTemplate(name string) *template.Template {
if err != nil { if err != nil {
log.Fatalf("ReadFile %s: %v", fileName, err) 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 { if err != nil {
log.Fatalf("%s: %v", fileName, err) log.Fatalf("%s: %v", fileName, err)
} }

View File

@ -1,3 +1,3 @@
<p> <p>
<span class="alert" style="font-size:120%">{@|html-esc}</span> <span class="alert" style="font-size:120%">{{.}}</span>
</p> </p>

View File

@ -1,11 +1,11 @@
<html> <html>
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
{.section Title} {{with $x := .Title}}
<title>{@|html-esc} - Camlistore</title> <title>{{$x}} - Camlistore</title>
{.or} {{else}}
<title>Camlistore</title> <title>Camlistore</title>
{.end} {{end}}
<link rel="stylesheet" href="/static/all.css" type="text/css" media="all" charset="utf-8"> <link rel="stylesheet" href="/static/all.css" type="text/css" media="all" charset="utf-8">
<script type="text/javascript" src="/static/all-async.js" async="true"></script> <script type="text/javascript" src="/static/all-async.js" async="true"></script>
</head> </head>
@ -27,7 +27,7 @@
<div class='content'> <div class='content'>
<!-- Content is HTML-escaped elsewhere --> <!-- Content is HTML-escaped elsewhere -->
{Content} {{.Content}}
</div> </div>
<div class='bar'><div class='hatecss'> <div class='bar'><div class='hatecss'>