mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-19 11:43:04 +00:00
parser: Spring code cleaning
This commit is contained in:
parent
937592cb85
commit
1cb7ed6ac7
4 changed files with 71 additions and 60 deletions
|
@ -45,7 +45,7 @@ var toJSONCmd = &cobra.Command{
|
||||||
Long: `toJSON converts all front matter in the content directory
|
Long: `toJSON converts all front matter in the content directory
|
||||||
to use JSON for the front matter.`,
|
to use JSON for the front matter.`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return convertContents(rune([]byte(parser.JSON_LEAD)[0]))
|
return convertContents(rune([]byte(parser.JSONLead)[0]))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ var toTOMLCmd = &cobra.Command{
|
||||||
Long: `toTOML converts all front matter in the content directory
|
Long: `toTOML converts all front matter in the content directory
|
||||||
to use TOML for the front matter.`,
|
to use TOML for the front matter.`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return convertContents(rune([]byte(parser.TOML_LEAD)[0]))
|
return convertContents(rune([]byte(parser.TOMLLead)[0]))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ var toYAMLCmd = &cobra.Command{
|
||||||
Long: `toYAML converts all front matter in the content directory
|
Long: `toYAML converts all front matter in the content directory
|
||||||
to use YAML for the front matter.`,
|
to use YAML for the front matter.`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return convertContents(rune([]byte(parser.YAML_LEAD)[0]))
|
return convertContents(rune([]byte(parser.YAMLLead)[0]))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -121,15 +121,15 @@ L:
|
||||||
|
|
||||||
// get the front matter as bytes and split it into lines
|
// get the front matter as bytes and split it into lines
|
||||||
var lineEnding []byte
|
var lineEnding []byte
|
||||||
fmLines := bytes.Split(fm, parser.UnixEnding)
|
fmLines := bytes.Split(fm, []byte("\n"))
|
||||||
if len(fmLines) == 1 { // if the result is only 1 element, try to split on dos line endings
|
if len(fmLines) == 1 { // if the result is only 1 element, try to split on dos line endings
|
||||||
fmLines = bytes.Split(fm, parser.DosEnding)
|
fmLines = bytes.Split(fm, []byte("\r\n"))
|
||||||
if len(fmLines) == 1 {
|
if len(fmLines) == 1 {
|
||||||
return buff, fmt.Errorf("unable to split FrontMatter into lines")
|
return buff, fmt.Errorf("unable to split FrontMatter into lines")
|
||||||
}
|
}
|
||||||
lineEnding = append(lineEnding, parser.DosEnding...)
|
lineEnding = append(lineEnding, []byte("\r\n")...)
|
||||||
} else {
|
} else {
|
||||||
lineEnding = append(lineEnding, parser.UnixEnding...)
|
lineEnding = append(lineEnding, []byte("\n")...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the front matter lines to the buffer, replacing as necessary
|
// Write the front matter lines to the buffer, replacing as necessary
|
||||||
|
|
|
@ -37,7 +37,7 @@ func InterfaceToConfig(in interface{}, mark rune) ([]byte, error) {
|
||||||
b := new(bytes.Buffer)
|
b := new(bytes.Buffer)
|
||||||
|
|
||||||
switch mark {
|
switch mark {
|
||||||
case rune(YAML_LEAD[0]):
|
case rune(YAMLLead[0]):
|
||||||
by, err := yaml.Marshal(in)
|
by, err := yaml.Marshal(in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -48,13 +48,13 @@ func InterfaceToConfig(in interface{}, mark rune) ([]byte, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return b.Bytes(), nil
|
return b.Bytes(), nil
|
||||||
case rune(TOML_LEAD[0]):
|
case rune(TOMLLead[0]):
|
||||||
err := toml.NewEncoder(b).Encode(in)
|
err := toml.NewEncoder(b).Encode(in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return b.Bytes(), nil
|
return b.Bytes(), nil
|
||||||
case rune(JSON_LEAD[0]):
|
case rune(JSONLead[0]):
|
||||||
by, err := json.MarshalIndent(in, "", " ")
|
by, err := json.MarshalIndent(in, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -78,8 +78,8 @@ func InterfaceToFrontMatter(in interface{}, mark rune) ([]byte, error) {
|
||||||
b := new(bytes.Buffer)
|
b := new(bytes.Buffer)
|
||||||
|
|
||||||
switch mark {
|
switch mark {
|
||||||
case rune(YAML_LEAD[0]):
|
case rune(YAMLLead[0]):
|
||||||
_, err := b.Write([]byte(YAML_DELIM_UNIX))
|
_, err := b.Write([]byte(YAMLDelimUnix))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -88,13 +88,13 @@ func InterfaceToFrontMatter(in interface{}, mark rune) ([]byte, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
b.Write(by)
|
b.Write(by)
|
||||||
_, err = b.Write([]byte(YAML_DELIM_UNIX))
|
_, err = b.Write([]byte(YAMLDelimUnix))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return b.Bytes(), nil
|
return b.Bytes(), nil
|
||||||
case rune(TOML_LEAD[0]):
|
case rune(TOMLLead[0]):
|
||||||
_, err := b.Write([]byte(TOML_DELIM_UNIX))
|
_, err := b.Write([]byte(TOMLDelimUnix))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -103,12 +103,12 @@ func InterfaceToFrontMatter(in interface{}, mark rune) ([]byte, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
_, err = b.Write([]byte("\n" + TOML_DELIM_UNIX))
|
_, err = b.Write([]byte("\n" + TOMLDelimUnix))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return b.Bytes(), nil
|
return b.Bytes(), nil
|
||||||
case rune(JSON_LEAD[0]):
|
case rune(JSONLead[0]):
|
||||||
by, err := json.MarshalIndent(in, "", " ")
|
by, err := json.MarshalIndent(in, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -127,11 +127,11 @@ func InterfaceToFrontMatter(in interface{}, mark rune) ([]byte, error) {
|
||||||
func FormatToLeadRune(kind string) rune {
|
func FormatToLeadRune(kind string) rune {
|
||||||
switch FormatSanitize(kind) {
|
switch FormatSanitize(kind) {
|
||||||
case "yaml":
|
case "yaml":
|
||||||
return rune([]byte(YAML_LEAD)[0])
|
return rune([]byte(YAMLLead)[0])
|
||||||
case "json":
|
case "json":
|
||||||
return rune([]byte(JSON_LEAD)[0])
|
return rune([]byte(JSONLead)[0])
|
||||||
default:
|
default:
|
||||||
return rune([]byte(TOML_LEAD)[0])
|
return rune([]byte(TOMLLead)[0])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,9 +152,9 @@ func FormatSanitize(kind string) string {
|
||||||
func detectFrontMatter(mark rune) (f *frontmatterType) {
|
func detectFrontMatter(mark rune) (f *frontmatterType) {
|
||||||
switch mark {
|
switch mark {
|
||||||
case '-':
|
case '-':
|
||||||
return &frontmatterType{[]byte(YAML_DELIM), []byte(YAML_DELIM), HandleYAMLMetaData, false}
|
return &frontmatterType{[]byte(YAMLDelim), []byte(YAMLDelim), HandleYAMLMetaData, false}
|
||||||
case '+':
|
case '+':
|
||||||
return &frontmatterType{[]byte(TOML_DELIM), []byte(TOML_DELIM), HandleTOMLMetaData, false}
|
return &frontmatterType{[]byte(TOMLDelim), []byte(TOMLDelim), HandleTOMLMetaData, false}
|
||||||
case '{':
|
case '{':
|
||||||
return &frontmatterType{[]byte{'{'}, []byte{'}'}, HandleJSONMetaData, true}
|
return &frontmatterType{[]byte{'{'}, []byte{'}'}, HandleJSONMetaData, true}
|
||||||
default:
|
default:
|
||||||
|
@ -172,7 +172,7 @@ func HandleTOMLMetaData(datum []byte) (interface{}, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeTOMLIdentifier(datum []byte) []byte {
|
func removeTOMLIdentifier(datum []byte) []byte {
|
||||||
return bytes.Replace(datum, []byte(TOML_DELIM), []byte(""), -1)
|
return bytes.Replace(datum, []byte(TOMLDelim), []byte(""), -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleYAMLMetaData(datum []byte) (interface{}, error) {
|
func HandleYAMLMetaData(datum []byte) (interface{}, error) {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright 2015 The Hugo Authors. All rights reserved.
|
// Copyright 2016n The Hugo Authors. All rights reserved.
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
|
@ -24,50 +24,61 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
HTML_LEAD = "<"
|
// TODO(bep) Do we really have to export these?
|
||||||
YAML_LEAD = "-"
|
|
||||||
YAML_DELIM_UNIX = "---\n"
|
// HTMLLead identifies the start of HTML documents.
|
||||||
YAML_DELIM_DOS = "---\r\n"
|
HTMLLead = "<"
|
||||||
YAML_DELIM = "---"
|
// YAMLLead identifies the start of YAML frontmatter.
|
||||||
TOML_LEAD = "+"
|
YAMLLead = "-"
|
||||||
TOML_DELIM_UNIX = "+++\n"
|
// YAMLDelimUnix identifies the end of YAML front matter on Unix.
|
||||||
TOML_DELIM_DOS = "+++\r\n"
|
YAMLDelimUnix = "---\n"
|
||||||
TOML_DELIM = "+++"
|
// YAMLDelimDOS identifies the end of YAML front matter on Windows.
|
||||||
JSON_LEAD = "{"
|
YAMLDelimDOS = "---\r\n"
|
||||||
HTML_COMMENT_START = "<!--"
|
// YAMLDelim identifies the YAML front matter delimiter.
|
||||||
HTML_COMMENT_END = "-->"
|
YAMLDelim = "---"
|
||||||
|
// TOMLLead identifies the start of TOML front matter.
|
||||||
|
TOMLLead = "+"
|
||||||
|
// TOMLDelimUnix identifies the end of TOML front matter on Unix.
|
||||||
|
TOMLDelimUnix = "+++\n"
|
||||||
|
// TOMLDelimDOS identifies the end of TOML front matter on Windows.
|
||||||
|
TOMLDelimDOS = "+++\r\n"
|
||||||
|
// TOMLDelim identifies the TOML front matter delimiter.
|
||||||
|
TOMLDelim = "+++"
|
||||||
|
// JSONLead identifies the start of JSON frontmatter.
|
||||||
|
JSONLead = "{"
|
||||||
|
// HTMLCommentStart identifies the start of HTML comment.
|
||||||
|
HTMLCommentStart = "<!--"
|
||||||
|
// HTMLCommentEnd identifies the end of HTML comment.
|
||||||
|
HTMLCommentEnd = "-->"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
delims = regexp.MustCompile(
|
delims = regexp.MustCompile(
|
||||||
"^(" + regexp.QuoteMeta(YAML_DELIM) + `\s*\n|` + regexp.QuoteMeta(TOML_DELIM) + `\s*\n|` + regexp.QuoteMeta(JSON_LEAD) + ")",
|
"^(" + regexp.QuoteMeta(YAMLDelim) + `\s*\n|` + regexp.QuoteMeta(TOMLDelim) + `\s*\n|` + regexp.QuoteMeta(JSONLead) + ")",
|
||||||
)
|
)
|
||||||
|
unixEnding = []byte("\n")
|
||||||
UnixEnding = []byte("\n")
|
dosEnding = []byte("\r\n")
|
||||||
DosEnding = []byte("\r\n")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type FrontMatter []byte
|
// Page represents a parsed content page.
|
||||||
type Content []byte
|
|
||||||
|
|
||||||
type Page interface {
|
type Page interface {
|
||||||
FrontMatter() FrontMatter
|
FrontMatter() []byte
|
||||||
Content() Content
|
Content() []byte
|
||||||
IsRenderable() bool
|
IsRenderable() bool
|
||||||
Metadata() (interface{}, error)
|
Metadata() (interface{}, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type page struct {
|
type page struct {
|
||||||
render bool
|
render bool
|
||||||
frontmatter FrontMatter
|
frontmatter []byte
|
||||||
content Content
|
content []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *page) Content() Content {
|
func (p *page) Content() []byte {
|
||||||
return p.content
|
return p.content
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *page) FrontMatter() FrontMatter {
|
func (p *page) FrontMatter() []byte {
|
||||||
return p.frontmatter
|
return p.frontmatter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,14 +157,14 @@ func chompFrontmatterStartComment(r *bufio.Reader) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
str := string(candidate)
|
str := string(candidate)
|
||||||
if strings.HasPrefix(str, HTML_COMMENT_START) {
|
if strings.HasPrefix(str, HTMLCommentStart) {
|
||||||
lineEnd := strings.IndexAny(str, "\n")
|
lineEnd := strings.IndexAny(str, "\n")
|
||||||
if lineEnd == -1 {
|
if lineEnd == -1 {
|
||||||
//TODO: if we can't find it, Peek more?
|
//TODO: if we can't find it, Peek more?
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
testStr := strings.TrimSuffix(str[0:lineEnd], "\r")
|
testStr := strings.TrimSuffix(str[0:lineEnd], "\r")
|
||||||
if strings.Index(testStr, HTML_COMMENT_END) != -1 {
|
if strings.Index(testStr, HTMLCommentEnd) != -1 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
buf := make([]byte, lineEnd)
|
buf := make([]byte, lineEnd)
|
||||||
|
@ -180,12 +191,12 @@ func chompFrontmatterEndComment(r *bufio.Reader) (err error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
testStr := strings.TrimSuffix(str[0:lineEnd], "\r")
|
testStr := strings.TrimSuffix(str[0:lineEnd], "\r")
|
||||||
if strings.Index(testStr, HTML_COMMENT_START) != -1 {
|
if strings.Index(testStr, HTMLCommentStart) != -1 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: if we can't find it, Peek more?
|
//TODO: if we can't find it, Peek more?
|
||||||
if strings.HasSuffix(testStr, HTML_COMMENT_END) {
|
if strings.HasSuffix(testStr, HTMLCommentEnd) {
|
||||||
buf := make([]byte, lineEnd)
|
buf := make([]byte, lineEnd)
|
||||||
if _, err = r.Read(buf); err != nil {
|
if _, err = r.Read(buf); err != nil {
|
||||||
return
|
return
|
||||||
|
@ -216,7 +227,7 @@ func shouldRender(lead []byte) (frontmatter bool) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytes.Equal(lead[:1], []byte(HTML_LEAD)) {
|
if bytes.Equal(lead[:1], []byte(HTMLLead)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
@ -231,16 +242,16 @@ func determineDelims(firstLine []byte) (left, right []byte) {
|
||||||
case 5:
|
case 5:
|
||||||
fallthrough
|
fallthrough
|
||||||
case 4:
|
case 4:
|
||||||
if firstLine[0] == YAML_LEAD[0] {
|
if firstLine[0] == YAMLLead[0] {
|
||||||
return []byte(YAML_DELIM), []byte(YAML_DELIM)
|
return []byte(YAMLDelim), []byte(YAMLDelim)
|
||||||
}
|
}
|
||||||
return []byte(TOML_DELIM), []byte(TOML_DELIM)
|
return []byte(TOMLDelim), []byte(TOMLDelim)
|
||||||
case 3:
|
case 3:
|
||||||
fallthrough
|
fallthrough
|
||||||
case 2:
|
case 2:
|
||||||
fallthrough
|
fallthrough
|
||||||
case 1:
|
case 1:
|
||||||
return []byte(JSON_LEAD), []byte("}")
|
return []byte(JSONLead), []byte("}")
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("Unable to determine delims from %q", firstLine))
|
panic(fmt.Sprintf("Unable to determine delims from %q", firstLine))
|
||||||
}
|
}
|
||||||
|
@ -249,7 +260,7 @@ func determineDelims(firstLine []byte) (left, right []byte) {
|
||||||
// extractFrontMatterDelims takes a frontmatter from the content bufio.Reader.
|
// extractFrontMatterDelims takes a frontmatter from the content bufio.Reader.
|
||||||
// Beginning white spaces of the bufio.Reader must be trimmed before call this
|
// Beginning white spaces of the bufio.Reader must be trimmed before call this
|
||||||
// function.
|
// function.
|
||||||
func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm FrontMatter, err error) {
|
func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm []byte, err error) {
|
||||||
var (
|
var (
|
||||||
c byte
|
c byte
|
||||||
buf bytes.Buffer
|
buf bytes.Buffer
|
||||||
|
@ -344,7 +355,7 @@ func extractFrontMatterDelims(r *bufio.Reader, left, right []byte) (fm FrontMatt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractContent(r io.Reader) (content Content, err error) {
|
func extractContent(r io.Reader) (content []byte, err error) {
|
||||||
wr := new(bytes.Buffer)
|
wr := new(bytes.Buffer)
|
||||||
if _, err = wr.ReadFrom(r); err != nil {
|
if _, err = wr.ReadFrom(r); err != nil {
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue