1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 "golang.org/x/tools/present"
22 http.HandleFunc("/", dirHandler)
25 // dirHandler serves a directory listing for the requested path, rooted at *contentPath.
26 func dirHandler(w http.ResponseWriter, r *http.Request) {
27 if r.URL.Path == "/favicon.ico" {
31 name := filepath.Join(*contentPath, r.URL.Path)
33 err := renderDoc(w, name)
36 http.Error(w, err.Error(), http.StatusInternalServerError)
40 if isDir, err := dirList(w, name); err != nil {
41 addr, _, e := net.SplitHostPort(r.RemoteAddr)
45 log.Printf("request from %s: %s", addr, err)
46 http.Error(w, err.Error(), http.StatusInternalServerError)
51 http.FileServer(http.Dir(*contentPath)).ServeHTTP(w, r)
54 func isDoc(path string) bool {
55 _, ok := contentTemplate[filepath.Ext(path)]
60 // dirListTemplate holds the front page template.
61 dirListTemplate *template.Template
63 // contentTemplate maps the presentable file extensions to the
64 // template to be executed.
65 contentTemplate map[string]*template.Template
68 func initTemplates(base string) error {
69 // Locate the template file.
70 actionTmpl := filepath.Join(base, "templates/action.tmpl")
72 contentTemplate = make(map[string]*template.Template)
74 for ext, contentTmpl := range map[string]string{
75 ".slide": "slides.tmpl",
76 ".article": "article.tmpl",
78 contentTmpl = filepath.Join(base, "templates", contentTmpl)
80 // Read and parse the input.
81 tmpl := present.Template()
82 tmpl = tmpl.Funcs(template.FuncMap{"playable": playable})
83 if _, err := tmpl.ParseFiles(actionTmpl, contentTmpl); err != nil {
86 contentTemplate[ext] = tmpl
90 dirListTemplate, err = template.ParseFiles(filepath.Join(base, "templates/dir.tmpl"))
94 // renderDoc reads the present file, gets its template representation,
95 // and executes the template, sending output to w.
96 func renderDoc(w io.Writer, docFile string) error {
97 // Read the input and build the doc structure.
98 doc, err := parse(docFile, 0)
103 // Find which template should be executed.
104 tmpl := contentTemplate[filepath.Ext(docFile)]
106 // Execute the template.
107 return doc.Render(w, tmpl)
110 func parse(name string, mode present.ParseMode) (*present.Doc, error) {
111 f, err := os.Open(name)
116 return present.Parse(f, name, mode)
119 // dirList scans the given path and writes a directory listing to w.
120 // It parses the first part of each .slide file it encounters to display the
121 // presentation title in the listing.
122 // If the given path is not a directory, it returns (isDir == false, err == nil)
123 // and writes nothing to w.
124 func dirList(w io.Writer, name string) (isDir bool, err error) {
125 f, err := os.Open(name)
134 if isDir = fi.IsDir(); !isDir {
137 fis, err := f.Readdir(0)
141 strippedPath := strings.TrimPrefix(name, filepath.Clean(*contentPath))
142 strippedPath = strings.TrimPrefix(strippedPath, "/")
143 d := &dirListData{Path: strippedPath}
144 for _, fi := range fis {
145 // skip the golang.org directory
146 if name == "." && fi.Name() == "golang.org" {
151 Path: filepath.ToSlash(filepath.Join(strippedPath, fi.Name())),
153 if fi.IsDir() && showDir(e.Name) {
154 d.Dirs = append(d.Dirs, e)
158 fn := filepath.ToSlash(filepath.Join(name, fi.Name()))
159 if p, err := parse(fn, present.TitlesOnly); err != nil {
160 log.Printf("parse(%q, present.TitlesOnly): %v", fn, err)
164 switch filepath.Ext(e.Path) {
166 d.Articles = append(d.Articles, e)
168 d.Slides = append(d.Slides, e)
170 } else if showFile(e.Name) {
171 d.Other = append(d.Other, e)
179 sort.Sort(d.Articles)
181 return true, dirListTemplate.Execute(w, d)
184 // showFile reports whether the given file should be displayed in the list.
185 func showFile(n string) bool {
186 switch filepath.Ext(n) {
196 // showDir reports whether the given directory should be displayed in the list.
197 func showDir(n string) bool {
198 if len(n) > 0 && (n[0] == '.' || n[0] == '_') || n == "present" {
204 type dirListData struct {
206 Dirs, Slides, Articles, Other dirEntrySlice
209 type dirEntry struct {
210 Name, Path, Title string
213 type dirEntrySlice []dirEntry
215 func (s dirEntrySlice) Len() int { return len(s) }
216 func (s dirEntrySlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
217 func (s dirEntrySlice) Less(i, j int) bool { return s[i].Name < s[j].Name }