From b97a7909d81dd909a5454aa6cb8ad464707af315 Mon Sep 17 00:00:00 2001 From: Tobias Weingartner Date: Sat, 16 Apr 2016 15:18:17 -0700 Subject: [PATCH] Nuke more redundant things. --- middleware/markdown/metadata/metadata.go | 59 ++----------------- middleware/markdown/metadata/metadata_json.go | 28 --------- middleware/markdown/metadata/metadata_none.go | 10 ---- middleware/markdown/metadata/metadata_toml.go | 25 -------- middleware/markdown/metadata/metadata_yaml.go | 25 -------- 5 files changed, 5 insertions(+), 142 deletions(-) diff --git a/middleware/markdown/metadata/metadata.go b/middleware/markdown/metadata/metadata.go index c5ccaaaa..027a8cdb 100644 --- a/middleware/markdown/metadata/metadata.go +++ b/middleware/markdown/metadata/metadata.go @@ -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) diff --git a/middleware/markdown/metadata/metadata_json.go b/middleware/markdown/metadata/metadata_json.go index bfc8c13d..c35148c4 100644 --- a/middleware/markdown/metadata/metadata_json.go +++ b/middleware/markdown/metadata/metadata_json.go @@ -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("}") -} diff --git a/middleware/markdown/metadata/metadata_none.go b/middleware/markdown/metadata/metadata_none.go index 3dc22c07..f2ca10f3 100644 --- a/middleware/markdown/metadata/metadata_none.go +++ b/middleware/markdown/metadata/metadata_none.go @@ -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("...") -} diff --git a/middleware/markdown/metadata/metadata_toml.go b/middleware/markdown/metadata/metadata_toml.go index cfd3a9f7..ed3a8451 100644 --- a/middleware/markdown/metadata/metadata_toml.go +++ b/middleware/markdown/metadata/metadata_toml.go @@ -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("+++") -} diff --git a/middleware/markdown/metadata/metadata_yaml.go b/middleware/markdown/metadata/metadata_yaml.go index e7877445..14161f5e 100644 --- a/middleware/markdown/metadata/metadata_yaml.go +++ b/middleware/markdown/metadata/metadata_yaml.go @@ -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("---") -}