2015-05-05 21:37:29 -05:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
2016-04-30 22:09:25 -05:00
|
|
|
"io"
|
2016-04-30 19:01:31 -05:00
|
|
|
"io/ioutil"
|
2016-04-16 18:50:45 -05:00
|
|
|
"os"
|
2015-05-06 18:19:02 -05:00
|
|
|
|
2015-07-21 00:58:34 -05:00
|
|
|
"github.com/mholt/caddy/middleware"
|
2016-04-16 16:45:32 -05:00
|
|
|
"github.com/mholt/caddy/middleware/markdown/metadata"
|
2016-04-30 19:01:31 -05:00
|
|
|
"github.com/mholt/caddy/middleware/markdown/summary"
|
2015-05-06 18:19:02 -05:00
|
|
|
"github.com/russross/blackfriday"
|
2015-05-05 21:37:29 -05:00
|
|
|
)
|
|
|
|
|
2016-04-30 19:01:31 -05:00
|
|
|
type FileInfo struct {
|
|
|
|
os.FileInfo
|
|
|
|
ctx middleware.Context
|
|
|
|
}
|
|
|
|
|
2016-04-30 19:04:44 -05:00
|
|
|
func (f FileInfo) Summarize(wordcount int) (string, error) {
|
2016-04-30 19:01:31 -05:00
|
|
|
fp, err := f.ctx.Root.Open(f.Name())
|
|
|
|
if err != nil {
|
2016-04-30 19:04:44 -05:00
|
|
|
return "", err
|
2016-04-30 19:01:31 -05:00
|
|
|
}
|
|
|
|
defer fp.Close()
|
|
|
|
|
|
|
|
buf, err := ioutil.ReadAll(fp)
|
|
|
|
if err != nil {
|
2016-04-30 19:04:44 -05:00
|
|
|
return "", err
|
2016-04-30 19:01:31 -05:00
|
|
|
}
|
|
|
|
|
2016-04-30 19:04:44 -05:00
|
|
|
return string(summary.Markdown(buf, wordcount)), nil
|
2016-04-30 19:01:31 -05:00
|
|
|
}
|
|
|
|
|
2016-04-16 16:45:32 -05:00
|
|
|
// Markdown processes the contents of a page in b. It parses the metadata
|
2015-05-09 22:12:28 -05:00
|
|
|
// (if any) and uses the template (if found).
|
2016-04-30 22:09:25 -05:00
|
|
|
func (c *Config) Markdown(title string, r io.Reader, dirents []os.FileInfo, ctx middleware.Context) ([]byte, error) {
|
|
|
|
body, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
parser := metadata.GetParser(body)
|
2016-04-16 16:45:32 -05:00
|
|
|
markdown := parser.Markdown()
|
|
|
|
mdata := parser.Metadata()
|
2015-05-09 05:49:54 -05:00
|
|
|
|
2015-05-05 21:37:29 -05:00
|
|
|
// 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
|
2016-04-30 22:09:25 -05:00
|
|
|
html := 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
|
2016-04-30 22:09:25 -05:00
|
|
|
mdata.Variables["body"] = string(html)
|
2016-04-30 19:01:31 -05:00
|
|
|
|
|
|
|
// fixup title
|
2016-04-30 22:09:25 -05:00
|
|
|
mdata.Variables["title"] = mdata.Title
|
|
|
|
if mdata.Variables["title"] == "" {
|
|
|
|
mdata.Variables["title"] = title
|
2015-07-25 15:39:13 -05:00
|
|
|
}
|
2015-05-07 07:45:27 -05:00
|
|
|
|
2016-04-30 19:01:31 -05:00
|
|
|
// massage possible files
|
|
|
|
files := []FileInfo{}
|
|
|
|
for _, ent := range dirents {
|
|
|
|
file := FileInfo{
|
|
|
|
FileInfo: ent,
|
|
|
|
ctx: ctx,
|
|
|
|
}
|
|
|
|
files = append(files, file)
|
2016-04-16 18:50:45 -05:00
|
|
|
}
|
|
|
|
|
2016-04-30 19:01:31 -05:00
|
|
|
return execTemplate(c, mdata, files, ctx)
|
2015-05-08 10:20:07 -05:00
|
|
|
}
|