2015-05-05 01:49:49 +08:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
2015-05-06 05:50:42 +08:00
|
|
|
"net/http"
|
2015-05-07 20:45:27 +08:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2015-05-10 11:12:28 +08:00
|
|
|
"strings"
|
2015-05-06 05:50:42 +08:00
|
|
|
|
2015-05-05 01:49:49 +08:00
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
"github.com/mholt/caddy/middleware/markdown"
|
|
|
|
"github.com/russross/blackfriday"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Markdown configures a new Markdown middleware instance.
|
|
|
|
func Markdown(c *Controller) (middleware.Middleware, error) {
|
|
|
|
mdconfigs, err := markdownParse(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
md := markdown.Markdown{
|
2015-05-06 05:50:42 +08:00
|
|
|
Root: c.Root,
|
|
|
|
FileSys: http.Dir(c.Root),
|
|
|
|
Configs: mdconfigs,
|
|
|
|
IndexFiles: []string{"index.md"},
|
2015-05-05 01:49:49 +08:00
|
|
|
}
|
|
|
|
|
2015-08-05 22:15:52 +08:00
|
|
|
// Sweep the whole path at startup to at least generate link index, maybe generate static site
|
2015-05-10 11:12:28 +08:00
|
|
|
c.Startup = append(c.Startup, func() error {
|
2015-08-05 06:35:09 +08:00
|
|
|
for i := range mdconfigs {
|
2015-09-11 06:12:46 +08:00
|
|
|
cfg := mdconfigs[i]
|
2015-05-10 11:12:28 +08:00
|
|
|
|
2015-08-05 22:15:52 +08:00
|
|
|
// Generate link index and static files (if enabled)
|
2015-08-05 16:55:04 +08:00
|
|
|
if err := markdown.GenerateStatic(md, cfg); err != nil {
|
2015-07-28 12:21:09 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-08-05 16:55:04 +08:00
|
|
|
|
|
|
|
// Watch file changes for static site generation if not in development mode.
|
2015-08-05 07:41:04 +08:00
|
|
|
if !cfg.Development {
|
2015-08-05 06:35:09 +08:00
|
|
|
markdown.Watch(md, cfg, markdown.DefaultInterval)
|
|
|
|
}
|
2015-05-10 11:12:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2015-05-05 01:49:49 +08:00
|
|
|
return func(next middleware.Handler) middleware.Handler {
|
|
|
|
md.Next = next
|
|
|
|
return md
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-09-11 06:12:46 +08:00
|
|
|
func markdownParse(c *Controller) ([]*markdown.Config, error) {
|
|
|
|
var mdconfigs []*markdown.Config
|
2015-05-05 01:49:49 +08:00
|
|
|
|
|
|
|
for c.Next() {
|
2015-09-11 06:12:46 +08:00
|
|
|
md := &markdown.Config{
|
2015-05-07 07:19:02 +08:00
|
|
|
Renderer: blackfriday.HtmlRenderer(0, "", ""),
|
|
|
|
Templates: make(map[string]string),
|
|
|
|
StaticFiles: make(map[string]string),
|
2015-05-05 01:49:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the path scope
|
|
|
|
if !c.NextArg() || c.Val() == "{" {
|
|
|
|
return mdconfigs, c.ArgErr()
|
|
|
|
}
|
|
|
|
md.PathScope = c.Val()
|
|
|
|
|
|
|
|
// Load any other configuration parameters
|
|
|
|
for c.NextBlock() {
|
2015-10-10 06:35:34 +08:00
|
|
|
if err := loadParams(c, md); err != nil {
|
|
|
|
return mdconfigs, err
|
2015-05-05 01:49:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no extensions were specified, assume .md
|
|
|
|
if len(md.Extensions) == 0 {
|
|
|
|
md.Extensions = []string{".md"}
|
|
|
|
}
|
|
|
|
|
|
|
|
mdconfigs = append(mdconfigs, md)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mdconfigs, nil
|
|
|
|
}
|
2015-10-10 06:35:34 +08:00
|
|
|
|
|
|
|
func loadParams(c *Controller, mdc *markdown.Config) error {
|
|
|
|
switch c.Val() {
|
|
|
|
case "ext":
|
|
|
|
exts := c.RemainingArgs()
|
|
|
|
if len(exts) == 0 {
|
|
|
|
return c.ArgErr()
|
|
|
|
}
|
|
|
|
mdc.Extensions = append(mdc.Extensions, exts...)
|
|
|
|
return nil
|
|
|
|
case "css":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return c.ArgErr()
|
|
|
|
}
|
|
|
|
mdc.Styles = append(mdc.Styles, c.Val())
|
|
|
|
return nil
|
|
|
|
case "js":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return c.ArgErr()
|
|
|
|
}
|
|
|
|
mdc.Scripts = append(mdc.Scripts, c.Val())
|
|
|
|
return nil
|
|
|
|
case "template":
|
|
|
|
tArgs := c.RemainingArgs()
|
|
|
|
switch len(tArgs) {
|
|
|
|
case 0:
|
|
|
|
return c.ArgErr()
|
|
|
|
case 1:
|
|
|
|
if _, ok := mdc.Templates[markdown.DefaultTemplate]; ok {
|
|
|
|
return c.Err("only one default template is allowed, use alias.")
|
|
|
|
}
|
2015-10-14 07:49:53 +08:00
|
|
|
fpath := filepath.ToSlash(filepath.Clean(c.Root + string(filepath.Separator) + tArgs[0]))
|
2015-10-10 06:35:34 +08:00
|
|
|
mdc.Templates[markdown.DefaultTemplate] = fpath
|
|
|
|
return nil
|
|
|
|
case 2:
|
2015-10-14 07:49:53 +08:00
|
|
|
fpath := filepath.ToSlash(filepath.Clean(c.Root + string(filepath.Separator) + tArgs[1]))
|
2015-10-10 06:35:34 +08:00
|
|
|
mdc.Templates[tArgs[0]] = fpath
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return c.ArgErr()
|
|
|
|
}
|
|
|
|
case "sitegen":
|
|
|
|
if c.NextArg() {
|
|
|
|
mdc.StaticDir = path.Join(c.Root, c.Val())
|
|
|
|
} else {
|
|
|
|
mdc.StaticDir = path.Join(c.Root, markdown.DefaultStaticDir)
|
|
|
|
}
|
|
|
|
if c.NextArg() {
|
|
|
|
// only 1 argument allowed
|
|
|
|
return c.ArgErr()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case "dev":
|
|
|
|
if c.NextArg() {
|
|
|
|
mdc.Development = strings.ToLower(c.Val()) == "true"
|
|
|
|
} else {
|
|
|
|
mdc.Development = true
|
|
|
|
}
|
|
|
|
if c.NextArg() {
|
|
|
|
// only 1 argument allowed
|
|
|
|
return c.ArgErr()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return c.Err("Expected valid markdown configuration property")
|
|
|
|
}
|
|
|
|
}
|