You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
5.0 KiB

  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.
  4. package main
  5. import (
  6. "html/template"
  7. "io"
  8. "log"
  9. "net"
  10. "net/http"
  11. "os"
  12. "path/filepath"
  13. "sort"
  14. "golang.org/x/tools/present"
  15. )
  16. func init() {
  17. http.HandleFunc("/", dirHandler)
  18. }
  19. // dirHandler serves a directory listing for the requested path, rooted at basePath.
  20. func dirHandler(w http.ResponseWriter, r *http.Request) {
  21. if r.URL.Path == "/favicon.ico" {
  22. http.Error(w, "not found", 404)
  23. return
  24. }
  25. const base = "."
  26. name := filepath.Join(base, r.URL.Path)
  27. if isDoc(name) {
  28. err := renderDoc(w, name)
  29. if err != nil {
  30. log.Println(err)
  31. http.Error(w, err.Error(), 500)
  32. }
  33. return
  34. }
  35. if isDir, err := dirList(w, name); err != nil {
  36. addr, _, e := net.SplitHostPort(r.RemoteAddr)
  37. if e != nil {
  38. addr = r.RemoteAddr
  39. }
  40. log.Printf("request from %s: %s", addr, err)
  41. http.Error(w, err.Error(), 500)
  42. return
  43. } else if isDir {
  44. return
  45. }
  46. http.FileServer(http.Dir(base)).ServeHTTP(w, r)
  47. }
  48. func isDoc(path string) bool {
  49. _, ok := contentTemplate[filepath.Ext(path)]
  50. return ok
  51. }
  52. var (
  53. // dirListTemplate holds the front page template.
  54. dirListTemplate *template.Template
  55. // contentTemplate maps the presentable file extensions to the
  56. // template to be executed.
  57. contentTemplate map[string]*template.Template
  58. )
  59. func initTemplates(base string) error {
  60. // Locate the template file.
  61. actionTmpl := filepath.Join(base, "templates/action.tmpl")
  62. contentTemplate = make(map[string]*template.Template)
  63. for ext, contentTmpl := range map[string]string{
  64. ".slide": "slides.tmpl",
  65. ".article": "article.tmpl",
  66. } {
  67. contentTmpl = filepath.Join(base, "templates", contentTmpl)
  68. // Read and parse the input.
  69. tmpl := present.Template()
  70. tmpl = tmpl.Funcs(template.FuncMap{"playable": playable})
  71. if _, err := tmpl.ParseFiles(actionTmpl, contentTmpl); err != nil {
  72. return err
  73. }
  74. contentTemplate[ext] = tmpl
  75. }
  76. var err error
  77. dirListTemplate, err = template.ParseFiles(filepath.Join(base, "templates/dir.tmpl"))
  78. return err
  79. }
  80. // renderDoc reads the present file, gets its template representation,
  81. // and executes the template, sending output to w.
  82. func renderDoc(w io.Writer, docFile string) error {
  83. // Read the input and build the doc structure.
  84. doc, err := parse(docFile, 0)
  85. if err != nil {
  86. return err
  87. }
  88. // Find which template should be executed.
  89. tmpl := contentTemplate[filepath.Ext(docFile)]
  90. // Execute the template.
  91. return doc.Render(w, tmpl)
  92. }
  93. func parse(name string, mode present.ParseMode) (*present.Doc, error) {
  94. f, err := os.Open(name)
  95. if err != nil {
  96. return nil, err
  97. }
  98. defer f.Close()
  99. return present.Parse(f, name, 0)
  100. }
  101. // dirList scans the given path and writes a directory listing to w.
  102. // It parses the first part of each .slide file it encounters to display the
  103. // presentation title in the listing.
  104. // If the given path is not a directory, it returns (isDir == false, err == nil)
  105. // and writes nothing to w.
  106. func dirList(w io.Writer, name string) (isDir bool, err error) {
  107. f, err := os.Open(name)
  108. if err != nil {
  109. return false, err
  110. }
  111. defer f.Close()
  112. fi, err := f.Stat()
  113. if err != nil {
  114. return false, err
  115. }
  116. if isDir = fi.IsDir(); !isDir {
  117. return false, nil
  118. }
  119. fis, err := f.Readdir(0)
  120. if err != nil {
  121. return false, err
  122. }
  123. d := &dirListData{Path: name}
  124. for _, fi := range fis {
  125. // skip the golang.org directory
  126. if name == "." && fi.Name() == "golang.org" {
  127. continue
  128. }
  129. e := dirEntry{
  130. Name: fi.Name(),
  131. Path: filepath.ToSlash(filepath.Join(name, fi.Name())),
  132. }
  133. if fi.IsDir() && showDir(e.Name) {
  134. d.Dirs = append(d.Dirs, e)
  135. continue
  136. }
  137. if isDoc(e.Name) {
  138. if p, err := parse(e.Path, present.TitlesOnly); err != nil {
  139. log.Println(err)
  140. } else {
  141. e.Title = p.Title
  142. }
  143. switch filepath.Ext(e.Path) {
  144. case ".article":
  145. d.Articles = append(d.Articles, e)
  146. case ".slide":
  147. d.Slides = append(d.Slides, e)
  148. }
  149. } else if showFile(e.Name) {
  150. d.Other = append(d.Other, e)
  151. }
  152. }
  153. if d.Path == "." {
  154. d.Path = ""
  155. }
  156. sort.Sort(d.Dirs)
  157. sort.Sort(d.Slides)
  158. sort.Sort(d.Articles)
  159. sort.Sort(d.Other)
  160. return true, dirListTemplate.Execute(w, d)
  161. }
  162. // showFile reports whether the given file should be displayed in the list.
  163. func showFile(n string) bool {
  164. switch filepath.Ext(n) {
  165. case ".pdf":
  166. case ".html":
  167. case ".go":
  168. default:
  169. return isDoc(n)
  170. }
  171. return true
  172. }
  173. // showDir reports whether the given directory should be displayed in the list.
  174. func showDir(n string) bool {
  175. if len(n) > 0 && (n[0] == '.' || n[0] == '_') || n == "present" {
  176. return false
  177. }
  178. return true
  179. }
  180. type dirListData struct {
  181. Path string
  182. Dirs, Slides, Articles, Other dirEntrySlice
  183. }
  184. type dirEntry struct {
  185. Name, Path, Title string
  186. }
  187. type dirEntrySlice []dirEntry
  188. func (s dirEntrySlice) Len() int { return len(s) }
  189. func (s dirEntrySlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  190. func (s dirEntrySlice) Less(i, j int) bool { return s[i].Name < s[j].Name }