From c072558b90f1bbedc2022b0f30c8b1ac4712538e Mon Sep 17 00:00:00 2001
From: Ondřej Surý
-func viewHandler(w http.ResponseWriter, r *http.Request, title string) { +func viewHandler(w http.ResponseWriter, r *http.Request) { + title, err := getTitle(w, r) + if err != nil { + return + } p, err := loadPage(title) if err != nil { http.Redirect(w, r, "/edit/"+title, http.StatusFound) @@ -658,10 +662,14 @@ Now let's fix upsaveHandler
:-func saveHandler(w http.ResponseWriter, r *http.Request, title string) { +func saveHandler(w http.ResponseWriter, r *http.Request) { + title, err := getTitle(w, r) + if err != nil { + return + } body := r.FormValue("body") p := &Page{Title: title, Body: []byte(body)} - err := p.save() + err = p.save() if err != nil { http.Error(w, err.String(), http.StatusInternalServerError) return @@ -702,7 +710,7 @@ Then we create aninit
function, which will be called beforeParseFile
that does not return an error code; instead, it panics if an error is encountered. A panic is appropriate here; if the templates can't be loaded the only sensible thing to do is exit the program. -func init() { @@ -726,7 +734,7 @@ theExecute
method on the appropriateTemplate
fromfunc renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { - err := templates[tmpl].Execute(p, w) + err := templates[tmpl].Execute(w, p) if err != nil { http.Error(w, err.String(), http.StatusInternalServerError) } @@ -747,6 +755,7 @@ Then we can create a global variable to store our validation regexp:+var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$")diff --git a/doc/codelab/wiki/srcextract.go b/doc/codelab/wiki/srcextract.go index cab092f58..67294784e 100644 --- a/doc/codelab/wiki/srcextract.go +++ b/doc/codelab/wiki/srcextract.go @@ -9,6 +9,7 @@ import ( "go/token" "log" "os" + "template" ) var ( @@ -31,15 +32,6 @@ func main() { if err != nil { log.Fatal(err) } - // create printer - p := &printer.Config{ - Mode: 0, - Tabwidth: 8, - Styler: nil, - } - if *html { - p.Mode = printer.GenHTML - } // create filter filter := func(name string) bool { return name == *getName @@ -48,8 +40,9 @@ func main() { if !ast.FilterFile(file, filter) { os.Exit(1) } - b := new(bytes.Buffer) - p.Fprint(b, fs, file) + // print the AST + var b bytes.Buffer + printer.Fprint(&b, fs, file) // drop package declaration if !*showPkg { for { @@ -71,5 +64,9 @@ func main() { } } // output - b.WriteTo(os.Stdout) + if *html { + template.HTMLEscape(os.Stdout, b.Bytes()) + } else { + b.WriteTo(os.Stdout) + } } diff --git a/doc/codelab/wiki/wiki.html b/doc/codelab/wiki/wiki.html index ff2c3088b..7ef97b45b 100644 --- a/doc/codelab/wiki/wiki.html +++ b/doc/codelab/wiki/wiki.html @@ -477,7 +477,7 @@ redirect the client to the edit Page so the content may be created:
-!./srcextract.bin -src=final.go -name=viewHandler +!./srcextract.bin -src=final-noclosure.go -name=viewHandler@@ -539,7 +539,7 @@ Now let's fix up
saveHandler
:-!./srcextract.bin -src=final.go -name=saveHandler +!./srcextract.bin -src=final-noclosure.go -name=saveHandler@@ -574,7 +574,7 @@ Then we create an
init
function, which will be called beforeParseFile
that does not return an error code; instead, it panics if an error is encountered. A panic is appropriate here; if the templates can't be loaded the only sensible thing to do is exit the program. -!./srcextract.bin -src=final.go -name=init @@ -610,7 +610,7 @@ Then we can create a global variable to store our validation regexp:-!./srcextract.bin -src=final-noclosure.go -name=TitleValidator +!./srcextract.bin -src=final-noclosure.go -name=titleValidator-- cgit v1.2.3