summaryrefslogtreecommitdiffstats
path: root/parser
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-03-23 14:51:16 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-03-23 14:51:16 +0100
commit1cb7ed6ac7fd808b7755a47ffc4878506a42d13c (patch)
tree42e3e2e242acc256e8875375c926ee6ee2d2c805 /parser
parent937592cb85f6b4bf4603230050ac0afdcc93058b (diff)
parser: Spring code cleaning
Diffstat (limited to 'parser')
-rw-r--r--parser/frontmatter.go32
-rw-r--r--parser/page.go85
2 files changed, 64 insertions, 53 deletions
diff --git a/parser/frontmatter.go b/parser/frontmatter.go
index cbeb10be3..d12a7b6d6 100644
--- a/parser/frontmatter.go
+++ b/parser/frontmatter.go
@@ -37,7 +37,7 @@ func InterfaceToConfig(in interface{}, mark rune) ([]byte, error) {
b := new(bytes.Buffer)
switch mark {
- case rune(YAML_LEAD[0]):
+ case rune(YAMLLead[0]):
by, err := yaml.Marshal(in)
if err != nil {
return nil, err
@@ -48,13 +48,13 @@ func InterfaceToConfig(in interface{}, mark rune) ([]byte, error) {
return nil, err
}
return b.Bytes(), nil
- case rune(TOML_LEAD[0]):
+ case rune(TOMLLead[0]):
err := toml.NewEncoder(b).Encode(in)
if err != nil {
return nil, err
}
return b.Bytes(), nil
- case rune(JSON_LEAD[0]):
+ case rune(JSONLead[0]):
by, err := json.MarshalIndent(in, "", " ")
if err != nil {
return nil, err
@@ -78,8 +78,8 @@ func InterfaceToFrontMatter(in interface{}, mark rune) ([]byte, error) {
b := new(bytes.Buffer)
switch mark {
- case rune(YAML_LEAD[0]):
- _, err := b.Write([]byte(YAML_DELIM_UNIX))
+ case rune(YAMLLead[0]):
+ _, err := b.Write([]byte(YAMLDelimUnix))
if err != nil {
return nil, err
}
@@ -88,13 +88,13 @@ func InterfaceToFrontMatter(in interface{}, mark rune) ([]byte, error) {
return nil, err
}
b.Write(by)
- _, err = b.Write([]byte(YAML_DELIM_UNIX))
+ _, err = b.Write([]byte(YAMLDelimUnix))
if err != nil {
return nil, err
}
return b.Bytes(), nil
- case rune(TOML_LEAD[0]):
- _, err := b.Write([]byte(TOML_DELIM_UNIX))
+ case rune(TOMLLead[0]):
+ _, err := b.Write([]byte(TOMLDelimUnix))
if err != nil {
return nil, err
}
@@ -103,12 +103,12 @@ func InterfaceToFrontMatter(in interface{}, mark rune) ([]byte, error) {
if err != nil {
return nil, err
}
- _, err = b.Write([]byte("\n" + TOML_DELIM_UNIX))
+ _, err = b.Write([]byte("\n" + TOMLDelimUnix))
if err != nil {
return nil, err
}
return b.Bytes(), nil
- case rune(JSON_LEAD[0]):
+ case rune(JSONLead[0]):
by, err := json.MarshalIndent(in, "", " ")
if err != nil {
return nil, err
@@ -127,11 +127,11 @@ func InterfaceToFrontMatter(in interface{}, mark rune) ([]byte, error) {
func FormatToLeadRune(kind string) rune {
switch FormatSanitize(kind) {
case "yaml":
- return rune([]byte(YAML_LEAD)[0])
+ return rune([]byte(YAMLLead)[0])
case "json":
- return rune([]byte(JSON_LEAD)[0])
+ return rune([]byte(JSONLead)[0])
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) {
switch mark {
case '-':
- return &frontmatterType{[]byte(YAML_DELIM), []byte(YAML_DELIM), HandleYAMLMetaData, false}
+ return &frontmatterType{[]byte(YAMLDelim), []byte(YAMLDelim), HandleYAMLMetaData, false}
case '+':
- return &frontmatterType{[]byte(TOML_DELIM), []byte(TOML_DELIM), HandleTOMLMetaData, false}
+ return &frontmatterType{[]byte(TOMLDelim), []byte(TOMLDelim), HandleTOMLMetaData, false}
case '{':
return &frontmatterType{[]byte{'{'}, []byte{'}'}, HandleJSONMetaData, true}
default:
@@ -172,7 +172,7 @@ func HandleTOMLMetaData(datum []byte) (interface{}, error) {
}
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) {
diff --git a/parser/page.go b/parser/page.go
index 018b3d35d..79badf397 100644
--- a/parser/page.go
+++ b/parser/page.go
@@ -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");
// you may not use this file except in compliance with the License.
@@ -24,50 +24,61 @@ import (
)
const (
- HTML_LEAD = "<"
- YAML_LEAD = "-"
- YAML_DELIM_UNIX = "---\n"
- YAML_DELIM_DOS = "---\r\n"
- YAML_DELIM = "---"
- TOML_LEAD = "+"
- TOML_DELIM_UNIX = "+++\n"
- TOML_DELIM_DOS = "+++\r\n"
- TOML_DELIM = "+++"
- JSON_LEAD = "{"
- HTML_COMMENT_START = "<!--"
- HTML_COMMENT_END = "-->"
+ // TODO(bep) Do we really have to export these?
+
+ // HTMLLead identifies the start of HTML documents.
+ HTMLLead = "<"
+ // YAMLLead identifies the start of YAML frontmatter.
+ YAMLLead = "-"
+ // YAMLDelimUnix identifies the end of YAML front matter on Unix.
+ YAMLDelimUnix = "---\n"
+ // YAMLDelimDOS identifies the end of YAML front matter on Windows.
+ YAMLDelimDOS = "---\r\n"
+ // YAMLDelim identifies the YAML front matter delimiter.
+ 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 (
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")
- DosEnding = []byte("\r\n")
+ unixEnding = []byte("\n")
+ dosEnding = []byte("\r\n")
)
-type FrontMatter []byte
-type Content []byte
-
+// Page represents a parsed content page.
type Page interface {
- FrontMatter() FrontMatter
- Content() Content
+ FrontMatter() []byte
+ Content() []byte
IsRenderable() bool
Metadata() (interface{}, error)
}
type page struct {
render bool
- frontmatter FrontMatter
- content Content
+ frontmatter []byte
+ content []byte
}
-func (p *page) Content() Content {
+func (p *page) Content() []byte {
return p.content
}
-func (p *page) FrontMatter() FrontMatter {
+func (p *page) FrontMatter() []byte {
return p.frontmatter
}
@@ -146,14 +157,14 @@ func chompFrontmatterStartComment(r *bufio.Reader) (err error) {
}
str := string(candidate)
- if strings.HasPrefix(str, HTML_COMMENT_START) {
+ if strings.HasPrefix(str, HTMLCommentStart) {
lineEnd := strings.IndexAny(str, "\n")
if lineEnd == -1 {
//TODO: if we can't find it, Peek more?
return nil
}
testStr := strings.TrimSuffix(str[0:lineEnd], "\r")
- if strings.Index(testStr, HTML_COMMENT_END) != -1 {
+ if strings.Index(testStr, HTMLCommentEnd) != -1 {
return nil
}
buf := make([]byte, lineEnd)
@@ -180,12 +191,12 @@ func chompFrontmatterEndComment(r *bufio.Reader) (err error) {
return nil
}
testStr := strings.TrimSuffix(str[0:lineEnd], "\r")
- if strings.Index(testStr, HTML_COMMENT_START) != -1 {
+ if strings.Index(testStr, HTMLCommentStart) != -1 {
return nil
}
//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)
if _, err = r.Read(buf); err != nil {
return
@@ -216,7 +227,7 @@ func shouldRender(lead []byte) (frontmatter bool) {
return
}
- if bytes.Equal(lead[:1], []byte(HTML_LEAD)) {
+ if bytes.Equal(lead[:1], []byte(HTMLLead)) {
return
}
return true
@@ -231,16 +242,16 @@ func determineDelims(firstLine []byte) (left, right []byte) {
case 5:
fallthrough
case 4:
- if firstLine[0] == YAML_LEAD[0] {
- return []byte(YAML_DELIM), []byte(YAML_DELIM)
+ if firstLine[0] == YAMLLead[0] {
+ return []byte(YAMLDelim), []byte(YAMLDelim)
}
- return []byte(TOML_DELIM), []byte(TOML_DELIM)
+ return []byte(TOMLDelim), []byte(TOMLDelim)
case 3:
fallthrough
case 2:
fallthrough
case 1:
- return []byte(JSON_LEAD), []byte("}")
+ return []byte(JSONLead), []byte("}")
default:
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.
// Beginning white spaces of the bufio.Reader must be trimmed before call this
// 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 (
c byte
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)
if _, err = wr.ReadFrom(r); err != nil {
return