aboutsummaryrefslogtreecommitdiffhomepage
path: root/parser/frontmatter.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2018-10-20 17:38:49 +0200
committerBjørn Erik Pedersen <[email protected]>2018-10-22 20:46:14 +0200
commiteb038cfa0a8ada29dfcba1204ec5c432da9ed7e0 (patch)
tree9ed33e3517e49ef16f349b70b328feff47ee60d3 /parser/frontmatter.go
parent129c27ee6e9fed98dbfebeaa272fd52757b475b2 (diff)
downloadhugo-eb038cfa0a8ada29dfcba1204ec5c432da9ed7e0.tar.gz
hugo-eb038cfa0a8ada29dfcba1204ec5c432da9ed7e0.zip
Convert the rest to new page parser code paths
And remove some now unused code. See #5324
Diffstat (limited to 'parser/frontmatter.go')
-rw-r--r--parser/frontmatter.go165
1 files changed, 13 insertions, 152 deletions
diff --git a/parser/frontmatter.go b/parser/frontmatter.go
index a42db0cca..ab1bc4d55 100644
--- a/parser/frontmatter.go
+++ b/parser/frontmatter.go
@@ -13,14 +13,10 @@
package parser
-// TODO(bep) archetype remove unused from this package.
-
import (
- "bytes"
"encoding/json"
"errors"
"io"
- "strings"
"github.com/gohugoio/hugo/parser/metadecoders"
@@ -29,53 +25,12 @@ import (
"gopkg.in/yaml.v2"
)
-// FrontmatterType represents a type of frontmatter.
-type FrontmatterType struct {
- // Parse decodes content into a Go interface.
- Parse func([]byte) (map[string]interface{}, error)
-
- markstart, markend []byte // starting and ending delimiters
- includeMark bool // include start and end mark in output
-}
-
-// InterfaceToConfig encodes a given input based upon the mark and writes to w.
-func InterfaceToConfig(in interface{}, mark rune, w io.Writer) error {
- if in == nil {
- return errors.New("input was nil")
- }
-
- switch mark {
- case rune(YAMLLead[0]):
- b, err := yaml.Marshal(in)
- if err != nil {
- return err
- }
-
- _, err = w.Write(b)
- return err
-
- case rune(TOMLLead[0]):
- return toml.NewEncoder(w).Encode(in)
- case rune(JSONLead[0]):
- b, err := json.MarshalIndent(in, "", " ")
- if err != nil {
- return err
- }
-
- _, err = w.Write(b)
- if err != nil {
- return err
- }
-
- _, err = w.Write([]byte{'\n'})
- return err
-
- default:
- return errors.New("Unsupported Format provided")
- }
-}
+const (
+ yamlDelimLf = "---\n"
+ tomlDelimLf = "+++\n"
+)
-func InterfaceToConfig2(in interface{}, format metadecoders.Format, w io.Writer) error {
+func InterfaceToConfig(in interface{}, format metadecoders.Format, w io.Writer) error {
if in == nil {
return errors.New("input was nil")
}
@@ -111,136 +66,42 @@ func InterfaceToConfig2(in interface{}, format metadecoders.Format, w io.Writer)
}
}
-func InterfaceToFrontMatter2(in interface{}, format metadecoders.Format, w io.Writer) error {
+func InterfaceToFrontMatter(in interface{}, format metadecoders.Format, w io.Writer) error {
if in == nil {
return errors.New("input was nil")
}
switch format {
case metadecoders.YAML:
- _, err := w.Write([]byte(YAMLDelimUnix))
+ _, err := w.Write([]byte(yamlDelimLf))
if err != nil {
return err
}
- err = InterfaceToConfig2(in, format, w)
+ err = InterfaceToConfig(in, format, w)
if err != nil {
return err
}
- _, err = w.Write([]byte(YAMLDelimUnix))
+ _, err = w.Write([]byte(yamlDelimLf))
return err
case metadecoders.TOML:
- _, err := w.Write([]byte(TOMLDelimUnix))
+ _, err := w.Write([]byte(tomlDelimLf))
if err != nil {
return err
}
- err = InterfaceToConfig2(in, format, w)
+ err = InterfaceToConfig(in, format, w)
if err != nil {
return err
}
- _, err = w.Write([]byte("\n" + TOMLDelimUnix))
+ _, err = w.Write([]byte("\n" + tomlDelimLf))
return err
default:
- return InterfaceToConfig2(in, format, w)
- }
-}
-
-// InterfaceToFrontMatter encodes a given input into a frontmatter
-// representation based upon the mark with the appropriate front matter delimiters
-// surrounding the output, which is written to w.
-func InterfaceToFrontMatter(in interface{}, mark rune, w io.Writer) error {
- if in == nil {
- return errors.New("input was nil")
+ return InterfaceToConfig(in, format, w)
}
-
- switch mark {
- case rune(YAMLLead[0]):
- _, err := w.Write([]byte(YAMLDelimUnix))
- if err != nil {
- return err
- }
-
- err = InterfaceToConfig(in, mark, w)
- if err != nil {
- return err
- }
-
- _, err = w.Write([]byte(YAMLDelimUnix))
- return err
-
- case rune(TOMLLead[0]):
- _, err := w.Write([]byte(TOMLDelimUnix))
- if err != nil {
- return err
- }
-
- err = InterfaceToConfig(in, mark, w)
-
- if err != nil {
- return err
- }
-
- _, err = w.Write([]byte("\n" + TOMLDelimUnix))
- return err
-
- default:
- return InterfaceToConfig(in, mark, w)
- }
-}
-
-// FormatToLeadRune takes a given format kind and return the leading front
-// matter delimiter.
-func FormatToLeadRune(kind string) rune {
- switch FormatSanitize(kind) {
- case "yaml":
- return rune([]byte(YAMLLead)[0])
- case "json":
- return rune([]byte(JSONLead)[0])
- case "org":
- return '#'
- default:
- return rune([]byte(TOMLLead)[0])
- }
-}
-
-// FormatSanitize returns the canonical format name for a given kind.
-//
-// TODO(bep) move to helpers
-func FormatSanitize(kind string) string {
- switch strings.ToLower(kind) {
- case "yaml", "yml":
- return "yaml"
- case "toml", "tml":
- return "toml"
- case "json", "js":
- return "json"
- case "org":
- return kind
- default:
- return "toml"
- }
-}
-
-// removeTOMLIdentifier removes, if necessary, beginning and ending TOML
-// frontmatter delimiters from a byte slice.
-func removeTOMLIdentifier(datum []byte) []byte {
- ld := len(datum)
- if ld < 8 {
- return datum
- }
-
- b := bytes.TrimPrefix(datum, []byte(TOMLDelim))
- if ld-len(b) != 3 {
- // No TOML prefix trimmed, so bail out
- return datum
- }
-
- b = bytes.Trim(b, "\r\n")
- return bytes.TrimSuffix(b, []byte(TOMLDelim))
}