示例#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
// 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
}
示例#3
0
文件: main.go 项目: nf/blog
func loadDocs() ([]*Doc, error) {
	var docs []*Doc
	fn := func(path string, info os.FileInfo, err error) error {
		if filepath.Ext(path) != ".article" {
			return nil
		}
		f, err := os.Open(path)
		if err != nil {
			return err
		}
		defer f.Close()
		d, err := present.Parse(f, filepath.Base(path), 0)
		if err != nil {
			return err
		}
		docs = append(docs, &Doc{d, path})
		return nil
	}
	err := filepath.Walk(".", fn)
	if err != nil {
		return nil, err
	}
	sort.Sort(docsByTime(docs))
	return docs, nil
}
示例#4
0
func parse(name string, mode present.ParseMode) (*present.Doc, error) {
	f, err := os.Open(name)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	return present.Parse(f, name, 0)
}
示例#5
0
文件: main.go 项目: AlekSi/gddo
func servePresentHome(resp web.Response, req *web.Request) error {
	fname := filepath.Join(*assetsDir, "presentHome.article")
	f, err := os.Open(fname)
	if err != nil {
		return err
	}
	defer f.Close()
	doc, err := present.Parse(f, fname, 0)
	if err != nil {
		return err
	}
	return renderPresentation(resp, fname, doc)
}