Commit 7d154353 authored by Toby Allen's avatar Toby Allen Committed by Matt Holt

markdown: Match index file for each extension; fix #1418 (#1559)

* Create list of index files based on extensions and check on a per config
basis

* remove log lines

* fixed tests

* made gofmt suggested change

* Changes made to simplify
parent e26a855d
...@@ -29,9 +29,6 @@ type Markdown struct { ...@@ -29,9 +29,6 @@ type Markdown struct {
// The list of markdown configurations // The list of markdown configurations
Configs []*Config Configs []*Config
// The list of index files to try
IndexFiles []string
} }
// Config stores markdown middleware configurations. // Config stores markdown middleware configurations.
...@@ -51,6 +48,9 @@ type Config struct { ...@@ -51,6 +48,9 @@ type Config struct {
// List of JavaScript files to load for each markdown file // List of JavaScript files to load for each markdown file
Scripts []string Scripts []string
// The list of index files to try
IndexFiles []string
// Template(s) to render with // Template(s) to render with
Template *template.Template Template *template.Template
} }
...@@ -78,7 +78,7 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error ...@@ -78,7 +78,7 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
var dirents []os.FileInfo var dirents []os.FileInfo
var lastModTime time.Time var lastModTime time.Time
fpath := r.URL.Path fpath := r.URL.Path
if idx, ok := httpserver.IndexFile(md.FileSys, fpath, md.IndexFiles); ok { if idx, ok := httpserver.IndexFile(md.FileSys, fpath, cfg.IndexFiles); ok {
// We're serving a directory index file, which may be a markdown // We're serving a directory index file, which may be a markdown
// file with a template. Let's grab a list of files this directory // file with a template. Let's grab a list of files this directory
// URL points to, and pass that in to any possible template invocations, // URL points to, and pass that in to any possible template invocations,
......
...@@ -33,9 +33,10 @@ func TestMarkdown(t *testing.T) { ...@@ -33,9 +33,10 @@ func TestMarkdown(t *testing.T) {
Extensions: map[string]struct{}{ Extensions: map[string]struct{}{
".md": {}, ".md": {},
}, },
Styles: []string{}, IndexFiles: []string{"index.md"},
Scripts: []string{}, Styles: []string{},
Template: setDefaultTemplate(f("markdown_tpl.html")), Scripts: []string{},
Template: setDefaultTemplate(f("markdown_tpl.html")),
}, },
{ {
Renderer: blackfriday.HtmlRenderer(0, "", ""), Renderer: blackfriday.HtmlRenderer(0, "", ""),
...@@ -43,9 +44,10 @@ func TestMarkdown(t *testing.T) { ...@@ -43,9 +44,10 @@ func TestMarkdown(t *testing.T) {
Extensions: map[string]struct{}{ Extensions: map[string]struct{}{
".md": {}, ".md": {},
}, },
Styles: []string{}, IndexFiles: []string{"index.md"},
Scripts: []string{}, Styles: []string{},
Template: setDefaultTemplate(f("docflags/template.txt")), Scripts: []string{},
Template: setDefaultTemplate(f("docflags/template.txt")),
}, },
{ {
Renderer: blackfriday.HtmlRenderer(0, "", ""), Renderer: blackfriday.HtmlRenderer(0, "", ""),
...@@ -53,9 +55,10 @@ func TestMarkdown(t *testing.T) { ...@@ -53,9 +55,10 @@ func TestMarkdown(t *testing.T) {
Extensions: map[string]struct{}{ Extensions: map[string]struct{}{
".md": {}, ".md": {},
}, },
Styles: []string{"/resources/css/log.css", "/resources/css/default.css"}, IndexFiles: []string{"index.md"},
Scripts: []string{"/resources/js/log.js", "/resources/js/default.js"}, Styles: []string{"/resources/css/log.css", "/resources/css/default.css"},
Template: GetDefaultTemplate(), Scripts: []string{"/resources/js/log.js", "/resources/js/default.js"},
Template: GetDefaultTemplate(),
}, },
{ {
Renderer: blackfriday.HtmlRenderer(0, "", ""), Renderer: blackfriday.HtmlRenderer(0, "", ""),
...@@ -63,12 +66,13 @@ func TestMarkdown(t *testing.T) { ...@@ -63,12 +66,13 @@ func TestMarkdown(t *testing.T) {
Extensions: map[string]struct{}{ Extensions: map[string]struct{}{
".md": {}, ".md": {},
}, },
Styles: []string{}, IndexFiles: []string{"index.md"},
Scripts: []string{}, Styles: []string{},
Template: setDefaultTemplate(f("markdown_tpl.html")), Scripts: []string{},
Template: setDefaultTemplate(f("markdown_tpl.html")),
}, },
}, },
IndexFiles: []string{"index.html"},
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
t.Fatalf("Next shouldn't be called") t.Fatalf("Next shouldn't be called")
return 0, nil return 0, nil
......
...@@ -26,10 +26,9 @@ func setup(c *caddy.Controller) error { ...@@ -26,10 +26,9 @@ func setup(c *caddy.Controller) error {
cfg := httpserver.GetConfig(c) cfg := httpserver.GetConfig(c)
md := Markdown{ md := Markdown{
Root: cfg.Root, Root: cfg.Root,
FileSys: http.Dir(cfg.Root), FileSys: http.Dir(cfg.Root),
Configs: mdconfigs, Configs: mdconfigs,
IndexFiles: []string{"index.md"},
} }
cfg.AddMiddleware(func(next httpserver.Handler) httpserver.Handler { cfg.AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
...@@ -48,6 +47,7 @@ func markdownParse(c *caddy.Controller) ([]*Config, error) { ...@@ -48,6 +47,7 @@ func markdownParse(c *caddy.Controller) ([]*Config, error) {
Renderer: blackfriday.HtmlRenderer(0, "", ""), Renderer: blackfriday.HtmlRenderer(0, "", ""),
Extensions: make(map[string]struct{}), Extensions: make(map[string]struct{}),
Template: GetDefaultTemplate(), Template: GetDefaultTemplate(),
IndexFiles: []string{},
} }
// Get the path scope // Get the path scope
...@@ -75,6 +75,10 @@ func markdownParse(c *caddy.Controller) ([]*Config, error) { ...@@ -75,6 +75,10 @@ func markdownParse(c *caddy.Controller) ([]*Config, error) {
md.Extensions[".mdown"] = struct{}{} md.Extensions[".mdown"] = struct{}{}
} }
// Make a list of index files to match extensions
for ext := range md.Extensions {
md.IndexFiles = append(md.IndexFiles, "index"+ext)
}
mdconfigs = append(mdconfigs, md) mdconfigs = append(mdconfigs, md)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment