Add IsRenderable to Page

As pages are read from the target, they will be assessed if they should
be rendered or not.  The logic for IsRenderable is in the parser/page.go
and looks for anything exception '<'.
This commit is contained in:
Noah Campbell 2013-09-18 10:17:43 -07:00
parent d8e1834910
commit 67b2abaf09
3 changed files with 66 additions and 32 deletions

View file

@ -44,6 +44,7 @@ type Page struct {
Aliases []string Aliases []string
Tmpl bundle.Template Tmpl bundle.Template
Markup string Markup string
renderable bool
PageMeta PageMeta
File File
Position Position
@ -128,6 +129,10 @@ func StripHTML(s string) string {
return output return output
} }
func (p *Page) IsRenderable() bool {
return p.renderable
}
func (p *Page) guessSection() { func (p *Page) guessSection() {
if p.Section == "" { if p.Section == "" {
x := strings.Split(p.FileName, "/") x := strings.Split(p.FileName, "/")
@ -323,12 +328,15 @@ type frontmatterType struct {
includeMark bool includeMark bool
} }
const YAML_DELIM = "---"
const TOML_DELIM = "+++"
func (page *Page) detectFrontMatter(mark rune) (f *frontmatterType) { func (page *Page) detectFrontMatter(mark rune) (f *frontmatterType) {
switch mark { switch mark {
case '-': case '-':
return &frontmatterType{[]byte{'-', '-', '-'}, []byte{'-', '-', '-'}, page.handleYamlMetaData, false} return &frontmatterType{[]byte(YAML_DELIM), []byte(YAML_DELIM), page.handleYamlMetaData, false}
case '+': case '+':
return &frontmatterType{[]byte{'+', '+', '+'}, []byte{'+', '+', '+'}, page.handleTomlMetaData, false} return &frontmatterType{[]byte(TOML_DELIM), []byte(TOML_DELIM), page.handleTomlMetaData, false}
case '{': case '{':
return &frontmatterType{[]byte{'{'}, []byte{'}'}, page.handleJsonMetaData, true} return &frontmatterType{[]byte{'{'}, []byte{'}'}, page.handleJsonMetaData, true}
default: default:
@ -359,18 +367,20 @@ func (page *Page) parse(reader io.Reader) error {
return err return err
} }
front := p.FrontMatter() page.renderable = p.IsRenderable()
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 { front := p.FrontMatter()
return err
if len(front) != 0 {
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 { switch page.Markup {

View file

@ -10,24 +10,25 @@ import (
var EMPTY_PAGE = "" var EMPTY_PAGE = ""
var SIMPLE_PAGE = "---\ntitle: Simple\n---\nSimple Page\n" const (
var INVALID_FRONT_MATTER_MISSING = "This is a test" SIMPLE_PAGE = "---\ntitle: Simple\n---\nSimple Page\n"
INVALID_FRONT_MATTER_MISSING = "This is a test"
var INVALID_FRONT_MATTER_SHORT_DELIM = ` RENDER_NO_FRONT_MATTER = "<!doctype><html><head></head><body>This is a test</body></html>"
INVALID_FRONT_MATTER_SHORT_DELIM = `
-- --
title: Short delim start title: Short delim start
--- ---
Short Delim Short Delim
` `
var INVALID_FRONT_MATTER_SHORT_DELIM_ENDING = ` INVALID_FRONT_MATTER_SHORT_DELIM_ENDING = `
--- ---
title: Short delim ending title: Short delim ending
-- --
Short Delim Short Delim
` `
var INVALID_FRONT_MATTER_LEADING_WS = ` INVALID_FRONT_MATTER_LEADING_WS = `
--- ---
title: Leading WS title: Leading WS
@ -35,7 +36,7 @@ title: Leading WS
Leading Leading
` `
var SIMPLE_PAGE_JSON = ` SIMPLE_PAGE_JSON = `
{ {
"title": "spf13-vim 3.0 release and new website", "title": "spf13-vim 3.0 release and new website",
"description": "spf13-vim is a cross platform distribution of vim plugins and resources for Vim.", "description": "spf13-vim is a cross platform distribution of vim plugins and resources for Vim.",
@ -50,8 +51,8 @@ var SIMPLE_PAGE_JSON = `
Content of the file goes Here Content of the file goes Here
` `
var SIMPLE_PAGE_RFC3339_DATE = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content" SIMPLE_PAGE_RFC3339_DATE = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content"
var SIMPLE_PAGE_JSON_MULTIPLE = ` SIMPLE_PAGE_JSON_MULTIPLE = `
{ {
"title": "foobar", "title": "foobar",
"customData": { "foo": "bar" }, "customData": { "foo": "bar" },
@ -60,34 +61,34 @@ var SIMPLE_PAGE_JSON_MULTIPLE = `
Some text Some text
` `
var SIMPLE_PAGE_JSON_COMPACT = ` SIMPLE_PAGE_JSON_COMPACT = `
{"title":"foobar","customData":{"foo":"bar"},"date":"2012-08-06"} {"title":"foobar","customData":{"foo":"bar"},"date":"2012-08-06"}
Text Text
` `
var SIMPLE_PAGE_NOLAYOUT = `--- SIMPLE_PAGE_NOLAYOUT = `---
title: simple_no_layout title: simple_no_layout
--- ---
No Layout called out` No Layout called out`
var SIMPLE_PAGE_LAYOUT_FOOBAR = `--- SIMPLE_PAGE_LAYOUT_FOOBAR = `---
title: simple layout foobar title: simple layout foobar
layout: foobar layout: foobar
--- ---
Layout foobar` Layout foobar`
var SIMPLE_PAGE_TYPE_FOOBAR = `--- SIMPLE_PAGE_TYPE_FOOBAR = `---
type: foobar type: foobar
--- ---
type foobar` type foobar`
var SIMPLE_PAGE_TYPE_LAYOUT = `--- SIMPLE_PAGE_TYPE_LAYOUT = `---
type: barfoo type: barfoo
layout: buzfoo layout: buzfoo
--- ---
type and layout set` type and layout set`
var SIMPLE_PAGE_WITH_SUMMARY_DELIMITER = `--- SIMPLE_PAGE_WITH_SUMMARY_DELIMITER = `---
title: Simple title: Simple
--- ---
Summary Next Line Summary Next Line
@ -96,17 +97,18 @@ Summary Next Line
Some more text Some more text
` `
var SIMPLE_PAGE_WITH_SUMMARY_DELIMITER_SAME_LINE = `--- SIMPLE_PAGE_WITH_SUMMARY_DELIMITER_SAME_LINE = `---
title: Simple title: Simple
--- ---
Summary Same Line<!--more--> Summary Same Line<!--more-->
Some more text Some more text
` `
)
func checkError(t *testing.T, err error, expected string) { func checkError(t *testing.T, err error, expected string) {
if err == nil { if err == nil {
t.Fatalf("err is nil") t.Fatalf("err is nil. Expected: %s", expected)
} }
if err.Error() != expected { if err.Error() != expected {
t.Errorf("err.Error() returned: '%s'. Expected: '%s'", err.Error(), expected) t.Errorf("err.Error() returned: '%s'. Expected: '%s'", err.Error(), expected)
@ -237,9 +239,7 @@ func TestDegenerateInvalidFrontMatterShortDelim(t *testing.T) {
r string r string
err string err string
}{ }{
{INVALID_FRONT_MATTER_SHORT_DELIM, "Unable to locate frontmatter"},
{INVALID_FRONT_MATTER_SHORT_DELIM_ENDING, "Unable to read frontmatter at filepos 45: EOF"}, {INVALID_FRONT_MATTER_SHORT_DELIM_ENDING, "Unable to read frontmatter at filepos 45: EOF"},
{INVALID_FRONT_MATTER_MISSING, "Unable to locate frontmatter"},
} }
for _, test := range tests { for _, test := range tests {
_, err := ReadFrom(strings.NewReader(test.r), "invalid/front/matter/short/delim") _, err := ReadFrom(strings.NewReader(test.r), "invalid/front/matter/short/delim")
@ -247,6 +247,25 @@ func TestDegenerateInvalidFrontMatterShortDelim(t *testing.T) {
} }
} }
func TestShouldRenderContent(t *testing.T) {
var tests = []struct {
text string
render bool
}{
{INVALID_FRONT_MATTER_MISSING, true},
// TODO how to deal with malformed frontmatter. In this case it'll be rendered as markdown.
{INVALID_FRONT_MATTER_SHORT_DELIM, true},
{RENDER_NO_FRONT_MATTER, false},
}
for _, test := range tests {
p := pageMust(ReadFrom(strings.NewReader(test.text), "render/front/matter"))
if p.IsRenderable() != test.render {
t.Errorf("expected p.IsRenderable() == %t, got %t", test.render, p.IsRenderable())
}
}
}
func TestDegenerateInvalidFrontMatterLeadingWhitespace(t *testing.T) { func TestDegenerateInvalidFrontMatterLeadingWhitespace(t *testing.T) {
_, err := ReadFrom(strings.NewReader(INVALID_FRONT_MATTER_LEADING_WS), "invalid/front/matter/leading/ws") _, err := ReadFrom(strings.NewReader(INVALID_FRONT_MATTER_LEADING_WS), "invalid/front/matter/leading/ws")
if err != nil { if err != nil {

View file

@ -39,6 +39,7 @@ type Content []byte
type Page interface { type Page interface {
FrontMatter() FrontMatter FrontMatter() FrontMatter
Content() Content Content() Content
IsRenderable() bool
} }
type page struct { type page struct {
@ -55,6 +56,10 @@ func (p *page) FrontMatter() FrontMatter {
return p.frontmatter return p.frontmatter
} }
func (p *page) IsRenderable() bool {
return p.render
}
// ReadFrom reads the content from an io.Reader and constructs a page. // ReadFrom reads the content from an io.Reader and constructs a page.
func ReadFrom(r io.Reader) (p Page, err error) { func ReadFrom(r io.Reader) (p Page, err error) {
reader := bufio.NewReader(r) reader := bufio.NewReader(r)