From 6451e10d3ef9d1de4f79bf5ded9cb6e2052ab5ad Mon Sep 17 00:00:00 2001 From: Maxime Date: Tue, 21 Jul 2015 07:58:34 +0200 Subject: [PATCH] Add context to markdown template Created a struct containing middleware.Context, Title, Markdown and the variables from the user to use to render the template. The title now can be accessed via {{.Title}}. The variables can now be accessed via {{.Var.myVariableName}}. --- config/setup/markdown.go | 3 +- middleware/markdown/blog/test.md | 15 ++++++ middleware/markdown/header.html | 1 + middleware/markdown/markdown.go | 7 ++- middleware/markdown/markdown_test.go | 69 +++++++++++++++++++++++++++ middleware/markdown/markdown_tpl.html | 11 +++++ middleware/markdown/process.go | 23 +++++++-- 7 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 middleware/markdown/blog/test.md create mode 100644 middleware/markdown/header.html create mode 100644 middleware/markdown/markdown_test.go create mode 100644 middleware/markdown/markdown_tpl.html diff --git a/config/setup/markdown.go b/config/setup/markdown.go index fd92f20a..bbccf9d5 100644 --- a/config/setup/markdown.go +++ b/config/setup/markdown.go @@ -62,7 +62,8 @@ func Markdown(c *Controller) (middleware.Middleware, error) { reqPath = "/" + reqPath // Generate the static file - _, err = md.Process(cfg, reqPath, body) + ctx := middleware.Context{Root: md.FileSys} + _, err = md.Process(cfg, reqPath, body, ctx) if err != nil { return err } diff --git a/middleware/markdown/blog/test.md b/middleware/markdown/blog/test.md new file mode 100644 index 00000000..7ec76616 --- /dev/null +++ b/middleware/markdown/blog/test.md @@ -0,0 +1,15 @@ +--- +title: Markdown test +variables: + sitename: A Caddy website +--- + +## Welcome on the blog + +Body + +``` go +func getTrue() bool { + return true +} +``` diff --git a/middleware/markdown/header.html b/middleware/markdown/header.html new file mode 100644 index 00000000..78e5a6a4 --- /dev/null +++ b/middleware/markdown/header.html @@ -0,0 +1 @@ +

Header

diff --git a/middleware/markdown/markdown.go b/middleware/markdown/markdown.go index 6b7f3556..71b18906 100644 --- a/middleware/markdown/markdown.go +++ b/middleware/markdown/markdown.go @@ -119,7 +119,12 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error return http.StatusInternalServerError, err } - html, err := md.Process(m, fpath, body) + ctx := middleware.Context{ + Root: md.FileSys, + Req: r, + URL: r.URL, + } + html, err := md.Process(m, fpath, body, ctx) if err != nil { return http.StatusInternalServerError, err } diff --git a/middleware/markdown/markdown_test.go b/middleware/markdown/markdown_test.go new file mode 100644 index 00000000..3f8b50ec --- /dev/null +++ b/middleware/markdown/markdown_test.go @@ -0,0 +1,69 @@ +package markdown + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/russross/blackfriday" +) + +func TestMarkdown(t *testing.T) { + templates := make(map[string]string) + templates[DefaultTemplate] = "markdown_tpl.html" + md := Markdown{ + Root: "/blog", + FileSys: http.Dir("."), + Configs: []Config{ + Config{ + Renderer: blackfriday.HtmlRenderer(0, "", ""), + PathScope: "/blog", + Extensions: []string{"md"}, + Styles: []string{}, + Scripts: []string{}, + Templates: templates, + }, + }, + IndexFiles: []string{"index.html"}, + } + + req, err := http.NewRequest("GET", "/blog/test.md", nil) + if err != nil { + t.Fatalf("Could not create HTTP request: %v", err) + } + + rec := httptest.NewRecorder() + + md.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, rec.Code) + } + + respBody := rec.Body.String() + expectedBody := ` + + +Markdown test + + +

Header

+ +Welcome to A Caddy website! +

Welcome on the blog

+ +

Body

+ +

go +func getTrue() bool { + return true +} +

+ + + +` + + if respBody != expectedBody { + t.Fatalf("Expected body: %v got: %v", expectedBody, respBody) + } +} diff --git a/middleware/markdown/markdown_tpl.html b/middleware/markdown/markdown_tpl.html new file mode 100644 index 00000000..2d0c7435 --- /dev/null +++ b/middleware/markdown/markdown_tpl.html @@ -0,0 +1,11 @@ + + + +{{.Title}} + + +{{.Include "header.html"}} +Welcome to {{.Var.sitename}}! +{{.Markdown}} + + diff --git a/middleware/markdown/process.go b/middleware/markdown/process.go index 95f7e51b..ce9b5a7c 100644 --- a/middleware/markdown/process.go +++ b/middleware/markdown/process.go @@ -9,6 +9,7 @@ import ( "strings" "text/template" + "github.com/mholt/caddy/middleware" "github.com/russross/blackfriday" ) @@ -17,9 +18,16 @@ const ( DefaultStaticDir = "generated_site" ) +type MarkdownData struct { + middleware.Context + Var map[string]interface{} + Title string + Markdown string +} + // Process processes the contents of a page in b. It parses the metadata // (if any) and uses the template (if found). -func (md Markdown) Process(c Config, requestPath string, b []byte) ([]byte, error) { +func (md Markdown) Process(c Config, requestPath string, b []byte, ctx middleware.Context) ([]byte, error) { var metadata = Metadata{Variables: make(map[string]interface{})} var markdown []byte var err error @@ -63,12 +71,12 @@ func (md Markdown) Process(c Config, requestPath string, b []byte) ([]byte, erro // set it as body for template metadata.Variables["markdown"] = string(markdown) - return md.processTemplate(c, requestPath, tmpl, metadata) + return md.processTemplate(c, requestPath, tmpl, metadata, ctx) } // processTemplate processes a template given a requestPath, // template (tmpl) and metadata -func (md Markdown) processTemplate(c Config, requestPath string, tmpl []byte, metadata Metadata) ([]byte, error) { +func (md Markdown) processTemplate(c Config, requestPath string, tmpl []byte, metadata Metadata, ctx middleware.Context) ([]byte, error) { // if template is not specified, // use the default template if tmpl == nil { @@ -81,7 +89,14 @@ func (md Markdown) processTemplate(c Config, requestPath string, tmpl []byte, me if err != nil { return nil, err } - if err = t.Execute(b, metadata.Variables); err != nil { + mdData := MarkdownData{ + Context: ctx, + Var: metadata.Variables, + Title: metadata.Title, + Markdown: metadata.Variables["markdown"].(string), + } + + if err = t.Execute(b, mdData); err != nil { return nil, err }