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
Łukasz Nowak
caddy
Commits
33aba7eb
Commit
33aba7eb
authored
Jun 20, 2016
by
Matt Holt
Committed by
GitHub
Jun 20, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #894 from RobbieMcKinstry/master
Refactoring to remove lint warnings
parents
81c4ea6b
d252d406
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
64 additions
and
48 deletions
+64
-48
caddyhttp/markdown/markdown.go
caddyhttp/markdown/markdown.go
+3
-3
caddyhttp/markdown/metadata/metadata.go
caddyhttp/markdown/metadata/metadata.go
+10
-10
caddyhttp/markdown/metadata/metadata_json.go
caddyhttp/markdown/metadata/metadata_json.go
+13
-10
caddyhttp/markdown/metadata/metadata_none.go
caddyhttp/markdown/metadata/metadata_none.go
+11
-8
caddyhttp/markdown/metadata/metadata_test.go
caddyhttp/markdown/metadata/metadata_test.go
+4
-4
caddyhttp/markdown/metadata/metadata_toml.go
caddyhttp/markdown/metadata/metadata_toml.go
+9
-7
caddyhttp/markdown/metadata/metadata_yaml.go
caddyhttp/markdown/metadata/metadata_yaml.go
+9
-6
caddyhttp/markdown/process.go
caddyhttp/markdown/process.go
+3
-0
caddyhttp/markdown/template.go
caddyhttp/markdown/template.go
+2
-0
No files found.
caddyhttp/markdown/markdown.go
View file @
33aba7eb
...
...
@@ -127,11 +127,11 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
}
defer
f
.
Close
()
if
fs
,
err
:=
f
.
Stat
();
err
!=
nil
{
fs
,
err
:=
f
.
Stat
()
if
err
!=
nil
{
return
http
.
StatusGone
,
nil
}
else
{
lastModTime
=
latest
(
lastModTime
,
fs
.
ModTime
())
}
lastModTime
=
latest
(
lastModTime
,
fs
.
ModTime
())
ctx
:=
httpserver
.
Context
{
Root
:
md
.
FileSys
,
...
...
caddyhttp/markdown/metadata/metadata.go
View file @
33aba7eb
...
...
@@ -33,7 +33,7 @@ type Metadata struct {
Flags
map
[
string
]
bool
}
// NewMetadata
()
returns a new Metadata struct, loaded with the given map
// NewMetadata returns a new Metadata struct, loaded with the given map
func
NewMetadata
(
parsedMap
map
[
string
]
interface
{})
Metadata
{
md
:=
Metadata
{
Variables
:
make
(
map
[
string
]
string
),
...
...
@@ -74,8 +74,8 @@ func (m *Metadata) load(parsedMap map[string]interface{}) {
}
}
//
Metadata
Parser is a an interface that must be satisfied by each parser
type
Metadata
Parser
interface
{
// Parser is a an interface that must be satisfied by each parser
type
Parser
interface
{
// Initialize a parser
Init
(
b
*
bytes
.
Buffer
)
bool
...
...
@@ -90,7 +90,7 @@ type MetadataParser interface {
}
// GetParser returns a parser for the given data
func
GetParser
(
buf
[]
byte
)
Metadata
Parser
{
func
GetParser
(
buf
[]
byte
)
Parser
{
for
_
,
p
:=
range
parsers
()
{
b
:=
bytes
.
NewBuffer
(
buf
)
if
p
.
Init
(
b
)
{
...
...
@@ -102,14 +102,14 @@ func GetParser(buf []byte) MetadataParser {
}
// parsers returns all available parsers
func
parsers
()
[]
Metadata
Parser
{
return
[]
Metadata
Parser
{
&
TOML
Metadata
Parser
{},
&
YAML
Metadata
Parser
{},
&
JSON
Metadata
Parser
{},
func
parsers
()
[]
Parser
{
return
[]
Parser
{
&
TOMLParser
{},
&
YAMLParser
{},
&
JSONParser
{},
// This one must be last
&
None
Metadata
Parser
{},
&
NoneParser
{},
}
}
...
...
caddyhttp/markdown/metadata/metadata_json.go
View file @
33aba7eb
...
...
@@ -5,30 +5,32 @@ import (
"encoding/json"
)
// JSON
Metadata
Parser is the MetadataParser for JSON
type
JSON
Metadata
Parser
struct
{
// JSONParser is the MetadataParser for JSON
type
JSONParser
struct
{
metadata
Metadata
markdown
*
bytes
.
Buffer
}
func
(
j
*
JSONMetadataParser
)
Type
()
string
{
// Type returns the kind of metadata parser implemented by this struct.
func
(
j
*
JSONParser
)
Type
()
string
{
return
"JSON"
}
//
Parse metadata/markdown file
func
(
j
*
JSON
Metadata
Parser
)
Init
(
b
*
bytes
.
Buffer
)
bool
{
//
Init prepares the metadata metadata/markdown file and parses it
func
(
j
*
JSONParser
)
Init
(
b
*
bytes
.
Buffer
)
bool
{
m
:=
make
(
map
[
string
]
interface
{})
err
:=
json
.
Unmarshal
(
b
.
Bytes
(),
&
m
)
if
err
!=
nil
{
var
offset
int
if
jerr
,
ok
:=
err
.
(
*
json
.
SyntaxError
);
!
ok
{
jerr
,
ok
:=
err
.
(
*
json
.
SyntaxError
)
if
!
ok
{
return
false
}
else
{
offset
=
int
(
jerr
.
Offset
)
}
offset
=
int
(
jerr
.
Offset
)
m
=
make
(
map
[
string
]
interface
{})
err
=
json
.
Unmarshal
(
b
.
Next
(
offset
-
1
),
&
m
)
if
err
!=
nil
{
...
...
@@ -44,10 +46,11 @@ func (j *JSONMetadataParser) Init(b *bytes.Buffer) bool {
// Metadata returns parsed metadata. It should be called
// only after a call to Parse returns without error.
func
(
j
*
JSON
Metadata
Parser
)
Metadata
()
Metadata
{
func
(
j
*
JSONParser
)
Metadata
()
Metadata
{
return
j
.
metadata
}
func
(
j
*
JSONMetadataParser
)
Markdown
()
[]
byte
{
// Markdown returns the markdown text. It should be called only after a call to Parse returns without error.
func
(
j
*
JSONParser
)
Markdown
()
[]
byte
{
return
j
.
markdown
.
Bytes
()
}
caddyhttp/markdown/metadata/metadata_none.go
View file @
33aba7eb
...
...
@@ -4,18 +4,19 @@ import (
"bytes"
)
//
TOMLMetadataParser is the MetadataParser for TOML
type
None
Metadata
Parser
struct
{
//
NoneParser is the parser for plaintext markdown with no metadata.
type
NoneParser
struct
{
metadata
Metadata
markdown
*
bytes
.
Buffer
}
func
(
n
*
NoneMetadataParser
)
Type
()
string
{
// Type returns the kind of parser this struct is.
func
(
n
*
NoneParser
)
Type
()
string
{
return
"None"
}
//
Parse metadata/
markdown file
func
(
n
*
None
Metadata
Parser
)
Init
(
b
*
bytes
.
Buffer
)
bool
{
//
Init prepases and parses the metadata and
markdown file
func
(
n
*
NoneParser
)
Init
(
b
*
bytes
.
Buffer
)
bool
{
m
:=
make
(
map
[
string
]
interface
{})
n
.
metadata
=
NewMetadata
(
m
)
n
.
markdown
=
bytes
.
NewBuffer
(
b
.
Bytes
())
...
...
@@ -24,16 +25,18 @@ func (n *NoneMetadataParser) Init(b *bytes.Buffer) bool {
}
// Parse the metadata
func
(
n
*
None
Metadata
Parser
)
Parse
(
b
[]
byte
)
([]
byte
,
error
)
{
func
(
n
*
NoneParser
)
Parse
(
b
[]
byte
)
([]
byte
,
error
)
{
return
nil
,
nil
}
// Metadata returns parsed metadata. It should be called
// only after a call to Parse returns without error.
func
(
n
*
None
Metadata
Parser
)
Metadata
()
Metadata
{
func
(
n
*
NoneParser
)
Metadata
()
Metadata
{
return
n
.
metadata
}
func
(
n
*
NoneMetadataParser
)
Markdown
()
[]
byte
{
// Markdown returns parsed markdown. It should be called
// only after a call to Parse returns without error.
func
(
n
*
NoneParser
)
Markdown
()
[]
byte
{
return
n
.
markdown
.
Bytes
()
}
caddyhttp/markdown/metadata/metadata_test.go
View file @
33aba7eb
...
...
@@ -158,13 +158,13 @@ func TestParsers(t *testing.T) {
}
data
:=
[]
struct
{
parser
Metadata
Parser
parser
Parser
testData
[
5
]
string
name
string
}{
{
&
JSON
Metadata
Parser
{},
JSON
,
"JSON"
},
{
&
YAML
Metadata
Parser
{},
YAML
,
"YAML"
},
{
&
TOML
Metadata
Parser
{},
TOML
,
"TOML"
},
{
&
JSONParser
{},
JSON
,
"JSON"
},
{
&
YAMLParser
{},
YAML
,
"YAML"
},
{
&
TOMLParser
{},
TOML
,
"TOML"
},
}
for
_
,
v
:=
range
data
{
...
...
caddyhttp/markdown/metadata/metadata_toml.go
View file @
33aba7eb
...
...
@@ -6,18 +6,19 @@ import (
"github.com/BurntSushi/toml"
)
// TOML
MetadataParser is the Metadata
Parser for TOML
type
TOML
Metadata
Parser
struct
{
// TOML
Parser is the
Parser for TOML
type
TOMLParser
struct
{
metadata
Metadata
markdown
*
bytes
.
Buffer
}
func
(
t
*
TOMLMetadataParser
)
Type
()
string
{
// Type returns the kind of parser this struct is.
func
(
t
*
TOMLParser
)
Type
()
string
{
return
"TOML"
}
//
Parse metadata/markdown file
func
(
t
*
TOML
Metadata
Parser
)
Init
(
b
*
bytes
.
Buffer
)
bool
{
//
Init prepares and parses the metadata and markdown file itself
func
(
t
*
TOMLParser
)
Init
(
b
*
bytes
.
Buffer
)
bool
{
meta
,
data
:=
splitBuffer
(
b
,
"+++"
)
if
meta
==
nil
||
data
==
nil
{
return
false
...
...
@@ -35,10 +36,11 @@ func (t *TOMLMetadataParser) Init(b *bytes.Buffer) bool {
// Metadata returns parsed metadata. It should be called
// only after a call to Parse returns without error.
func
(
t
*
TOML
Metadata
Parser
)
Metadata
()
Metadata
{
func
(
t
*
TOMLParser
)
Metadata
()
Metadata
{
return
t
.
metadata
}
func
(
t
*
TOMLMetadataParser
)
Markdown
()
[]
byte
{
// Markdown returns parser markdown. It should be called only after a call to Parse returns without error.
func
(
t
*
TOMLParser
)
Markdown
()
[]
byte
{
return
t
.
markdown
.
Bytes
()
}
caddyhttp/markdown/metadata/metadata_yaml.go
View file @
33aba7eb
...
...
@@ -6,17 +6,19 @@ import (
"gopkg.in/yaml.v2"
)
// YAML
MetadataParser is the Metadata
Parser for YAML
type
YAML
Metadata
Parser
struct
{
// YAML
Parser is the
Parser for YAML
type
YAMLParser
struct
{
metadata
Metadata
markdown
*
bytes
.
Buffer
}
func
(
y
*
YAMLMetadataParser
)
Type
()
string
{
// Type returns the kind of metadata parser.
func
(
y
*
YAMLParser
)
Type
()
string
{
return
"YAML"
}
func
(
y
*
YAMLMetadataParser
)
Init
(
b
*
bytes
.
Buffer
)
bool
{
// Init prepares the metadata parser for parsing.
func
(
y
*
YAMLParser
)
Init
(
b
*
bytes
.
Buffer
)
bool
{
meta
,
data
:=
splitBuffer
(
b
,
"---"
)
if
meta
==
nil
||
data
==
nil
{
return
false
...
...
@@ -34,10 +36,11 @@ func (y *YAMLMetadataParser) Init(b *bytes.Buffer) bool {
// Metadata returns parsed metadata. It should be called
// only after a call to Parse returns without error.
func
(
y
*
YAML
Metadata
Parser
)
Metadata
()
Metadata
{
func
(
y
*
YAMLParser
)
Metadata
()
Metadata
{
return
y
.
metadata
}
func
(
y
*
YAMLMetadataParser
)
Markdown
()
[]
byte
{
// Markdown renders the text as a byte array
func
(
y
*
YAMLParser
)
Markdown
()
[]
byte
{
return
y
.
markdown
.
Bytes
()
}
caddyhttp/markdown/process.go
View file @
33aba7eb
...
...
@@ -11,11 +11,14 @@ import (
"github.com/russross/blackfriday"
)
// FileInfo represents a file in a particular server context. It wraps the os.FileInfo struct.
type
FileInfo
struct
{
os
.
FileInfo
ctx
httpserver
.
Context
}
// Summarize returns an abbreviated string representation of the markdown stored in this file.
// wordcount is the number of words returned in the summary.
func
(
f
FileInfo
)
Summarize
(
wordcount
int
)
(
string
,
error
)
{
fp
,
err
:=
f
.
ctx
.
Root
.
Open
(
f
.
Name
())
if
err
!=
nil
{
...
...
caddyhttp/markdown/template.go
View file @
33aba7eb
...
...
@@ -45,6 +45,7 @@ func execTemplate(c *Config, mdata metadata.Metadata, files []FileInfo, ctx http
return
b
.
Bytes
(),
nil
}
// SetTemplate reads in the template with the filename provided. If the file does not exist or is not parsable, it will return an error.
func
SetTemplate
(
t
*
template
.
Template
,
name
,
filename
string
)
error
{
// Read template
...
...
@@ -64,6 +65,7 @@ func SetTemplate(t *template.Template, name, filename string) error {
return
err
}
// GetDefaultTemplate returns the default template.
func
GetDefaultTemplate
()
*
template
.
Template
{
return
template
.
Must
(
template
.
New
(
""
)
.
Parse
(
defaultTemplate
))
}
...
...
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