Skip to content

Commit

Permalink
Merge pull request caddyserver#894 from RobbieMcKinstry/master
Browse files Browse the repository at this point in the history
Refactoring to remove lint warnings
  • Loading branch information
mholt authored Jun 21, 2016
2 parents 81c4ea6 + d252d40 commit 33aba7e
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 48 deletions.
6 changes: 3 additions & 3 deletions caddyhttp/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions caddyhttp/markdown/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -74,8 +74,8 @@ func (m *Metadata) load(parsedMap map[string]interface{}) {
}
}

// MetadataParser is a an interface that must be satisfied by each parser
type MetadataParser interface {
// Parser is a an interface that must be satisfied by each parser
type Parser interface {
// Initialize a parser
Init(b *bytes.Buffer) bool

Expand All @@ -90,7 +90,7 @@ type MetadataParser interface {
}

// GetParser returns a parser for the given data
func GetParser(buf []byte) MetadataParser {
func GetParser(buf []byte) Parser {
for _, p := range parsers() {
b := bytes.NewBuffer(buf)
if p.Init(b) {
Expand All @@ -102,14 +102,14 @@ func GetParser(buf []byte) MetadataParser {
}

// parsers returns all available parsers
func parsers() []MetadataParser {
return []MetadataParser{
&TOMLMetadataParser{},
&YAMLMetadataParser{},
&JSONMetadataParser{},
func parsers() []Parser {
return []Parser{
&TOMLParser{},
&YAMLParser{},
&JSONParser{},

// This one must be last
&NoneMetadataParser{},
&NoneParser{},
}
}

Expand Down
23 changes: 13 additions & 10 deletions caddyhttp/markdown/metadata/metadata_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,32 @@ import (
"encoding/json"
)

// JSONMetadataParser is the MetadataParser for JSON
type JSONMetadataParser 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 *JSONMetadataParser) 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 {
Expand All @@ -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 *JSONMetadataParser) 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()
}
19 changes: 11 additions & 8 deletions caddyhttp/markdown/metadata/metadata_none.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import (
"bytes"
)

// TOMLMetadataParser is the MetadataParser for TOML
type NoneMetadataParser 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 *NoneMetadataParser) 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())
Expand All @@ -24,16 +25,18 @@ func (n *NoneMetadataParser) Init(b *bytes.Buffer) bool {
}

// Parse the metadata
func (n *NoneMetadataParser) 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 *NoneMetadataParser) 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()
}
8 changes: 4 additions & 4 deletions caddyhttp/markdown/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ func TestParsers(t *testing.T) {
}

data := []struct {
parser MetadataParser
parser Parser
testData [5]string
name string
}{
{&JSONMetadataParser{}, JSON, "JSON"},
{&YAMLMetadataParser{}, YAML, "YAML"},
{&TOMLMetadataParser{}, TOML, "TOML"},
{&JSONParser{}, JSON, "JSON"},
{&YAMLParser{}, YAML, "YAML"},
{&TOMLParser{}, TOML, "TOML"},
}

for _, v := range data {
Expand Down
16 changes: 9 additions & 7 deletions caddyhttp/markdown/metadata/metadata_toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (
"github.com/BurntSushi/toml"
)

// TOMLMetadataParser is the MetadataParser for TOML
type TOMLMetadataParser struct {
// TOMLParser 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 *TOMLMetadataParser) 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
Expand All @@ -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 *TOMLMetadataParser) 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()
}
15 changes: 9 additions & 6 deletions caddyhttp/markdown/metadata/metadata_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import (
"gopkg.in/yaml.v2"
)

// YAMLMetadataParser is the MetadataParser for YAML
type YAMLMetadataParser struct {
// YAMLParser 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
Expand All @@ -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 *YAMLMetadataParser) 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()
}
3 changes: 3 additions & 0 deletions caddyhttp/markdown/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions caddyhttp/markdown/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
Expand Down

0 comments on commit 33aba7e

Please sign in to comment.