-
Notifications
You must be signed in to change notification settings - Fork 6
/
conv.go
73 lines (62 loc) · 1.47 KB
/
conv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2013 GDG Korea Golang. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"html/template"
"io"
"log"
"os"
"path/filepath"
"code.google.com/p/go.tools/present"
)
func init() {
present.PlayEnabled = true
}
func main() {
f, err := os.Create("index.html")
if err != nil {
log.Fatal(err)
}
defer f.Close()
err = renderTour(f, "")
if err != nil {
log.Fatal(err)
}
log.Println("Succeed.")
}
// renderTour loads tour.article and the relevant HTML templates from the given
// tour root, and renders the template to the provided writer.
func renderTour(w io.Writer, root string) error {
// Open and parse source file.
source := filepath.Join(root, "tour.article")
f, err := os.Open(source)
if err != nil {
return err
}
defer f.Close()
doc, err := present.Parse(f, source, 0)
if err != nil {
return err
}
// Set up templates.
action := filepath.Join(root, "template", "action.tmpl")
tour := filepath.Join(root, "template", "tour.tmpl")
t := present.Template().Funcs(template.FuncMap{"nocode": nocode})
_, err = t.ParseFiles(action, tour)
if err != nil {
return err
}
// Render.
return doc.Render(w, t)
}
// nocode returns true if the provided Section contains
// no Code elements with Play enabled.
func nocode(s present.Section) bool {
for _, e := range s.Elem {
if c, ok := e.(present.Code); ok && c.Play {
return false
}
}
return true
}