2015-05-06 10:37:29 +08:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2015-05-09 06:45:31 +08:00
|
|
|
"log"
|
|
|
|
"os"
|
2015-05-07 07:19:02 +08:00
|
|
|
"path/filepath"
|
2015-05-09 06:45:31 +08:00
|
|
|
"strings"
|
2015-05-06 10:37:29 +08:00
|
|
|
"text/template"
|
2015-05-07 07:19:02 +08:00
|
|
|
|
2015-07-21 13:58:34 +08:00
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-05-07 07:19:02 +08:00
|
|
|
"github.com/russross/blackfriday"
|
2015-05-06 10:37:29 +08:00
|
|
|
)
|
|
|
|
|
2015-05-07 20:45:27 +08:00
|
|
|
const (
|
2015-05-10 11:12:28 +08:00
|
|
|
DefaultTemplate = "defaultTemplate"
|
|
|
|
DefaultStaticDir = "generated_site"
|
2015-05-07 20:45:27 +08:00
|
|
|
)
|
|
|
|
|
2015-07-21 13:58:34 +08:00
|
|
|
type MarkdownData struct {
|
|
|
|
middleware.Context
|
2015-07-28 12:21:09 +08:00
|
|
|
Doc map[string]string
|
|
|
|
Links []PageLink
|
2015-07-21 13:58:34 +08:00
|
|
|
}
|
|
|
|
|
2015-05-10 11:12:28 +08: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 06:12:46 +08:00
|
|
|
func (md Markdown) Process(c *Config, requestPath string, b []byte, ctx middleware.Context) ([]byte, error) {
|
2015-07-27 00:32:34 +08:00
|
|
|
var metadata = Metadata{Variables: make(map[string]string)}
|
2015-05-09 18:49:54 +08:00
|
|
|
var markdown []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// find parser compatible with page contents
|
|
|
|
parser := findParser(b)
|
|
|
|
|
2015-05-10 11:12:28 +08: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 18:49:54 +08:00
|
|
|
markdown, err = parser.Parse(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
metadata = parser.Metadata()
|
2015-05-06 10:37:29 +08:00
|
|
|
}
|
2015-05-09 18:49:54 +08:00
|
|
|
|
2015-05-07 07:19:02 +08: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-06 10:37:29 +08:00
|
|
|
var tmpl []byte
|
|
|
|
if metadata.Template != "" {
|
|
|
|
if t, ok := c.Templates[metadata.Template]; ok {
|
2015-05-07 07:19:02 +08:00
|
|
|
tmpl, err = ioutil.ReadFile(t)
|
2015-05-06 10:37:29 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// process markdown
|
2015-05-07 07:19:02 +08:00
|
|
|
markdown = blackfriday.Markdown(markdown, c.Renderer, 0)
|
2015-05-08 23:20:07 +08:00
|
|
|
|
2015-05-07 07:19:02 +08:00
|
|
|
// set it as body for template
|
2015-07-25 04:14:05 +08:00
|
|
|
metadata.Variables["body"] = string(markdown)
|
2015-07-26 04:39:13 +08: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 10:37:29 +08:00
|
|
|
|
2015-07-21 13:58:34 +08:00
|
|
|
return md.processTemplate(c, requestPath, tmpl, metadata, ctx)
|
2015-05-06 10:37:29 +08:00
|
|
|
}
|
|
|
|
|
2015-05-08 23:20:07 +08:00
|
|
|
// processTemplate processes a template given a requestPath,
|
|
|
|
// template (tmpl) and metadata
|
2015-09-11 06:12:46 +08:00
|
|
|
func (md Markdown) processTemplate(c *Config, requestPath string, tmpl []byte, metadata Metadata, ctx middleware.Context) ([]byte, error) {
|
2015-05-08 23:20:07 +08:00
|
|
|
// if template is not specified,
|
|
|
|
// use the default template
|
|
|
|
if tmpl == nil {
|
|
|
|
tmpl = defaultTemplate(c, metadata, requestPath)
|
2015-05-07 07:19:02 +08:00
|
|
|
}
|
|
|
|
|
2015-05-08 23:20:07 +08:00
|
|
|
// process the template
|
2015-05-10 11:12:28 +08:00
|
|
|
b := new(bytes.Buffer)
|
2015-05-07 07:19:02 +08:00
|
|
|
t, err := template.New("").Parse(string(tmpl))
|
2015-05-06 10:37:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-21 13:58:34 +08:00
|
|
|
mdData := MarkdownData{
|
2015-07-25 04:14:05 +08:00
|
|
|
Context: ctx,
|
|
|
|
Doc: metadata.Variables,
|
2015-07-28 12:21:09 +08:00
|
|
|
Links: c.Links,
|
2015-07-21 13:58:34 +08:00
|
|
|
}
|
|
|
|
|
2015-07-29 19:15:02 +08:00
|
|
|
c.RLock()
|
2015-07-28 12:21:09 +08:00
|
|
|
err = t.Execute(b, mdData)
|
2015-07-29 19:15:02 +08:00
|
|
|
c.RUnlock()
|
2015-07-28 12:21:09 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2015-05-07 07:19:02 +08:00
|
|
|
return nil, err
|
2015-05-06 10:37:29 +08:00
|
|
|
}
|
2015-05-07 20:45:27 +08:00
|
|
|
|
|
|
|
// generate static page
|
2015-05-08 23:20:07 +08:00
|
|
|
if err = md.generatePage(c, requestPath, b.Bytes()); err != nil {
|
2015-05-07 20:45:27 +08:00
|
|
|
// if static page generation fails,
|
|
|
|
// nothing fatal, only log the error.
|
2015-05-10 11:12:28 +08:00
|
|
|
// TODO: Report this non-fatal error, but don't log it here
|
2015-08-05 22:15:52 +08:00
|
|
|
log.Println("Rendering error (markdown):", err)
|
2015-05-07 20:45:27 +08:00
|
|
|
}
|
|
|
|
|
2015-05-07 07:19:02 +08:00
|
|
|
return b.Bytes(), nil
|
|
|
|
|
2015-05-06 10:37:29 +08:00
|
|
|
}
|
|
|
|
|
2015-05-10 11:12:28 +08:00
|
|
|
// generatePage generates a static html page from the markdown in content if c.StaticDir
|
|
|
|
// is a non-empty value, meaning that the user enabled static site generation.
|
2015-09-11 06:12:46 +08:00
|
|
|
func (md Markdown) generatePage(c *Config, requestPath string, content []byte) error {
|
2015-05-10 11:12:28 +08:00
|
|
|
// Only generate the page if static site generation is enabled
|
|
|
|
if c.StaticDir != "" {
|
|
|
|
// if static directory is not existing, create it
|
|
|
|
if _, err := os.Stat(c.StaticDir); err != nil {
|
|
|
|
err := os.MkdirAll(c.StaticDir, os.FileMode(0755))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-07 20:45:27 +08:00
|
|
|
}
|
|
|
|
|
2015-05-10 11:12:28 +08:00
|
|
|
filePath := filepath.Join(c.StaticDir, requestPath)
|
2015-05-07 20:45:27 +08:00
|
|
|
|
2015-05-10 11:12:28 +08:00
|
|
|
// If it is index file, use the directory instead
|
|
|
|
if md.IsIndexFile(filepath.Base(requestPath)) {
|
|
|
|
filePath, _ = filepath.Split(filePath)
|
|
|
|
}
|
2015-05-08 23:20:07 +08:00
|
|
|
|
2015-05-10 11:12:28 +08:00
|
|
|
// Create the directory in case it is not existing
|
|
|
|
if err := os.MkdirAll(filePath, os.FileMode(0744)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-07 20:45:27 +08:00
|
|
|
|
2015-05-10 11:12:28 +08:00
|
|
|
// generate index.html file in the directory
|
|
|
|
filePath = filepath.Join(filePath, "index.html")
|
|
|
|
err := ioutil.WriteFile(filePath, content, os.FileMode(0664))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-11 23:34:52 +08:00
|
|
|
c.Lock()
|
2015-05-10 11:12:28 +08:00
|
|
|
c.StaticFiles[requestPath] = filePath
|
2015-09-11 23:34:52 +08:00
|
|
|
c.Unlock()
|
2015-05-07 20:45:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-08 23:20:07 +08:00
|
|
|
// defaultTemplate constructs a default template.
|
2015-09-11 06:12:46 +08:00
|
|
|
func defaultTemplate(c *Config, metadata Metadata, requestPath string) []byte {
|
2015-05-08 23:20:07 +08:00
|
|
|
var scripts, styles bytes.Buffer
|
|
|
|
for _, style := range c.Styles {
|
|
|
|
styles.WriteString(strings.Replace(cssTemplate, "{{url}}", style, 1))
|
|
|
|
styles.WriteString("\r\n")
|
|
|
|
}
|
|
|
|
for _, script := range c.Scripts {
|
|
|
|
scripts.WriteString(strings.Replace(jsTemplate, "{{url}}", script, 1))
|
|
|
|
scripts.WriteString("\r\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Title is first line (length-limited), otherwise filename
|
2015-07-27 00:32:34 +08:00
|
|
|
title, _ := metadata.Variables["title"]
|
2015-05-08 23:20:07 +08:00
|
|
|
|
|
|
|
html := []byte(htmlTemplate)
|
|
|
|
html = bytes.Replace(html, []byte("{{title}}"), []byte(title), 1)
|
|
|
|
html = bytes.Replace(html, []byte("{{css}}"), styles.Bytes(), 1)
|
|
|
|
html = bytes.Replace(html, []byte("{{js}}"), scripts.Bytes(), 1)
|
|
|
|
|
|
|
|
return html
|
|
|
|
}
|
|
|
|
|
2015-05-07 07:19:02 +08:00
|
|
|
const (
|
|
|
|
htmlTemplate = `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>{{title}}</title>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
{{css}}
|
|
|
|
{{js}}
|
|
|
|
</head>
|
|
|
|
<body>
|
2015-07-25 04:14:05 +08:00
|
|
|
{{.Doc.body}}
|
2015-05-07 07:19:02 +08:00
|
|
|
</body>
|
|
|
|
</html>`
|
|
|
|
cssTemplate = `<link rel="stylesheet" href="{{url}}">`
|
|
|
|
jsTemplate = `<script src="{{url}}"></script>`
|
|
|
|
)
|