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
90c24d2f
Commit
90c24d2f
authored
Mar 07, 2016
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Included files in Markdown templates have access to document vars (fixes #660)
Refactor how middleware.Context includes files
parent
88e3a26c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
43 additions
and
29 deletions
+43
-29
middleware/context.go
middleware/context.go
+34
-26
middleware/markdown/markdown_test.go
middleware/markdown/markdown_test.go
+2
-2
middleware/markdown/process.go
middleware/markdown/process.go
+6
-0
middleware/markdown/testdata/header.html
middleware/markdown/testdata/header.html
+1
-1
No files found.
middleware/context.go
View file @
90c24d2f
...
...
@@ -20,35 +20,12 @@ import (
type
Context
struct
{
Root
http
.
FileSystem
Req
*
http
.
Request
// This is used to access information about the URL.
URL
*
url
.
URL
URL
*
url
.
URL
}
// Include returns the contents of filename relative to the site root
// 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
)
if
err
!=
nil
{
return
""
,
err
}
defer
file
.
Close
()
body
,
err
:=
ioutil
.
ReadAll
(
file
)
if
err
!=
nil
{
return
""
,
err
}
tpl
,
err
:=
template
.
New
(
filename
)
.
Parse
(
string
(
body
))
if
err
!=
nil
{
return
""
,
err
}
var
buf
bytes
.
Buffer
err
=
tpl
.
Execute
(
&
buf
,
c
)
if
err
!=
nil
{
return
""
,
err
}
return
buf
.
String
(),
nil
return
ContextInclude
(
filename
,
c
,
c
.
Root
)
}
// Now returns the current timestamp in the specified format.
...
...
@@ -212,3 +189,34 @@ func (c Context) Markdown(filename string) (string, error) {
return
string
(
markdown
),
nil
}
// ContextInclude opens filename using fs and executes a template with the context ctx.
// This does the same thing that Context.Include() does, but with the ability to provide
// your own context so that the included files can have access to additional fields your
// type may provide. You can embed Context in your type, then override its Include method
// to call this function with ctx being the instance of your type, and fs being Context.Root.
func
ContextInclude
(
filename
string
,
ctx
interface
{},
fs
http
.
FileSystem
)
(
string
,
error
)
{
file
,
err
:=
fs
.
Open
(
filename
)
if
err
!=
nil
{
return
""
,
err
}
defer
file
.
Close
()
body
,
err
:=
ioutil
.
ReadAll
(
file
)
if
err
!=
nil
{
return
""
,
err
}
tpl
,
err
:=
template
.
New
(
filename
)
.
Parse
(
string
(
body
))
if
err
!=
nil
{
return
""
,
err
}
var
buf
bytes
.
Buffer
err
=
tpl
.
Execute
(
&
buf
,
ctx
)
if
err
!=
nil
{
return
""
,
err
}
return
buf
.
String
(),
nil
}
middleware/markdown/markdown_test.go
View file @
90c24d2f
...
...
@@ -107,7 +107,7 @@ func TestMarkdown(t *testing.T) {
<title>Markdown test 1</title>
</head>
<body>
<h1>Header</h1>
<h1>Header
for: Markdown test 1
</h1>
Welcome to A Caddy website!
<h2>Welcome on the blog</h2>
...
...
@@ -208,7 +208,7 @@ DocFlags.var_bool true`
<title>first_post</title>
</head>
<body>
<h1>Header</h1>
<h1>Header
for: first_post
</h1>
Welcome to title!
<h1>Test h1</h1>
...
...
middleware/markdown/process.go
View file @
90c24d2f
...
...
@@ -28,6 +28,12 @@ type Data struct {
Links
[]
PageLink
}
// Include "overrides" the embedded middleware.Context's Include()
// method so that included files have access to d's fields.
func
(
d
Data
)
Include
(
filename
string
)
(
string
,
error
)
{
return
middleware
.
ContextInclude
(
filename
,
d
,
d
.
Root
)
}
// 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
,
ctx
middleware
.
Context
)
([]
byte
,
error
)
{
...
...
middleware/markdown/testdata/header.html
View file @
90c24d2f
<h1>
Header
</h1>
<h1>
Header
for: {{.Doc.title}}
</h1>
\ No newline at end of file
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