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
b97a7909
Commit
b97a7909
authored
Apr 16, 2016
by
Tobias Weingartner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Nuke more redundant things.
parent
7c986791
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
5 additions
and
142 deletions
+5
-142
middleware/markdown/metadata/metadata.go
middleware/markdown/metadata/metadata.go
+5
-54
middleware/markdown/metadata/metadata_json.go
middleware/markdown/metadata/metadata_json.go
+0
-28
middleware/markdown/metadata/metadata_none.go
middleware/markdown/metadata/metadata_none.go
+0
-10
middleware/markdown/metadata/metadata_toml.go
middleware/markdown/metadata/metadata_toml.go
+0
-25
middleware/markdown/metadata/metadata_yaml.go
middleware/markdown/metadata/metadata_yaml.go
+0
-25
No files found.
middleware/markdown/metadata/metadata.go
View file @
b97a7909
...
...
@@ -3,7 +3,6 @@ package metadata
import
(
"bufio"
"bytes"
"fmt"
"time"
)
...
...
@@ -71,12 +70,6 @@ type MetadataParser interface {
// Type of metadata
Type
()
string
// Opening identifier
Opening
()
[]
byte
// Closing identifier
Closing
()
[]
byte
// Parsed metadata.
Metadata
()
Metadata
...
...
@@ -84,35 +77,7 @@ type MetadataParser interface {
Markdown
()
[]
byte
}
// extractMetadata separates metadata content from from markdown content in b.
// It returns the metadata, the remaining bytes (markdown), and an error, if any.
func
extractMetadata
(
parser
MetadataParser
,
b
[]
byte
)
(
metadata
[]
byte
,
markdown
[]
byte
,
err
error
)
{
b
=
bytes
.
TrimSpace
(
b
)
openingLine
:=
parser
.
Opening
()
closingLine
:=
parser
.
Closing
()
if
!
bytes
.
HasPrefix
(
b
,
openingLine
)
{
return
nil
,
b
,
fmt
.
Errorf
(
"first line missing expected metadata identifier"
)
}
metaStart
:=
len
(
openingLine
)
if
_
,
ok
:=
parser
.
(
*
JSONMetadataParser
);
ok
{
metaStart
=
0
}
metaEnd
:=
bytes
.
Index
(
b
[
metaStart
:
],
closingLine
)
if
metaEnd
==
-
1
{
return
nil
,
nil
,
fmt
.
Errorf
(
"metadata not closed ('%s' not found)"
,
parser
.
Closing
())
}
metaEnd
+=
metaStart
if
_
,
ok
:=
parser
.
(
*
JSONMetadataParser
);
ok
{
metaEnd
+=
len
(
closingLine
)
}
metadata
=
b
[
metaStart
:
metaEnd
]
markdown
=
b
[
metaEnd
:
]
if
_
,
ok
:=
parser
.
(
*
JSONMetadataParser
);
!
ok
{
markdown
=
b
[
metaEnd
+
len
(
closingLine
)
:
]
}
return
metadata
,
markdown
,
nil
}
// GetParser returns a parser for the given data
func
GetParser
(
buf
[]
byte
)
MetadataParser
{
for
_
,
p
:=
range
parsers
()
{
b
:=
bytes
.
NewBuffer
(
buf
)
...
...
@@ -124,22 +89,6 @@ func GetParser(buf []byte) MetadataParser {
return
nil
}
// findParser finds the parser using line that contains opening identifier
func
FindParser
(
b
[]
byte
)
MetadataParser
{
var
line
[]
byte
// Read first line
if
_
,
err
:=
fmt
.
Fscanln
(
bytes
.
NewReader
(
b
),
&
line
);
err
!=
nil
{
return
nil
}
line
=
bytes
.
TrimSpace
(
line
)
for
_
,
parser
:=
range
parsers
()
{
if
bytes
.
Equal
(
parser
.
Opening
(),
line
)
{
return
parser
}
}
return
nil
}
func
NewMetadata
()
Metadata
{
return
Metadata
{
Variables
:
make
(
map
[
string
]
string
),
...
...
@@ -151,13 +100,15 @@ func NewMetadata() Metadata {
func
parsers
()
[]
MetadataParser
{
return
[]
MetadataParser
{
&
TOMLMetadataParser
{},
&
YAMLMetadataParser
{
metadata
:
NewMetadata
()
},
&
YAMLMetadataParser
{},
&
JSONMetadataParser
{},
// This one must be last
&
NoneMetadataParser
{},
}
}
// Split out
"normal"
metadata with given delimiter
// Split out
prefixed/suffixed
metadata with given delimiter
func
splitBuffer
(
b
*
bytes
.
Buffer
,
delim
string
)
(
*
bytes
.
Buffer
,
*
bytes
.
Buffer
)
{
scanner
:=
bufio
.
NewScanner
(
b
)
...
...
middleware/markdown/metadata/metadata_json.go
View file @
b97a7909
...
...
@@ -43,24 +43,6 @@ func (j *JSONMetadataParser) Init(b *bytes.Buffer) bool {
return
true
}
// Parse the metadata
func
(
j
*
JSONMetadataParser
)
Parse
(
b
[]
byte
)
([]
byte
,
error
)
{
b
,
markdown
,
err
:=
extractMetadata
(
j
,
b
)
if
err
!=
nil
{
return
markdown
,
err
}
m
:=
make
(
map
[
string
]
interface
{})
// Read the preceding JSON object
decoder
:=
json
.
NewDecoder
(
bytes
.
NewReader
(
b
))
if
err
:=
decoder
.
Decode
(
&
m
);
err
!=
nil
{
return
markdown
,
err
}
j
.
metadata
.
load
(
m
)
return
markdown
,
nil
}
// Metadata returns parsed metadata. It should be called
// only after a call to Parse returns without error.
func
(
j
*
JSONMetadataParser
)
Metadata
()
Metadata
{
...
...
@@ -70,13 +52,3 @@ func (j *JSONMetadataParser) Metadata() Metadata {
func
(
j
*
JSONMetadataParser
)
Markdown
()
[]
byte
{
return
j
.
markdown
.
Bytes
()
}
// 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
(
"}"
)
}
middleware/markdown/metadata/metadata_none.go
View file @
b97a7909
...
...
@@ -38,13 +38,3 @@ func (n *NoneMetadataParser) Metadata() Metadata {
func
(
n
*
NoneMetadataParser
)
Markdown
()
[]
byte
{
return
n
.
markdown
.
Bytes
()
}
// Opening returns the opening identifier TOML metadata
func
(
n
*
NoneMetadataParser
)
Opening
()
[]
byte
{
return
[]
byte
(
"..."
)
}
// Closing returns the closing identifier TOML metadata
func
(
n
*
NoneMetadataParser
)
Closing
()
[]
byte
{
return
[]
byte
(
"..."
)
}
middleware/markdown/metadata/metadata_toml.go
View file @
b97a7909
...
...
@@ -34,21 +34,6 @@ func (t *TOMLMetadataParser) Init(b *bytes.Buffer) bool {
return
true
}
// Parse the metadata
func
(
t
*
TOMLMetadataParser
)
Parse
(
b
[]
byte
)
([]
byte
,
error
)
{
b
,
markdown
,
err
:=
extractMetadata
(
t
,
b
)
if
err
!=
nil
{
return
markdown
,
err
}
m
:=
make
(
map
[
string
]
interface
{})
if
err
:=
toml
.
Unmarshal
(
b
,
&
m
);
err
!=
nil
{
return
markdown
,
err
}
t
.
metadata
.
load
(
m
)
return
markdown
,
nil
}
// Metadata returns parsed metadata. It should be called
// only after a call to Parse returns without error.
func
(
t
*
TOMLMetadataParser
)
Metadata
()
Metadata
{
...
...
@@ -58,13 +43,3 @@ func (t *TOMLMetadataParser) Metadata() Metadata {
func
(
t
*
TOMLMetadataParser
)
Markdown
()
[]
byte
{
return
t
.
markdown
.
Bytes
()
}
// 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
(
"+++"
)
}
middleware/markdown/metadata/metadata_yaml.go
View file @
b97a7909
...
...
@@ -33,21 +33,6 @@ func (y *YAMLMetadataParser) Init(b *bytes.Buffer) bool {
return
true
}
// Parse the metadata
func
(
y
*
YAMLMetadataParser
)
Parse
(
b
[]
byte
)
([]
byte
,
error
)
{
b
,
markdown
,
err
:=
extractMetadata
(
y
,
b
)
if
err
!=
nil
{
return
markdown
,
err
}
m
:=
make
(
map
[
string
]
interface
{})
if
err
:=
yaml
.
Unmarshal
(
b
,
&
m
);
err
!=
nil
{
return
markdown
,
err
}
y
.
metadata
.
load
(
m
)
return
markdown
,
nil
}
// Metadata returns parsed metadata. It should be called
// only after a call to Parse returns without error.
func
(
y
*
YAMLMetadataParser
)
Metadata
()
Metadata
{
...
...
@@ -57,13 +42,3 @@ func (y *YAMLMetadataParser) Metadata() Metadata {
func
(
y
*
YAMLMetadataParser
)
Markdown
()
[]
byte
{
return
y
.
markdown
.
Bytes
()
}
// Opening returns the opening identifier YAML metadata
func
(
y
*
YAMLMetadataParser
)
Opening
()
[]
byte
{
return
[]
byte
(
"---"
)
}
// Closing returns the closing identifier YAML metadata
func
(
y
*
YAMLMetadataParser
)
Closing
()
[]
byte
{
return
[]
byte
(
"---"
)
}
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