Commit b97a7909 authored by Tobias Weingartner's avatar Tobias Weingartner

Nuke more redundant things.

parent 7c986791
...@@ -3,7 +3,6 @@ package metadata ...@@ -3,7 +3,6 @@ package metadata
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"fmt"
"time" "time"
) )
...@@ -71,12 +70,6 @@ type MetadataParser interface { ...@@ -71,12 +70,6 @@ type MetadataParser interface {
// Type of metadata // Type of metadata
Type() string Type() string
// Opening identifier
Opening() []byte
// Closing identifier
Closing() []byte
// Parsed metadata. // Parsed metadata.
Metadata() Metadata Metadata() Metadata
...@@ -84,35 +77,7 @@ type MetadataParser interface { ...@@ -84,35 +77,7 @@ type MetadataParser interface {
Markdown() []byte Markdown() []byte
} }
// extractMetadata separates metadata content from from markdown content in b. // GetParser returns a parser for the given data
// 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
}
func GetParser(buf []byte) MetadataParser { func GetParser(buf []byte) MetadataParser {
for _, p := range parsers() { for _, p := range parsers() {
b := bytes.NewBuffer(buf) b := bytes.NewBuffer(buf)
...@@ -124,22 +89,6 @@ func GetParser(buf []byte) MetadataParser { ...@@ -124,22 +89,6 @@ func GetParser(buf []byte) MetadataParser {
return nil 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 { func NewMetadata() Metadata {
return Metadata{ return Metadata{
Variables: make(map[string]string), Variables: make(map[string]string),
...@@ -151,13 +100,15 @@ func NewMetadata() Metadata { ...@@ -151,13 +100,15 @@ func NewMetadata() Metadata {
func parsers() []MetadataParser { func parsers() []MetadataParser {
return []MetadataParser{ return []MetadataParser{
&TOMLMetadataParser{}, &TOMLMetadataParser{},
&YAMLMetadataParser{metadata: NewMetadata()}, &YAMLMetadataParser{},
&JSONMetadataParser{}, &JSONMetadataParser{},
// This one must be last
&NoneMetadataParser{}, &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) { func splitBuffer(b *bytes.Buffer, delim string) (*bytes.Buffer, *bytes.Buffer) {
scanner := bufio.NewScanner(b) scanner := bufio.NewScanner(b)
......
...@@ -43,24 +43,6 @@ func (j *JSONMetadataParser) Init(b *bytes.Buffer) bool { ...@@ -43,24 +43,6 @@ func (j *JSONMetadataParser) Init(b *bytes.Buffer) bool {
return true 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 // 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 *JSONMetadataParser) Metadata() Metadata {
...@@ -70,13 +52,3 @@ func (j *JSONMetadataParser) Metadata() Metadata { ...@@ -70,13 +52,3 @@ func (j *JSONMetadataParser) Metadata() Metadata {
func (j *JSONMetadataParser) Markdown() []byte { func (j *JSONMetadataParser) Markdown() []byte {
return j.markdown.Bytes() 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("}")
}
...@@ -38,13 +38,3 @@ func (n *NoneMetadataParser) Metadata() Metadata { ...@@ -38,13 +38,3 @@ func (n *NoneMetadataParser) Metadata() Metadata {
func (n *NoneMetadataParser) Markdown() []byte { func (n *NoneMetadataParser) Markdown() []byte {
return n.markdown.Bytes() 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("...")
}
...@@ -34,21 +34,6 @@ func (t *TOMLMetadataParser) Init(b *bytes.Buffer) bool { ...@@ -34,21 +34,6 @@ func (t *TOMLMetadataParser) Init(b *bytes.Buffer) bool {
return true 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 // 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 *TOMLMetadataParser) Metadata() Metadata {
...@@ -58,13 +43,3 @@ func (t *TOMLMetadataParser) Metadata() Metadata { ...@@ -58,13 +43,3 @@ func (t *TOMLMetadataParser) Metadata() Metadata {
func (t *TOMLMetadataParser) Markdown() []byte { func (t *TOMLMetadataParser) Markdown() []byte {
return t.markdown.Bytes() 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("+++")
}
...@@ -33,21 +33,6 @@ func (y *YAMLMetadataParser) Init(b *bytes.Buffer) bool { ...@@ -33,21 +33,6 @@ func (y *YAMLMetadataParser) Init(b *bytes.Buffer) bool {
return true 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 // 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 *YAMLMetadataParser) Metadata() Metadata {
...@@ -57,13 +42,3 @@ func (y *YAMLMetadataParser) Metadata() Metadata { ...@@ -57,13 +42,3 @@ func (y *YAMLMetadataParser) Metadata() Metadata {
func (y *YAMLMetadataParser) Markdown() []byte { func (y *YAMLMetadataParser) Markdown() []byte {
return y.markdown.Bytes() 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("---")
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment