Continue with TOC integration and page refactor. Updated a few tests to match new generated output.

This commit is contained in:
spf13 2014-01-28 23:11:05 -05:00
parent f45c6bc38a
commit 1da3fd039a
2 changed files with 411 additions and 354 deletions

View file

@ -33,22 +33,23 @@ import (
) )
type Page struct { type Page struct {
Status string Status string
Images []string Images []string
rawContent []byte rawContent []byte
Content template.HTML Content template.HTML
Summary template.HTML Summary template.HTML
Truncated bool TableOfContents template.HTML
plain string // TODO should be []byte Truncated bool
Params map[string]interface{} plain string // TODO should be []byte
contentType string Params map[string]interface{}
Draft bool contentType string
Aliases []string Draft bool
Tmpl bundle.Template Aliases []string
Markup string Tmpl bundle.Template
renderable bool Markup string
layout string renderable bool
linkTitle string layout string
linkTitle string
PageMeta PageMeta
File File
Position Position
@ -75,7 +76,7 @@ type Pages []*Page
func (p *Page) Plain() string { func (p *Page) Plain() string {
if len(p.plain) == 0 { if len(p.plain) == 0 {
p.plain = StripHTML(StripShortcodes(string(p.rawContent))) p.plain = StripHTML(StripShortcodes(string(p.renderBytes(p.rawContent))))
} }
return p.plain return p.plain
} }
@ -96,6 +97,10 @@ func (p *Page) setSummary() {
} }
} }
func stripEmptyNav(in []byte) []byte {
return bytes.Replace(in, []byte("<nav>\n</nav>\n\n"), []byte(``), -1)
}
func bytesToHTML(b []byte) template.HTML { func bytesToHTML(b []byte) template.HTML {
return template.HTML(string(b)) return template.HTML(string(b))
} }
@ -104,16 +109,27 @@ func (p *Page) renderBytes(content []byte) []byte {
return renderBytes(content, p.guessMarkupType()) return renderBytes(content, p.guessMarkupType())
} }
func (p *Page) renderString(content string) []byte { func (p *Page) renderContent(content []byte) []byte {
return renderBytes([]byte(content), p.guessMarkupType()) return renderBytesWithTOC(content, p.guessMarkupType())
}
func renderBytesWithTOC(content []byte, pagefmt string) []byte {
switch pagefmt {
default:
return markdownRenderWithTOC(content)
case "markdown":
return markdownRenderWithTOC(content)
case "rst":
return []byte(getRstContent(content))
}
} }
func renderBytes(content []byte, pagefmt string) []byte { func renderBytes(content []byte, pagefmt string) []byte {
switch pagefmt { switch pagefmt {
default: default:
return blackfriday.MarkdownCommon(content) return markdownRender(content)
case "markdown": case "markdown":
return blackfriday.MarkdownCommon(content) return markdownRender(content)
case "rst": case "rst":
return []byte(getRstContent(content)) return []byte(getRstContent(content))
} }
@ -553,7 +569,9 @@ func (page *Page) Convert() error {
markupType := page.guessMarkupType() markupType := page.guessMarkupType()
switch markupType { switch markupType {
case "markdown", "rst": case "markdown", "rst":
page.Content = bytesToHTML(page.renderString(string(RemoveSummaryDivider(page.rawContent)))) tmpContent, tmpTableOfContents := extractTOC(page.renderContent(RemoveSummaryDivider(page.rawContent)))
page.Content = bytesToHTML(tmpContent)
page.TableOfContents = bytesToHTML(tmpTableOfContents)
case "html": case "html":
page.Content = bytesToHTML(page.rawContent) page.Content = bytesToHTML(page.rawContent)
default: default:
@ -562,19 +580,58 @@ func (page *Page) Convert() error {
return nil return nil
} }
// Lazily generate the TOC func markdownRender(content []byte) []byte {
func (page *Page) TableOfContents() template.HTML { htmlFlags := 0
return tableOfContentsFromBytes([]byte(page.Content)) htmlFlags |= blackfriday.HTML_SKIP_SCRIPT
htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
renderer := blackfriday.HtmlRenderer(htmlFlags, "", "")
return blackfriday.Markdown(content, renderer, 0)
} }
func tableOfContentsFromBytes(content []byte) template.HTML { func markdownRenderWithTOC(content []byte) []byte {
htmlFlags := 0 htmlFlags := 0
htmlFlags |= blackfriday.HTML_SKIP_SCRIPT htmlFlags |= blackfriday.HTML_SKIP_SCRIPT
htmlFlags |= blackfriday.HTML_TOC htmlFlags |= blackfriday.HTML_TOC
htmlFlags |= blackfriday.HTML_OMIT_CONTENTS htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
renderer := blackfriday.HtmlRenderer(htmlFlags, "", "") renderer := blackfriday.HtmlRenderer(htmlFlags, "", "")
return template.HTML(string(blackfriday.Markdown(RemoveSummaryDivider(content), renderer, 0))) return blackfriday.Markdown(content, renderer, 0)
}
func extractTOC(content []byte) (newcontent []byte, toc []byte) {
origContent := make([]byte, len(content))
copy(origContent, content)
first := []byte(`<nav>
<ul>`)
last := []byte(`</ul>
</nav>`)
replacement := []byte(`<nav id="TableOfContents">
<ul>`)
startOfTOC := bytes.Index(content, first)
peekEnd := len(content)
if peekEnd > 70+startOfTOC {
peekEnd = 70 + startOfTOC
}
if startOfTOC < 0 {
return stripEmptyNav(content), toc
}
// Need to peek ahead to see if this nav element is actually the right one.
correctNav := bytes.Index(content[startOfTOC:peekEnd], []byte(`#toc_0`))
if correctNav < 0 { // no match found
return content, toc
}
lengthOfTOC := bytes.Index(content[startOfTOC:], last) + len(last)
endOfTOC := startOfTOC + lengthOfTOC
newcontent = append(content[:startOfTOC], content[endOfTOC:]...)
toc = append(replacement, origContent[startOfTOC+len(first):endOfTOC]...)
return
} }
func ReaderToBytes(lines io.Reader) []byte { func ReaderToBytes(lines io.Reader) []byte {

View file

@ -1,37 +1,37 @@
package hugolib package hugolib
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/spf13/hugo/source" "github.com/spf13/hugo/source"
"github.com/spf13/hugo/target" "github.com/spf13/hugo/target"
"html/template" "html/template"
"io" "io"
"strings" "strings"
"testing" "testing"
) )
const ( const (
TEMPLATE_TITLE = "{{ .Title }}" TEMPLATE_TITLE = "{{ .Title }}"
PAGE_SIMPLE_TITLE = `--- PAGE_SIMPLE_TITLE = `---
title: simple template title: simple template
--- ---
content` content`
TEMPLATE_MISSING_FUNC = "{{ .Title | funcdoesnotexists }}" TEMPLATE_MISSING_FUNC = "{{ .Title | funcdoesnotexists }}"
TEMPLATE_FUNC = "{{ .Title | urlize }}" TEMPLATE_FUNC = "{{ .Title | urlize }}"
TEMPLATE_CONTENT = "{{ .Content }}" TEMPLATE_CONTENT = "{{ .Content }}"
TEMPLATE_DATE = "{{ .Date }}" TEMPLATE_DATE = "{{ .Date }}"
INVALID_TEMPLATE_FORMAT_DATE = "{{ .Date.Format time.RFC3339 }}" INVALID_TEMPLATE_FORMAT_DATE = "{{ .Date.Format time.RFC3339 }}"
TEMPLATE_WITH_URL_REL = "<a href=\"foobar.jpg\">Going</a>" TEMPLATE_WITH_URL_REL = "<a href=\"foobar.jpg\">Going</a>"
TEMPLATE_WITH_URL_ABS = "<a href=\"/foobar.jpg\">Going</a>" TEMPLATE_WITH_URL_ABS = "<a href=\"/foobar.jpg\">Going</a>"
PAGE_URL_SPECIFIED = `--- PAGE_URL_SPECIFIED = `---
title: simple template title: simple template
url: "mycategory/my-whatever-content/" url: "mycategory/my-whatever-content/"
--- ---
content` content`
PAGE_WITH_MD = `--- PAGE_WITH_MD = `---
title: page with md title: page with md
--- ---
# heading 1 # heading 1
@ -42,306 +42,306 @@ more text
) )
func pageMust(p *Page, err error) *Page { func pageMust(p *Page, err error) *Page {
if err != nil { if err != nil {
panic(err) panic(err)
} }
return p return p
} }
func TestDegenerateRenderThingMissingTemplate(t *testing.T) { func TestDegenerateRenderThingMissingTemplate(t *testing.T) {
p, _ := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md") p, _ := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
p.Convert() p.Convert()
s := new(Site) s := new(Site)
s.prepTemplates() s.prepTemplates()
err := s.renderThing(p, "foobar", nil) err := s.renderThing(p, "foobar", nil)
if err == nil { if err == nil {
t.Errorf("Expected err to be returned when missing the template.") t.Errorf("Expected err to be returned when missing the template.")
} }
} }
func TestAddInvalidTemplate(t *testing.T) { func TestAddInvalidTemplate(t *testing.T) {
s := new(Site) s := new(Site)
s.prepTemplates() s.prepTemplates()
err := s.addTemplate("missing", TEMPLATE_MISSING_FUNC) err := s.addTemplate("missing", TEMPLATE_MISSING_FUNC)
if err == nil { if err == nil {
t.Fatalf("Expecting the template to return an error") t.Fatalf("Expecting the template to return an error")
} }
} }
type nopCloser struct { type nopCloser struct {
io.Writer io.Writer
} }
func (nopCloser) Close() error { return nil } func (nopCloser) Close() error { return nil }
func NopCloser(w io.Writer) io.WriteCloser { func NopCloser(w io.Writer) io.WriteCloser {
return nopCloser{w} return nopCloser{w}
} }
func matchRender(t *testing.T, s *Site, p *Page, tmplName string, expected string) { func matchRender(t *testing.T, s *Site, p *Page, tmplName string, expected string) {
content := new(bytes.Buffer) content := new(bytes.Buffer)
err := s.renderThing(p, tmplName, NopCloser(content)) err := s.renderThing(p, tmplName, NopCloser(content))
if err != nil { if err != nil {
t.Fatalf("Unable to render template.") t.Fatalf("Unable to render template.")
} }
if string(content.Bytes()) != expected { if string(content.Bytes()) != expected {
t.Fatalf("Content did not match expected: %s. got: %s", expected, content) t.Fatalf("Content did not match expected: %s. got: %s", expected, content)
} }
} }
func TestRenderThing(t *testing.T) { func TestRenderThing(t *testing.T) {
tests := []struct { tests := []struct {
content string content string
template string template string
expected string expected string
}{ }{
{PAGE_SIMPLE_TITLE, TEMPLATE_TITLE, "simple template"}, {PAGE_SIMPLE_TITLE, TEMPLATE_TITLE, "simple template"},
{PAGE_SIMPLE_TITLE, TEMPLATE_FUNC, "simple-template"}, {PAGE_SIMPLE_TITLE, TEMPLATE_FUNC, "simple-template"},
{PAGE_WITH_MD, TEMPLATE_CONTENT, "<h1>heading 1</h1>\n\n<p>text</p>\n\n<h2>heading 2</h2>\n\n<p>more text</p>\n"}, {PAGE_WITH_MD, TEMPLATE_CONTENT, "\n\n<h1 id=\"toc_0\">heading 1</h1>\n\n<p>text</p>\n\n<h2 id=\"toc_1\">heading 2</h2>\n\n<p>more text</p>\n"},
{SIMPLE_PAGE_RFC3339_DATE, TEMPLATE_DATE, "2013-05-17 16:59:30 &#43;0000 UTC"}, {SIMPLE_PAGE_RFC3339_DATE, TEMPLATE_DATE, "2013-05-17 16:59:30 &#43;0000 UTC"},
} }
s := new(Site) s := new(Site)
s.prepTemplates() s.prepTemplates()
for i, test := range tests { for i, test := range tests {
p, err := ReadFrom(strings.NewReader(test.content), "content/a/file.md") p, err := ReadFrom(strings.NewReader(test.content), "content/a/file.md")
p.Convert() p.Convert()
if err != nil { if err != nil {
t.Fatalf("Error parsing buffer: %s", err) t.Fatalf("Error parsing buffer: %s", err)
} }
templateName := fmt.Sprintf("foobar%d", i) templateName := fmt.Sprintf("foobar%d", i)
err = s.addTemplate(templateName, test.template) err = s.addTemplate(templateName, test.template)
if err != nil { if err != nil {
t.Fatalf("Unable to add template") t.Fatalf("Unable to add template")
} }
p.Content = template.HTML(p.Content) p.Content = template.HTML(p.Content)
html := new(bytes.Buffer) html := new(bytes.Buffer)
err = s.renderThing(p, templateName, NopCloser(html)) err = s.renderThing(p, templateName, NopCloser(html))
if err != nil { if err != nil {
t.Errorf("Unable to render html: %s", err) t.Errorf("Unable to render html: %s", err)
} }
if string(html.Bytes()) != test.expected { if string(html.Bytes()) != test.expected {
t.Errorf("Content does not match.\nExpected\n\t'%q'\ngot\n\t'%q'", test.expected, html) t.Errorf("Content does not match.\nExpected\n\t'%q'\ngot\n\t'%q'", test.expected, html)
} }
} }
} }
func HTML(in string) string { func HTML(in string) string {
return in return in
} }
func TestRenderThingOrDefault(t *testing.T) { func TestRenderThingOrDefault(t *testing.T) {
tests := []struct { tests := []struct {
content string content string
missing bool missing bool
template string template string
expected string expected string
}{ }{
{PAGE_SIMPLE_TITLE, true, TEMPLATE_TITLE, HTML("simple template")}, {PAGE_SIMPLE_TITLE, true, TEMPLATE_TITLE, HTML("simple template")},
{PAGE_SIMPLE_TITLE, true, TEMPLATE_FUNC, HTML("simple-template")}, {PAGE_SIMPLE_TITLE, true, TEMPLATE_FUNC, HTML("simple-template")},
{PAGE_SIMPLE_TITLE, false, TEMPLATE_TITLE, HTML("simple template")}, {PAGE_SIMPLE_TITLE, false, TEMPLATE_TITLE, HTML("simple template")},
{PAGE_SIMPLE_TITLE, false, TEMPLATE_FUNC, HTML("simple-template")}, {PAGE_SIMPLE_TITLE, false, TEMPLATE_FUNC, HTML("simple-template")},
} }
files := make(map[string][]byte) files := make(map[string][]byte)
target := &target.InMemoryTarget{Files: files} target := &target.InMemoryTarget{Files: files}
s := &Site{ s := &Site{
Target: target, Target: target,
} }
s.prepTemplates() s.prepTemplates()
for i, test := range tests { for i, test := range tests {
p, err := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md") p, err := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
if err != nil { if err != nil {
t.Fatalf("Error parsing buffer: %s", err) t.Fatalf("Error parsing buffer: %s", err)
} }
templateName := fmt.Sprintf("default%d", i) templateName := fmt.Sprintf("default%d", i)
err = s.addTemplate(templateName, test.template) err = s.addTemplate(templateName, test.template)
if err != nil { if err != nil {
t.Fatalf("Unable to add template") t.Fatalf("Unable to add template")
} }
var err2 error var err2 error
if test.missing { if test.missing {
err2 = s.render(p, "out", "missing", templateName) err2 = s.render(p, "out", "missing", templateName)
} else { } else {
err2 = s.render(p, "out", templateName, "missing_default") err2 = s.render(p, "out", templateName, "missing_default")
} }
if err2 != nil { if err2 != nil {
t.Errorf("Unable to render html: %s", err) t.Errorf("Unable to render html: %s", err)
} }
if string(files["out"]) != test.expected { if string(files["out"]) != test.expected {
t.Errorf("Content does not match. Expected '%s', got '%s'", test.expected, files["out"]) t.Errorf("Content does not match. Expected '%s', got '%s'", test.expected, files["out"])
} }
} }
} }
func TestTargetPath(t *testing.T) { func TestTargetPath(t *testing.T) {
tests := []struct { tests := []struct {
doc string doc string
content string content string
expectedOutFile string expectedOutFile string
expectedSection string expectedSection string
}{ }{
{"content/a/file.md", PAGE_URL_SPECIFIED, "mycategory/my-whatever-content/index.html", "a"}, {"content/a/file.md", PAGE_URL_SPECIFIED, "mycategory/my-whatever-content/index.html", "a"},
{"content/x/y/deepfile.md", SIMPLE_PAGE, "x/y/deepfile.html", "x/y"}, {"content/x/y/deepfile.md", SIMPLE_PAGE, "x/y/deepfile.html", "x/y"},
{"content/x/y/z/deeperfile.md", SIMPLE_PAGE, "x/y/z/deeperfile.html", "x/y/z"}, {"content/x/y/z/deeperfile.md", SIMPLE_PAGE, "x/y/z/deeperfile.html", "x/y/z"},
{"content/b/file.md", SIMPLE_PAGE, "b/file.html", "b"}, {"content/b/file.md", SIMPLE_PAGE, "b/file.html", "b"},
{"a/file.md", SIMPLE_PAGE, "a/file.html", "a"}, {"a/file.md", SIMPLE_PAGE, "a/file.html", "a"},
{"file.md", SIMPLE_PAGE, "file.html", ""}, {"file.md", SIMPLE_PAGE, "file.html", ""},
} }
if true { if true {
return return
} }
for _, test := range tests { for _, test := range tests {
s := &Site{ s := &Site{
Config: Config{ContentDir: "content"}, Config: Config{ContentDir: "content"},
} }
p := pageMust(ReadFrom(strings.NewReader(test.content), s.Config.GetAbsPath(test.doc))) p := pageMust(ReadFrom(strings.NewReader(test.content), s.Config.GetAbsPath(test.doc)))
expected := test.expectedOutFile expected := test.expectedOutFile
if p.TargetPath() != expected { if p.TargetPath() != expected {
t.Errorf("%s => OutFile expected: '%s', got: '%s'", test.doc, expected, p.TargetPath()) t.Errorf("%s => OutFile expected: '%s', got: '%s'", test.doc, expected, p.TargetPath())
} }
if p.Section != test.expectedSection { if p.Section != test.expectedSection {
t.Errorf("%s => p.Section expected: %s, got: %s", test.doc, test.expectedSection, p.Section) t.Errorf("%s => p.Section expected: %s, got: %s", test.doc, test.expectedSection, p.Section)
} }
} }
} }
func TestSkipRender(t *testing.T) { func TestSkipRender(t *testing.T) {
files := make(map[string][]byte) files := make(map[string][]byte)
target := &target.InMemoryTarget{Files: files} target := &target.InMemoryTarget{Files: files}
sources := []source.ByteSource{ sources := []source.ByteSource{
{"sect/doc1.html", []byte("---\nmarkup: markdown\n---\n# title\nsome *content*"), "sect"}, {"sect/doc1.html", []byte("---\nmarkup: markdown\n---\n# title\nsome *content*"), "sect"},
{"sect/doc2.html", []byte("<!doctype html><html><body>more content</body></html>"), "sect"}, {"sect/doc2.html", []byte("<!doctype html><html><body>more content</body></html>"), "sect"},
{"sect/doc3.md", []byte("# doc3\n*some* content"), "sect"}, {"sect/doc3.md", []byte("# doc3\n*some* content"), "sect"},
{"sect/doc4.md", []byte("---\ntitle: doc4\n---\n# doc4\n*some content*"), "sect"}, {"sect/doc4.md", []byte("---\ntitle: doc4\n---\n# doc4\n*some content*"), "sect"},
{"sect/doc5.html", []byte("<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>"), "sect"}, {"sect/doc5.html", []byte("<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>"), "sect"},
{"sect/doc6.html", []byte("<!doctype html><html>{{ template \"head_abs\" }}<body>body5</body></html>"), "sect"}, {"sect/doc6.html", []byte("<!doctype html><html>{{ template \"head_abs\" }}<body>body5</body></html>"), "sect"},
{"doc7.html", []byte("<html><body>doc7 content</body></html>"), ""}, {"doc7.html", []byte("<html><body>doc7 content</body></html>"), ""},
{"sect/doc8.html", []byte("---\nmarkup: md\n---\n# title\nsome *content*"), "sect"}, {"sect/doc8.html", []byte("---\nmarkup: md\n---\n# title\nsome *content*"), "sect"},
} }
s := &Site{ s := &Site{
Target: target, Target: target,
Config: Config{ Config: Config{
Verbose: true, Verbose: true,
BaseUrl: "http://auth/bub", BaseUrl: "http://auth/bub",
CanonifyUrls: true, CanonifyUrls: true,
}, },
Source: &source.InMemorySource{sources}, Source: &source.InMemorySource{sources},
} }
s.initializeSiteInfo() s.initializeSiteInfo()
s.prepTemplates() s.prepTemplates()
must(s.addTemplate("_default/single.html", "{{.Content}}")) must(s.addTemplate("_default/single.html", "{{.Content}}"))
must(s.addTemplate("head", "<head><script src=\"script.js\"></script></head>")) must(s.addTemplate("head", "<head><script src=\"script.js\"></script></head>"))
must(s.addTemplate("head_abs", "<head><script src=\"/script.js\"></script></head>")) must(s.addTemplate("head_abs", "<head><script src=\"/script.js\"></script></head>"))
if err := s.CreatePages(); err != nil { if err := s.CreatePages(); err != nil {
t.Fatalf("Unable to create pages: %s", err) t.Fatalf("Unable to create pages: %s", err)
} }
if err := s.BuildSiteMeta(); err != nil { if err := s.BuildSiteMeta(); err != nil {
t.Fatalf("Unable to build site metadata: %s", err) t.Fatalf("Unable to build site metadata: %s", err)
} }
if err := s.RenderPages(); err != nil { if err := s.RenderPages(); err != nil {
t.Fatalf("Unable to render pages. %s", err) t.Fatalf("Unable to render pages. %s", err)
} }
tests := []struct { tests := []struct {
doc string doc string
expected string expected string
}{ }{
{"sect/doc1.html", "<h1>title</h1>\n\n<p>some <em>content</em></p>\n"}, {"sect/doc1.html", "\n\n<h1 id=\"toc_0\">title</h1>\n\n<p>some <em>content</em></p>\n"},
{"sect/doc2.html", "<!doctype html><html><body>more content</body></html>"}, {"sect/doc2.html", "<!doctype html><html><body>more content</body></html>"},
{"sect/doc3.html", "<h1>doc3</h1>\n\n<p><em>some</em> content</p>\n"}, {"sect/doc3.html", "\n\n<h1 id=\"toc_0\">doc3</h1>\n\n<p><em>some</em> content</p>\n"},
{"sect/doc4.html", "<h1>doc4</h1>\n\n<p><em>some content</em></p>\n"}, {"sect/doc4.html", "\n\n<h1 id=\"toc_0\">doc4</h1>\n\n<p><em>some content</em></p>\n"},
{"sect/doc5.html", "<!doctype html><html><head><script src=\"script.js\"></script></head><body>body5</body></html>"}, {"sect/doc5.html", "<!doctype html><html><head><script src=\"script.js\"></script></head><body>body5</body></html>"},
{"sect/doc6.html", "<!doctype html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"}, {"sect/doc6.html", "<!doctype html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"},
{"doc7.html", "<html><body>doc7 content</body></html>"}, {"doc7.html", "<html><body>doc7 content</body></html>"},
{"sect/doc8.html", "<h1>title</h1>\n\n<p>some <em>content</em></p>\n"}, {"sect/doc8.html", "\n\n<h1 id=\"toc_0\">title</h1>\n\n<p>some <em>content</em></p>\n"},
} }
for _, test := range tests { for _, test := range tests {
content, ok := target.Files[test.doc] content, ok := target.Files[test.doc]
if !ok { if !ok {
t.Fatalf("Did not find %s in target. %v", test.doc, target.Files) t.Fatalf("Did not find %s in target. %v", test.doc, target.Files)
} }
if !bytes.Equal(content, []byte(test.expected)) { if !bytes.Equal(content, []byte(test.expected)) {
t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, string(content)) t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, string(content))
} }
} }
} }
func TestAbsUrlify(t *testing.T) { func TestAbsUrlify(t *testing.T) {
files := make(map[string][]byte) files := make(map[string][]byte)
target := &target.InMemoryTarget{Files: files} target := &target.InMemoryTarget{Files: files}
sources := []source.ByteSource{ sources := []source.ByteSource{
{"sect/doc1.html", []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"), "sect"}, {"sect/doc1.html", []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"), "sect"},
{"content/blue/doc2.html", []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>"), "blue"}, {"content/blue/doc2.html", []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>"), "blue"},
} }
for _, canonify := range []bool{true, false} { for _, canonify := range []bool{true, false} {
s := &Site{ s := &Site{
Target: target, Target: target,
Config: Config{ Config: Config{
BaseUrl: "http://auth/bub", BaseUrl: "http://auth/bub",
CanonifyUrls: canonify, CanonifyUrls: canonify,
}, },
Source: &source.InMemorySource{sources}, Source: &source.InMemorySource{sources},
} }
t.Logf("Rendering with BaseUrl %q and CanonifyUrls set %v", s.Config.BaseUrl, canonify) t.Logf("Rendering with BaseUrl %q and CanonifyUrls set %v", s.Config.BaseUrl, canonify)
s.initializeSiteInfo() s.initializeSiteInfo()
s.prepTemplates() s.prepTemplates()
must(s.addTemplate("blue/single.html", TEMPLATE_WITH_URL_ABS)) must(s.addTemplate("blue/single.html", TEMPLATE_WITH_URL_ABS))
if err := s.CreatePages(); err != nil { if err := s.CreatePages(); err != nil {
t.Fatalf("Unable to create pages: %s", err) t.Fatalf("Unable to create pages: %s", err)
} }
if err := s.BuildSiteMeta(); err != nil { if err := s.BuildSiteMeta(); err != nil {
t.Fatalf("Unable to build site metadata: %s", err) t.Fatalf("Unable to build site metadata: %s", err)
} }
if err := s.RenderPages(); err != nil { if err := s.RenderPages(); err != nil {
t.Fatalf("Unable to render pages. %s", err) t.Fatalf("Unable to render pages. %s", err)
} }
tests := []struct { tests := []struct {
file, expected string file, expected string
}{ }{
{"content/blue/doc2.html", "<a href=\"http://auth/bub/foobar.jpg\">Going</a>"}, {"content/blue/doc2.html", "<a href=\"http://auth/bub/foobar.jpg\">Going</a>"},
{"sect/doc1.html", "<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"}, {"sect/doc1.html", "<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"},
} }
for _, test := range tests { for _, test := range tests {
content, ok := target.Files[test.file] content, ok := target.Files[test.file]
if !ok { if !ok {
t.Fatalf("Unable to locate rendered content: %s", test.file) t.Fatalf("Unable to locate rendered content: %s", test.file)
} }
expected := test.expected expected := test.expected
if !canonify { if !canonify {
expected = strings.Replace(expected, s.Config.BaseUrl, "", -1) expected = strings.Replace(expected, s.Config.BaseUrl, "", -1)
} }
if string(content) != expected { if string(content) != expected {
t.Errorf("AbsUrlify content expected:\n%q\ngot\n%q", expected, string(content)) t.Errorf("AbsUrlify content expected:\n%q\ngot\n%q", expected, string(content))
} }
} }
} }
} }
var WEIGHTED_PAGE_1 = []byte(`+++ var WEIGHTED_PAGE_1 = []byte(`+++
@ -371,58 +371,58 @@ date = "2012-01-01"
Front Matter with Ordered Pages 4. This is longer content`) Front Matter with Ordered Pages 4. This is longer content`)
var WEIGHTED_SOURCES = []source.ByteSource{ var WEIGHTED_SOURCES = []source.ByteSource{
{"sect/doc1.md", WEIGHTED_PAGE_1, "sect"}, {"sect/doc1.md", WEIGHTED_PAGE_1, "sect"},
{"sect/doc2.md", WEIGHTED_PAGE_2, "sect"}, {"sect/doc2.md", WEIGHTED_PAGE_2, "sect"},
{"sect/doc3.md", WEIGHTED_PAGE_3, "sect"}, {"sect/doc3.md", WEIGHTED_PAGE_3, "sect"},
{"sect/doc4.md", WEIGHTED_PAGE_4, "sect"}, {"sect/doc4.md", WEIGHTED_PAGE_4, "sect"},
} }
func TestOrderedPages(t *testing.T) { func TestOrderedPages(t *testing.T) {
files := make(map[string][]byte) files := make(map[string][]byte)
target := &target.InMemoryTarget{Files: files} target := &target.InMemoryTarget{Files: files}
s := &Site{ s := &Site{
Target: target, Target: target,
Config: Config{BaseUrl: "http://auth/bub/"}, Config: Config{BaseUrl: "http://auth/bub/"},
Source: &source.InMemorySource{WEIGHTED_SOURCES}, Source: &source.InMemorySource{WEIGHTED_SOURCES},
} }
s.initializeSiteInfo() s.initializeSiteInfo()
if err := s.CreatePages(); err != nil { if err := s.CreatePages(); err != nil {
t.Fatalf("Unable to create pages: %s", err) t.Fatalf("Unable to create pages: %s", err)
} }
if err := s.BuildSiteMeta(); err != nil { if err := s.BuildSiteMeta(); err != nil {
t.Fatalf("Unable to build site metadata: %s", err) t.Fatalf("Unable to build site metadata: %s", err)
} }
if s.Sections["sect"][0].Weight != 2 || s.Sections["sect"][3].Weight != 6 { if s.Sections["sect"][0].Weight != 2 || s.Sections["sect"][3].Weight != 6 {
t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", 2, s.Sections["sect"][0].Weight) t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", 2, s.Sections["sect"][0].Weight)
} }
if s.Sections["sect"][1].Page.Title != "Three" || s.Sections["sect"][2].Page.Title != "Four" { if s.Sections["sect"][1].Page.Title != "Three" || s.Sections["sect"][2].Page.Title != "Four" {
t.Errorf("Pages in unexpected order. Second should be '%s', got '%s'", "Three", s.Sections["sect"][1].Page.Title) t.Errorf("Pages in unexpected order. Second should be '%s', got '%s'", "Three", s.Sections["sect"][1].Page.Title)
} }
bydate := s.Pages.ByDate() bydate := s.Pages.ByDate()
if bydate[0].Title != "One" { if bydate[0].Title != "One" {
t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bydate[0].Title) t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bydate[0].Title)
} }
rev := bydate.Reverse() rev := bydate.Reverse()
if rev[0].Title != "Three" { if rev[0].Title != "Three" {
t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Three", rev[0].Title) t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Three", rev[0].Title)
} }
bylength := s.Pages.ByLength() bylength := s.Pages.ByLength()
if bylength[0].Title != "One" { if bylength[0].Title != "One" {
t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bylength[0].Title) t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bylength[0].Title)
} }
rbylength := bylength.Reverse() rbylength := bylength.Reverse()
if rbylength[0].Title != "Four" { if rbylength[0].Title != "Four" {
t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Four", rbylength[0].Title) t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Four", rbylength[0].Title)
} }
} }
var PAGE_WITH_WEIGHTED_INDEXES_2 = []byte(`+++ var PAGE_WITH_WEIGHTED_INDEXES_2 = []byte(`+++
@ -455,41 +455,41 @@ date = 2010-05-27T07:32:00Z
Front Matter with weighted tags and categories`) Front Matter with weighted tags and categories`)
func TestWeightedIndexes(t *testing.T) { func TestWeightedIndexes(t *testing.T) {
files := make(map[string][]byte) files := make(map[string][]byte)
target := &target.InMemoryTarget{Files: files} target := &target.InMemoryTarget{Files: files}
sources := []source.ByteSource{ sources := []source.ByteSource{
{"sect/doc1.md", PAGE_WITH_WEIGHTED_INDEXES_1, "sect"}, {"sect/doc1.md", PAGE_WITH_WEIGHTED_INDEXES_1, "sect"},
{"sect/doc2.md", PAGE_WITH_WEIGHTED_INDEXES_2, "sect"}, {"sect/doc2.md", PAGE_WITH_WEIGHTED_INDEXES_2, "sect"},
{"sect/doc3.md", PAGE_WITH_WEIGHTED_INDEXES_3, "sect"}, {"sect/doc3.md", PAGE_WITH_WEIGHTED_INDEXES_3, "sect"},
} }
indexes := make(map[string]string) indexes := make(map[string]string)
indexes["tag"] = "tags" indexes["tag"] = "tags"
indexes["category"] = "categories" indexes["category"] = "categories"
s := &Site{ s := &Site{
Target: target, Target: target,
Config: Config{BaseUrl: "http://auth/bub/", Indexes: indexes}, Config: Config{BaseUrl: "http://auth/bub/", Indexes: indexes},
Source: &source.InMemorySource{sources}, Source: &source.InMemorySource{sources},
} }
s.initializeSiteInfo() s.initializeSiteInfo()
if err := s.CreatePages(); err != nil { if err := s.CreatePages(); err != nil {
t.Fatalf("Unable to create pages: %s", err) t.Fatalf("Unable to create pages: %s", err)
} }
if err := s.BuildSiteMeta(); err != nil { if err := s.BuildSiteMeta(); err != nil {
t.Fatalf("Unable to build site metadata: %s", err) t.Fatalf("Unable to build site metadata: %s", err)
} }
if s.Indexes["tags"]["a"][0].Page.Title != "foo" { if s.Indexes["tags"]["a"][0].Page.Title != "foo" {
t.Errorf("Pages in unexpected order, 'foo' expected first, got '%v'", s.Indexes["tags"]["a"][0].Page.Title) t.Errorf("Pages in unexpected order, 'foo' expected first, got '%v'", s.Indexes["tags"]["a"][0].Page.Title)
} }
if s.Indexes["categories"]["d"][0].Page.Title != "bar" { if s.Indexes["categories"]["d"][0].Page.Title != "bar" {
t.Errorf("Pages in unexpected order, 'bar' expected first, got '%v'", s.Indexes["categories"]["d"][0].Page.Title) t.Errorf("Pages in unexpected order, 'bar' expected first, got '%v'", s.Indexes["categories"]["d"][0].Page.Title)
} }
if s.Indexes["categories"]["e"][0].Page.Title != "bza" { if s.Indexes["categories"]["e"][0].Page.Title != "bza" {
t.Errorf("Pages in unexpected order, 'bza' expected first, got '%v'", s.Indexes["categories"]["e"][0].Page.Title) t.Errorf("Pages in unexpected order, 'bza' expected first, got '%v'", s.Indexes["categories"]["e"][0].Page.Title)
} }
} }