mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-10-31 10:37:24 -04:00 
			
		
		
		
	Refactoring to remove lint
This commit is contained in:
		
							parent
							
								
									81c4ea6be7
								
							
						
					
					
						commit
						d252d40681
					
				| @ -127,11 +127,11 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error | |||||||
| 	} | 	} | ||||||
| 	defer f.Close() | 	defer f.Close() | ||||||
| 
 | 
 | ||||||
| 	if fs, err := f.Stat(); err != nil { | 	fs, err := f.Stat() | ||||||
|  | 	if err != nil { | ||||||
| 		return http.StatusGone, nil | 		return http.StatusGone, nil | ||||||
| 	} else { |  | ||||||
| 		lastModTime = latest(lastModTime, fs.ModTime()) |  | ||||||
| 	} | 	} | ||||||
|  | 	lastModTime = latest(lastModTime, fs.ModTime()) | ||||||
| 
 | 
 | ||||||
| 	ctx := httpserver.Context{ | 	ctx := httpserver.Context{ | ||||||
| 		Root: md.FileSys, | 		Root: md.FileSys, | ||||||
|  | |||||||
| @ -33,7 +33,7 @@ type Metadata struct { | |||||||
| 	Flags map[string]bool | 	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 { | func NewMetadata(parsedMap map[string]interface{}) Metadata { | ||||||
| 	md := Metadata{ | 	md := Metadata{ | ||||||
| 		Variables: make(map[string]string), | 		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 | // Parser is a an interface that must be satisfied by each parser | ||||||
| type MetadataParser interface { | type Parser interface { | ||||||
| 	// Initialize a parser | 	// Initialize a parser | ||||||
| 	Init(b *bytes.Buffer) bool | 	Init(b *bytes.Buffer) bool | ||||||
| 
 | 
 | ||||||
| @ -90,7 +90,7 @@ type MetadataParser interface { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // GetParser returns a parser for the given data | // GetParser returns a parser for the given data | ||||||
| func GetParser(buf []byte) MetadataParser { | func GetParser(buf []byte) Parser { | ||||||
| 	for _, p := range parsers() { | 	for _, p := range parsers() { | ||||||
| 		b := bytes.NewBuffer(buf) | 		b := bytes.NewBuffer(buf) | ||||||
| 		if p.Init(b) { | 		if p.Init(b) { | ||||||
| @ -102,14 +102,14 @@ func GetParser(buf []byte) MetadataParser { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // parsers returns all available parsers | // parsers returns all available parsers | ||||||
| func parsers() []MetadataParser { | func parsers() []Parser { | ||||||
| 	return []MetadataParser{ | 	return []Parser{ | ||||||
| 		&TOMLMetadataParser{}, | 		&TOMLParser{}, | ||||||
| 		&YAMLMetadataParser{}, | 		&YAMLParser{}, | ||||||
| 		&JSONMetadataParser{}, | 		&JSONParser{}, | ||||||
| 
 | 
 | ||||||
| 		// This one must be last | 		// This one must be last | ||||||
| 		&NoneMetadataParser{}, | 		&NoneParser{}, | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -5,30 +5,32 @@ import ( | |||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // JSONMetadataParser is the MetadataParser for JSON | // JSONParser is the MetadataParser for JSON | ||||||
| type JSONMetadataParser struct { | type JSONParser struct { | ||||||
| 	metadata Metadata | 	metadata Metadata | ||||||
| 	markdown *bytes.Buffer | 	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" | 	return "JSON" | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Parse metadata/markdown file | // Init prepares the metadata metadata/markdown file and parses it | ||||||
| func (j *JSONMetadataParser) Init(b *bytes.Buffer) bool { | func (j *JSONParser) Init(b *bytes.Buffer) bool { | ||||||
| 	m := make(map[string]interface{}) | 	m := make(map[string]interface{}) | ||||||
| 
 | 
 | ||||||
| 	err := json.Unmarshal(b.Bytes(), &m) | 	err := json.Unmarshal(b.Bytes(), &m) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		var offset int | 		var offset int | ||||||
| 
 | 
 | ||||||
| 		if jerr, ok := err.(*json.SyntaxError); !ok { | 		jerr, ok := err.(*json.SyntaxError) | ||||||
|  | 		if !ok { | ||||||
| 			return false | 			return false | ||||||
| 		} else { |  | ||||||
| 			offset = int(jerr.Offset) |  | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|  | 		offset = int(jerr.Offset) | ||||||
|  | 
 | ||||||
| 		m = make(map[string]interface{}) | 		m = make(map[string]interface{}) | ||||||
| 		err = json.Unmarshal(b.Next(offset-1), &m) | 		err = json.Unmarshal(b.Next(offset-1), &m) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| @ -44,10 +46,11 @@ func (j *JSONMetadataParser) Init(b *bytes.Buffer) bool { | |||||||
| 
 | 
 | ||||||
| // Metadata returns parsed metadata.  It should be called | // Metadata returns parsed metadata.  It should be called | ||||||
| // only after a call to Parse returns without error. | // only after a call to Parse returns without error. | ||||||
| func (j *JSONMetadataParser) Metadata() Metadata { | func (j *JSONParser) Metadata() Metadata { | ||||||
| 	return j.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() | 	return j.markdown.Bytes() | ||||||
| } | } | ||||||
|  | |||||||
| @ -4,18 +4,19 @@ import ( | |||||||
| 	"bytes" | 	"bytes" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // TOMLMetadataParser is the MetadataParser for TOML | // NoneParser is the parser for plaintext markdown with no metadata. | ||||||
| type NoneMetadataParser struct { | type NoneParser struct { | ||||||
| 	metadata Metadata | 	metadata Metadata | ||||||
| 	markdown *bytes.Buffer | 	markdown *bytes.Buffer | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (n *NoneMetadataParser) Type() string { | // Type returns the kind of parser this struct is. | ||||||
|  | func (n *NoneParser) Type() string { | ||||||
| 	return "None" | 	return "None" | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Parse metadata/markdown file | // Init prepases and parses the metadata and markdown file | ||||||
| func (n *NoneMetadataParser) Init(b *bytes.Buffer) bool { | func (n *NoneParser) Init(b *bytes.Buffer) bool { | ||||||
| 	m := make(map[string]interface{}) | 	m := make(map[string]interface{}) | ||||||
| 	n.metadata = NewMetadata(m) | 	n.metadata = NewMetadata(m) | ||||||
| 	n.markdown = bytes.NewBuffer(b.Bytes()) | 	n.markdown = bytes.NewBuffer(b.Bytes()) | ||||||
| @ -24,16 +25,18 @@ func (n *NoneMetadataParser) Init(b *bytes.Buffer) bool { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Parse the metadata | // Parse the metadata | ||||||
| func (n *NoneMetadataParser) Parse(b []byte) ([]byte, error) { | func (n *NoneParser) Parse(b []byte) ([]byte, error) { | ||||||
| 	return nil, nil | 	return nil, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Metadata returns parsed metadata.  It should be called | // Metadata returns parsed metadata.  It should be called | ||||||
| // only after a call to Parse returns without error. | // only after a call to Parse returns without error. | ||||||
| func (n *NoneMetadataParser) Metadata() Metadata { | func (n *NoneParser) Metadata() Metadata { | ||||||
| 	return n.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() | 	return n.markdown.Bytes() | ||||||
| } | } | ||||||
|  | |||||||
| @ -158,13 +158,13 @@ func TestParsers(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	data := []struct { | 	data := []struct { | ||||||
| 		parser   MetadataParser | 		parser   Parser | ||||||
| 		testData [5]string | 		testData [5]string | ||||||
| 		name     string | 		name     string | ||||||
| 	}{ | 	}{ | ||||||
| 		{&JSONMetadataParser{}, JSON, "JSON"}, | 		{&JSONParser{}, JSON, "JSON"}, | ||||||
| 		{&YAMLMetadataParser{}, YAML, "YAML"}, | 		{&YAMLParser{}, YAML, "YAML"}, | ||||||
| 		{&TOMLMetadataParser{}, TOML, "TOML"}, | 		{&TOMLParser{}, TOML, "TOML"}, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	for _, v := range data { | 	for _, v := range data { | ||||||
|  | |||||||
| @ -6,18 +6,19 @@ import ( | |||||||
| 	"github.com/BurntSushi/toml" | 	"github.com/BurntSushi/toml" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // TOMLMetadataParser is the MetadataParser for TOML | // TOMLParser is the Parser for TOML | ||||||
| type TOMLMetadataParser struct { | type TOMLParser struct { | ||||||
| 	metadata Metadata | 	metadata Metadata | ||||||
| 	markdown *bytes.Buffer | 	markdown *bytes.Buffer | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (t *TOMLMetadataParser) Type() string { | // Type returns the kind of parser this struct is. | ||||||
|  | func (t *TOMLParser) Type() string { | ||||||
| 	return "TOML" | 	return "TOML" | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Parse metadata/markdown file | // Init prepares and parses the metadata and markdown file itself | ||||||
| func (t *TOMLMetadataParser) Init(b *bytes.Buffer) bool { | func (t *TOMLParser) Init(b *bytes.Buffer) bool { | ||||||
| 	meta, data := splitBuffer(b, "+++") | 	meta, data := splitBuffer(b, "+++") | ||||||
| 	if meta == nil || data == nil { | 	if meta == nil || data == nil { | ||||||
| 		return false | 		return false | ||||||
| @ -35,10 +36,11 @@ func (t *TOMLMetadataParser) Init(b *bytes.Buffer) bool { | |||||||
| 
 | 
 | ||||||
| // Metadata returns parsed metadata.  It should be called | // Metadata returns parsed metadata.  It should be called | ||||||
| // only after a call to Parse returns without error. | // only after a call to Parse returns without error. | ||||||
| func (t *TOMLMetadataParser) Metadata() Metadata { | func (t *TOMLParser) Metadata() Metadata { | ||||||
| 	return t.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() | 	return t.markdown.Bytes() | ||||||
| } | } | ||||||
|  | |||||||
| @ -6,17 +6,19 @@ import ( | |||||||
| 	"gopkg.in/yaml.v2" | 	"gopkg.in/yaml.v2" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // YAMLMetadataParser is the MetadataParser for YAML | // YAMLParser is the Parser for YAML | ||||||
| type YAMLMetadataParser struct { | type YAMLParser struct { | ||||||
| 	metadata Metadata | 	metadata Metadata | ||||||
| 	markdown *bytes.Buffer | 	markdown *bytes.Buffer | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (y *YAMLMetadataParser) Type() string { | // Type returns the kind of metadata parser. | ||||||
|  | func (y *YAMLParser) Type() string { | ||||||
| 	return "YAML" | 	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, "---") | 	meta, data := splitBuffer(b, "---") | ||||||
| 	if meta == nil || data == nil { | 	if meta == nil || data == nil { | ||||||
| 		return false | 		return false | ||||||
| @ -34,10 +36,11 @@ func (y *YAMLMetadataParser) Init(b *bytes.Buffer) bool { | |||||||
| 
 | 
 | ||||||
| // Metadata returns parsed metadata.  It should be called | // Metadata returns parsed metadata.  It should be called | ||||||
| // only after a call to Parse returns without error. | // only after a call to Parse returns without error. | ||||||
| func (y *YAMLMetadataParser) Metadata() Metadata { | func (y *YAMLParser) Metadata() Metadata { | ||||||
| 	return y.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() | 	return y.markdown.Bytes() | ||||||
| } | } | ||||||
|  | |||||||
| @ -11,11 +11,14 @@ import ( | |||||||
| 	"github.com/russross/blackfriday" | 	"github.com/russross/blackfriday" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | // FileInfo represents a file in a particular server context. It wraps the os.FileInfo struct. | ||||||
| type FileInfo struct { | type FileInfo struct { | ||||||
| 	os.FileInfo | 	os.FileInfo | ||||||
| 	ctx httpserver.Context | 	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) { | func (f FileInfo) Summarize(wordcount int) (string, error) { | ||||||
| 	fp, err := f.ctx.Root.Open(f.Name()) | 	fp, err := f.ctx.Root.Open(f.Name()) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | |||||||
| @ -45,6 +45,7 @@ func execTemplate(c *Config, mdata metadata.Metadata, files []FileInfo, ctx http | |||||||
| 	return b.Bytes(), nil | 	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 { | func SetTemplate(t *template.Template, name, filename string) error { | ||||||
| 
 | 
 | ||||||
| 	// Read template | 	// Read template | ||||||
| @ -64,6 +65,7 @@ func SetTemplate(t *template.Template, name, filename string) error { | |||||||
| 	return err | 	return err | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // GetDefaultTemplate returns the default template. | ||||||
| func GetDefaultTemplate() *template.Template { | func GetDefaultTemplate() *template.Template { | ||||||
| 	return template.Must(template.New("").Parse(defaultTemplate)) | 	return template.Must(template.New("").Parse(defaultTemplate)) | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user