2015-03-16 12:45:51 -05:00
|
|
|
// Package markdown is middleware to render markdown files as HTML
|
|
|
|
// on-the-fly.
|
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
2015-03-20 01:01:39 -05:00
|
|
|
"bytes"
|
2015-03-16 12:45:51 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-03-28 17:55:40 -05:00
|
|
|
"os"
|
2015-03-16 12:45:51 -05:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
"github.com/russross/blackfriday"
|
|
|
|
)
|
|
|
|
|
2015-03-21 11:59:29 -05:00
|
|
|
// Markdown implements a layer of middleware that serves
|
|
|
|
// markdown as HTML.
|
2015-03-16 12:45:51 -05:00
|
|
|
type Markdown struct {
|
|
|
|
// Server root
|
|
|
|
Root string
|
|
|
|
|
|
|
|
// Next HTTP handler in the chain
|
2015-03-28 17:55:40 -05:00
|
|
|
Next middleware.HandlerFunc
|
2015-03-16 12:45:51 -05:00
|
|
|
|
2015-03-21 11:59:29 -05:00
|
|
|
// The list of markdown configurations
|
|
|
|
Configs []MarkdownConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkdownConfig stores markdown middleware configurations.
|
|
|
|
type MarkdownConfig struct {
|
2015-03-16 12:45:51 -05:00
|
|
|
// Markdown renderer
|
|
|
|
Renderer blackfriday.Renderer
|
|
|
|
|
|
|
|
// Base path to match
|
|
|
|
PathScope string
|
|
|
|
|
|
|
|
// List of extensions to consider as markdown files
|
|
|
|
Extensions []string
|
|
|
|
|
|
|
|
// List of style sheets to load for each markdown file
|
|
|
|
Styles []string
|
|
|
|
|
|
|
|
// List of JavaScript files to load for each markdown file
|
|
|
|
Scripts []string
|
|
|
|
}
|
|
|
|
|
2015-03-21 11:59:29 -05:00
|
|
|
// New creates a new instance of Markdown middleware that
|
|
|
|
// renders markdown to HTML on-the-fly.
|
|
|
|
func New(c middleware.Controller) (middleware.Middleware, error) {
|
|
|
|
mdconfigs, err := parse(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
md := Markdown{
|
|
|
|
Root: c.Root(),
|
|
|
|
Configs: mdconfigs,
|
|
|
|
}
|
|
|
|
|
2015-03-28 17:55:40 -05:00
|
|
|
return func(next middleware.HandlerFunc) middleware.HandlerFunc {
|
2015-03-21 11:59:29 -05:00
|
|
|
md.Next = next
|
|
|
|
return md.ServeHTTP
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-03-16 12:45:51 -05:00
|
|
|
// ServeHTTP implements the http.Handler interface.
|
2015-03-28 17:55:40 -05:00
|
|
|
func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
2015-03-21 11:59:29 -05:00
|
|
|
for _, m := range md.Configs {
|
|
|
|
if !middleware.Path(r.URL.Path).Matches(m.PathScope) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-03-16 12:45:51 -05:00
|
|
|
for _, ext := range m.Extensions {
|
|
|
|
if strings.HasSuffix(r.URL.Path, ext) {
|
2015-03-21 11:59:29 -05:00
|
|
|
fpath := md.Root + r.URL.Path
|
2015-03-16 12:45:51 -05:00
|
|
|
|
|
|
|
body, err := ioutil.ReadFile(fpath)
|
|
|
|
if err != nil {
|
2015-03-28 17:55:40 -05:00
|
|
|
if os.IsPermission(err) {
|
|
|
|
return http.StatusForbidden, err
|
|
|
|
}
|
|
|
|
return http.StatusNotFound, nil
|
2015-03-16 12:45:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
content := blackfriday.Markdown(body, m.Renderer, 0)
|
|
|
|
|
|
|
|
var scripts, styles string
|
|
|
|
for _, style := range m.Styles {
|
|
|
|
styles += strings.Replace(cssTemplate, "{{url}}", style, 1) + "\r\n"
|
|
|
|
}
|
|
|
|
for _, script := range m.Scripts {
|
|
|
|
scripts += strings.Replace(jsTemplate, "{{url}}", script, 1) + "\r\n"
|
|
|
|
}
|
|
|
|
|
2015-03-20 01:01:39 -05:00
|
|
|
// Title is first line (length-limited), otherwise filename
|
|
|
|
title := path.Base(fpath)
|
|
|
|
newline := bytes.Index(body, []byte("\n"))
|
|
|
|
if newline > -1 {
|
|
|
|
firstline := body[:newline]
|
|
|
|
newTitle := strings.TrimSpace(string(firstline))
|
|
|
|
if len(newTitle) > 1 {
|
|
|
|
if len(newTitle) > 128 {
|
|
|
|
title = newTitle[:128]
|
|
|
|
} else {
|
|
|
|
title = newTitle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 12:45:51 -05:00
|
|
|
html := htmlTemplate
|
2015-03-20 01:01:39 -05:00
|
|
|
html = strings.Replace(html, "{{title}}", title, 1)
|
2015-03-16 12:45:51 -05:00
|
|
|
html = strings.Replace(html, "{{css}}", styles, 1)
|
|
|
|
html = strings.Replace(html, "{{js}}", scripts, 1)
|
|
|
|
html = strings.Replace(html, "{{body}}", string(content), 1)
|
|
|
|
|
|
|
|
w.Write([]byte(html))
|
2015-03-20 01:01:39 -05:00
|
|
|
|
2015-03-28 17:55:40 -05:00
|
|
|
return http.StatusOK, nil
|
2015-03-16 12:45:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 01:01:39 -05:00
|
|
|
// Didn't qualify to serve as markdown; pass-thru
|
2015-03-28 17:55:40 -05:00
|
|
|
return md.Next(w, r)
|
2015-03-16 12:45:51 -05:00
|
|
|
}
|
|
|
|
|
2015-03-21 11:59:29 -05:00
|
|
|
// parse creates new instances of Markdown middleware.
|
|
|
|
func parse(c middleware.Controller) ([]MarkdownConfig, error) {
|
|
|
|
var mdconfigs []MarkdownConfig
|
2015-03-16 12:45:51 -05:00
|
|
|
|
|
|
|
for c.Next() {
|
2015-03-21 11:59:29 -05:00
|
|
|
md := MarkdownConfig{
|
|
|
|
Renderer: blackfriday.HtmlRenderer(0, "", ""),
|
|
|
|
}
|
|
|
|
|
2015-03-16 12:45:51 -05:00
|
|
|
// Get the path scope
|
|
|
|
if !c.NextArg() {
|
2015-03-21 11:59:29 -05:00
|
|
|
return mdconfigs, c.ArgErr()
|
2015-03-16 12:45:51 -05:00
|
|
|
}
|
|
|
|
md.PathScope = c.Val()
|
|
|
|
|
|
|
|
// Load any other configuration parameters
|
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
|
|
|
case "ext":
|
|
|
|
exts := c.RemainingArgs()
|
|
|
|
if len(exts) == 0 {
|
2015-03-21 11:59:29 -05:00
|
|
|
return mdconfigs, c.ArgErr()
|
2015-03-16 12:45:51 -05:00
|
|
|
}
|
|
|
|
md.Extensions = append(md.Extensions, exts...)
|
|
|
|
case "css":
|
|
|
|
if !c.NextArg() {
|
2015-03-21 11:59:29 -05:00
|
|
|
return mdconfigs, c.ArgErr()
|
2015-03-16 12:45:51 -05:00
|
|
|
}
|
|
|
|
md.Styles = append(md.Styles, c.Val())
|
|
|
|
case "js":
|
|
|
|
if !c.NextArg() {
|
2015-03-21 11:59:29 -05:00
|
|
|
return mdconfigs, c.ArgErr()
|
2015-03-16 12:45:51 -05:00
|
|
|
}
|
|
|
|
md.Scripts = append(md.Scripts, c.Val())
|
|
|
|
default:
|
2015-03-21 11:59:29 -05:00
|
|
|
return mdconfigs, c.Err("Expected valid markdown configuration property")
|
2015-03-16 12:45:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 00:41:16 -05:00
|
|
|
// If no extensions were specified, assume .md
|
|
|
|
if len(md.Extensions) == 0 {
|
|
|
|
md.Extensions = []string{".md"}
|
|
|
|
}
|
|
|
|
|
2015-03-21 11:59:29 -05:00
|
|
|
mdconfigs = append(mdconfigs, md)
|
2015-03-16 12:45:51 -05:00
|
|
|
}
|
|
|
|
|
2015-03-21 11:59:29 -05:00
|
|
|
return mdconfigs, nil
|
2015-03-16 12:45:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
htmlTemplate = `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>{{title}}</title>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
{{css}}
|
|
|
|
{{js}}
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
{{body}}
|
|
|
|
</body>
|
|
|
|
</html>`
|
|
|
|
cssTemplate = `<link rel="stylesheet" href="{{url}}">`
|
|
|
|
jsTemplate = `<script src="{{url}}"></script>`
|
|
|
|
)
|