Add support for native Org dates in frontmatter

This commit is contained in:
Sebastian Boehm 2020-06-26 23:52:12 +02:00 committed by Bjørn Erik Pedersen
parent 127d5feb32
commit c66dc6c74f
2 changed files with 12 additions and 0 deletions

View file

@ -18,6 +18,7 @@ import (
"encoding/csv" "encoding/csv"
"encoding/json" "encoding/json"
"fmt" "fmt"
"regexp"
"strings" "strings"
"github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/common/herrors"
@ -203,6 +204,14 @@ func (d Decoder) unmarshalCSV(data []byte, v interface{}) error {
} }
func parseORGDate(s string) string {
r := regexp.MustCompile(`[<\[](\d{4}-\d{2}-\d{2}) .*[>\]]`)
if m := r.FindStringSubmatch(s); m != nil {
return m[1]
}
return s
}
func (d Decoder) unmarshalORG(data []byte, v interface{}) error { func (d Decoder) unmarshalORG(data []byte, v interface{}) error {
config := org.New() config := org.New()
config.Log = jww.WARN config.Log = jww.WARN
@ -218,6 +227,8 @@ func (d Decoder) unmarshalORG(data []byte, v interface{}) error {
} else if k == "tags" || k == "categories" || k == "aliases" { } else if k == "tags" || k == "categories" || k == "aliases" {
jww.WARN.Printf("Please use '#+%s[]:' notation, automatic conversion is deprecated.", k) jww.WARN.Printf("Please use '#+%s[]:' notation, automatic conversion is deprecated.", k)
frontMatter[k] = strings.Fields(v) frontMatter[k] = strings.Fields(v)
} else if k == "date" {
frontMatter[k] = parseORGDate(v)
} else { } else {
frontMatter[k] = v frontMatter[k] = v
} }

View file

@ -69,6 +69,7 @@ func TestUnmarshalToInterface(t *testing.T) {
{`[ "Brecker", "Blake", "Redman" ]`, JSON, []interface{}{"Brecker", "Blake", "Redman"}}, {`[ "Brecker", "Blake", "Redman" ]`, JSON, []interface{}{"Brecker", "Blake", "Redman"}},
{`{ "a": "b" }`, JSON, expect}, {`{ "a": "b" }`, JSON, expect},
{`#+a: b`, ORG, expect}, {`#+a: b`, ORG, expect},
{`#+DATE: <2020-06-26 Fri>`, ORG, map[string]interface{}{"date": "2020-06-26"}},
{`a = "b"`, TOML, expect}, {`a = "b"`, TOML, expect},
{`a: "b"`, YAML, expect}, {`a: "b"`, YAML, expect},
{`a,b,c`, CSV, [][]string{{"a", "b", "c"}}}, {`a,b,c`, CSV, [][]string{{"a", "b", "c"}}},