Пример #1
0
// 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)
}
Пример #2
0
// renderDoc reads the present file, builds its template representation,
// 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.
	doc, err := parse(docFile, 0)
	if err != nil {
		return err
	}

	// Find which template should be executed.
	ext := filepath.Ext(docFile)
	contentTmpl, ok := extensions[ext]
	if !ok {
		return fmt.Errorf("no template for extension %v", ext)
	}

	// Locate the template file.
	actionTmpl := filepath.Join(base, "templates/action.tmpl")
	contentTmpl = filepath.Join(base, "templates", contentTmpl)

	// Read and parse the input.
	tmpl := present.Template()
	if _, err := tmpl.ParseFiles(actionTmpl, contentTmpl); err != nil {
		return err
	}

	// Execute the template.
	return doc.Render(w, tmpl)
}
Пример #3
0
// initTour loads tour.article and the relevant HTML templates from the given
// tour root, and renders the template to the tourContent global variable.
func initTour(root string) error {
	// Make sure playground is enabled before rendering.
	present.PlayEnabled = true

	// 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(prepContent(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, "socketAddr": socketAddr})
	_, err = t.ParseFiles(action, tour)
	if err != nil {
		return err
	}

	// Render.
	buf := new(bytes.Buffer)
	if err := doc.Render(buf, t); err != nil {
		return err
	}
	tourContent = buf.Bytes()
	return nil
}
Пример #4
0
func parsePresentTemplates(sets [][]string) error {
	for _, set := range sets {
		t := present.Template()
		if _, err := t.ParseFiles(joinTemplateDir(*presentDir, set[1:])...); err != nil {
			return err
		}
		t = t.Lookup("root")
		if t == nil {
			return fmt.Errorf("root template not found in %v", set)
		}
		presentTemplates[set[0]] = t
	}
	return nil
}
Пример #5
0
Файл: main.go Проект: nf/blog
	"sort"

	"code.google.com/p/go.talks/pkg/present"
)

type Doc struct {
	*present.Doc
	Path string
}

var (
	docs      []*Doc
	docPaths  = make(map[string]*Doc)
	httpAddr  = flag.String("http", ":8080", "HTTP listen address")
	rTemplate = template.Must(template.ParseFiles("root.tmpl"))
	pTemplate = template.Must(present.Template().ParseFiles("action.tmpl", "article.tmpl"))
)

func main() {
	flag.Parse()
	var err error
	docs, err = loadDocs()
	if err != nil {
		log.Fatal(err)
	}
	for _, d := range docs {
		d.Template = pTemplate
		docPaths[d.Path] = d
	}
	http.HandleFunc("/", handler)
	http.Handle("/static/", http.FileServer(http.Dir(".")))