forked from caddyserver/caddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
77 lines (64 loc) · 1.9 KB
/
process.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package markdown
import (
"io"
"io/ioutil"
"os"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/mholt/caddy/caddyhttp/markdown/metadata"
"github.com/mholt/caddy/caddyhttp/markdown/summary"
"github.com/russross/blackfriday"
)
// FileInfo represents a file in a particular server context. It wraps the os.FileInfo struct.
type FileInfo struct {
os.FileInfo
ctx httpserver.Context
}
// Summarize returns an abbreviated string representation of the markdown stored in this file.
// wordcount is the number of words returned in the summary.
func (f FileInfo) Summarize(wordcount int) (string, error) {
fp, err := f.ctx.Root.Open(f.Name())
if err != nil {
return "", err
}
defer fp.Close()
buf, err := ioutil.ReadAll(fp)
if err != nil {
return "", err
}
return string(summary.Markdown(buf, wordcount)), nil
}
// Markdown processes the contents of a page in b. It parses the metadata
// (if any) and uses the template (if found).
func (c *Config) Markdown(title string, r io.Reader, dirents []os.FileInfo, ctx httpserver.Context) ([]byte, error) {
body, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
parser := metadata.GetParser(body)
markdown := parser.Markdown()
mdata := parser.Metadata()
// process markdown
extns := 0
extns |= blackfriday.EXTENSION_TABLES
extns |= blackfriday.EXTENSION_FENCED_CODE
extns |= blackfriday.EXTENSION_STRIKETHROUGH
extns |= blackfriday.EXTENSION_DEFINITION_LISTS
html := blackfriday.Markdown(markdown, c.Renderer, extns)
// set it as body for template
mdata.Variables["body"] = string(html)
// fixup title
mdata.Variables["title"] = mdata.Title
if mdata.Variables["title"] == "" {
mdata.Variables["title"] = title
}
// massage possible files
files := []FileInfo{}
for _, ent := range dirents {
file := FileInfo{
FileInfo: ent,
ctx: ctx,
}
files = append(files, file)
}
return execTemplate(c, mdata, files, ctx)
}