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"
"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)
}

View File

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

View File

@ -1,11 +1,11 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
{.section Title}
<title>{@|html-esc} - Camlistore</title>
{.or}
{{with $x := .Title}}
<title>{{$x}} - Camlistore</title>
{{else}}
<title>Camlistore</title>
{.end}
{{end}}
<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>
</head>
@ -27,7 +27,7 @@
<div class='content'>
<!-- Content is HTML-escaped elsewhere -->
{Content}
{{.Content}}
</div>
<div class='bar'><div class='hatecss'>