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
25847a61
Commit
25847a61
authored
May 06, 2015
by
Abiola Ibrahim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
markdown: added template codes. awaiting integration and tests
parent
70d6caf9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
250 additions
and
0 deletions
+250
-0
middleware/markdown/markdown.go
middleware/markdown/markdown.go
+3
-0
middleware/markdown/metadata.go
middleware/markdown/metadata.go
+132
-0
middleware/markdown/process.go
middleware/markdown/process.go
+115
-0
No files found.
middleware/markdown/markdown.go
View file @
25847a61
...
...
@@ -49,6 +49,9 @@ type Config struct {
// List of JavaScript files to load for each markdown file
Scripts
[]
string
// Map of registered templates
Templates
map
[
string
]
string
}
// ServeHTTP implements the http.Handler interface.
...
...
middleware/markdown/metadata.go
0 → 100644
View file @
25847a61
package
markdown
import
(
"encoding/json"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
)
var
(
parsers
=
[]
MetadataParser
{
&
JSONMetadataParser
{},
&
TOMLMetadataParser
{},
&
YAMLMetadataParser
{},
}
)
// Metadata stores a page's metadata
type
Metadata
struct
{
Template
string
Variables
map
[
string
]
interface
{}
}
// Load loads parsed metadata into m
func
(
m
*
Metadata
)
load
(
parsedMap
map
[
string
]
interface
{})
{
if
template
,
ok
:=
parsedMap
[
"template"
];
ok
{
m
.
Template
,
_
=
template
.
(
string
)
}
if
variables
,
ok
:=
parsedMap
[
"variables"
];
ok
{
m
.
Variables
,
_
=
variables
.
(
map
[
string
]
interface
{})
}
}
// MetadataParser parses the page metadata
// into Metadata
type
MetadataParser
interface
{
// Identifiers
Opening
()
[]
byte
Closing
()
[]
byte
Parse
([]
byte
)
error
Metadata
()
Metadata
}
// JSONMetadataParser is the MetdataParser for JSON
type
JSONMetadataParser
struct
{
metadata
Metadata
}
// Parse parses b into metadata
func
(
j
*
JSONMetadataParser
)
Parse
(
b
[]
byte
)
error
{
m
:=
make
(
map
[
string
]
interface
{})
if
err
:=
json
.
Unmarshal
(
b
,
&
m
);
err
!=
nil
{
return
err
}
j
.
metadata
.
load
(
m
)
return
nil
}
// Metadata returns the metadata parsed by this parser
func
(
j
*
JSONMetadataParser
)
Metadata
()
Metadata
{
return
j
.
metadata
}
// Opening returns the opening identifier JSON metadata
func
(
j
*
JSONMetadataParser
)
Opening
()
[]
byte
{
return
[]
byte
(
"{"
)
}
// Closing returns the closing identifier JSON metadata
func
(
j
*
JSONMetadataParser
)
Closing
()
[]
byte
{
return
[]
byte
(
"}"
)
}
// TOMLMetadataParser is the MetadataParser for TOML
type
TOMLMetadataParser
struct
{
metadata
Metadata
}
// Parse parses b into metadata
func
(
t
*
TOMLMetadataParser
)
Parse
(
b
[]
byte
)
error
{
m
:=
make
(
map
[
string
]
interface
{})
if
err
:=
toml
.
Unmarshal
(
b
,
&
m
);
err
!=
nil
{
return
err
}
t
.
metadata
.
load
(
m
)
return
nil
}
// Metadata returns the metadata parsed by this parser
func
(
t
*
TOMLMetadataParser
)
Metadata
()
Metadata
{
return
t
.
metadata
}
// Opening returns the opening identifier TOML metadata
func
(
t
*
TOMLMetadataParser
)
Opening
()
[]
byte
{
return
[]
byte
(
"+++"
)
}
// Closing returns the closing identifier TOML metadata
func
(
t
*
TOMLMetadataParser
)
Closing
()
[]
byte
{
return
[]
byte
(
"+++"
)
}
// YAMLMetadataParser is the MetdataParser for YAML
type
YAMLMetadataParser
struct
{
metadata
Metadata
}
// Parse parses b into metadata
func
(
y
*
YAMLMetadataParser
)
Parse
(
b
[]
byte
)
error
{
m
:=
make
(
map
[
string
]
interface
{})
if
err
:=
yaml
.
Unmarshal
(
b
,
&
m
);
err
!=
nil
{
return
err
}
y
.
metadata
.
load
(
m
)
return
nil
}
// Metadata returns the metadata parsed by this parser
func
(
y
*
YAMLMetadataParser
)
Metadata
()
Metadata
{
return
y
.
metadata
}
// Opening returns the opening identifier TOML metadata
func
(
y
*
YAMLMetadataParser
)
Opening
()
[]
byte
{
return
[]
byte
(
"---"
)
}
// Closing returns the closing identifier TOML metadata
func
(
y
*
YAMLMetadataParser
)
Closing
()
[]
byte
{
return
[]
byte
(
"---"
)
}
middleware/markdown/process.go
0 → 100644
View file @
25847a61
package
markdown
import
(
"bufio"
"bytes"
"fmt"
"io/ioutil"
"text/template"
)
// Process the contents of a page.
// It parses the metadata if any and uses the template if found
func
Process
(
c
Config
,
b
[]
byte
)
([]
byte
,
error
)
{
metadata
,
markdown
,
err
:=
extractMetadata
(
b
)
if
err
!=
nil
{
return
nil
,
err
}
// if metadata template is included
var
tmpl
[]
byte
if
metadata
.
Template
!=
""
{
if
t
,
ok
:=
c
.
Templates
[
metadata
.
Template
];
ok
{
tmpl
,
err
=
loadTemplate
(
t
)
}
if
err
!=
nil
{
return
nil
,
err
}
}
// if no template loaded
// use default template
if
tmpl
==
nil
{
tmpl
=
[]
byte
(
htmlTemplate
)
}
// process markdown
if
markdown
,
err
=
processMarkdown
(
markdown
,
metadata
.
Variables
);
err
!=
nil
{
return
nil
,
err
}
tmpl
=
bytes
.
Replace
(
tmpl
,
[]
byte
(
"{{body}}"
),
markdown
,
1
)
return
tmpl
,
nil
}
// extractMetadata extracts metadata content from a page.
// it returns the metadata, the remaining bytes (markdown),
// and an error if any
func
extractMetadata
(
b
[]
byte
)
(
metadata
Metadata
,
markdown
[]
byte
,
err
error
)
{
b
=
bytes
.
TrimSpace
(
b
)
reader
:=
bytes
.
NewBuffer
(
b
)
scanner
:=
bufio
.
NewScanner
(
reader
)
var
parser
MetadataParser
// if scanner.Scan() &&
// Read first line
if
scanner
.
Scan
()
{
line
:=
scanner
.
Bytes
()
parser
=
findParser
(
line
)
// if no parser found
// assume metadata not present
if
parser
==
nil
{
return
metadata
,
b
,
nil
}
}
// buffer for metadata contents
buf
:=
bytes
.
Buffer
{}
// Read remaining lines until closing identifier is found
for
scanner
.
Scan
()
{
line
:=
scanner
.
Bytes
()
// closing identifier found
if
bytes
.
Equal
(
bytes
.
TrimSpace
(
line
),
parser
.
Closing
())
{
if
err
:=
parser
.
Parse
(
buf
.
Bytes
());
err
!=
nil
{
return
metadata
,
nil
,
err
}
return
parser
.
Metadata
(),
reader
.
Bytes
(),
nil
}
buf
.
Write
(
line
)
}
return
metadata
,
nil
,
fmt
.
Errorf
(
"Metadata not closed. '%v' not found"
,
string
(
parser
.
Closing
()))
}
// findParser locates the parser for an opening identifier
func
findParser
(
line
[]
byte
)
MetadataParser
{
line
=
bytes
.
TrimSpace
(
line
)
for
_
,
parser
:=
range
parsers
{
if
bytes
.
Equal
(
parser
.
Opening
(),
line
)
{
return
parser
}
}
return
nil
}
func
loadTemplate
(
tmpl
string
)
([]
byte
,
error
)
{
b
,
err
:=
ioutil
.
ReadFile
(
tmpl
)
if
err
!=
nil
{
return
nil
,
err
}
if
!
bytes
.
Contains
(
b
,
[]
byte
(
"{{body}}"
))
{
return
nil
,
fmt
.
Errorf
(
"template missing {{body}}"
)
}
return
b
,
nil
}
func
processMarkdown
(
b
[]
byte
,
vars
map
[
string
]
interface
{})
([]
byte
,
error
)
{
buf
:=
&
bytes
.
Buffer
{}
t
,
err
:=
template
.
New
(
"markdown"
)
.
Parse
(
string
(
b
))
if
err
!=
nil
{
return
nil
,
err
}
if
err
:=
t
.
Execute
(
buf
,
vars
);
err
!=
nil
{
return
nil
,
err
}
return
buf
.
Bytes
(),
nil
}
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