2017-06-24 11:12:15 +00:00
|
|
|
package frontmatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HasRune checks if the file has the frontmatter rune
|
2017-06-29 09:17:35 +00:00
|
|
|
func HasRune(file string) bool {
|
|
|
|
return strings.HasPrefix(file, "---") ||
|
|
|
|
strings.HasPrefix(file, "+++") ||
|
|
|
|
strings.HasPrefix(file, "{")
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppendRune appends the frontmatter rune to a file
|
2017-06-29 09:17:35 +00:00
|
|
|
func AppendRune(frontmatter string, mark rune) string {
|
|
|
|
frontmatter = strings.TrimSpace(frontmatter)
|
2017-06-24 11:12:15 +00:00
|
|
|
|
|
|
|
switch mark {
|
|
|
|
case '-':
|
2017-06-29 09:17:35 +00:00
|
|
|
return "---\n" + frontmatter + "\n---"
|
2017-06-24 11:12:15 +00:00
|
|
|
case '+':
|
2017-06-29 09:17:35 +00:00
|
|
|
return "+++\n" + frontmatter + "\n+++"
|
2017-06-24 11:12:15 +00:00
|
|
|
case '{':
|
2017-06-29 09:17:35 +00:00
|
|
|
return "{\n" + frontmatter + "\n}"
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return frontmatter
|
|
|
|
}
|
|
|
|
|
|
|
|
// RuneToStringFormat converts the rune to a string with the format
|
|
|
|
func RuneToStringFormat(mark rune) (string, error) {
|
|
|
|
switch mark {
|
|
|
|
case '-':
|
|
|
|
return "yaml", nil
|
|
|
|
case '+':
|
|
|
|
return "toml", nil
|
|
|
|
case '{', '}':
|
|
|
|
return "json", nil
|
|
|
|
default:
|
|
|
|
return "", errors.New("Unsupported format type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringFormatToRune converts the format name to its rune
|
|
|
|
func StringFormatToRune(format string) (rune, error) {
|
|
|
|
switch format {
|
|
|
|
case "yaml":
|
|
|
|
return '-', nil
|
|
|
|
case "toml":
|
|
|
|
return '+', nil
|
|
|
|
case "json":
|
|
|
|
return '{', nil
|
|
|
|
default:
|
|
|
|
return '0', errors.New("Unsupported format type")
|
|
|
|
}
|
|
|
|
}
|