Sergiusz Bazanski | c881cf3 | 2020-04-08 20:03:12 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Sergiusz Bazanski | f157b4d | 2020-04-10 17:39:43 +0200 | [diff] [blame] | 4 | "bytes" |
Sergiusz Bazanski | c881cf3 | 2020-04-08 20:03:12 +0200 | [diff] [blame] | 5 | "html/template" |
Sergiusz Bazanski | f157b4d | 2020-04-10 17:39:43 +0200 | [diff] [blame] | 6 | "net/url" |
Sergiusz Bazanski | c881cf3 | 2020-04-08 20:03:12 +0200 | [diff] [blame] | 7 | "strings" |
| 8 | |
| 9 | "code.hackerspace.pl/hscloud/devtools/hackdoc/config" |
Sergiusz Bazanski | c881cf3 | 2020-04-08 20:03:12 +0200 | [diff] [blame] | 10 | |
Sergiusz Bazanski | 8adbd49 | 2020-04-10 21:20:53 +0200 | [diff] [blame] | 11 | "github.com/gabriel-vasile/mimetype" |
Sergiusz Bazanski | c881cf3 | 2020-04-08 20:03:12 +0200 | [diff] [blame] | 12 | "github.com/golang/glog" |
| 13 | "gopkg.in/russross/blackfriday.v2" |
| 14 | ) |
| 15 | |
Sergiusz Bazanski | f157b4d | 2020-04-10 17:39:43 +0200 | [diff] [blame] | 16 | // renderMarkdown renders markdown to HTML, replacing all relative (intra-hackdoc) links with version that have ref set. |
| 17 | func renderMarkdown(input []byte, ref string) []byte { |
| 18 | r := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{ |
Sergiusz Bazanski | 5bce7ce | 2020-04-11 20:16:58 +0200 | [diff] [blame] | 19 | Flags: blackfriday.CommonHTMLFlags | blackfriday.TOC, |
Sergiusz Bazanski | f157b4d | 2020-04-10 17:39:43 +0200 | [diff] [blame] | 20 | }) |
| 21 | |
| 22 | parser := blackfriday.New(blackfriday.WithRenderer(r), blackfriday.WithExtensions(blackfriday.CommonExtensions)) |
| 23 | ast := parser.Parse(input) |
| 24 | |
| 25 | var buf bytes.Buffer |
Sergiusz Bazanski | 5bce7ce | 2020-04-11 20:16:58 +0200 | [diff] [blame] | 26 | buf.Write([]byte(`<div class="toc"><h1>Page Contents</h1>`)) |
| 27 | r.RenderHeader(&buf, ast) |
| 28 | buf.Write([]byte(`</div><div class="content">`)) |
Sergiusz Bazanski | f157b4d | 2020-04-10 17:39:43 +0200 | [diff] [blame] | 29 | ast.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus { |
Sergiusz Bazanski | 4b4a33a | 2020-04-13 01:35:33 +0200 | [diff] [blame] | 30 | if ref != "" && entering && node.Type == blackfriday.Link || node.Type == blackfriday.Image { |
Sergiusz Bazanski | f157b4d | 2020-04-10 17:39:43 +0200 | [diff] [blame] | 31 | dest := string(node.Destination) |
| 32 | u, err := url.Parse(dest) |
| 33 | if err == nil && !u.IsAbs() { |
| 34 | q := u.Query() |
| 35 | q["ref"] = []string{ref} |
| 36 | u.RawQuery = q.Encode() |
| 37 | node.Destination = []byte(u.String()) |
Sergiusz Bazanski | 8adbd49 | 2020-04-10 21:20:53 +0200 | [diff] [blame] | 38 | glog.Infof("link fix %q -> %q", dest, u.String()) |
Sergiusz Bazanski | f157b4d | 2020-04-10 17:39:43 +0200 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | return r.RenderNode(&buf, node, entering) |
| 42 | }) |
Sergiusz Bazanski | 5bce7ce | 2020-04-11 20:16:58 +0200 | [diff] [blame] | 43 | buf.Write([]byte(`</div>`)) |
| 44 | r.RenderFooter(&buf, ast) |
Sergiusz Bazanski | f157b4d | 2020-04-10 17:39:43 +0200 | [diff] [blame] | 45 | return buf.Bytes() |
| 46 | } |
| 47 | |
Sergiusz Bazanski | 8adbd49 | 2020-04-10 21:20:53 +0200 | [diff] [blame] | 48 | type pathPart struct { |
| 49 | Label string |
| 50 | Path string |
| 51 | } |
| 52 | |
| 53 | func (r *request) handleFile(path string, cfg *config.Config) { |
Sergiusz Bazanski | f157b4d | 2020-04-10 17:39:43 +0200 | [diff] [blame] | 54 | data, err := r.source.ReadFile(r.ctx, path) |
Sergiusz Bazanski | c881cf3 | 2020-04-08 20:03:12 +0200 | [diff] [blame] | 55 | if err != nil { |
| 56 | glog.Errorf("ReadFile(%q): %w", err) |
Sergiusz Bazanski | f157b4d | 2020-04-10 17:39:43 +0200 | [diff] [blame] | 57 | r.handle500() |
Sergiusz Bazanski | c881cf3 | 2020-04-08 20:03:12 +0200 | [diff] [blame] | 58 | return |
| 59 | } |
| 60 | |
Sergiusz Bazanski | 8adbd49 | 2020-04-10 21:20:53 +0200 | [diff] [blame] | 61 | // TODO(q3k): do MIME detection instead. |
| 62 | if strings.HasSuffix(path, ".md") { |
| 63 | rendered := renderMarkdown([]byte(data), r.ref) |
Sergiusz Bazanski | c881cf3 | 2020-04-08 20:03:12 +0200 | [diff] [blame] | 64 | |
Sergiusz Bazanski | 8adbd49 | 2020-04-10 21:20:53 +0200 | [diff] [blame] | 65 | r.logRequest("serving markdown at %s, cfg %+v", path, cfg) |
Sergiusz Bazanski | c881cf3 | 2020-04-08 20:03:12 +0200 | [diff] [blame] | 66 | |
Sergiusz Bazanski | 8adbd49 | 2020-04-10 21:20:53 +0200 | [diff] [blame] | 67 | // TODO(q3k): allow markdown files to override which template to load |
| 68 | tmpl, ok := cfg.Templates["default"] |
| 69 | if !ok { |
| 70 | glog.Errorf("No default template found for %s", path) |
| 71 | // TODO(q3k): implement fallback template |
| 72 | r.w.Write(rendered) |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | pathInDepot := strings.TrimPrefix(path, "//") |
| 77 | pathParts := []pathPart{ |
| 78 | {Label: "//", Path: "/"}, |
| 79 | } |
| 80 | parts := strings.Split(pathInDepot, "/") |
| 81 | fullPath := "" |
| 82 | for i, p := range parts { |
| 83 | label := p |
| 84 | if i != len(parts)-1 { |
| 85 | label = label + "/" |
| 86 | } |
| 87 | fullPath += "/" + p |
| 88 | pathParts = append(pathParts, pathPart{Label: label, Path: fullPath}) |
| 89 | } |
| 90 | |
| 91 | vars := map[string]interface{}{ |
| 92 | "Rendered": template.HTML(rendered), |
| 93 | "Title": path, |
| 94 | "Path": path, |
| 95 | "PathInDepot": pathInDepot, |
| 96 | "PathParts": pathParts, |
| 97 | "HackdocURL": flagHackdocURL, |
| 98 | "WebLinks": r.source.WebLinks(pathInDepot), |
| 99 | } |
| 100 | err = tmpl.Execute(r.w, vars) |
| 101 | if err != nil { |
| 102 | glog.Errorf("Could not execute template for %s: %v", err) |
| 103 | } |
| 104 | |
Sergiusz Bazanski | c881cf3 | 2020-04-08 20:03:12 +0200 | [diff] [blame] | 105 | return |
| 106 | } |
| 107 | |
Sergiusz Bazanski | 8adbd49 | 2020-04-10 21:20:53 +0200 | [diff] [blame] | 108 | // Just serve the file. |
| 109 | mime := mimetype.Detect(data) |
| 110 | r.w.Header().Set("Content-Type", mime.String()) |
| 111 | r.w.Write(data) |
Sergiusz Bazanski | c881cf3 | 2020-04-08 20:03:12 +0200 | [diff] [blame] | 112 | } |