1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-04 18:07:51 +01:00
gojekyll/frontmatter/read.go

40 lines
1.1 KiB
Go
Raw Normal View History

package frontmatter
2017-06-22 17:02:32 +02:00
import (
"bytes"
"regexp"
"github.com/osteele/gojekyll/templates"
2017-06-22 17:02:32 +02:00
yaml "gopkg.in/yaml.v2"
)
2017-06-24 20:00:19 +02:00
var (
frontMatterMatcher = regexp.MustCompile(`(?s)^---\n(.+?\n)---\n`)
emptyFontMatterMatcher = regexp.MustCompile(`(?s)^---\n+---\n`)
)
// Read reads the frontmatter from a document. It modifies srcPtr to point to the
// content after the frontmatter, and sets firstLine to its 1-indexed line number.
func Read(sourcePtr *[]byte, firstLine *int) (frontMatter templates.VariableMap, err error) {
2017-06-22 17:02:32 +02:00
var (
source = *sourcePtr
start = 0
)
// Replace Windows line feeds. This allows the following regular expressions to work.
2017-06-22 17:02:32 +02:00
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]
}
if firstLine != nil {
*firstLine = 1 + bytes.Count(source[:start], []byte("\n"))
}
*sourcePtr = source[start:]
2017-06-22 17:02:32 +02:00
return
}