Commit b541c717 authored by Tobias Weingartner's avatar Tobias Weingartner

Add ability to markdown a directory with a template.

parent e652d12c
...@@ -89,7 +89,6 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error ...@@ -89,7 +89,6 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
// Set path to found index file // Set path to found index file
fpath = idx fpath = idx
_ = dirents
} }
// If supported extension, process it // If supported extension, process it
...@@ -117,7 +116,7 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error ...@@ -117,7 +116,7 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
Req: r, Req: r,
URL: r.URL, URL: r.URL,
} }
html, err := cfg.Markdown(fpath, body, ctx) html, err := cfg.Markdown(fpath, body, dirents, ctx)
if err != nil { if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }
......
...@@ -3,6 +3,7 @@ package metadata ...@@ -3,6 +3,7 @@ package metadata
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"os"
"time" "time"
) )
...@@ -30,6 +31,9 @@ type Metadata struct { ...@@ -30,6 +31,9 @@ type Metadata struct {
// Flags to be used with Template // Flags to be used with Template
Flags map[string]bool Flags map[string]bool
// Directory entries present, if a directory
Dirents []os.FileInfo
} }
// NewMetadata() returns a new Metadata struct, loaded with the given map // NewMetadata() returns a new Metadata struct, loaded with the given map
......
package markdown package markdown
import ( import (
"os"
"path/filepath" "path/filepath"
"github.com/mholt/caddy/middleware" "github.com/mholt/caddy/middleware"
...@@ -10,7 +11,7 @@ import ( ...@@ -10,7 +11,7 @@ import (
// Markdown processes the contents of a page in b. It parses the metadata // Markdown processes the contents of a page in b. It parses the metadata
// (if any) and uses the template (if found). // (if any) and uses the template (if found).
func (c *Config) Markdown(requestPath string, b []byte, ctx middleware.Context) ([]byte, error) { func (c *Config) Markdown(requestPath string, b []byte, dirents []os.FileInfo, ctx middleware.Context) ([]byte, error) {
parser := metadata.GetParser(b) parser := metadata.GetParser(b)
markdown := parser.Markdown() markdown := parser.Markdown()
mdata := parser.Metadata() mdata := parser.Metadata()
...@@ -33,5 +34,12 @@ func (c *Config) Markdown(requestPath string, b []byte, ctx middleware.Context) ...@@ -33,5 +34,12 @@ func (c *Config) Markdown(requestPath string, b []byte, ctx middleware.Context)
} }
mdata.Variables["title"] = title mdata.Variables["title"] = title
if len(dirents) > 0 {
mdata.Flags["dirents"] = true
mdata.Dirents = dirents
} else {
mdata.Flags["dirents"] = false
}
return execTemplate(c, mdata, ctx) return execTemplate(c, mdata, ctx)
} }
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