2016-10-18 20:49:46 +00:00
|
|
|
package frontmatter
|
|
|
|
|
2017-01-18 18:20:56 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"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
|
|
|
|
|
|
|
func RuneToStringFormat(mark rune) string {
|
|
|
|
switch mark {
|
|
|
|
case '-':
|
|
|
|
return "yaml"
|
|
|
|
case '+':
|
|
|
|
return "toml"
|
|
|
|
case '{':
|
|
|
|
return "json"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func StringFormatToRune(format string) rune {
|
|
|
|
switch format {
|
|
|
|
case "yaml":
|
|
|
|
return '-'
|
|
|
|
case "toml":
|
|
|
|
return '+'
|
|
|
|
case "json":
|
|
|
|
return '{'
|
|
|
|
default:
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
}
|