From d252d40681ac6d2483cbd4a621afe70fe524d2c1 Mon Sep 17 00:00:00 2001 From: Robbie McKinstry Date: Mon, 20 Jun 2016 19:11:29 -0400 Subject: [PATCH] Refactoring to remove lint --- caddyhttp/markdown/markdown.go | 6 ++--- caddyhttp/markdown/metadata/metadata.go | 20 ++++++++--------- caddyhttp/markdown/metadata/metadata_json.go | 23 +++++++++++--------- caddyhttp/markdown/metadata/metadata_none.go | 19 +++++++++------- caddyhttp/markdown/metadata/metadata_test.go | 8 +++---- caddyhttp/markdown/metadata/metadata_toml.go | 16 ++++++++------ caddyhttp/markdown/metadata/metadata_yaml.go | 15 ++++++++----- caddyhttp/markdown/process.go | 3 +++ caddyhttp/markdown/template.go | 2 ++ 9 files changed, 64 insertions(+), 48 deletions(-) diff --git a/caddyhttp/markdown/markdown.go b/caddyhttp/markdown/markdown.go index bef1dce0..94b5b61e 100644 --- a/caddyhttp/markdown/markdown.go +++ b/caddyhttp/markdown/markdown.go @@ -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, diff --git a/caddyhttp/markdown/metadata/metadata.go b/caddyhttp/markdown/metadata/metadata.go index ade7fcc9..cecdc11e 100644 --- a/caddyhttp/markdown/metadata/metadata.go +++ b/caddyhttp/markdown/metadata/metadata.go @@ -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{}) { } } -// 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 @@ -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) { @@ -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{}, } } diff --git a/caddyhttp/markdown/metadata/metadata_json.go b/caddyhttp/markdown/metadata/metadata_json.go index d3b9991f..61343f28 100644 --- a/caddyhttp/markdown/metadata/metadata_json.go +++ b/caddyhttp/markdown/metadata/metadata_json.go @@ -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 { @@ -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() } diff --git a/caddyhttp/markdown/metadata/metadata_none.go b/caddyhttp/markdown/metadata/metadata_none.go index ed034f2f..e87bfb43 100644 --- a/caddyhttp/markdown/metadata/metadata_none.go +++ b/caddyhttp/markdown/metadata/metadata_none.go @@ -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()) @@ -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() } diff --git a/caddyhttp/markdown/metadata/metadata_test.go b/caddyhttp/markdown/metadata/metadata_test.go index 0c155d37..d3b91ee0 100644 --- a/caddyhttp/markdown/metadata/metadata_test.go +++ b/caddyhttp/markdown/metadata/metadata_test.go @@ -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 { diff --git a/caddyhttp/markdown/metadata/metadata_toml.go b/caddyhttp/markdown/metadata/metadata_toml.go index 75c2067f..ec7388a4 100644 --- a/caddyhttp/markdown/metadata/metadata_toml.go +++ b/caddyhttp/markdown/metadata/metadata_toml.go @@ -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 @@ -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() } diff --git a/caddyhttp/markdown/metadata/metadata_yaml.go b/caddyhttp/markdown/metadata/metadata_yaml.go index f7ef5bb4..f8e9acfd 100644 --- a/caddyhttp/markdown/metadata/metadata_yaml.go +++ b/caddyhttp/markdown/metadata/metadata_yaml.go @@ -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 @@ -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() } diff --git a/caddyhttp/markdown/process.go b/caddyhttp/markdown/process.go index 32c887c7..df2f5f77 100644 --- a/caddyhttp/markdown/process.go +++ b/caddyhttp/markdown/process.go @@ -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 { diff --git a/caddyhttp/markdown/template.go b/caddyhttp/markdown/template.go index fcd8f31a..34a14747 100644 --- a/caddyhttp/markdown/template.go +++ b/caddyhttp/markdown/template.go @@ -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)) }