2016-10-18 20:49:46 +00:00
|
|
|
package frontmatter
|
|
|
|
|
2017-01-18 18:20:56 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
2017-01-18 18:40:20 +00:00
|
|
|
"errors"
|
2017-01-18 18:20:56 +00:00
|
|
|
"strings"
|
|
|
|
)
|
2016-10-18 20:49:46 +00:00
|
|
|
|
|
|
|
// HasRune checks if the file has the frontmatter rune
|
|
|
|
func HasRune(file []byte) bool {
|
|
|
|
return strings.HasPrefix(string(file), "---") ||
|
|
|
|
strings.HasPrefix(string(file), "+++") ||
|
|
|
|
strings.HasPrefix(string(file), "{")
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendRune appends the frontmatter rune to a file
|
2017-01-18 18:20:56 +00:00
|
|
|
func AppendRune(frontmatter []byte, mark rune) []byte {
|
|
|
|
frontmatter = bytes.TrimSpace(frontmatter)
|
|
|
|
|
|
|
|
switch mark {
|
|
|
|
case '-':
|
2016-10-18 20:49:46 +00:00
|
|
|
return []byte("---\n" + string(frontmatter) + "\n---")
|
2017-01-18 18:20:56 +00:00
|
|
|
case '+':
|
2016-10-18 20:49:46 +00:00
|
|
|
return []byte("+++\n" + string(frontmatter) + "\n+++")
|
2017-01-18 18:20:56 +00:00
|
|
|
case '{':
|
2017-01-04 18:17:47 +00:00
|
|
|
return []byte("{\n" + string(frontmatter) + "\n}")
|
2016-10-18 20:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return frontmatter
|
|
|
|
}
|
2017-01-18 18:20:56 +00:00
|
|
|
|
2017-01-18 18:40:20 +00:00
|
|
|
// RuneToStringFormat converts the rune to a string with the format
|
|
|
|
func RuneToStringFormat(mark rune) (string, error) {
|
2017-01-18 18:20:56 +00:00
|
|
|
switch mark {
|
|
|
|
case '-':
|
2017-01-18 18:40:20 +00:00
|
|
|
return "yaml", nil
|
2017-01-18 18:20:56 +00:00
|
|
|
case '+':
|
2017-01-18 18:40:20 +00:00
|
|
|
return "toml", nil
|
2017-04-25 09:51:48 +00:00
|
|
|
case '{', '}':
|
2017-01-18 18:40:20 +00:00
|
|
|
return "json", nil
|
2017-01-18 18:20:56 +00:00
|
|
|
default:
|
2017-04-25 09:51:48 +00:00
|
|
|
return "", errors.New("Unsupported format type")
|
2017-01-18 18:20:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 18:40:20 +00:00
|
|
|
// StringFormatToRune converts the format name to its rune
|
|
|
|
func StringFormatToRune(format string) (rune, error) {
|
2017-01-18 18:20:56 +00:00
|
|
|
switch format {
|
|
|
|
case "yaml":
|
2017-01-18 18:40:20 +00:00
|
|
|
return '-', nil
|
2017-01-18 18:20:56 +00:00
|
|
|
case "toml":
|
2017-01-18 18:40:20 +00:00
|
|
|
return '+', nil
|
2017-01-18 18:20:56 +00:00
|
|
|
case "json":
|
2017-01-18 18:40:20 +00:00
|
|
|
return '{', nil
|
2017-01-18 18:20:56 +00:00
|
|
|
default:
|
2017-04-25 09:51:48 +00:00
|
|
|
return '0', errors.New("Unsupported format type")
|
2017-01-18 18:20:56 +00:00
|
|
|
}
|
|
|
|
}
|