aboutsummaryrefslogtreecommitdiffhomepage
path: root/parser
diff options
context:
space:
mode:
Diffstat (limited to 'parser')
-rw-r--r--parser/metadecoders/format.go18
-rw-r--r--parser/metadecoders/format_test.go18
-rw-r--r--parser/pageparser/pageparser.go56
-rw-r--r--parser/pageparser/pageparser_test.go19
4 files changed, 75 insertions, 36 deletions
diff --git a/parser/metadecoders/format.go b/parser/metadecoders/format.go
index 4f81528c3..9e9cc2e1f 100644
--- a/parser/metadecoders/format.go
+++ b/parser/metadecoders/format.go
@@ -18,8 +18,6 @@ import (
"strings"
"github.com/gohugoio/hugo/media"
-
- "github.com/gohugoio/hugo/parser/pageparser"
)
type Format string
@@ -72,22 +70,6 @@ func FormatFromMediaType(m media.Type) Format {
return ""
}
-// FormatFromFrontMatterType will return empty if not supported.
-func FormatFromFrontMatterType(typ pageparser.ItemType) Format {
- switch typ {
- case pageparser.TypeFrontMatterJSON:
- return JSON
- case pageparser.TypeFrontMatterORG:
- return ORG
- case pageparser.TypeFrontMatterTOML:
- return TOML
- case pageparser.TypeFrontMatterYAML:
- return YAML
- default:
- return ""
- }
-}
-
// FormatFromContentString tries to detect the format (JSON, YAML or TOML)
// in the given string.
// It return an empty string if no format could be detected.
diff --git a/parser/metadecoders/format_test.go b/parser/metadecoders/format_test.go
index 74d105010..2f625935e 100644
--- a/parser/metadecoders/format_test.go
+++ b/parser/metadecoders/format_test.go
@@ -18,8 +18,6 @@ import (
"github.com/gohugoio/hugo/media"
- "github.com/gohugoio/hugo/parser/pageparser"
-
qt "github.com/frankban/quicktest"
)
@@ -57,22 +55,6 @@ func TestFormatFromMediaType(t *testing.T) {
}
}
-func TestFormatFromFrontMatterType(t *testing.T) {
- c := qt.New(t)
- for _, test := range []struct {
- typ pageparser.ItemType
- expect Format
- }{
- {pageparser.TypeFrontMatterJSON, JSON},
- {pageparser.TypeFrontMatterTOML, TOML},
- {pageparser.TypeFrontMatterYAML, YAML},
- {pageparser.TypeFrontMatterORG, ORG},
- {pageparser.TypeIgnore, ""},
- } {
- c.Assert(FormatFromFrontMatterType(test.typ), qt.Equals, test.expect)
- }
-}
-
func TestFormatFromContentString(t *testing.T) {
t.Parallel()
c := qt.New(t)
diff --git a/parser/pageparser/pageparser.go b/parser/pageparser/pageparser.go
index acdb09587..f73eee706 100644
--- a/parser/pageparser/pageparser.go
+++ b/parser/pageparser/pageparser.go
@@ -22,6 +22,7 @@ import (
"io"
"io/ioutil"
+ "github.com/gohugoio/hugo/parser/metadecoders"
"github.com/pkg/errors"
)
@@ -43,6 +44,61 @@ func Parse(r io.Reader, cfg Config) (Result, error) {
return parseSection(r, cfg, lexIntroSection)
}
+type ContentFrontMatter struct {
+ Content []byte
+ FrontMatter map[string]interface{}
+ FrontMatterFormat metadecoders.Format
+}
+
+// ParseFrontMatterAndContent is a convenience method to extract front matter
+// and content from a content page.
+func ParseFrontMatterAndContent(r io.Reader) (ContentFrontMatter, error) {
+ var cf ContentFrontMatter
+
+ psr, err := Parse(r, Config{})
+ if err != nil {
+ return cf, err
+ }
+
+ var frontMatterSource []byte
+
+ iter := psr.Iterator()
+
+ walkFn := func(item Item) bool {
+ if frontMatterSource != nil {
+ // The rest is content.
+ cf.Content = psr.Input()[item.Pos:]
+ // Done
+ return false
+ } else if item.IsFrontMatter() {
+ cf.FrontMatterFormat = FormatFromFrontMatterType(item.Type)
+ frontMatterSource = item.Val
+ }
+ return true
+
+ }
+
+ iter.PeekWalk(walkFn)
+
+ cf.FrontMatter, err = metadecoders.Default.UnmarshalToMap(frontMatterSource, cf.FrontMatterFormat)
+ return cf, err
+}
+
+func FormatFromFrontMatterType(typ ItemType) metadecoders.Format {
+ switch typ {
+ case TypeFrontMatterJSON:
+ return metadecoders.JSON
+ case TypeFrontMatterORG:
+ return metadecoders.ORG
+ case TypeFrontMatterTOML:
+ return metadecoders.TOML
+ case TypeFrontMatterYAML:
+ return metadecoders.YAML
+ default:
+ return ""
+ }
+}
+
// ParseMain parses starting with the main section. Used in tests.
func ParseMain(r io.Reader, cfg Config) (Result, error) {
return parseSection(r, cfg, lexMainSection)
diff --git a/parser/pageparser/pageparser_test.go b/parser/pageparser/pageparser_test.go
index f54376c33..f7f719938 100644
--- a/parser/pageparser/pageparser_test.go
+++ b/parser/pageparser/pageparser_test.go
@@ -16,6 +16,9 @@ package pageparser
import (
"strings"
"testing"
+
+ qt "github.com/frankban/quicktest"
+ "github.com/gohugoio/hugo/parser/metadecoders"
)
func BenchmarkParse(b *testing.B) {
@@ -69,3 +72,19 @@ This is some summary. This is some summary. This is some summary. This is some s
}
}
}
+
+func TestFormatFromFrontMatterType(t *testing.T) {
+ c := qt.New(t)
+ for _, test := range []struct {
+ typ ItemType
+ expect metadecoders.Format
+ }{
+ {TypeFrontMatterJSON, metadecoders.JSON},
+ {TypeFrontMatterTOML, metadecoders.TOML},
+ {TypeFrontMatterYAML, metadecoders.YAML},
+ {TypeFrontMatterORG, metadecoders.ORG},
+ {TypeIgnore, ""},
+ } {
+ c.Assert(FormatFromFrontMatterType(test.typ), qt.Equals, test.expect)
+ }
+}