1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-23 08:01:18 +01:00
gojekyll/pages/frontmatter.go
2017-06-22 11:02:32 -04:00

34 lines
1007 B
Go

package pages
import (
"bytes"
"regexp"
"github.com/osteele/gojekyll/templates"
yaml "gopkg.in/yaml.v2"
)
// ReadFrontMatter reads the front matter from a document.
func ReadFrontMatter(sourcePtr *[]byte) (frontMatter templates.VariableMap, err error) {
var (
source = *sourcePtr
start = 0
)
// Replace Windows linefeeds. This allows the following regular expressions to work.
source = bytes.Replace(source, []byte("\r\n"), []byte("\n"), -1)
if match := frontMatterMatcher.FindSubmatchIndex(source); match != nil {
start = match[1]
if err = yaml.Unmarshal(source[match[2]:match[3]], &frontMatter); err != nil {
return
}
} else if match := emptyFontMatterMatcher.FindSubmatchIndex(source); match != nil {
start = match[1]
}
// This fixes the line numbers, so that template errors show with the correct line.
// TODO find a less hack-ey solution
*sourcePtr = append(
regexp.MustCompile(`[^\n\r]+`).ReplaceAllLiteral(source[:start], []byte{}),
source[start:]...)
return
}