diff options
Diffstat (limited to 'doc/codelab')
-rw-r--r-- | doc/codelab/wiki/final.go | 2 | ||||
-rw-r--r-- | doc/codelab/wiki/index.html | 19 | ||||
-rw-r--r-- | doc/codelab/wiki/srcextract.go | 21 | ||||
-rw-r--r-- | doc/codelab/wiki/wiki.html | 8 |
4 files changed, 28 insertions, 22 deletions
diff --git a/doc/codelab/wiki/final.go b/doc/codelab/wiki/final.go index 8ecd97d74..c97a699d4 100644 --- a/doc/codelab/wiki/final.go +++ b/doc/codelab/wiki/final.go @@ -64,7 +64,7 @@ func init() { } func 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) } diff --git a/doc/codelab/wiki/index.html b/doc/codelab/wiki/index.html index fe99c32d1..fc8c27bfa 100644 --- a/doc/codelab/wiki/index.html +++ b/doc/codelab/wiki/index.html @@ -573,7 +573,11 @@ redirect the client to the edit Page so the content may be created: </p> <pre> -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 up <code>saveHandler</code>: </p> <pre> -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 an <code>init</code> function, which will be called before <code>ParseFile</code> 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. -</p +</p> <pre> func init() { @@ -726,7 +734,7 @@ the <code>Execute</code> method on the appropriate <code>Template</code> from <pre> func 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: </p> <pre> +var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$") </pre> <p> 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: </p> <pre> -!./srcextract.bin -src=final.go -name=viewHandler +!./srcextract.bin -src=final-noclosure.go -name=viewHandler </pre> <p> @@ -539,7 +539,7 @@ Now let's fix up <code>saveHandler</code>: </p> <pre> -!./srcextract.bin -src=final.go -name=saveHandler +!./srcextract.bin -src=final-noclosure.go -name=saveHandler </pre> <p> @@ -574,7 +574,7 @@ Then we create an <code>init</code> function, which will be called before <code>ParseFile</code> 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. -</p +</p> <pre> !./srcextract.bin -src=final.go -name=init @@ -610,7 +610,7 @@ Then we can create a global variable to store our validation regexp: </p> <pre> -!./srcextract.bin -src=final-noclosure.go -name=TitleValidator +!./srcextract.bin -src=final-noclosure.go -name=titleValidator </pre> <p> |