From f536bc94b2e26cd1640d6361cee38977c0f98930 Mon Sep 17 00:00:00 2001 From: Maxime Date: Fri, 17 Jul 2015 20:07:24 +0200 Subject: [PATCH 1/9] Added the Context to the browse directive Moved the Context type to middleware and exported it. Users can use .Include and others in browse directive templating Created test for the templates directive. --- config/setup/browse.go | 2 +- middleware/browse/browse.go | 14 ++- middleware/{templates => }/context.go | 53 +++++------ middleware/templates/header.html | 1 + middleware/templates/images/header.html | 1 + middleware/templates/images/img.htm | 1 + middleware/templates/photos/test.html | 1 + middleware/templates/root.html | 1 + middleware/templates/templates.go | 2 +- middleware/templates/templates_test.go | 121 ++++++++++++++++++++++++ 10 files changed, 165 insertions(+), 32 deletions(-) rename middleware/{templates => }/context.go (63%) create mode 100644 middleware/templates/header.html create mode 100644 middleware/templates/images/header.html create mode 100644 middleware/templates/images/img.htm create mode 100644 middleware/templates/photos/test.html create mode 100644 middleware/templates/root.html create mode 100644 middleware/templates/templates_test.go diff --git a/config/setup/browse.go b/config/setup/browse.go index 7db8b40ac..a98ec313d 100644 --- a/config/setup/browse.go +++ b/config/setup/browse.go @@ -2,8 +2,8 @@ package setup import ( "fmt" - "html/template" "io/ioutil" + "text/template" "github.com/mholt/caddy/middleware" "github.com/mholt/caddy/middleware/browse" diff --git a/middleware/browse/browse.go b/middleware/browse/browse.go index c0090acd6..5caeb5ac5 100644 --- a/middleware/browse/browse.go +++ b/middleware/browse/browse.go @@ -5,13 +5,13 @@ package browse import ( "bytes" "errors" - "html/template" "net/http" "net/url" "os" "path" "sort" "strings" + "text/template" "time" "github.com/dustin/go-humanize" @@ -51,6 +51,8 @@ type Listing struct { // And which order Order string + + middleware.Context } // FileInfo is the info about a particular file or directory @@ -135,8 +137,9 @@ var IndexPages = []string{ "default.htm", } -func directoryListing(files []os.FileInfo, urlPath string, canGoUp bool) (Listing, error) { +func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root string) (Listing, error) { var fileinfos []FileInfo + var urlPath = r.URL.Path for _, f := range files { name := f.Name() @@ -168,6 +171,11 @@ func directoryListing(files []os.FileInfo, urlPath string, canGoUp bool) (Listin Path: urlPath, CanGoUp: canGoUp, Items: fileinfos, + Context: middleware.Context{ + Root: http.Dir(root), + Req: r, + URL: r.URL, + }, }, nil } @@ -222,7 +230,7 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { } } // Assemble listing of directory contents - listing, err := directoryListing(files, r.URL.Path, canGoUp) + listing, err := directoryListing(files, r, canGoUp, b.Root) if err != nil { // directory isn't browsable continue } diff --git a/middleware/templates/context.go b/middleware/context.go similarity index 63% rename from middleware/templates/context.go rename to middleware/context.go index 4f125c991..508ec6042 100644 --- a/middleware/templates/context.go +++ b/middleware/context.go @@ -1,4 +1,4 @@ -package templates +package middleware import ( "bytes" @@ -8,23 +8,22 @@ import ( "net/url" "text/template" "time" - - "github.com/mholt/caddy/middleware" ) // This file contains the context and functions available for // use in the templates. // context is the context with which templates are executed. -type context struct { - root http.FileSystem - req *http.Request - URL *url.URL +type Context struct { + Root http.FileSystem + Req *http.Request + // This is used to access information about the URL. + URL *url.URL } // Include returns the contents of filename relative to the site root -func (c context) Include(filename string) (string, error) { - file, err := c.root.Open(filename) +func (c Context) Include(filename string) (string, error) { + file, err := c.Root.Open(filename) if err != nil { return "", err } @@ -50,13 +49,13 @@ func (c context) Include(filename string) (string, error) { } // Date returns the current timestamp in the specified format -func (c context) Date(format string) string { +func (c Context) Date(format string) string { return time.Now().Format(format) } // Cookie gets the value of a cookie with name name. -func (c context) Cookie(name string) string { - cookies := c.req.Cookies() +func (c Context) Cookie(name string) string { + cookies := c.Req.Cookies() for _, cookie := range cookies { if cookie.Name == name { return cookie.Value @@ -66,15 +65,15 @@ func (c context) Cookie(name string) string { } // Header gets the value of a request header with field name. -func (c context) Header(name string) string { - return c.req.Header.Get(name) +func (c Context) Header(name string) string { + return c.Req.Header.Get(name) } // IP gets the (remote) IP address of the client making the request. -func (c context) IP() string { - ip, _, err := net.SplitHostPort(c.req.RemoteAddr) +func (c Context) IP() string { + ip, _, err := net.SplitHostPort(c.Req.RemoteAddr) if err != nil { - return c.req.RemoteAddr + return c.Req.RemoteAddr } return ip } @@ -82,14 +81,14 @@ func (c context) IP() string { // URI returns the raw, unprocessed request URI (including query // string and hash) obtained directly from the Request-Line of // the HTTP request. -func (c context) URI() string { - return c.req.RequestURI +func (c Context) URI() string { + return c.Req.RequestURI } // Host returns the hostname portion of the Host header // from the HTTP request. -func (c context) Host() (string, error) { - host, _, err := net.SplitHostPort(c.req.Host) +func (c Context) Host() (string, error) { + host, _, err := net.SplitHostPort(c.Req.Host) if err != nil { return "", err } @@ -97,8 +96,8 @@ func (c context) Host() (string, error) { } // Port returns the port portion of the Host header if specified. -func (c context) Port() (string, error) { - _, port, err := net.SplitHostPort(c.req.Host) +func (c Context) Port() (string, error) { + _, port, err := net.SplitHostPort(c.Req.Host) if err != nil { return "", err } @@ -106,12 +105,12 @@ func (c context) Port() (string, error) { } // Method returns the method (GET, POST, etc.) of the request. -func (c context) Method() string { - return c.req.Method +func (c Context) Method() string { + return c.Req.Method } // PathMatches returns true if the path portion of the request // URL matches pattern. -func (c context) PathMatches(pattern string) bool { - return middleware.Path(c.req.URL.Path).Matches(pattern) +func (c Context) PathMatches(pattern string) bool { + return Path(c.Req.URL.Path).Matches(pattern) } diff --git a/middleware/templates/header.html b/middleware/templates/header.html new file mode 100644 index 000000000..9c96e0e37 --- /dev/null +++ b/middleware/templates/header.html @@ -0,0 +1 @@ +

Header title

diff --git a/middleware/templates/images/header.html b/middleware/templates/images/header.html new file mode 100644 index 000000000..9c96e0e37 --- /dev/null +++ b/middleware/templates/images/header.html @@ -0,0 +1 @@ +

Header title

diff --git a/middleware/templates/images/img.htm b/middleware/templates/images/img.htm new file mode 100644 index 000000000..865a73809 --- /dev/null +++ b/middleware/templates/images/img.htm @@ -0,0 +1 @@ +img{{.Include "header.html"}} diff --git a/middleware/templates/photos/test.html b/middleware/templates/photos/test.html new file mode 100644 index 000000000..e2e95e133 --- /dev/null +++ b/middleware/templates/photos/test.html @@ -0,0 +1 @@ +test page{{.Include "../header.html"}} diff --git a/middleware/templates/root.html b/middleware/templates/root.html new file mode 100644 index 000000000..e1720e726 --- /dev/null +++ b/middleware/templates/root.html @@ -0,0 +1 @@ +root{{.Include "header.html"}} diff --git a/middleware/templates/templates.go b/middleware/templates/templates.go index fc4f8242e..a699d0026 100644 --- a/middleware/templates/templates.go +++ b/middleware/templates/templates.go @@ -31,7 +31,7 @@ func (t Templates) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error for _, ext := range rule.Extensions { if reqExt == ext { // Create execution context - ctx := context{root: t.FileSys, req: r, URL: r.URL} + ctx := middleware.Context{Root: t.FileSys, Req: r, URL: r.URL} // Build the template tpl, err := template.ParseFiles(filepath.Join(t.Root, fpath)) diff --git a/middleware/templates/templates_test.go b/middleware/templates/templates_test.go new file mode 100644 index 000000000..a519de99e --- /dev/null +++ b/middleware/templates/templates_test.go @@ -0,0 +1,121 @@ +package templates + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/mholt/caddy/middleware" +) + +func Test(t *testing.T) { + tmpl := Templates{ + Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { + return 0, nil + }), + Rules: []Rule{ + Rule{ + Extensions: []string{".html"}, + IndexFiles: []string{"index.html"}, + Path: "/photos", + }, + Rule{ + Extensions: []string{".html", ".htm"}, + IndexFiles: []string{"index.html", "index.htm"}, + Path: "/images", + }, + }, + Root: ".", + FileSys: http.Dir("."), + } + + tmplroot := Templates{ + Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { + return 0, nil + }), + Rules: []Rule{ + Rule{ + Extensions: []string{".html"}, + IndexFiles: []string{"index.html"}, + Path: "/", + }, + }, + Root: ".", + FileSys: http.Dir("."), + } + + /* + * Test tmpl on /photos/test.html + */ + req, err := http.NewRequest("GET", "/photos/test.html", nil) + if err != nil { + t.Fatalf("Test: Could not create HTTP request: %v", err) + } + + rec := httptest.NewRecorder() + + tmpl.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("Test: Wrong response code: %d, should be %d", rec.Code, http.StatusOK) + } + + respBody := rec.Body.String() + expectedBody := `test page

Header title

+ +` + + if respBody != expectedBody { + t.Fatalf("Test: the expected body %v is different from the response one: %v", expectedBody, respBody) + } + + /* + * Test tmpl on /images/img.htm + */ + req, err = http.NewRequest("GET", "/images/img.htm", nil) + if err != nil { + t.Fatalf("Could not create HTTP request: %v", err) + } + + rec = httptest.NewRecorder() + + tmpl.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("Test: Wrong response code: %d, should be %d", rec.Code, http.StatusOK) + } + + respBody = rec.Body.String() + expectedBody = `img

Header title

+ +` + + if respBody != expectedBody { + t.Fatalf("Test: the expected body %v is different from the response one: %v", expectedBody, respBody) + } + + /* + * Test tmplroot on /root.html + */ + req, err = http.NewRequest("GET", "/root.html", nil) + if err != nil { + t.Fatalf("Could not create HTTP request: %v", err) + } + + rec = httptest.NewRecorder() + + tmplroot.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("Test: Wrong response code: %d, should be %d", rec.Code, http.StatusOK) + } + + respBody = rec.Body.String() + expectedBody = `root

Header title

+ +` + + if respBody != expectedBody { + t.Fatalf("Test: the expected body %v is different from the response one: %v", expectedBody, respBody) + } +} From 2d5320c454e03056ba3a3ad6a82f7c2178183cc1 Mon Sep 17 00:00:00 2001 From: Maxime Date: Sat, 18 Jul 2015 11:37:05 +0200 Subject: [PATCH 2/9] Added test for the browse directive Created sample files for the test --- middleware/browse/browse_test.go | 61 +++++++++++++++++++++++++++++ middleware/browse/header.html | 1 + middleware/browse/photos.tpl | 13 ++++++ middleware/browse/photos/test.html | 8 ++++ middleware/browse/photos/test2.html | 8 ++++ 5 files changed, 91 insertions(+) create mode 100644 middleware/browse/header.html create mode 100644 middleware/browse/photos.tpl create mode 100644 middleware/browse/photos/test.html create mode 100644 middleware/browse/photos/test2.html diff --git a/middleware/browse/browse_test.go b/middleware/browse/browse_test.go index 33218a195..4c5183b6d 100644 --- a/middleware/browse/browse_test.go +++ b/middleware/browse/browse_test.go @@ -1,9 +1,14 @@ package browse import ( + "net/http" + "net/http/httptest" "sort" "testing" + "text/template" "time" + + "github.com/mholt/caddy/middleware" ) // "sort" package has "IsSorted" function, but no "IsReversed"; @@ -94,3 +99,59 @@ func TestSort(t *testing.T) { t.Errorf("The listing isn't reversed by time: %v", listing.Items) } } + +func TestBrowseTemplate(t *testing.T) { + tmpl, err := template.ParseFiles("photos.tpl") + if err != nil { + t.Fatalf("An error occured while parsing the template: %v", err) + } + + b := Browse{ + Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { + t.Fatalf("Next shouldn't be called") + return 0, nil + }), + Root: ".", + Configs: []Config{ + Config{ + PathScope: "/photos", + Template: tmpl, + }, + }, + } + + req, err := http.NewRequest("GET", "/photos/", nil) + if err != nil { + t.Fatalf("Test: Could not create HTTP request: %v", err) + } + + rec := httptest.NewRecorder() + + b.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("Wrong status, expected %d, got %d", http.StatusOK, rec.Code) + } + + respBody := rec.Body.String() + expectedBody := ` + + +Template + + +

Header

+ +

/photos/

+ +test.html
+ +test2.html
+ + + +` + + if respBody != expectedBody { + t.Fatalf("Expected body: %v got: %v", expectedBody, respBody) + } +} diff --git a/middleware/browse/header.html b/middleware/browse/header.html new file mode 100644 index 000000000..78e5a6a48 --- /dev/null +++ b/middleware/browse/header.html @@ -0,0 +1 @@ +

Header

diff --git a/middleware/browse/photos.tpl b/middleware/browse/photos.tpl new file mode 100644 index 000000000..5163ca008 --- /dev/null +++ b/middleware/browse/photos.tpl @@ -0,0 +1,13 @@ + + + +Template + + +{{.Include "header.html"}} +

{{.Path}}

+{{range .Items}} +{{.Name}}
+{{end}} + + diff --git a/middleware/browse/photos/test.html b/middleware/browse/photos/test.html new file mode 100644 index 000000000..40535a223 --- /dev/null +++ b/middleware/browse/photos/test.html @@ -0,0 +1,8 @@ + + + +Test + + + + diff --git a/middleware/browse/photos/test2.html b/middleware/browse/photos/test2.html new file mode 100644 index 000000000..8e10c5780 --- /dev/null +++ b/middleware/browse/photos/test2.html @@ -0,0 +1,8 @@ + + + +Test 2 + + + + From 6451e10d3ef9d1de4f79bf5ded9cb6e2052ab5ad Mon Sep 17 00:00:00 2001 From: Maxime Date: Tue, 21 Jul 2015 07:58:34 +0200 Subject: [PATCH 3/9] 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 fd92f20ad..bbccf9d5f 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 000000000..7ec766160 --- /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 000000000..78e5a6a48 --- /dev/null +++ b/middleware/markdown/header.html @@ -0,0 +1 @@ +

Header

diff --git a/middleware/markdown/markdown.go b/middleware/markdown/markdown.go index 6b7f35563..71b189068 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 000000000..3f8b50ec6 --- /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 000000000..2d0c74354 --- /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 95f7e51b3..ce9b5a7ca 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 } From 2df30d186ebf1151be1f9e0abe4344f850371027 Mon Sep 17 00:00:00 2001 From: Maxime Date: Tue, 21 Jul 2015 17:45:32 +0200 Subject: [PATCH 4/9] Added a test on markdown for the default template --- middleware/markdown/log/test.md | 15 ++++++++ middleware/markdown/markdown_test.go | 54 +++++++++++++++++++++++++++- middleware/markdown/process.go | 2 +- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 middleware/markdown/log/test.md diff --git a/middleware/markdown/log/test.md b/middleware/markdown/log/test.md new file mode 100644 index 000000000..7ec766160 --- /dev/null +++ b/middleware/markdown/log/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/markdown_test.go b/middleware/markdown/markdown_test.go index 3f8b50ec6..2e3ac85e6 100644 --- a/middleware/markdown/markdown_test.go +++ b/middleware/markdown/markdown_test.go @@ -3,6 +3,7 @@ package markdown import ( "net/http" "net/http/httptest" + "strings" "testing" "github.com/russross/blackfriday" @@ -18,11 +19,19 @@ func TestMarkdown(t *testing.T) { Config{ Renderer: blackfriday.HtmlRenderer(0, "", ""), PathScope: "/blog", - Extensions: []string{"md"}, + Extensions: []string{".md"}, Styles: []string{}, Scripts: []string{}, Templates: templates, }, + Config{ + Renderer: blackfriday.HtmlRenderer(0, "", ""), + PathScope: "/log", + Extensions: []string{".md"}, + Styles: []string{"/resources/css/log.css", "/resources/css/default.css"}, + Scripts: []string{"/resources/js/log.js", "/resources/js/default.js"}, + Templates: make(map[string]string), + }, }, IndexFiles: []string{"index.html"}, } @@ -62,7 +71,50 @@ func getTrue() bool { ` + if respBody != expectedBody { + t.Fatalf("Expected body: %v got: %v", expectedBody, respBody) + } + req, err = http.NewRequest("GET", "/log/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 + + + + + + + + + +

Welcome on the blog

+ +

Body

+ +

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

+ + +` + + replacer := strings.NewReplacer("\r", "", "\n", "") + respBody = replacer.Replace(respBody) + expectedBody = replacer.Replace(expectedBody) if respBody != expectedBody { t.Fatalf("Expected body: %v got: %v", expectedBody, respBody) } diff --git a/middleware/markdown/process.go b/middleware/markdown/process.go index ce9b5a7ca..02905b5f7 100644 --- a/middleware/markdown/process.go +++ b/middleware/markdown/process.go @@ -191,7 +191,7 @@ const ( {{js}} - {{.markdown}} + {{.Markdown}} ` cssTemplate = `` From bc2feece4b29836e6d91fe7533aaf7deef3d2b5b Mon Sep 17 00:00:00 2001 From: Maxime Date: Thu, 23 Jul 2015 09:35:46 +0200 Subject: [PATCH 5/9] Moved test files to testdata folder. Changed the tests accordingly. --- middleware/browse/browse_test.go | 4 ++-- middleware/browse/{ => testdata}/header.html | 0 middleware/browse/{ => testdata}/photos.tpl | 0 middleware/browse/{ => testdata}/photos/test.html | 0 middleware/browse/{ => testdata}/photos/test2.html | 0 middleware/markdown/markdown.go | 2 ++ middleware/markdown/markdown_test.go | 11 ++++++++--- middleware/markdown/process.go | 2 ++ middleware/markdown/{ => testdata}/blog/test.md | 0 middleware/markdown/{ => testdata}/header.html | 0 middleware/markdown/{ => testdata}/log/test.md | 0 middleware/markdown/{ => testdata}/markdown_tpl.html | 0 middleware/templates/templates_test.go | 8 ++++---- middleware/templates/{ => testdata}/header.html | 0 .../templates/{ => testdata}/images/header.html | 0 middleware/templates/{ => testdata}/images/img.htm | 0 middleware/templates/{ => testdata}/photos/test.html | 0 middleware/templates/{ => testdata}/root.html | 0 18 files changed, 18 insertions(+), 9 deletions(-) rename middleware/browse/{ => testdata}/header.html (100%) rename middleware/browse/{ => testdata}/photos.tpl (100%) rename middleware/browse/{ => testdata}/photos/test.html (100%) rename middleware/browse/{ => testdata}/photos/test2.html (100%) rename middleware/markdown/{ => testdata}/blog/test.md (100%) rename middleware/markdown/{ => testdata}/header.html (100%) rename middleware/markdown/{ => testdata}/log/test.md (100%) rename middleware/markdown/{ => testdata}/markdown_tpl.html (100%) rename middleware/templates/{ => testdata}/header.html (100%) rename middleware/templates/{ => testdata}/images/header.html (100%) rename middleware/templates/{ => testdata}/images/img.htm (100%) rename middleware/templates/{ => testdata}/photos/test.html (100%) rename middleware/templates/{ => testdata}/root.html (100%) diff --git a/middleware/browse/browse_test.go b/middleware/browse/browse_test.go index 4c5183b6d..b0ff28db6 100644 --- a/middleware/browse/browse_test.go +++ b/middleware/browse/browse_test.go @@ -101,7 +101,7 @@ func TestSort(t *testing.T) { } func TestBrowseTemplate(t *testing.T) { - tmpl, err := template.ParseFiles("photos.tpl") + tmpl, err := template.ParseFiles("testdata/photos.tpl") if err != nil { t.Fatalf("An error occured while parsing the template: %v", err) } @@ -111,7 +111,7 @@ func TestBrowseTemplate(t *testing.T) { t.Fatalf("Next shouldn't be called") return 0, nil }), - Root: ".", + Root: "./testdata", Configs: []Config{ Config{ PathScope: "/photos", diff --git a/middleware/browse/header.html b/middleware/browse/testdata/header.html similarity index 100% rename from middleware/browse/header.html rename to middleware/browse/testdata/header.html diff --git a/middleware/browse/photos.tpl b/middleware/browse/testdata/photos.tpl similarity index 100% rename from middleware/browse/photos.tpl rename to middleware/browse/testdata/photos.tpl diff --git a/middleware/browse/photos/test.html b/middleware/browse/testdata/photos/test.html similarity index 100% rename from middleware/browse/photos/test.html rename to middleware/browse/testdata/photos/test.html diff --git a/middleware/browse/photos/test2.html b/middleware/browse/testdata/photos/test2.html similarity index 100% rename from middleware/browse/photos/test2.html rename to middleware/browse/testdata/photos/test2.html diff --git a/middleware/markdown/markdown.go b/middleware/markdown/markdown.go index 71b189068..8aeb1f735 100644 --- a/middleware/markdown/markdown.go +++ b/middleware/markdown/markdown.go @@ -3,6 +3,7 @@ package markdown import ( + "fmt" "io/ioutil" "net/http" "os" @@ -125,6 +126,7 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error URL: r.URL, } html, err := md.Process(m, fpath, body, ctx) + fmt.Printf("%v", html) if err != nil { return http.StatusInternalServerError, err } diff --git a/middleware/markdown/markdown_test.go b/middleware/markdown/markdown_test.go index 2e3ac85e6..e01d88e8e 100644 --- a/middleware/markdown/markdown_test.go +++ b/middleware/markdown/markdown_test.go @@ -6,15 +6,16 @@ import ( "strings" "testing" + "github.com/mholt/caddy/middleware" "github.com/russross/blackfriday" ) func TestMarkdown(t *testing.T) { templates := make(map[string]string) - templates[DefaultTemplate] = "markdown_tpl.html" + templates[DefaultTemplate] = "testdata/markdown_tpl.html" md := Markdown{ - Root: "/blog", - FileSys: http.Dir("."), + Root: "./testdata", + FileSys: http.Dir("./testdata"), Configs: []Config{ Config{ Renderer: blackfriday.HtmlRenderer(0, "", ""), @@ -34,6 +35,10 @@ func TestMarkdown(t *testing.T) { }, }, IndexFiles: []string{"index.html"}, + Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { + t.Fatalf("Next shouldn't be called") + return 0, nil + }), } req, err := http.NewRequest("GET", "/blog/test.md", nil) diff --git a/middleware/markdown/process.go b/middleware/markdown/process.go index 02905b5f7..1761ffc89 100644 --- a/middleware/markdown/process.go +++ b/middleware/markdown/process.go @@ -2,6 +2,7 @@ package markdown import ( "bytes" + "fmt" "io/ioutil" "log" "os" @@ -106,6 +107,7 @@ func (md Markdown) processTemplate(c Config, requestPath string, tmpl []byte, me // nothing fatal, only log the error. // TODO: Report this non-fatal error, but don't log it here log.Println(err) + fmt.Printf("Error: %v", err) } return b.Bytes(), nil diff --git a/middleware/markdown/blog/test.md b/middleware/markdown/testdata/blog/test.md similarity index 100% rename from middleware/markdown/blog/test.md rename to middleware/markdown/testdata/blog/test.md diff --git a/middleware/markdown/header.html b/middleware/markdown/testdata/header.html similarity index 100% rename from middleware/markdown/header.html rename to middleware/markdown/testdata/header.html diff --git a/middleware/markdown/log/test.md b/middleware/markdown/testdata/log/test.md similarity index 100% rename from middleware/markdown/log/test.md rename to middleware/markdown/testdata/log/test.md diff --git a/middleware/markdown/markdown_tpl.html b/middleware/markdown/testdata/markdown_tpl.html similarity index 100% rename from middleware/markdown/markdown_tpl.html rename to middleware/markdown/testdata/markdown_tpl.html diff --git a/middleware/templates/templates_test.go b/middleware/templates/templates_test.go index a519de99e..5c283390f 100644 --- a/middleware/templates/templates_test.go +++ b/middleware/templates/templates_test.go @@ -25,8 +25,8 @@ func Test(t *testing.T) { Path: "/images", }, }, - Root: ".", - FileSys: http.Dir("."), + Root: "./testdata", + FileSys: http.Dir("./testdata"), } tmplroot := Templates{ @@ -40,8 +40,8 @@ func Test(t *testing.T) { Path: "/", }, }, - Root: ".", - FileSys: http.Dir("."), + Root: "./testdata", + FileSys: http.Dir("./testdata"), } /* diff --git a/middleware/templates/header.html b/middleware/templates/testdata/header.html similarity index 100% rename from middleware/templates/header.html rename to middleware/templates/testdata/header.html diff --git a/middleware/templates/images/header.html b/middleware/templates/testdata/images/header.html similarity index 100% rename from middleware/templates/images/header.html rename to middleware/templates/testdata/images/header.html diff --git a/middleware/templates/images/img.htm b/middleware/templates/testdata/images/img.htm similarity index 100% rename from middleware/templates/images/img.htm rename to middleware/templates/testdata/images/img.htm diff --git a/middleware/templates/photos/test.html b/middleware/templates/testdata/photos/test.html similarity index 100% rename from middleware/templates/photos/test.html rename to middleware/templates/testdata/photos/test.html diff --git a/middleware/templates/root.html b/middleware/templates/testdata/root.html similarity index 100% rename from middleware/templates/root.html rename to middleware/templates/testdata/root.html From 97dcc79a7f751c6c3db4b449e9da51899431738f Mon Sep 17 00:00:00 2001 From: Maxime Date: Thu, 23 Jul 2015 11:53:15 +0200 Subject: [PATCH 6/9] Remove undesired committed debug logs --- middleware/markdown/process.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/middleware/markdown/process.go b/middleware/markdown/process.go index 1761ffc89..02905b5f7 100644 --- a/middleware/markdown/process.go +++ b/middleware/markdown/process.go @@ -2,7 +2,6 @@ package markdown import ( "bytes" - "fmt" "io/ioutil" "log" "os" @@ -107,7 +106,6 @@ func (md Markdown) processTemplate(c Config, requestPath string, tmpl []byte, me // nothing fatal, only log the error. // TODO: Report this non-fatal error, but don't log it here log.Println(err) - fmt.Printf("Error: %v", err) } return b.Bytes(), nil From 24bdb433c95447ba01233b04732fd86937e3e3df Mon Sep 17 00:00:00 2001 From: Maxime Date: Fri, 24 Jul 2015 22:14:05 +0200 Subject: [PATCH 7/9] Changed .Var to .Doc in Markdown templates Put the title into the .Doc variables as well. Changed the test template file to use new names. --- middleware/markdown/process.go | 18 ++++++++---------- middleware/markdown/testdata/markdown_tpl.html | 6 +++--- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/middleware/markdown/process.go b/middleware/markdown/process.go index 02905b5f7..e6ac5dc48 100644 --- a/middleware/markdown/process.go +++ b/middleware/markdown/process.go @@ -20,9 +20,8 @@ const ( type MarkdownData struct { middleware.Context - Var map[string]interface{} - Title string - Markdown string + Doc map[string]interface{} + markdown string } // Process processes the contents of a page in b. It parses the metadata @@ -69,7 +68,8 @@ func (md Markdown) Process(c Config, requestPath string, b []byte, ctx middlewar markdown = blackfriday.Markdown(markdown, c.Renderer, 0) // set it as body for template - metadata.Variables["markdown"] = string(markdown) + metadata.Variables["body"] = string(markdown) + metadata.Variables["title"] = metadata.Title return md.processTemplate(c, requestPath, tmpl, metadata, ctx) } @@ -90,10 +90,8 @@ func (md Markdown) processTemplate(c Config, requestPath string, tmpl []byte, me return nil, err } mdData := MarkdownData{ - Context: ctx, - Var: metadata.Variables, - Title: metadata.Title, - Markdown: metadata.Variables["markdown"].(string), + Context: ctx, + Doc: metadata.Variables, } if err = t.Execute(b, mdData); err != nil { @@ -166,7 +164,7 @@ func defaultTemplate(c Config, metadata Metadata, requestPath string) []byte { title := metadata.Title if title == "" { title = filepath.Base(requestPath) - if body, _ := metadata.Variables["markdown"].([]byte); len(body) > 128 { + if body, _ := metadata.Variables["body"].([]byte); len(body) > 128 { title = string(body[:128]) } else if len(body) > 0 { title = string(body) @@ -191,7 +189,7 @@ const ( {{js}} - {{.Markdown}} + {{.Doc.body}} ` cssTemplate = `` diff --git a/middleware/markdown/testdata/markdown_tpl.html b/middleware/markdown/testdata/markdown_tpl.html index 2d0c74354..7c6978500 100644 --- a/middleware/markdown/testdata/markdown_tpl.html +++ b/middleware/markdown/testdata/markdown_tpl.html @@ -1,11 +1,11 @@ -{{.Title}} +{{.Doc.title}} {{.Include "header.html"}} -Welcome to {{.Var.sitename}}! -{{.Markdown}} +Welcome to {{.Doc.sitename}}! +{{.Doc.body}} From ef4a4b0ab81a24bdd7b78aa88cad96315d5a50c3 Mon Sep 17 00:00:00 2001 From: Maxime Date: Fri, 24 Jul 2015 22:17:14 +0200 Subject: [PATCH 8/9] Removed attribute not needed. --- middleware/markdown/process.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/middleware/markdown/process.go b/middleware/markdown/process.go index e6ac5dc48..f06baff7c 100644 --- a/middleware/markdown/process.go +++ b/middleware/markdown/process.go @@ -20,8 +20,7 @@ const ( type MarkdownData struct { middleware.Context - Doc map[string]interface{} - markdown string + Doc map[string]interface{} } // Process processes the contents of a page in b. It parses the metadata From 604c8abb59d36eabcb8b93dfe3a23f733b826551 Mon Sep 17 00:00:00 2001 From: Maxime Date: Sat, 25 Jul 2015 22:39:13 +0200 Subject: [PATCH 9/9] Remove debug line, add file name as default title --- middleware/markdown/markdown.go | 2 -- middleware/markdown/process.go | 18 ++++++++---------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/middleware/markdown/markdown.go b/middleware/markdown/markdown.go index 8aeb1f735..71b189068 100644 --- a/middleware/markdown/markdown.go +++ b/middleware/markdown/markdown.go @@ -3,7 +3,6 @@ package markdown import ( - "fmt" "io/ioutil" "net/http" "os" @@ -126,7 +125,6 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error URL: r.URL, } html, err := md.Process(m, fpath, body, ctx) - fmt.Printf("%v", html) if err != nil { return http.StatusInternalServerError, err } diff --git a/middleware/markdown/process.go b/middleware/markdown/process.go index f06baff7c..81e2f4ec8 100644 --- a/middleware/markdown/process.go +++ b/middleware/markdown/process.go @@ -68,7 +68,13 @@ func (md Markdown) Process(c Config, requestPath string, b []byte, ctx middlewar // set it as body for template metadata.Variables["body"] = string(markdown) - metadata.Variables["title"] = metadata.Title + title := metadata.Title + if title == "" { + title = filepath.Base(requestPath) + var extension = filepath.Ext(requestPath) + title = title[0 : len(title)-len(extension)] + } + metadata.Variables["title"] = title return md.processTemplate(c, requestPath, tmpl, metadata, ctx) } @@ -160,15 +166,7 @@ func defaultTemplate(c Config, metadata Metadata, requestPath string) []byte { } // Title is first line (length-limited), otherwise filename - title := metadata.Title - if title == "" { - title = filepath.Base(requestPath) - if body, _ := metadata.Variables["body"].([]byte); len(body) > 128 { - title = string(body[:128]) - } else if len(body) > 0 { - title = string(body) - } - } + title, _ := metadata.Variables["title"].(string) html := []byte(htmlTemplate) html = bytes.Replace(html, []byte("{{title}}"), []byte(title), 1)