2015-12-07 13:57:01 -05:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
2014-05-01 13:19:51 -04:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-05-01 13:19:51 -04:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-05-01 13:19:51 -04:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package parser
|
|
|
|
|
2017-06-18 13:06:28 -04:00
|
|
|
// TODO(bep) archetype remove unused from this package.
|
|
|
|
|
2014-05-01 13:19:51 -04:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2016-11-18 16:54:57 -05:00
|
|
|
"errors"
|
2017-12-01 14:37:19 -05:00
|
|
|
"fmt"
|
2016-12-26 16:23:20 -05:00
|
|
|
"io"
|
2014-05-02 01:01:44 -04:00
|
|
|
"strings"
|
2014-05-01 13:19:51 -04:00
|
|
|
|
2018-02-12 11:39:11 -05:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
|
2017-05-31 06:10:46 -04:00
|
|
|
"github.com/BurntSushi/toml"
|
2017-02-21 02:46:03 -05:00
|
|
|
"github.com/chaseadamsio/goorgeous"
|
2016-08-20 15:28:38 -04:00
|
|
|
|
2015-01-24 06:30:00 -05:00
|
|
|
"gopkg.in/yaml.v2"
|
2014-05-01 13:19:51 -04:00
|
|
|
)
|
|
|
|
|
2016-12-26 16:23:20 -05:00
|
|
|
// FrontmatterType represents a type of frontmatter.
|
|
|
|
type FrontmatterType struct {
|
|
|
|
// Parse decodes content into a Go interface.
|
2018-01-25 22:54:15 -05:00
|
|
|
Parse func([]byte) (map[string]interface{}, error)
|
2016-12-26 16:23:20 -05:00
|
|
|
|
|
|
|
markstart, markend []byte // starting and ending delimiters
|
|
|
|
includeMark bool // include start and end mark in output
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
|
|
|
|
2016-12-26 16:23:20 -05:00
|
|
|
// InterfaceToConfig encodes a given input based upon the mark and writes to w.
|
|
|
|
func InterfaceToConfig(in interface{}, mark rune, w io.Writer) error {
|
2014-05-08 18:30:11 -04:00
|
|
|
if in == nil {
|
2016-12-26 16:23:20 -05:00
|
|
|
return errors.New("input was nil")
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch mark {
|
2016-03-23 09:51:16 -04:00
|
|
|
case rune(YAMLLead[0]):
|
2016-12-26 16:23:20 -05:00
|
|
|
b, err := yaml.Marshal(in)
|
2014-05-08 18:30:11 -04:00
|
|
|
if err != nil {
|
2016-12-26 16:23:20 -05:00
|
|
|
return err
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
2016-12-26 16:23:20 -05:00
|
|
|
|
|
|
|
_, err = w.Write(b)
|
|
|
|
return err
|
|
|
|
|
2016-03-23 09:51:16 -04:00
|
|
|
case rune(TOMLLead[0]):
|
2017-05-31 06:10:46 -04:00
|
|
|
return toml.NewEncoder(w).Encode(in)
|
2016-03-23 09:51:16 -04:00
|
|
|
case rune(JSONLead[0]):
|
2016-12-26 16:23:20 -05:00
|
|
|
b, err := json.MarshalIndent(in, "", " ")
|
2014-05-08 18:30:11 -04:00
|
|
|
if err != nil {
|
2016-12-26 16:23:20 -05:00
|
|
|
return err
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
2016-12-26 16:23:20 -05:00
|
|
|
|
|
|
|
_, err = w.Write(b)
|
2014-05-08 18:30:11 -04:00
|
|
|
if err != nil {
|
2016-12-26 16:23:20 -05:00
|
|
|
return err
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
2016-12-26 16:23:20 -05:00
|
|
|
|
|
|
|
_, err = w.Write([]byte{'\n'})
|
|
|
|
return err
|
|
|
|
|
2014-05-08 18:30:11 -04:00
|
|
|
default:
|
2016-12-26 16:23:20 -05:00
|
|
|
return errors.New("Unsupported Format provided")
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 16:23:20 -05:00
|
|
|
// 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 {
|
2014-05-01 13:19:51 -04:00
|
|
|
if in == nil {
|
2016-12-26 16:23:20 -05:00
|
|
|
return errors.New("input was nil")
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch mark {
|
2016-03-23 09:51:16 -04:00
|
|
|
case rune(YAMLLead[0]):
|
2016-12-26 16:23:20 -05:00
|
|
|
_, err := w.Write([]byte(YAMLDelimUnix))
|
2014-05-01 13:19:51 -04:00
|
|
|
if err != nil {
|
2016-12-26 16:23:20 -05:00
|
|
|
return err
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
2016-12-26 16:23:20 -05:00
|
|
|
|
|
|
|
err = InterfaceToConfig(in, mark, w)
|
2014-05-01 13:19:51 -04:00
|
|
|
if err != nil {
|
2016-12-26 16:23:20 -05:00
|
|
|
return err
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
2016-12-26 16:23:20 -05:00
|
|
|
|
|
|
|
_, err = w.Write([]byte(YAMLDelimUnix))
|
|
|
|
return err
|
|
|
|
|
2016-03-23 09:51:16 -04:00
|
|
|
case rune(TOMLLead[0]):
|
2016-12-26 16:23:20 -05:00
|
|
|
_, err := w.Write([]byte(TOMLDelimUnix))
|
2014-05-01 13:19:51 -04:00
|
|
|
if err != nil {
|
2016-12-26 16:23:20 -05:00
|
|
|
return err
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
|
|
|
|
2016-12-26 16:23:20 -05:00
|
|
|
err = InterfaceToConfig(in, mark, w)
|
2017-03-15 03:04:52 -04:00
|
|
|
|
2014-05-01 13:19:51 -04:00
|
|
|
if err != nil {
|
2016-12-26 16:23:20 -05:00
|
|
|
return err
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
2016-12-26 16:23:20 -05:00
|
|
|
|
|
|
|
_, err = w.Write([]byte("\n" + TOMLDelimUnix))
|
|
|
|
return err
|
|
|
|
|
2014-05-01 13:19:51 -04:00
|
|
|
default:
|
2016-12-26 16:23:20 -05:00
|
|
|
return InterfaceToConfig(in, mark, w)
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 16:23:20 -05:00
|
|
|
// FormatToLeadRune takes a given format kind and return the leading front
|
|
|
|
// matter delimiter.
|
2014-05-02 01:01:44 -04:00
|
|
|
func FormatToLeadRune(kind string) rune {
|
2014-05-08 18:30:11 -04:00
|
|
|
switch FormatSanitize(kind) {
|
2014-05-02 01:01:44 -04:00
|
|
|
case "yaml":
|
2016-03-23 09:51:16 -04:00
|
|
|
return rune([]byte(YAMLLead)[0])
|
2014-05-02 01:01:44 -04:00
|
|
|
case "json":
|
2016-03-23 09:51:16 -04:00
|
|
|
return rune([]byte(JSONLead)[0])
|
2016-12-26 16:23:20 -05:00
|
|
|
case "org":
|
|
|
|
return '#'
|
2014-05-02 01:01:44 -04:00
|
|
|
default:
|
2016-03-23 09:51:16 -04:00
|
|
|
return rune([]byte(TOMLLead)[0])
|
2014-05-02 01:01:44 -04:00
|
|
|
}
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
2014-05-02 01:01:44 -04:00
|
|
|
|
2016-12-26 16:23:20 -05:00
|
|
|
// FormatSanitize returns the canonical format name for a given kind.
|
|
|
|
//
|
2016-03-14 12:52:11 -04:00
|
|
|
// TODO(bep) move to helpers
|
2014-05-08 18:30:11 -04:00
|
|
|
func FormatSanitize(kind string) string {
|
|
|
|
switch strings.ToLower(kind) {
|
|
|
|
case "yaml", "yml":
|
|
|
|
return "yaml"
|
|
|
|
case "toml", "tml":
|
|
|
|
return "toml"
|
|
|
|
case "json", "js":
|
|
|
|
return "json"
|
2016-12-26 16:23:20 -05:00
|
|
|
case "org":
|
|
|
|
return kind
|
2014-05-08 18:30:11 -04:00
|
|
|
default:
|
|
|
|
return "toml"
|
|
|
|
}
|
2014-05-02 01:01:44 -04:00
|
|
|
}
|
|
|
|
|
2016-07-13 17:53:06 -04:00
|
|
|
// DetectFrontMatter detects the type of frontmatter analysing its first character.
|
2016-12-26 16:23:20 -05:00
|
|
|
func DetectFrontMatter(mark rune) (f *FrontmatterType) {
|
2014-05-01 13:19:51 -04:00
|
|
|
switch mark {
|
|
|
|
case '-':
|
2016-12-26 16:23:20 -05:00
|
|
|
return &FrontmatterType{HandleYAMLMetaData, []byte(YAMLDelim), []byte(YAMLDelim), false}
|
2014-05-01 13:19:51 -04:00
|
|
|
case '+':
|
2016-12-26 16:23:20 -05:00
|
|
|
return &FrontmatterType{HandleTOMLMetaData, []byte(TOMLDelim), []byte(TOMLDelim), false}
|
2014-05-01 13:19:51 -04:00
|
|
|
case '{':
|
2016-12-26 16:23:20 -05:00
|
|
|
return &FrontmatterType{HandleJSONMetaData, []byte{'{'}, []byte{'}'}, true}
|
2017-02-21 02:46:03 -05:00
|
|
|
case '#':
|
2016-12-26 16:23:20 -05:00
|
|
|
return &FrontmatterType{HandleOrgMetaData, []byte("#+"), []byte("\n"), false}
|
2014-05-01 13:19:51 -04:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 16:23:20 -05:00
|
|
|
// HandleTOMLMetaData unmarshals TOML-encoded datum and returns a Go interface
|
|
|
|
// representing the encoded data structure.
|
2018-01-25 22:54:15 -05:00
|
|
|
func HandleTOMLMetaData(datum []byte) (map[string]interface{}, error) {
|
2014-05-01 13:19:51 -04:00
|
|
|
m := map[string]interface{}{}
|
2015-03-11 13:34:57 -04:00
|
|
|
datum = removeTOMLIdentifier(datum)
|
2016-08-20 15:28:38 -04:00
|
|
|
|
2017-05-31 06:10:46 -04:00
|
|
|
_, err := toml.Decode(string(datum), &m)
|
2016-08-20 15:28:38 -04:00
|
|
|
|
2017-05-31 06:10:46 -04:00
|
|
|
return m, err
|
2016-08-20 15:28:38 -04:00
|
|
|
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
|
|
|
|
2016-12-26 16:23:20 -05:00
|
|
|
// removeTOMLIdentifier removes, if necessary, beginning and ending TOML
|
|
|
|
// frontmatter delimiters from a byte slice.
|
2015-03-11 13:34:57 -04:00
|
|
|
func removeTOMLIdentifier(datum []byte) []byte {
|
2016-12-26 16:23:20 -05:00
|
|
|
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))
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
|
|
|
|
2016-12-26 16:23:20 -05:00
|
|
|
// HandleYAMLMetaData unmarshals YAML-encoded datum and returns a Go interface
|
|
|
|
// representing the encoded data structure.
|
2018-01-25 22:54:15 -05:00
|
|
|
func HandleYAMLMetaData(datum []byte) (map[string]interface{}, error) {
|
2014-05-01 13:19:51 -04:00
|
|
|
m := map[string]interface{}{}
|
2016-08-30 13:29:48 -04:00
|
|
|
err := yaml.Unmarshal(datum, &m)
|
2017-12-01 14:37:19 -05:00
|
|
|
|
|
|
|
// To support boolean keys, the `yaml` package unmarshals maps to
|
|
|
|
// map[interface{}]interface{}. Here we recurse through the result
|
|
|
|
// and change all maps to map[string]interface{} like we would've
|
|
|
|
// gotten from `json`.
|
|
|
|
if err == nil {
|
|
|
|
for k, v := range m {
|
2018-02-12 11:39:11 -05:00
|
|
|
if vv, changed := stringifyMapKeys(v); changed {
|
|
|
|
m[k] = vv
|
|
|
|
}
|
2017-12-01 14:37:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 13:29:48 -04:00
|
|
|
return m, err
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
|
|
|
|
2018-02-11 19:10:49 -05:00
|
|
|
// HandleYAMLData unmarshals YAML-encoded datum and returns a Go interface
|
|
|
|
// representing the encoded data structure.
|
|
|
|
func HandleYAMLData(datum []byte) (interface{}, error) {
|
|
|
|
var m interface{}
|
|
|
|
err := yaml.Unmarshal(datum, &m)
|
2018-02-12 11:39:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-11 19:10:49 -05:00
|
|
|
|
|
|
|
// To support boolean keys, the `yaml` package unmarshals maps to
|
|
|
|
// map[interface{}]interface{}. Here we recurse through the result
|
|
|
|
// and change all maps to map[string]interface{} like we would've
|
|
|
|
// gotten from `json`.
|
2018-02-12 11:39:11 -05:00
|
|
|
if mm, changed := stringifyMapKeys(m); changed {
|
|
|
|
return mm, nil
|
2018-02-11 19:10:49 -05:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:39:11 -05:00
|
|
|
return m, nil
|
2018-02-11 19:10:49 -05:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:32:42 -05:00
|
|
|
// stringifyMapKeys recurses into in and changes all instances of
|
2017-12-01 14:37:19 -05:00
|
|
|
// map[interface{}]interface{} to map[string]interface{}. This is useful to
|
|
|
|
// work around the impedence mismatch between JSON and YAML unmarshaling that's
|
|
|
|
// described here: https://github.com/go-yaml/yaml/issues/139
|
|
|
|
//
|
|
|
|
// Inspired by https://github.com/stripe/stripe-mock, MIT licensed
|
2018-02-12 11:39:11 -05:00
|
|
|
func stringifyMapKeys(in interface{}) (interface{}, bool) {
|
2017-12-01 14:37:19 -05:00
|
|
|
switch in := in.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
for i, v := range in {
|
2018-02-12 11:39:11 -05:00
|
|
|
if vv, replaced := stringifyMapKeys(v); replaced {
|
|
|
|
in[i] = vv
|
|
|
|
}
|
2017-12-01 14:37:19 -05:00
|
|
|
}
|
|
|
|
case map[interface{}]interface{}:
|
|
|
|
res := make(map[string]interface{})
|
|
|
|
for k, v := range in {
|
2018-02-12 11:39:11 -05:00
|
|
|
ks, err := cast.ToStringE(k)
|
|
|
|
if err != nil {
|
|
|
|
ks = fmt.Sprintf("%v", k)
|
|
|
|
}
|
|
|
|
if vv, replaced := stringifyMapKeys(v); replaced {
|
|
|
|
res[ks] = vv
|
|
|
|
} else {
|
|
|
|
res[ks] = v
|
|
|
|
}
|
2017-12-01 14:37:19 -05:00
|
|
|
}
|
2018-02-12 11:39:11 -05:00
|
|
|
return res, true
|
2017-12-01 14:37:19 -05:00
|
|
|
}
|
2018-02-12 11:39:11 -05:00
|
|
|
|
|
|
|
return nil, false
|
2017-12-01 14:37:19 -05:00
|
|
|
}
|
|
|
|
|
2016-12-26 16:23:20 -05:00
|
|
|
// HandleJSONMetaData unmarshals JSON-encoded datum and returns a Go interface
|
|
|
|
// representing the encoded data structure.
|
2018-01-25 22:54:15 -05:00
|
|
|
func HandleJSONMetaData(datum []byte) (map[string]interface{}, error) {
|
2018-02-02 01:35:26 -05:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
|
|
|
|
if datum == nil {
|
|
|
|
// Package json returns on error on nil input.
|
|
|
|
// Return an empty map to be consistent with our other supported
|
|
|
|
// formats.
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(datum, &m)
|
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleJSONData unmarshals JSON-encoded datum and returns a Go interface
|
|
|
|
// representing the encoded data structure.
|
|
|
|
func HandleJSONData(datum []byte) (interface{}, error) {
|
2016-12-26 16:23:20 -05:00
|
|
|
if datum == nil {
|
|
|
|
// Package json returns on error on nil input.
|
|
|
|
// Return an empty map to be consistent with our other supported
|
|
|
|
// formats.
|
|
|
|
return make(map[string]interface{}), nil
|
|
|
|
}
|
|
|
|
|
2018-02-02 01:35:26 -05:00
|
|
|
var f interface{}
|
2016-08-30 13:29:48 -04:00
|
|
|
err := json.Unmarshal(datum, &f)
|
|
|
|
return f, err
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
2017-02-21 02:46:03 -05:00
|
|
|
|
2016-12-26 16:23:20 -05:00
|
|
|
// HandleOrgMetaData unmarshals org-mode encoded datum and returns a Go
|
|
|
|
// interface representing the encoded data structure.
|
2018-01-25 22:54:15 -05:00
|
|
|
func HandleOrgMetaData(datum []byte) (map[string]interface{}, error) {
|
2017-02-21 02:46:03 -05:00
|
|
|
return goorgeous.OrgHeaders(datum)
|
|
|
|
}
|