2015-05-05 21:37:29 -05:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2015-05-06 18:19:02 -05:00
|
|
|
"path/filepath"
|
2015-05-05 21:37:29 -05:00
|
|
|
"text/template"
|
2015-05-06 18:19:02 -05:00
|
|
|
|
2015-07-21 00:58:34 -05:00
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-05-06 18:19:02 -05:00
|
|
|
"github.com/russross/blackfriday"
|
2015-05-05 21:37:29 -05:00
|
|
|
)
|
|
|
|
|
2015-05-07 07:45:27 -05:00
|
|
|
const (
|
2015-11-02 14:28:50 -05:00
|
|
|
// DefaultTemplate is the default template.
|
|
|
|
DefaultTemplate = "defaultTemplate"
|
2015-05-07 07:45:27 -05:00
|
|
|
)
|
|
|
|
|
2015-11-02 14:28:50 -05:00
|
|
|
// Data represents a markdown document.
|
2015-10-09 17:35:34 -05:00
|
|
|
type Data struct {
|
2015-07-21 00:58:34 -05:00
|
|
|
middleware.Context
|
2016-02-22 05:53:47 -05:00
|
|
|
Doc map[string]string
|
|
|
|
DocFlags map[string]bool
|
2016-04-10 19:20:09 -05:00
|
|
|
Styles []string
|
|
|
|
Scripts []string
|
2015-07-21 00:58:34 -05:00
|
|
|
}
|
|
|
|
|
2016-03-07 17:32:07 -05:00
|
|
|
// Include "overrides" the embedded middleware.Context's Include()
|
|
|
|
// method so that included files have access to d's fields.
|
|
|
|
func (d Data) Include(filename string) (string, error) {
|
|
|
|
return middleware.ContextInclude(filename, d, d.Root)
|
|
|
|
}
|
|
|
|
|
2015-05-09 22:12:28 -05:00
|
|
|
// Process processes the contents of a page in b. It parses the metadata
|
|
|
|
// (if any) and uses the template (if found).
|
2015-09-10 17:12:46 -05:00
|
|
|
func (md Markdown) Process(c *Config, requestPath string, b []byte, ctx middleware.Context) ([]byte, error) {
|
2016-02-22 05:53:47 -05:00
|
|
|
var metadata = newMetadata()
|
2015-05-09 05:49:54 -05:00
|
|
|
var markdown []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// find parser compatible with page contents
|
|
|
|
parser := findParser(b)
|
|
|
|
|
2015-05-09 22:12:28 -05:00
|
|
|
if parser == nil {
|
|
|
|
// if not found, assume whole file is markdown (no front matter)
|
|
|
|
markdown = b
|
|
|
|
} else {
|
|
|
|
// if found, assume metadata present and parse.
|
2015-05-09 05:49:54 -05:00
|
|
|
markdown, err = parser.Parse(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
metadata = parser.Metadata()
|
2015-05-05 21:37:29 -05:00
|
|
|
}
|
2015-05-09 05:49:54 -05:00
|
|
|
|
2015-05-06 18:19:02 -05:00
|
|
|
// if template is not specified, check if Default template is set
|
|
|
|
if metadata.Template == "" {
|
|
|
|
if _, ok := c.Templates[DefaultTemplate]; ok {
|
|
|
|
metadata.Template = DefaultTemplate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if template is set, load it
|
2015-05-05 21:37:29 -05:00
|
|
|
var tmpl []byte
|
|
|
|
if metadata.Template != "" {
|
|
|
|
if t, ok := c.Templates[metadata.Template]; ok {
|
2015-05-06 18:19:02 -05:00
|
|
|
tmpl, err = ioutil.ReadFile(t)
|
2015-05-05 21:37:29 -05:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// process markdown
|
2016-04-10 19:20:09 -05:00
|
|
|
extns := 0
|
|
|
|
extns |= blackfriday.EXTENSION_TABLES
|
|
|
|
extns |= blackfriday.EXTENSION_FENCED_CODE
|
|
|
|
extns |= blackfriday.EXTENSION_STRIKETHROUGH
|
|
|
|
extns |= blackfriday.EXTENSION_DEFINITION_LISTS
|
2015-10-29 10:59:14 -05:00
|
|
|
markdown = blackfriday.Markdown(markdown, c.Renderer, extns)
|
2015-05-08 10:20:07 -05:00
|
|
|
|
2015-05-06 18:19:02 -05:00
|
|
|
// set it as body for template
|
2015-07-24 15:14:05 -05:00
|
|
|
metadata.Variables["body"] = string(markdown)
|
2015-07-25 15:39:13 -05:00
|
|
|
title := metadata.Title
|
|
|
|
if title == "" {
|
|
|
|
title = filepath.Base(requestPath)
|
|
|
|
var extension = filepath.Ext(requestPath)
|
|
|
|
title = title[0 : len(title)-len(extension)]
|
|
|
|
}
|
|
|
|
metadata.Variables["title"] = title
|
2015-05-05 21:37:29 -05:00
|
|
|
|
2015-07-21 00:58:34 -05:00
|
|
|
return md.processTemplate(c, requestPath, tmpl, metadata, ctx)
|
2015-05-05 21:37:29 -05:00
|
|
|
}
|
|
|
|
|
2015-05-08 10:20:07 -05:00
|
|
|
// processTemplate processes a template given a requestPath,
|
|
|
|
// template (tmpl) and metadata
|
2015-09-10 17:12:46 -05:00
|
|
|
func (md Markdown) processTemplate(c *Config, requestPath string, tmpl []byte, metadata Metadata, ctx middleware.Context) ([]byte, error) {
|
2016-04-10 19:20:09 -05:00
|
|
|
var t *template.Template
|
|
|
|
var err error
|
|
|
|
|
2015-05-08 10:20:07 -05:00
|
|
|
// if template is not specified,
|
|
|
|
// use the default template
|
|
|
|
if tmpl == nil {
|
2016-04-10 19:20:09 -05:00
|
|
|
t = template.Must(template.New("").Parse(htmlTemplate))
|
|
|
|
} else {
|
|
|
|
t, err = template.New("").Parse(string(tmpl))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-06 18:19:02 -05:00
|
|
|
}
|
|
|
|
|
2015-05-08 10:20:07 -05:00
|
|
|
// process the template
|
2015-10-09 17:35:34 -05:00
|
|
|
mdData := Data{
|
2016-02-22 05:53:47 -05:00
|
|
|
Context: ctx,
|
|
|
|
Doc: metadata.Variables,
|
|
|
|
DocFlags: metadata.Flags,
|
2016-04-10 19:20:09 -05:00
|
|
|
Styles: c.Styles,
|
|
|
|
Scripts: c.Scripts,
|
2015-07-21 00:58:34 -05:00
|
|
|
}
|
|
|
|
|
2016-04-10 19:20:09 -05:00
|
|
|
b := new(bytes.Buffer)
|
2015-07-27 23:21:09 -05:00
|
|
|
err = t.Execute(b, mdData)
|
|
|
|
if err != nil {
|
2015-05-06 18:19:02 -05:00
|
|
|
return nil, err
|
2015-05-05 21:37:29 -05:00
|
|
|
}
|
2015-05-07 07:45:27 -05:00
|
|
|
|
2015-05-06 18:19:02 -05:00
|
|
|
return b.Bytes(), nil
|
2015-05-08 10:20:07 -05:00
|
|
|
}
|
|
|
|
|
2015-05-06 18:19:02 -05:00
|
|
|
const (
|
|
|
|
htmlTemplate = `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
2016-04-10 19:20:09 -05:00
|
|
|
<title>{{.Doc.title}}</title>
|
2015-05-06 18:19:02 -05:00
|
|
|
<meta charset="utf-8">
|
2016-04-10 19:20:09 -05:00
|
|
|
{{range .Styles}}<link rel="stylesheet" href="{{.}}">
|
|
|
|
{{end -}}
|
|
|
|
{{range .Scripts}}<script src="{{.}}"></script>
|
|
|
|
{{end -}}
|
2015-05-06 18:19:02 -05:00
|
|
|
</head>
|
|
|
|
<body>
|
2015-07-24 15:14:05 -05:00
|
|
|
{{.Doc.body}}
|
2015-05-06 18:19:02 -05:00
|
|
|
</body>
|
|
|
|
</html>`
|
|
|
|
)
|