1
0
mirror of https://github.com/gohugoio/hugo.git synced 2024-05-11 05:54:58 +00:00

parser: Refactor frontmatter parser and add tests

Lots of cleanups here:

- Refactor InterfaceToConfig and InterfaceToFrontMatter to use io.Writer.
- Simplify InterfaceToFrontMatter by wrapping InterfaceToConfig.
- Export FrontmatterType since we return it in DetectFrontMatter.
- Refactor removeTOMLIdentifier to avoid blindly replacing "+++".
- Update HandleJSONMetaData to return an empty map on nil input.
- Updates vendored goorgeous package and test for org-mode frontmatter.
- Add tests and godoc comments.

Coverage for parser package increased from 45.2% to 85.2%.
This commit is contained in:
Cameron Moore
2016-12-26 15:23:20 -06:00
committed by Bjørn Erik Pedersen
parent ddc8cc0082
commit f039e3be9e
9 changed files with 552 additions and 91 deletions

View File

@@ -64,30 +64,42 @@ var (
// Page represents a parsed content page.
type Page interface {
// FrontMatter contains the raw frontmatter with relevant delimiters.
FrontMatter() []byte
// Content contains the raw page content.
Content() []byte
// IsRenderable denotes that the page should be rendered.
IsRenderable() bool
// Metadata returns the unmarshalled frontmatter data.
Metadata() (interface{}, error)
}
// page implements the Page interface.
type page struct {
render bool
frontmatter []byte
content []byte
}
// Content returns the raw page content.
func (p *page) Content() []byte {
return p.content
}
// FrontMatter contains the raw frontmatter with relevant delimiters.
func (p *page) FrontMatter() []byte {
return p.frontmatter
}
// IsRenderable denotes that the page should be rendered.
func (p *page) IsRenderable() bool {
return p.render
}
// Metadata returns the unmarshalled frontmatter data.
func (p *page) Metadata() (meta interface{}, err error) {
frontmatter := p.FrontMatter()
@@ -151,6 +163,7 @@ func ReadFrom(r io.Reader) (p Page, err error) {
return newp, nil
}
// chompBOM scans any leading Unicode Byte Order Markers from r.
func chompBOM(r io.RuneScanner) (err error) {
for {
c, _, err := r.ReadRune()
@@ -164,6 +177,7 @@ func chompBOM(r io.RuneScanner) (err error) {
}
}
// chompWhitespace scans any leading Unicode whitespace from r.
func chompWhitespace(r io.RuneScanner) (err error) {
for {
c, _, err := r.ReadRune()
@@ -177,6 +191,9 @@ func chompWhitespace(r io.RuneScanner) (err error) {
}
}
// chompFrontmatterStartComment checks r for a leading HTML comment. If a
// comment is found, it is read from r and then whitespace is trimmed from the
// beginning of r.
func chompFrontmatterStartComment(r *bufio.Reader) (err error) {
candidate, err := r.Peek(32)
if err != nil {
@@ -206,6 +223,7 @@ func chompFrontmatterStartComment(r *bufio.Reader) (err error) {
return nil
}
// chompFrontmatterEndComment checks r for a trailing HTML comment.
func chompFrontmatterEndComment(r *bufio.Reader) (err error) {
candidate, err := r.Peek(32)
if err != nil {