mirror of
https://github.com/caddyserver/caddy.git
synced 2025-06-06 04:34:37 +08:00
markdown: working version of template integration. Awaiting static site generation and tests.
This commit is contained in:
@ -3,11 +3,9 @@
|
||||
package markdown
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/mholt/caddy/middleware"
|
||||
@ -51,7 +49,10 @@ type Config struct {
|
||||
Scripts []string
|
||||
|
||||
// Map of registered templates
|
||||
Templates map[string] string
|
||||
Templates map[string]string
|
||||
|
||||
// Static files
|
||||
StaticFiles map[string]string
|
||||
}
|
||||
|
||||
// ServeHTTP implements the http.Handler interface.
|
||||
@ -81,36 +82,10 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
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"
|
||||
html, err := Process(md, fpath, body)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
for _, script := range m.Scripts {
|
||||
scripts += strings.Replace(jsTemplate, "{{url}}", script, 1) + "\r\n"
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
html := htmlTemplate
|
||||
html = strings.Replace(html, "{{title}}", title, 1)
|
||||
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))
|
||||
|
||||
@ -122,20 +97,3 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
|
||||
// Didn't qualify to serve as markdown; pass-thru
|
||||
return md.Next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
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>`
|
||||
)
|
||||
|
Reference in New Issue
Block a user