render.go
package main
import (
"bytes"
"fmt"
"html/template"
"net/http"
"os"
"strings"
"github.com/yuin/goldmark"
gmext "github.com/yuin/goldmark/extension"
)
// maxRenderSize caps how much we'll read into memory for rendered views.
// Bigger files still work fine via ?fmt=raw, which streams.
const maxRenderSize = 4 << 20
var markdown = goldmark.New(goldmark.WithExtensions(gmext.GFM))
type dirEntry struct {
Name string
Href string
}
type crumb struct {
Name string
Href string
}
type pageData struct {
Title string
Crumbs []crumb
RawHref string // if set, a "raw" link is shown
Listing []dirEntry // directory listing mode
Content template.HTML // rendered-markdown mode
Iframe string // iframe-passthrough mode: src URL
}
// crumbs builds a breadcrumb trail: home, root name, then each path segment.
func crumbs(name, rel string) []crumb {
cs := []crumb{{Name: "repoview", Href: "/"}}
if name == "" {
return cs
}
href := "/" + name + "/"
cs = append(cs, crumb{Name: name, Href: href})
if rel == "." {
return cs
}
for _, seg := range strings.Split(rel, "/") {
href += seg + "/"
cs = append(cs, crumb{Name: seg, Href: href})
}
// The last segment may be a file; its correct href has no trailing slash,
// but browsers resolve it identically because we link the exact page anyway.
cs[len(cs)-1].Href = ""
return cs
}
// serveRendered shows a file as a page: markdown rendered to HTML,
// anything else passed through in an iframe pointed at the raw view.
func serveRendered(w http.ResponseWriter, r *http.Request, root *os.Root, name, rel string, size int64) {
pd := pageData{
Title: name + "/" + rel,
Crumbs: crumbs(name, rel),
RawHref: "?fmt=raw",
}
if strings.HasSuffix(rel, ".md") && size <= maxRenderSize {
src, err := root.ReadFile(rel)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var buf bytes.Buffer
if err := markdown.Convert(src, &buf); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
pd.Content = template.HTML(buf.String())
} else {
pd.Iframe = "?fmt=raw"
}
renderPage(w, pd)
}
var pageTmpl = template.Must(template.New("page").Parse(`<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{.Title}}</title>
<style>
body { font-family: sans-serif; max-width: 56rem; margin: 1rem auto; padding: 0 1rem; }
nav { border-bottom: 1px solid #ccc; padding-bottom: .5rem; margin-bottom: 1rem; }
nav .raw { float: right; }
ul.listing { list-style: none; padding-left: 0; }
ul.listing li { padding: .1rem 0; }
iframe.passthrough { width: 100%; height: 75vh; border: 1px solid #ccc; }
article pre { background: #f4f4f4; padding: .5rem; overflow-x: auto; }
article code { background: #f4f4f4; }
article table { border-collapse: collapse; }
article th, article td { border: 1px solid #ccc; padding: .2rem .5rem; }
</style>
</head>
<body>
<nav>
{{range $i, $c := .Crumbs}}{{if $i}} / {{end}}{{if $c.Href}}<a href="{{$c.Href}}">{{$c.Name}}</a>{{else}}{{$c.Name}}{{end}}{{end}}
{{if .RawHref}}<a class="raw" href="{{.RawHref}}">raw</a>{{end}}
</nav>
{{if .Listing}}<ul class="listing">{{range .Listing}}<li><a href="{{.Href}}">{{.Name}}</a></li>{{end}}</ul>{{end}}
{{if .Content}}<article>{{.Content}}</article>{{end}}
{{if .Iframe}}<iframe class="passthrough" src="{{.Iframe}}"></iframe>{{end}}
</body>
</html>
`))
func renderPage(w http.ResponseWriter, pd pageData) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := pageTmpl.Execute(w, pd); err != nil {
fmt.Fprintf(w, "\n<!-- template error: %v -->", err)
}
}
render.go · 3.6 KiB · ef04c20893