blob: 14ffe45ed1b6db0a6ab76dbdc7ce97e5490b5642 [file] [log] [blame]
Sergiusz Bazanskic881cf32020-04-08 20:03:12 +02001package main
2
3import (
Sergiusz Bazanskif157b4d2020-04-10 17:39:43 +02004 "bytes"
Sergiusz Bazanskic881cf32020-04-08 20:03:12 +02005 "html/template"
Sergiusz Bazanskif157b4d2020-04-10 17:39:43 +02006 "net/url"
Sergiusz Bazanskic881cf32020-04-08 20:03:12 +02007 "strings"
8
9 "code.hackerspace.pl/hscloud/devtools/hackdoc/config"
Sergiusz Bazanskic881cf32020-04-08 20:03:12 +020010
Sergiusz Bazanski8adbd492020-04-10 21:20:53 +020011 "github.com/gabriel-vasile/mimetype"
Sergiusz Bazanskic881cf32020-04-08 20:03:12 +020012 "github.com/golang/glog"
13 "gopkg.in/russross/blackfriday.v2"
14)
15
Sergiusz Bazanskif157b4d2020-04-10 17:39:43 +020016// renderMarkdown renders markdown to HTML, replacing all relative (intra-hackdoc) links with version that have ref set.
17func renderMarkdown(input []byte, ref string) []byte {
18 r := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
Sergiusz Bazanski5bce7ce2020-04-11 20:16:58 +020019 Flags: blackfriday.CommonHTMLFlags | blackfriday.TOC,
Sergiusz Bazanskif157b4d2020-04-10 17:39:43 +020020 })
21
22 parser := blackfriday.New(blackfriday.WithRenderer(r), blackfriday.WithExtensions(blackfriday.CommonExtensions))
23 ast := parser.Parse(input)
24
25 var buf bytes.Buffer
Sergiusz Bazanski5bce7ce2020-04-11 20:16:58 +020026 buf.Write([]byte(`<div class="toc"><h1>Page Contents</h1>`))
27 r.RenderHeader(&buf, ast)
28 buf.Write([]byte(`</div><div class="content">`))
Sergiusz Bazanskif157b4d2020-04-10 17:39:43 +020029 ast.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
Sergiusz Bazanski4b4a33a2020-04-13 01:35:33 +020030 if ref != "" && entering && node.Type == blackfriday.Link || node.Type == blackfriday.Image {
Sergiusz Bazanskif157b4d2020-04-10 17:39:43 +020031 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 Bazanski8adbd492020-04-10 21:20:53 +020038 glog.Infof("link fix %q -> %q", dest, u.String())
Sergiusz Bazanskif157b4d2020-04-10 17:39:43 +020039 }
40 }
41 return r.RenderNode(&buf, node, entering)
42 })
Sergiusz Bazanski5bce7ce2020-04-11 20:16:58 +020043 buf.Write([]byte(`</div>`))
44 r.RenderFooter(&buf, ast)
Sergiusz Bazanskif157b4d2020-04-10 17:39:43 +020045 return buf.Bytes()
46}
47
Sergiusz Bazanski8adbd492020-04-10 21:20:53 +020048type pathPart struct {
49 Label string
50 Path string
51}
52
53func (r *request) handleFile(path string, cfg *config.Config) {
Sergiusz Bazanskif157b4d2020-04-10 17:39:43 +020054 data, err := r.source.ReadFile(r.ctx, path)
Sergiusz Bazanskic881cf32020-04-08 20:03:12 +020055 if err != nil {
56 glog.Errorf("ReadFile(%q): %w", err)
Sergiusz Bazanskif157b4d2020-04-10 17:39:43 +020057 r.handle500()
Sergiusz Bazanskic881cf32020-04-08 20:03:12 +020058 return
59 }
60
Sergiusz Bazanski8adbd492020-04-10 21:20:53 +020061 // TODO(q3k): do MIME detection instead.
62 if strings.HasSuffix(path, ".md") {
63 rendered := renderMarkdown([]byte(data), r.ref)
Sergiusz Bazanskic881cf32020-04-08 20:03:12 +020064
Sergiusz Bazanski8adbd492020-04-10 21:20:53 +020065 r.logRequest("serving markdown at %s, cfg %+v", path, cfg)
Sergiusz Bazanskic881cf32020-04-08 20:03:12 +020066
Sergiusz Bazanski8adbd492020-04-10 21:20:53 +020067 // 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 Bazanskic881cf32020-04-08 20:03:12 +0200105 return
106 }
107
Sergiusz Bazanski8adbd492020-04-10 21:20:53 +0200108 // Just serve the file.
109 mime := mimetype.Detect(data)
110 r.w.Header().Set("Content-Type", mime.String())
111 r.w.Write(data)
Sergiusz Bazanskic881cf32020-04-08 20:03:12 +0200112}