Add support for native Org dates in frontmatter
authorSebastian Boehm <sebastian@sometimesfood.org>
Fri, 26 Jun 2020 21:52:12 +0000 (23:52 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 30 Jun 2020 08:30:55 +0000 (10:30 +0200)
parser/metadecoders/decoder.go
parser/metadecoders/decoder_test.go

index f90dc57035172c6e071a095789a9db21453805ea..1a4a57076ba18e461014e175f7097a61d1d5aca1 100644 (file)
@@ -18,6 +18,7 @@ import (
        "encoding/csv"
        "encoding/json"
        "fmt"
+       "regexp"
        "strings"
 
        "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 {
        config := org.New()
        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" {
                        jww.WARN.Printf("Please use '#+%s[]:' notation, automatic conversion is deprecated.", k)
                        frontMatter[k] = strings.Fields(v)
+               } else if k == "date" {
+                       frontMatter[k] = parseORGDate(v)
                } else {
                        frontMatter[k] = v
                }
index 3cb2e6365363615dff80f6d19497c5dc826171dc..d11d578ba04b5b3a78ca7066e4425e579719bc7a 100644 (file)
@@ -69,6 +69,7 @@ func TestUnmarshalToInterface(t *testing.T) {
                {`[ "Brecker", "Blake", "Redman" ]`, JSON, []interface{}{"Brecker", "Blake", "Redman"}},
                {`{ "a": "b" }`, JSON, expect},
                {`#+a: b`, ORG, expect},
+               {`#+DATE: <2020-06-26 Fri>`, ORG, map[string]interface{}{"date": "2020-06-26"}},
                {`a = "b"`, TOML, expect},
                {`a: "b"`, YAML, expect},
                {`a,b,c`, CSV, [][]string{{"a", "b", "c"}}},