Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
3bc92540
Commit
3bc92540
authored
Jun 27, 2017
by
Matt Holt
Committed by
GitHub
Jun 27, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1682 from tw4452852/markdown
markdown: reload template on each request and fix fake tests
parents
43b56d62
655e61ab
Changes
13
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
212 additions
and
98 deletions
+212
-98
caddyhttp/markdown/markdown.go
caddyhttp/markdown/markdown.go
+3
-0
caddyhttp/markdown/markdown_test.go
caddyhttp/markdown/markdown_test.go
+100
-82
caddyhttp/markdown/setup.go
caddyhttp/markdown/setup.go
+23
-8
caddyhttp/markdown/setup_test.go
caddyhttp/markdown/setup_test.go
+13
-7
caddyhttp/markdown/template.go
caddyhttp/markdown/template.go
+11
-1
caddyhttp/markdown/testdata/blog/test.md
caddyhttp/markdown/testdata/blog/test.md
+14
-0
caddyhttp/markdown/testdata/docflags/template.txt
caddyhttp/markdown/testdata/docflags/template.txt
+2
-0
caddyhttp/markdown/testdata/docflags/test.md
caddyhttp/markdown/testdata/docflags/test.md
+4
-0
caddyhttp/markdown/testdata/header.html
caddyhttp/markdown/testdata/header.html
+1
-0
caddyhttp/markdown/testdata/log/test.md
caddyhttp/markdown/testdata/log/test.md
+14
-0
caddyhttp/markdown/testdata/markdown_tpl.html
caddyhttp/markdown/testdata/markdown_tpl.html
+11
-0
caddyhttp/markdown/testdata/og/first.md
caddyhttp/markdown/testdata/og/first.md
+5
-0
caddyhttp/markdown/testdata/tpl_with_include.html
caddyhttp/markdown/testdata/tpl_with_include.html
+11
-0
No files found.
caddyhttp/markdown/markdown.go
View file @
3bc92540
...
...
@@ -53,6 +53,9 @@ type Config struct {
// Template(s) to render with
Template
*
template
.
Template
// a pair of template's name and its underlying file path
TemplateFiles
map
[
string
]
string
}
// ServeHTTP implements the http.Handler interface.
...
...
caddyhttp/markdown/markdown_test.go
View file @
3bc92540
package
markdown
import
(
"bufio"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"text/template"
"time"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/russross/blackfriday"
)
...
...
@@ -79,19 +77,26 @@ func TestMarkdown(t *testing.T) {
}),
}
req
,
err
:=
http
.
NewRequest
(
"GET"
,
"/blog/test.md"
,
nil
)
get
:=
func
(
url
string
)
string
{
req
,
err
:=
http
.
NewRequest
(
"GET"
,
url
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"Could not create HTTP request: %v"
,
err
)
return
""
}
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
)
code
,
err
:=
md
.
ServeHTTP
(
rec
,
req
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
return
""
}
if
code
!=
http
.
StatusOK
{
t
.
Fatalf
(
"Wrong status, expected: %d and got %d"
,
http
.
StatusOK
,
code
)
return
""
}
return
rec
.
Body
.
String
()
}
respBody
:=
rec
.
Body
.
String
(
)
respBody
:=
get
(
"/blog/test.md"
)
expectedBody
:=
`<!DOCTYPE html>
<html>
<head>
...
...
@@ -99,7 +104,6 @@ func TestMarkdown(t *testing.T) {
</head>
<body>
<h1>Header for: Markdown test 1</h1>
Welcome to A Caddy website!
<h2>Welcome on the blog</h2>
...
...
@@ -113,46 +117,22 @@ Welcome to A Caddy website!
</body>
</html>
`
if
!
equalStrings
(
respBody
,
expectedBody
)
{
t
.
Fatalf
(
"Expected body: %v got: %v"
,
expectedBody
,
respBody
)
}
req
,
err
=
http
.
NewRequest
(
"GET"
,
"/docflags/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
()
respBody
=
get
(
"/docflags/test.md"
)
expectedBody
=
`Doc.var_string hello
Doc.var_bool <no value>
DocFlags.var_string <no value>
DocFlags.var_bool true`
if
!
equalStrings
(
respBody
,
expectedBody
)
{
t
.
Fatalf
(
"Expected body: %v got: %v"
,
expectedBody
,
respBody
)
}
Doc.var_bool true
`
req
,
err
=
http
.
NewRequest
(
"GET"
,
"/log/test.md"
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"Could not create HTTP request: %v"
,
err
)
if
respBody
!=
expectedBody
{
t
.
Fatalf
(
"Expected body:
\n
%q
\n
got:
\n
%q"
,
expectedBody
,
respBody
)
}
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
()
respBody
=
get
(
"/log/test.md"
)
expectedBody
=
`<!DOCTYPE html>
<html>
<head>
<title>Markdown test 2</title>
<meta charset="utf-8">
<link rel="stylesheet" href="/resources/css/log.css">
<link rel="stylesheet" href="/resources/css/default.css">
<script src="/resources/js/log.js"></script>
...
...
@@ -171,26 +151,11 @@ DocFlags.var_bool true`
</body>
</html>`
if
!
equalStrings
(
respBody
,
expectedBody
)
{
t
.
Fatalf
(
"Expected body: %v got: %v"
,
expectedBody
,
respBody
)
}
req
,
err
=
http
.
NewRequest
(
"GET"
,
"/og/first.md"
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"Could not create HTTP request: %v"
,
err
)
if
respBody
!=
expectedBody
{
t
.
Fatalf
(
"Expected body:
\n
%q
\n
got:
\n
%q"
,
expectedBody
,
respBody
)
}
rec
=
httptest
.
NewRecorder
()
currenttime
:=
time
.
Now
()
.
Local
()
.
Add
(
-
time
.
Second
)
_
=
os
.
Chtimes
(
"testdata/og/first.md"
,
currenttime
,
currenttime
)
currenttime
=
time
.
Now
()
.
Local
()
_
=
os
.
Chtimes
(
"testdata/og_static/og/first.md/index.html"
,
currenttime
,
currenttime
)
time
.
Sleep
(
time
.
Millisecond
*
200
)
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
()
respBody
=
get
(
"/og/first.md"
)
expectedBody
=
`<!DOCTYPE html>
<html>
<head>
...
...
@@ -198,30 +163,16 @@ DocFlags.var_bool true`
</head>
<body>
<h1>Header for: first_post</h1>
Welcome to title!
<h1>Test h1</h1>
</body>
</html>`
if
!
equalStrings
(
respBody
,
expectedBody
)
{
t
.
Fatalf
(
"Expected body: %v got: %v"
,
expectedBody
,
respBody
)
}
}
</html>
`
func
equalStrings
(
s1
,
s2
string
)
bool
{
s1
=
strings
.
TrimSpace
(
s1
)
s2
=
strings
.
TrimSpace
(
s2
)
in
:=
bufio
.
NewScanner
(
strings
.
NewReader
(
s1
))
for
in
.
Scan
()
{
txt
:=
strings
.
TrimSpace
(
in
.
Text
())
if
!
strings
.
HasPrefix
(
strings
.
TrimSpace
(
s2
),
txt
)
{
return
false
if
respBody
!=
expectedBody
{
t
.
Fatalf
(
"Expected body:
\n
%q
\n
got:
\n
%q"
,
expectedBody
,
respBody
)
}
s2
=
strings
.
Replace
(
s2
,
txt
,
""
,
1
)
}
return
true
}
func
setDefaultTemplate
(
filename
string
)
*
template
.
Template
{
...
...
@@ -232,3 +183,70 @@ func setDefaultTemplate(filename string) *template.Template {
return
template
.
Must
(
GetDefaultTemplate
()
.
Parse
(
string
(
buf
)))
}
func
TestTemplateReload
(
t
*
testing
.
T
)
{
const
(
templateFile
=
"testdata/test.html"
targetFile
=
"testdata/hello.md"
)
c
:=
caddy
.
NewTestController
(
"http"
,
`markdown {
template `
+
templateFile
+
`
}`
)
err
:=
ioutil
.
WriteFile
(
templateFile
,
[]
byte
(
"hello {{.Doc.body}}"
),
0644
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
err
=
ioutil
.
WriteFile
(
targetFile
,
[]
byte
(
"caddy"
),
0644
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
func
()
{
os
.
Remove
(
templateFile
)
os
.
Remove
(
targetFile
)
}()
config
,
err
:=
markdownParse
(
c
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
md
:=
Markdown
{
Root
:
"./testdata"
,
FileSys
:
http
.
Dir
(
"./testdata"
),
Configs
:
config
,
Next
:
httpserver
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
t
.
Fatalf
(
"Next shouldn't be called"
)
return
0
,
nil
}),
}
req
:=
httptest
.
NewRequest
(
"GET"
,
"/hello.md"
,
nil
)
get
:=
func
()
string
{
rec
:=
httptest
.
NewRecorder
()
code
,
err
:=
md
.
ServeHTTP
(
rec
,
req
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
return
""
}
if
code
!=
http
.
StatusOK
{
t
.
Fatalf
(
"Wrong status, expected: %d and got %d"
,
http
.
StatusOK
,
code
)
return
""
}
return
rec
.
Body
.
String
()
}
if
expect
,
got
:=
"hello <p>caddy</p>
\n
"
,
get
();
expect
!=
got
{
t
.
Fatalf
(
"Expected body:
\n
%q
\n
but got:
\n
%q"
,
expect
,
got
)
}
// update template
err
=
ioutil
.
WriteFile
(
templateFile
,
[]
byte
(
"hi {{.Doc.body}}"
),
0644
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
expect
,
got
:=
"hi <p>caddy</p>
\n
"
,
get
();
expect
!=
got
{
t
.
Fatalf
(
"Expected body:
\n
%q
\n
but got:
\n
%q"
,
expect
,
got
)
}
}
caddyhttp/markdown/setup.go
View file @
3bc92540
...
...
@@ -48,6 +48,7 @@ func markdownParse(c *caddy.Controller) ([]*Config, error) {
Extensions
:
make
(
map
[
string
]
struct
{}),
Template
:
GetDefaultTemplate
(),
IndexFiles
:
[]
string
{},
TemplateFiles
:
make
(
map
[
string
]
string
),
}
// Get the path scope
...
...
@@ -115,28 +116,42 @@ func loadParams(c *caddy.Controller, mdc *Config) error {
fpath
:=
filepath
.
ToSlash
(
filepath
.
Clean
(
cfg
.
Root
+
string
(
filepath
.
Separator
)
+
tArgs
[
0
]))
if
err
:=
SetTemplate
(
mdc
.
Template
,
""
,
fpath
);
err
!=
nil
{
c
.
Errf
(
"default template parse error: %v"
,
err
)
return
c
.
Errf
(
"default template parse error: %v"
,
err
)
}
mdc
.
TemplateFiles
[
""
]
=
fpath
return
nil
case
2
:
fpath
:=
filepath
.
ToSlash
(
filepath
.
Clean
(
cfg
.
Root
+
string
(
filepath
.
Separator
)
+
tArgs
[
1
]))
if
err
:=
SetTemplate
(
mdc
.
Template
,
tArgs
[
0
],
fpath
);
err
!=
nil
{
c
.
Errf
(
"template parse error: %v"
,
err
)
return
c
.
Errf
(
"template parse error: %v"
,
err
)
}
mdc
.
TemplateFiles
[
tArgs
[
0
]]
=
fpath
return
nil
}
case
"templatedir"
:
if
!
c
.
NextArg
()
{
return
c
.
ArgErr
()
}
_
,
err
:=
mdc
.
Template
.
ParseGlob
(
c
.
Val
())
pattern
:=
c
.
Val
()
_
,
err
:=
mdc
.
Template
.
ParseGlob
(
pattern
)
if
err
!=
nil
{
c
.
Errf
(
"template load error: %v"
,
err
)
return
c
.
Errf
(
"template load error: %v"
,
err
)
}
if
c
.
NextArg
()
{
return
c
.
ArgErr
()
}
paths
,
err
:=
filepath
.
Glob
(
pattern
)
if
err
!=
nil
{
return
c
.
Errf
(
"glob %q failed: %v"
,
pattern
,
err
)
}
for
_
,
path
:=
range
paths
{
mdc
.
TemplateFiles
[
filepath
.
Base
(
path
)]
=
path
}
return
nil
default
:
return
c
.
Err
(
"Expected valid markdown configuration property"
)
...
...
caddyhttp/markdown/setup_test.go
View file @
3bc92540
...
...
@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"net/http"
"reflect"
"testing"
"text/template"
...
...
@@ -62,6 +63,7 @@ func TestMarkdownParse(t *testing.T) {
Styles
:
[]
string
{
"/resources/css/blog.css"
},
Scripts
:
[]
string
{
"/resources/js/blog.js"
},
Template
:
GetDefaultTemplate
(),
TemplateFiles
:
make
(
map
[
string
]
string
),
}}},
{
`markdown /blog {
ext .md
...
...
@@ -71,12 +73,12 @@ func TestMarkdownParse(t *testing.T) {
Extensions
:
map
[
string
]
struct
{}{
".md"
:
{},
},
Template
:
GetDefaultTemplate
(),
Template
:
setDefaultTemplate
(
"./testdata/tpl_with_include.html"
),
TemplateFiles
:
map
[
string
]
string
{
""
:
"testdata/tpl_with_include.html"
,
},
}}},
}
// Setup the extra template
tmpl
:=
tests
[
1
]
.
expectedMarkdownConfig
[
0
]
.
Template
SetTemplate
(
tmpl
,
""
,
"./testdata/tpl_with_include.html"
)
for
i
,
test
:=
range
tests
{
c
:=
caddy
.
NewTestController
(
"http"
,
test
.
inputMarkdownConfig
)
...
...
@@ -110,6 +112,10 @@ func TestMarkdownParse(t *testing.T) {
if
ok
,
tx
,
ty
:=
equalTemplates
(
actualMarkdownConfig
.
Template
,
test
.
expectedMarkdownConfig
[
j
]
.
Template
);
!
ok
{
t
.
Errorf
(
"Test %d the %dth Markdown Config Templates did not match, expected %s to be %s"
,
i
,
j
,
tx
,
ty
)
}
if
expect
,
got
:=
test
.
expectedMarkdownConfig
[
j
]
.
TemplateFiles
,
actualMarkdownConfig
.
TemplateFiles
;
!
reflect
.
DeepEqual
(
expect
,
got
)
{
t
.
Errorf
(
"Test %d the %d Markdown config TemplateFiles did not match, expect %v, but got %v"
,
i
,
j
,
expect
,
got
)
}
}
}
}
...
...
caddyhttp/markdown/template.go
View file @
3bc92540
...
...
@@ -38,8 +38,18 @@ func execTemplate(c *Config, mdata metadata.Metadata, meta map[string]string, fi
Files
:
files
,
}
templateName
:=
mdata
.
Template
// reload template on every request for now
// TODO: cache templates by a general plugin
if
templateFile
,
ok
:=
c
.
TemplateFiles
[
templateName
];
ok
{
err
:=
SetTemplate
(
c
.
Template
,
templateName
,
templateFile
)
if
err
!=
nil
{
return
nil
,
err
}
}
b
:=
new
(
bytes
.
Buffer
)
if
err
:=
c
.
Template
.
ExecuteTemplate
(
b
,
mdata
.
Templat
e
,
mdData
);
err
!=
nil
{
if
err
:=
c
.
Template
.
ExecuteTemplate
(
b
,
templateNam
e
,
mdData
);
err
!=
nil
{
return
nil
,
err
}
...
...
caddyhttp/markdown/testdata/blog/test.md
0 → 100644
View file @
3bc92540
---
title
:
Markdown test
1
sitename
:
A Caddy website
---
## Welcome on the blog
Body
```
go
func
getTrue
()
bool
{
return
true
}
```
caddyhttp/markdown/testdata/docflags/template.txt
0 → 100644
View file @
3bc92540
Doc.var_string {{.Doc.var_string}}
Doc.var_bool {{.Doc.var_bool}}
caddyhttp/markdown/testdata/docflags/test.md
0 → 100644
View file @
3bc92540
---
var_string
:
hello
var_bool
:
true
---
caddyhttp/markdown/testdata/header.html
0 → 100644
View file @
3bc92540
<h1>
Header for: {{.Doc.title}}
</h1>
\ No newline at end of file
caddyhttp/markdown/testdata/log/test.md
0 → 100644
View file @
3bc92540
---
title
:
Markdown test
2
sitename
:
A Caddy website
---
## Welcome on the blog
Body
```
go
func
getTrue
()
bool
{
return
true
}
```
caddyhttp/markdown/testdata/markdown_tpl.html
0 → 100644
View file @
3bc92540
<!DOCTYPE html>
<html>
<head>
<title>
{{.Doc.title}}
</title>
</head>
<body>
{{.Include "header.html"}}
Welcome to {{.Doc.sitename}}!
{{.Doc.body}}
</body>
</html>
caddyhttp/markdown/testdata/og/first.md
0 → 100644
View file @
3bc92540
---
title
:
first_post
sitename
:
title
---
# Test h1
caddyhttp/markdown/testdata/tpl_with_include.html
0 → 100644
View file @
3bc92540
<!DOCTYPE html>
<html>
<head>
<title>
{{.Doc.title}}
</title>
</head>
<body>
Welcome to {{.Doc.sitename}}!
<br><br>
{{.Doc.body}}
</body>
</html>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment