1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-03 12:37:46 +01:00
gojekyll/frontmatter/read.go

51 lines
1.4 KiB
Go
Raw Normal View History

package frontmatter
2017-06-22 17:02:32 +02:00
import (
"bytes"
"regexp"
"github.com/osteele/gojekyll/utils"
2017-06-22 17:02:32 +02:00
yaml "gopkg.in/yaml.v2"
)
// The first four bytes of a file with front matter.
const fmMagic = "---\n"
var frontMatterMatcher = regexp.MustCompile(`(?s)^---\n(.+?\n)---\n+`)
var emptyFontMatterMatcher = regexp.MustCompile(`(?s)^---\n+---\n+`)
// FileHasFrontMatter returns a bool indicating whether the
// file looks like it has frontmatter.
func FileHasFrontMatter(filename string) (bool, error) {
magic, err := utils.ReadFileMagic(filename)
if err != nil {
return false, err
}
return string(magic) == fmMagic, nil
}
2017-06-24 20:00:19 +02:00
// 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) (fm FrontMatter, 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]], &fm); err != nil {
2017-06-22 17:02:32 +02:00
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
}