func initTemplates(base string) error { // Locate the template file. actionTmpl := filepath.Join(base, "templates/action.tmpl") contentTemplate = make(map[string]*template.Template) for ext, contentTmpl := range map[string]string{ ".slide": "slides.tmpl", ".article": "article.tmpl", } { contentTmpl = filepath.Join(base, "templates", contentTmpl) // Read and parse the input. tmpl := present.Template() tmpl = tmpl.Funcs(template.FuncMap{"playable": playable}) if _, err := tmpl.ParseFiles(actionTmpl, contentTmpl); err != nil { return err } contentTemplate[ext] = tmpl } var err error dirListTemplate, err = template.ParseFiles(filepath.Join(base, "templates/dir.tmpl")) if err != nil { return err } return nil }
func RenderDoc(w io.Writer, 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_tmpl[ext] if !ok { return fmt.Errorf("no template for extension %v", ext) } // Locate the template file. actionTmpl := action_tmpl //filepath.Join(base, "templates/action.tmpl") // Read and parse the input. tmpl := present.Template() tmpl = tmpl.Funcs(template.FuncMap{"playable": playable}) if tmpl, err = tmpl.New("action").Parse(actionTmpl); err != nil { return err } if tmpl, err = tmpl.New("content").Parse(contentTmpl); err != nil { return err } // Execute the template. return doc.Render(w, tmpl) }
func parsePresentTemplate(name string) *template.Template { t := present.Template() t = t.Funcs(template.FuncMap{"playable": playable}) if _, err := t.ParseFiles("present/templates/"+name, "present/templates/action.tmpl"); err != nil { panic(err) } t = t.Lookup("root") if t == nil { panic("root template not found for " + name) } return t }
// NewServer constructs a new Server, serving articles from the specified // contentPath generated from templates from templatePath. func NewServer(contentPath, templatePath string) (*Server, error) { present.PlayEnabled = true root := filepath.Join(templatePath, "root.tmpl") parse := func(name string) (*template.Template, error) { t := template.New("").Funcs(funcMap) return t.ParseFiles(root, filepath.Join(templatePath, name)) } s := &Server{} // Parse templates. var err error s.template.home, err = parse("home.tmpl") if err != nil { return nil, err } s.template.index, err = parse("index.tmpl") if err != nil { return nil, err } s.template.article, err = parse("article.tmpl") if err != nil { return nil, err } p := present.Template().Funcs(funcMap) s.template.doc, err = p.ParseFiles(filepath.Join(templatePath, "doc.tmpl")) if err != nil { return nil, err } // Load content. err = s.loadDocs(filepath.Clean(contentPath)) if err != nil { return nil, err } err = s.renderAtomFeed() if err != nil { return nil, err } err = s.renderJSONFeed() if err != nil { return nil, err } // Set up content file server. s.content = http.FileServer(http.Dir(contentPath)) return s, nil }
func presentSlide(w http.ResponseWriter, r *http.Request) { slideIdParam := strings.SplitN(r.URL.Path, "/", 2)[1] slideId := index.getSlideId(slideIdParam) if slideId == "" { http.NotFound(w, r) return } slideFile := filepath.Join(*slidesDir, slideId, "main.slide") f, err := os.Open(slideFile) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } doc, err := present.Parse(f, slideFile, 0) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } tmpl := present.Template() tmpl.Funcs(template.FuncMap{"playable": func(c present.Code) bool { return false }, "rSlideId": func() string { return slideIdParam }, "userRole": func() string { if slideId == slideIdParam { return "p" } else { return "v" } }}) _, err = tmpl.New("action").Parse(actionTmpl) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } _, err = tmpl.New("slides").Parse(slidesTmpl) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := doc.Render(w, tmpl); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }
// 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 := *article f, err := os.Open(source) if err != nil { // See if it exists in the content directory in the root. source = filepath.Join(root, "content", source) 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 }
// 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, transport string) error { // Make sure playground is enabled before rendering. present.PlayEnabled = true // Set up templates. action := filepath.Join(root, "template", "action.tmpl") tmpl, err := present.Template().ParseFiles(action) if err != nil { return fmt.Errorf("parse templates: %v", err) } // Init lessons. contentPath := filepath.Join(root, "content") if err := initLessons(tmpl, contentPath); err != nil { return fmt.Errorf("init lessons: %v", err) } // Init UI index := filepath.Join(root, "template", "index.tmpl") ui, err := template.ParseFiles(index) if err != nil { return fmt.Errorf("parse index.tmpl: %v", err) } buf := new(bytes.Buffer) data := struct { SocketAddr string Transport template.JS }{socketAddr(), template.JS(transport)} if err := ui.Execute(buf, data); err != nil { return fmt.Errorf("render UI: %v", err) } uiContent = buf.Bytes() return initScript(root) }