2015-05-06 05:37:29 +03:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-05-07 02:19:02 +03:00
|
|
|
"path/filepath"
|
|
|
|
|
2015-07-21 08:58:34 +03:00
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-05-07 02:19:02 +03:00
|
|
|
"github.com/russross/blackfriday"
|
2015-05-06 05:37:29 +03:00
|
|
|
)
|
|
|
|
|
2015-11-02 22:28:50 +03:00
|
|
|
// Data represents a markdown document.
|
2015-10-10 01:35:34 +03:00
|
|
|
type Data struct {
|
2015-07-21 08:58:34 +03:00
|
|
|
middleware.Context
|
2016-02-22 13:53:47 +03:00
|
|
|
Doc map[string]string
|
|
|
|
DocFlags map[string]bool
|
2016-04-11 03:20:09 +03:00
|
|
|
Styles []string
|
|
|
|
Scripts []string
|
2015-07-21 08:58:34 +03:00
|
|
|
}
|
|
|
|
|
2016-03-08 01:32:07 +03: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-10 06:12:28 +03:00
|
|
|
// Process processes the contents of a page in b. It parses the metadata
|
|
|
|
// (if any) and uses the template (if found).
|
2015-09-11 01:12:46 +03:00
|
|
|
func (md Markdown) Process(c *Config, requestPath string, b []byte, ctx middleware.Context) ([]byte, error) {
|
2016-02-22 13:53:47 +03:00
|
|
|
var metadata = newMetadata()
|
2015-05-09 13:49:54 +03:00
|
|
|
var markdown []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// find parser compatible with page contents
|
|
|
|
parser := findParser(b)
|
|
|
|
|
2015-05-10 06:12:28 +03: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 13:49:54 +03:00
|
|
|
markdown, err = parser.Parse(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
metadata = parser.Metadata()
|
2015-05-06 05:37:29 +03:00
|
|
|
}
|
2015-05-09 13:49:54 +03:00
|
|
|
|
2015-05-06 05:37:29 +03:00
|
|
|
// process markdown
|
2016-04-11 03:20:09 +03:00
|
|
|
extns := 0
|
|
|
|
extns |= blackfriday.EXTENSION_TABLES
|
|
|
|
extns |= blackfriday.EXTENSION_FENCED_CODE
|
|
|
|
extns |= blackfriday.EXTENSION_STRIKETHROUGH
|
|
|
|
extns |= blackfriday.EXTENSION_DEFINITION_LISTS
|
2015-10-29 18:59:14 +03:00
|
|
|
markdown = blackfriday.Markdown(markdown, c.Renderer, extns)
|
2015-05-08 18:20:07 +03:00
|
|
|
|
2015-05-07 02:19:02 +03:00
|
|
|
// set it as body for template
|
2015-07-24 23:14:05 +03:00
|
|
|
metadata.Variables["body"] = string(markdown)
|
2015-07-25 23:39:13 +03: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-06 05:37:29 +03:00
|
|
|
|
2016-04-11 09:17:01 +03:00
|
|
|
// return md.processTemplate(c, requestPath, metadata, ctx)
|
|
|
|
return md.doTemplate(c, requestPath, metadata, ctx)
|
2015-05-06 05:37:29 +03:00
|
|
|
}
|
|
|
|
|
2016-04-11 09:17:01 +03:00
|
|
|
// doTemplate executes a template given a requestPath, template, and metadata
|
|
|
|
func (md Markdown) doTemplate(c *Config, reqPath string, metadata Metadata, ctx middleware.Context) ([]byte, error) {
|
2015-10-10 01:35:34 +03:00
|
|
|
mdData := Data{
|
2016-02-22 13:53:47 +03:00
|
|
|
Context: ctx,
|
|
|
|
Doc: metadata.Variables,
|
|
|
|
DocFlags: metadata.Flags,
|
2016-04-11 03:20:09 +03:00
|
|
|
Styles: c.Styles,
|
|
|
|
Scripts: c.Scripts,
|
2015-07-21 08:58:34 +03:00
|
|
|
}
|
|
|
|
|
2016-04-11 03:20:09 +03:00
|
|
|
b := new(bytes.Buffer)
|
2016-04-11 09:17:01 +03:00
|
|
|
if err := c.Template.ExecuteTemplate(b, metadata.Template, mdData); err != nil {
|
2015-05-07 02:19:02 +03:00
|
|
|
return nil, err
|
2015-05-06 05:37:29 +03:00
|
|
|
}
|
2015-05-07 15:45:27 +03:00
|
|
|
|
2015-05-07 02:19:02 +03:00
|
|
|
return b.Bytes(), nil
|
2015-05-08 18:20:07 +03:00
|
|
|
}
|