go.talks/present: remove presentation logic from data structures

Remove the Template field from Doc, and the Doc field from Section.
The new Render method on Doc wraps the Doc in an anonymous struct that
also contains the template, before passing it to the template iteslf.
The renderElem function wraps Section elements in a similar way.

R=campoy, dsymonds
CC=golang-dev
https://golang.org/cl/6863058
diff --git a/pkg/present/parse.go b/pkg/present/parse.go
index e75ed34..ee3dc2b 100644
--- a/pkg/present/parse.go
+++ b/pkg/present/parse.go
@@ -24,12 +24,20 @@
 	funcs   = template.FuncMap{}
 )
 
-// Template returns an initialized template with the action functions in its
-// FuncMap.
+// Template returns an empty template with the action functions in its FuncMap.
 func Template() *template.Template {
 	return template.New("").Funcs(funcs)
 }
 
+// Render renders the doc to the given writer using the provided template.
+func (d *Doc) Render(w io.Writer, t *template.Template) error {
+	data := struct {
+		*Doc
+		Template *template.Template
+	}{d, t}
+	return t.ExecuteTemplate(w, "root", data)
+}
+
 type ParseFunc func(fileName string, lineNumber int, inputLine string) (Elem, error)
 
 // Register binds the named action, which does not being with a period, to the
@@ -49,7 +57,6 @@
 	Time     time.Time
 	Authors  []Author
 	Sections []Section
-	Template *template.Template
 }
 
 // Author represents the person who wrote and/or is presenting the document.
@@ -76,7 +83,6 @@
 	Number []int
 	Title  string
 	Elem   []Elem
-	Doc    *Doc
 }
 
 func (s Section) Sections() (sections []Section) {
@@ -115,7 +121,14 @@
 // renderElem implements the elem template function, used to render
 // sub-templates.
 func renderElem(t *template.Template, e Elem) (template.HTML, error) {
-	return execTemplate(t, e.TemplateName(), e)
+	var data interface{} = e
+	if s, ok := e.(Section); ok {
+		data = struct {
+			Section
+			Template *template.Template
+		}{s, t}
+	}
+	return execTemplate(t, e.TemplateName(), data)
 }
 
 func init() {
@@ -268,7 +281,6 @@
 		section := Section{
 			Number: append(append([]int{}, number...), i),
 			Title:  text[len(prefix)+1:],
-			Doc:    doc,
 		}
 		text, ok = lines.nextNonEmpty()
 		for ok && !lesserHeading(text, prefix) {
diff --git a/present/dir.go b/present/dir.go
index f309cf8..12100da 100644
--- a/present/dir.go
+++ b/present/dir.go
@@ -63,7 +63,7 @@
 // and executes the template, sending output to w.
 func renderDoc(w io.Writer, base, docFile string) error {
 	// Read the input and build the doc structure.
-	pres, err := parse(docFile, 0)
+	doc, err := parse(docFile, 0)
 	if err != nil {
 		return err
 	}
@@ -85,10 +85,8 @@
 		return err
 	}
 
-	pres.Template = tmpl
-
 	// Execute the template.
-	return tmpl.ExecuteTemplate(w, "root", pres)
+	return doc.Render(w, tmpl)
 }
 
 func parse(name string, mode present.ParseMode) (*present.Doc, error) {
diff --git a/present/templates/action.tmpl b/present/templates/action.tmpl
index d7943db..8bd0046 100644
--- a/present/templates/action.tmpl
+++ b/present/templates/action.tmpl
@@ -5,7 +5,7 @@
 
 {{define "section"}}
   <h{{len .Number}} id="TOC_{{.FormattedNumber}}">{{.FormattedNumber}} {{.Title}}</h{{len .Number}}>
-  {{range .Elem}}{{elem $.Doc.Template .}}{{end}}
+  {{range .Elem}}{{elem $.Template .}}{{end}}
 {{end}}
 
 {{define "list"}}