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
33908629
Commit
33908629
authored
Jun 25, 2017
by
Tw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
markdown: reload template on each request
Signed-off-by:
Tw
<
tw19881113@gmail.com
>
parent
47fc35ac
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
111 additions
and
10 deletions
+111
-10
caddyhttp/markdown/markdown.go
caddyhttp/markdown/markdown.go
+3
-0
caddyhttp/markdown/markdown_test.go
caddyhttp/markdown/markdown_test.go
+68
-1
caddyhttp/markdown/setup.go
caddyhttp/markdown/setup.go
+17
-5
caddyhttp/markdown/setup_test.go
caddyhttp/markdown/setup_test.go
+12
-3
caddyhttp/markdown/template.go
caddyhttp/markdown/template.go
+11
-1
No files found.
caddyhttp/markdown/markdown.go
View file @
33908629
...
@@ -53,6 +53,9 @@ type Config struct {
...
@@ -53,6 +53,9 @@ type Config struct {
// Template(s) to render with
// Template(s) to render with
Template
*
template
.
Template
Template
*
template
.
Template
// a pair of template's name and its underlying file path
TemplateFiles
map
[
string
]
string
}
}
// ServeHTTP implements the http.Handler interface.
// ServeHTTP implements the http.Handler interface.
...
...
caddyhttp/markdown/markdown_test.go
View file @
33908629
...
@@ -4,10 +4,12 @@ import (
...
@@ -4,10 +4,12 @@ import (
"io/ioutil"
"io/ioutil"
"net/http"
"net/http"
"net/http/httptest"
"net/http/httptest"
"os"
"path/filepath"
"path/filepath"
"testing"
"testing"
"text/template"
"text/template"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/russross/blackfriday"
"github.com/russross/blackfriday"
)
)
...
@@ -178,3 +180,68 @@ func setDefaultTemplate(filename string) *template.Template {
...
@@ -178,3 +180,68 @@ func setDefaultTemplate(filename string) *template.Template {
return
template
.
Must
(
GetDefaultTemplate
()
.
Parse
(
string
(
buf
)))
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
)
}
if
code
!=
http
.
StatusOK
{
t
.
Fatalf
(
"Wrong status, expected: %d and got %d"
,
http
.
StatusOK
,
code
)
}
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 @
33908629
...
@@ -48,6 +48,7 @@ func markdownParse(c *caddy.Controller) ([]*Config, error) {
...
@@ -48,6 +48,7 @@ func markdownParse(c *caddy.Controller) ([]*Config, error) {
Extensions
:
make
(
map
[
string
]
struct
{}),
Extensions
:
make
(
map
[
string
]
struct
{}),
Template
:
GetDefaultTemplate
(),
Template
:
GetDefaultTemplate
(),
IndexFiles
:
[]
string
{},
IndexFiles
:
[]
string
{},
TemplateFiles
:
make
(
map
[
string
]
string
),
}
}
// Get the path scope
// Get the path scope
...
@@ -118,6 +119,7 @@ func loadParams(c *caddy.Controller, mdc *Config) error {
...
@@ -118,6 +119,7 @@ func loadParams(c *caddy.Controller, mdc *Config) error {
return
c
.
Errf
(
"default template parse error: %v"
,
err
)
return
c
.
Errf
(
"default template parse error: %v"
,
err
)
}
}
mdc
.
TemplateFiles
[
""
]
=
fpath
return
nil
return
nil
case
2
:
case
2
:
fpath
:=
filepath
.
ToSlash
(
filepath
.
Clean
(
cfg
.
Root
+
string
(
filepath
.
Separator
)
+
tArgs
[
1
]))
fpath
:=
filepath
.
ToSlash
(
filepath
.
Clean
(
cfg
.
Root
+
string
(
filepath
.
Separator
)
+
tArgs
[
1
]))
...
@@ -126,13 +128,16 @@ func loadParams(c *caddy.Controller, mdc *Config) error {
...
@@ -126,13 +128,16 @@ func loadParams(c *caddy.Controller, mdc *Config) error {
return
c
.
Errf
(
"template parse error: %v"
,
err
)
return
c
.
Errf
(
"template parse error: %v"
,
err
)
}
}
mdc
.
TemplateFiles
[
tArgs
[
0
]]
=
fpath
return
nil
return
nil
}
}
case
"templatedir"
:
case
"templatedir"
:
if
!
c
.
NextArg
()
{
if
!
c
.
NextArg
()
{
return
c
.
ArgErr
()
return
c
.
ArgErr
()
}
}
_
,
err
:=
mdc
.
Template
.
ParseGlob
(
c
.
Val
())
pattern
:=
c
.
Val
()
_
,
err
:=
mdc
.
Template
.
ParseGlob
(
pattern
)
if
err
!=
nil
{
if
err
!=
nil
{
return
c
.
Errf
(
"template load error: %v"
,
err
)
return
c
.
Errf
(
"template load error: %v"
,
err
)
}
}
...
@@ -140,6 +145,13 @@ func loadParams(c *caddy.Controller, mdc *Config) error {
...
@@ -140,6 +145,13 @@ func loadParams(c *caddy.Controller, mdc *Config) error {
return
c
.
ArgErr
()
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
return
nil
default
:
default
:
return
c
.
Err
(
"Expected valid markdown configuration property"
)
return
c
.
Err
(
"Expected valid markdown configuration property"
)
...
...
caddyhttp/markdown/setup_test.go
View file @
33908629
...
@@ -4,6 +4,7 @@ import (
...
@@ -4,6 +4,7 @@ import (
"bytes"
"bytes"
"fmt"
"fmt"
"net/http"
"net/http"
"reflect"
"testing"
"testing"
"text/template"
"text/template"
...
@@ -62,6 +63,7 @@ func TestMarkdownParse(t *testing.T) {
...
@@ -62,6 +63,7 @@ func TestMarkdownParse(t *testing.T) {
Styles
:
[]
string
{
"/resources/css/blog.css"
},
Styles
:
[]
string
{
"/resources/css/blog.css"
},
Scripts
:
[]
string
{
"/resources/js/blog.js"
},
Scripts
:
[]
string
{
"/resources/js/blog.js"
},
Template
:
GetDefaultTemplate
(),
Template
:
GetDefaultTemplate
(),
TemplateFiles
:
make
(
map
[
string
]
string
),
}}},
}}},
{
`markdown /blog {
{
`markdown /blog {
ext .md
ext .md
...
@@ -72,6 +74,9 @@ func TestMarkdownParse(t *testing.T) {
...
@@ -72,6 +74,9 @@ func TestMarkdownParse(t *testing.T) {
".md"
:
{},
".md"
:
{},
},
},
Template
:
setDefaultTemplate
(
"./testdata/tpl_with_include.html"
),
Template
:
setDefaultTemplate
(
"./testdata/tpl_with_include.html"
),
TemplateFiles
:
map
[
string
]
string
{
""
:
"testdata/tpl_with_include.html"
,
},
}}},
}}},
}
}
...
@@ -107,6 +112,10 @@ func TestMarkdownParse(t *testing.T) {
...
@@ -107,6 +112,10 @@ func TestMarkdownParse(t *testing.T) {
if
ok
,
tx
,
ty
:=
equalTemplates
(
actualMarkdownConfig
.
Template
,
test
.
expectedMarkdownConfig
[
j
]
.
Template
);
!
ok
{
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
)
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 @
33908629
...
@@ -38,8 +38,18 @@ func execTemplate(c *Config, mdata metadata.Metadata, meta map[string]string, fi
...
@@ -38,8 +38,18 @@ func execTemplate(c *Config, mdata metadata.Metadata, meta map[string]string, fi
Files
:
files
,
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
)
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
return
nil
,
err
}
}
...
...
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