Merge remote-tracking branch 'origin/parser' into mrg_praser

Also brought in parse for github.com/noahcampbell/akebia

Conflicts:
	hugolib/page.go
	hugolib/page_test.go
This commit is contained in:
Noah Campbell
2013-09-17 15:52:40 -07:00
7 changed files with 832 additions and 141 deletions

View File

@@ -14,7 +14,6 @@
package hugolib
import (
"bufio"
"bytes"
"encoding/json"
"errors"
@@ -23,6 +22,7 @@ import (
helper "github.com/spf13/hugo/template"
"github.com/spf13/hugo/template/bundle"
"github.com/theplant/blackfriday"
"github.com/spf13/hugo/parser"
"html/template"
"io"
"launchpad.net/goyaml"
@@ -30,7 +30,6 @@ import (
"sort"
"strings"
"time"
"unicode"
)
type Page struct {
@@ -188,32 +187,6 @@ func (p *Page) analyzePage() {
p.FuzzyWordCount = int((p.WordCount+100)/100) * 100
}
func splitPageContent(data []byte, start string, end string) ([]string, []string) {
lines := strings.Split(string(data), "\n")
datum := lines[0:]
var found = 0
if start != end {
for i, line := range lines {
if strings.HasPrefix(line, start) {
found += 1
}
if strings.HasPrefix(line, end) {
found -= 1
}
if found == 0 {
datum = lines[0 : i+1]
lines = lines[i+1:]
break
}
}
}
return datum, lines
}
func (p *Page) Permalink() template.HTML {
baseUrl := string(p.Site.BaseUrl)
section := strings.TrimSpace(p.Section)
@@ -243,12 +216,17 @@ func (p *Page) Permalink() template.HTML {
func (page *Page) handleTomlMetaData(datum []byte) (interface{}, error) {
m := map[string]interface{}{}
datum = removeTomlIdentifier(datum)
if _, err := toml.Decode(string(datum), &m); err != nil {
return m, fmt.Errorf("Invalid TOML in %s \nError parsing page meta data: %s", page.FileName, err)
}
return m, nil
}
func removeTomlIdentifier(datum []byte) []byte {
return bytes.Replace(datum, []byte("+++"), []byte(""), -1)
}
func (page *Page) handleYamlMetaData(datum []byte) (interface{}, error) {
m := map[string]interface{}{}
if err := goyaml.Unmarshal(datum, &m); err != nil {
@@ -339,73 +317,6 @@ func (page *Page) GetParam(key string) interface{} {
return nil
}
var ErrDetectingFrontMatter = errors.New("unable to detect front matter")
var ErrMatchingStartingFrontMatterDelimiter = errors.New("unable to match beginning front matter delimiter")
var ErrMatchingEndingFrontMatterDelimiter = errors.New("unable to match ending front matter delimiter")
func (page *Page) parseFrontMatter(data *bufio.Reader) (err error) {
if err = checkEmpty(data); err != nil {
return fmt.Errorf("%s: %s", page.FileName, err)
}
var mark rune
if mark, err = chompWhitespace(data); err != nil {
return
}
f := page.detectFrontMatter(mark)
if f == nil {
return ErrDetectingFrontMatter
}
if found, err := beginFrontMatter(data, f); err != nil || !found {
return ErrMatchingStartingFrontMatterDelimiter
}
var frontmatter = new(bytes.Buffer)
for {
line, _, err := data.ReadLine()
if err != nil {
if err == io.EOF {
return ErrMatchingEndingFrontMatterDelimiter
}
return err
}
if bytes.Equal(line, f.markend) {
if f.includeMark {
frontmatter.Write(line)
}
break
}
frontmatter.Write(line)
frontmatter.Write([]byte{'\n'})
}
metadata, err := f.parse(frontmatter.Bytes())
if err != nil {
return
}
if err = page.update(metadata); err != nil {
return
}
return
}
func checkEmpty(data *bufio.Reader) (err error) {
if _, _, err = data.ReadRune(); err != nil {
return errors.New("unable to locate front matter")
}
if err = data.UnreadRune(); err != nil {
return errors.New("unable to unread first charactor in page buffer.")
}
return
}
type frontmatterType struct {
markstart, markend []byte
parse func([]byte) (interface{}, error)
@@ -425,37 +336,6 @@ func (page *Page) detectFrontMatter(mark rune) (f *frontmatterType) {
}
}
func beginFrontMatter(data *bufio.Reader, f *frontmatterType) (bool, error) {
var err error
var peek []byte
if f.includeMark {
peek, err = data.Peek(len(f.markstart))
} else {
peek = make([]byte, len(f.markstart))
_, err = data.Read(peek)
}
if err != nil {
return false, err
}
return bytes.Equal(peek, f.markstart), nil
}
func chompWhitespace(data *bufio.Reader) (r rune, err error) {
for {
r, _, err = data.ReadRune()
if err != nil {
return
}
if unicode.IsSpace(r) {
continue
}
if err := data.UnreadRune(); err != nil {
return r, errors.New("unable to unread first charactor in front matter.")
}
return r, nil
}
}
func (p *Page) Render(layout ...string) template.HTML {
curLayout := ""
@@ -474,18 +354,30 @@ func (p *Page) ExecuteTemplate(layout string) *bytes.Buffer {
}
func (page *Page) parse(reader io.Reader) error {
data := bufio.NewReader(reader)
err := page.parseFrontMatter(data)
p, err := parser.ReadFrom(reader)
if err != nil {
return err
}
front := p.FrontMatter()
if len(front) == 0 {
return errors.New("Unable to locate frontmatter")
}
fm := page.detectFrontMatter(rune(front[0]))
meta, err := fm.parse(front)
if err != nil {
return err
}
if err = page.update(meta); err != nil {
return err
}
switch page.Markup {
case "md":
page.convertMarkdown(data)
page.convertMarkdown(bytes.NewReader(p.Content()))
case "rst":
page.convertRestructuredText(data)
page.convertRestructuredText(bytes.NewReader(p.Content()))
}
return nil
}

View File

@@ -44,7 +44,7 @@ func TestParseIndexes(t *testing.T) {
} {
p, err := ReadFrom(strings.NewReader(test), "page/with/index")
if err != nil {
t.Fatalf("Failed parsing page: %s", err)
t.Fatalf("Failed parsing %q: %s", test, err)
}
param := p.GetParam("tags")

View File

@@ -133,7 +133,7 @@ func TestDegenerateEmptyPage(t *testing.T) {
t.Fatalf("Expected ReadFrom to return an error when an empty buffer is passed.")
}
checkError(t, err, "test: unable to locate front matter")
checkError(t, err, "EOF")
}
func checkPageTitle(t *testing.T, page *Page, title string) {
@@ -242,9 +242,9 @@ func TestDegenerateInvalidFrontMatterShortDelim(t *testing.T) {
r string
err string
}{
{INVALID_FRONT_MATTER_SHORT_DELIM, "unable to match beginning front matter delimiter"},
{INVALID_FRONT_MATTER_SHORT_DELIM_ENDING, "unable to match ending front matter delimiter"},
{INVALID_FRONT_MATTER_MISSING, "unable to detect front matter"},
{INVALID_FRONT_MATTER_SHORT_DELIM, "Unable to locate frontmatter"},
{INVALID_FRONT_MATTER_SHORT_DELIM_ENDING, "EOF"},
{INVALID_FRONT_MATTER_MISSING, "Unable to locate frontmatter"},
}
for _, test := range tests {
_, err := ReadFrom(strings.NewReader(test.r), "invalid/front/matter/short/delim")

View File

@@ -8,10 +8,9 @@ import (
"testing"
)
const SLUG_DOC_1 = "---\ntitle: slug doc 1\nslug: slug-doc-1\naliases:\n - sd1/foo/\n - sd2\n - sd3/\n - sd4.html\n---\nslug doc 1 content"
const SLUG_DOC_1 = "---\ntitle: slug doc 1\nslug: slug-doc-1\naliases:\n - sd1/foo/\n - sd2\n - sd3/\n - sd4.html\n---\nslug doc 1 content\n"
//const SLUG_DOC_1 = "---\ntitle: slug doc 1\nslug: slug-doc-1\n---\nslug doc 1 content"
const SLUG_DOC_2 = "---\ntitle: slug doc 2\nslug: slug-doc-2\n---\nslug doc 2 content"
const SLUG_DOC_2 = "---\ntitle: slug doc 2\nslug: slug-doc-2\n---\nslug doc 2 content\n"
const INDEX_TEMPLATE = "{{ range .Data.Pages }}.{{ end }}"
@@ -59,7 +58,7 @@ func (t *InMemoryAliasTarget) Publish(label string, permalink template.HTML) (er
var urlFakeSource = []byteSource{
{"content/blue/doc1.md", []byte(SLUG_DOC_1)},
{"content/blue/doc2.md", []byte(SLUG_DOC_2)},
// {"content/blue/doc2.md", []byte(SLUG_DOC_2)},
}
func TestPageCount(t *testing.T) {
@@ -96,7 +95,7 @@ func TestPageCount(t *testing.T) {
t.Errorf("No indexed rendered. %v", target.files)
}
expected := "<html><head></head><body>..</body></html>"
expected := "<html><head></head><body>.</body></html>"
if string(blueIndex) != expected {
t.Errorf("Index template does not match expected: %q, got: %q", expected, string(blueIndex))
}