diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-01-02 12:33:26 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-03-23 18:51:22 +0100 |
commit | 597e418cb02883418f2cebb41400e8e61413f651 (patch) | |
tree | 177ad9c540b2583b6dab138c9f0490d28989c7f7 /hugolib/page_test.go | |
parent | 44f5c1c14cb1f42cc5f01739c289e9cfc83602af (diff) |
Make Page an interface
The main motivation of this commit is to add a `page.Page` interface to replace the very file-oriented `hugolib.Page` struct.
This is all a preparation step for issue #5074, "pages from other data sources".
But this also fixes a set of annoying limitations, especially related to custom output formats, and shortcodes.
Most notable changes:
* The inner content of shortcodes using the `{{%` as the outer-most delimiter will now be sent to the content renderer, e.g. Blackfriday.
This means that any markdown will partake in the global ToC and footnote context etc.
* The Custom Output formats are now "fully virtualized". This removes many of the current limitations.
* The taxonomy list type now has a reference to the `Page` object.
This improves the taxonomy template `.Title` situation and make common template constructs much simpler.
See #5074
Fixes #5763
Fixes #5758
Fixes #5090
Fixes #5204
Fixes #4695
Fixes #5607
Fixes #5707
Fixes #5719
Fixes #3113
Fixes #5706
Fixes #5767
Fixes #5723
Fixes #5769
Fixes #5770
Fixes #5771
Fixes #5759
Fixes #5776
Fixes #5777
Fixes #5778
Diffstat (limited to 'hugolib/page_test.go')
-rw-r--r-- | hugolib/page_test.go | 901 |
1 files changed, 187 insertions, 714 deletions
diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 1db1d3522..5e9ac696c 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -1,4 +1,4 @@ -// Copyright 2018 The Hugo Authors. All rights reserved. +// Copyright 2019 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. @@ -14,88 +14,34 @@ package hugolib import ( - "bytes" "fmt" "html/template" "os" "path/filepath" - "reflect" - "sort" "strings" "testing" "time" "github.com/gohugoio/hugo/hugofs" - "github.com/spf13/afero" + "github.com/gohugoio/hugo/resources/page" + "github.com/gohugoio/hugo/resources/resource" + + "github.com/spf13/afero" "github.com/spf13/viper" "github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/helpers" - "github.com/spf13/cast" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -var emptyPage = "" - const ( - homePage = "---\ntitle: Home\n---\nHome Page Content\n" - simplePage = "---\ntitle: Simple\n---\nSimple Page\n" - renderNoFrontmatter = "<!doctype><html><head></head><body>This is a test</body></html>" - contentNoFrontmatter = "Page without front matter.\n" - contentWithCommentedFrontmatter = "<!--\n+++\ntitle = \"Network configuration\"\ndescription = \"Docker networking\"\nkeywords = [\"network\"]\n[menu.main]\nparent= \"smn_administrate\"\n+++\n-->\n\n# Network configuration\n\n##\nSummary" - contentWithCommentedTextFrontmatter = "<!--[metaData]>\n+++\ntitle = \"Network configuration\"\ndescription = \"Docker networking\"\nkeywords = [\"network\"]\n[menu.main]\nparent= \"smn_administrate\"\n+++\n<![end-metadata]-->\n\n# Network configuration\n\n##\nSummary" - contentWithCommentedLongFrontmatter = "<!--[metaData123456789012345678901234567890]>\n+++\ntitle = \"Network configuration\"\ndescription = \"Docker networking\"\nkeywords = [\"network\"]\n[menu.main]\nparent= \"smn_administrate\"\n+++\n<![end-metadata]-->\n\n# Network configuration\n\n##\nSummary" - contentWithCommentedLong2Frontmatter = "<!--[metaData]>\n+++\ntitle = \"Network configuration\"\ndescription = \"Docker networking\"\nkeywords = [\"network\"]\n[menu.main]\nparent= \"smn_administrate\"\n+++\n<![end-metadata123456789012345678901234567890]-->\n\n# Network configuration\n\n##\nSummary" - invalidFrontmatterShortDelim = ` --- -title: Short delim start ---- -Short Delim -` - - invalidFrontmatterShortDelimEnding = ` ---- -title: Short delim ending --- -Short Delim -` + homePage = "---\ntitle: Home\n---\nHome Page Content\n" + simplePage = "---\ntitle: Simple\n---\nSimple Page\n" - invalidFrontmatterLadingWs = ` - - --- -title: Leading WS ---- -Leading -` - - simplePageJSON = ` -{ -"title": "spf13-vim 3.0 release and new website", -"description": "spf13-vim is a cross platform distribution of vim plugins and resources for Vim.", -"tags": [ ".vimrc", "plugins", "spf13-vim", "VIm" ], -"date": "2012-04-06", -"categories": [ - "Development", - "VIM" -], -"slug": "-spf13-vim-3-0-release-and-new-website-" -} - -Content of the file goes Here -` - - simplePageRFC3339Date = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content" - simplePageJSONMultiple = ` -{ - "title": "foobar", - "customData": { "foo": "bar" }, - "date": "2012-08-06" -} -Some text -` + simplePageRFC3339Date = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content" simplePageWithSummaryDelimiter = `--- title: Simple @@ -137,14 +83,6 @@ Summary Same Line<!--more--> Some more text ` - simplePageWithSummaryDelimiterOnlySummary = `--- -title: Simple ---- -Summary text - -<!--more--> -` - simplePageWithAllCJKRunes = `--- title: Simple --- @@ -334,156 +272,17 @@ date: '2013-10-15T06:16:13' UTF8 Page With Date` ) -var pageWithVariousFrontmatterTypes = `+++ -a_string = "bar" -an_integer = 1 -a_float = 1.3 -a_bool = false -a_date = 1979-05-27T07:32:00Z - -[a_table] -a_key = "a_value" -+++ -Front Matter with various frontmatter types` - -var pageWithCalendarYAMLFrontmatter = `--- -type: calendar -weeks: - - - start: "Jan 5" - days: - - activity: class - room: EN1000 - - activity: lab - - activity: class - - activity: lab - - activity: class - - - start: "Jan 12" - days: - - activity: class - - activity: lab - - activity: class - - activity: lab - - activity: exam ---- - -Hi. -` - -var pageWithCalendarJSONFrontmatter = `{ - "type": "calendar", - "weeks": [ - { - "start": "Jan 5", - "days": [ - { "activity": "class", "room": "EN1000" }, - { "activity": "lab" }, - { "activity": "class" }, - { "activity": "lab" }, - { "activity": "class" } - ] - }, - { - "start": "Jan 12", - "days": [ - { "activity": "class" }, - { "activity": "lab" }, - { "activity": "class" }, - { "activity": "lab" }, - { "activity": "exam" } - ] - } - ] -} - -Hi. -` - -var pageWithCalendarTOMLFrontmatter = `+++ -type = "calendar" - -[[weeks]] -start = "Jan 5" - -[[weeks.days]] -activity = "class" -room = "EN1000" - -[[weeks.days]] -activity = "lab" - -[[weeks.days]] -activity = "class" - -[[weeks.days]] -activity = "lab" - -[[weeks.days]] -activity = "class" - -[[weeks]] -start = "Jan 12" - -[[weeks.days]] -activity = "class" - -[[weeks.days]] -activity = "lab" - -[[weeks.days]] -activity = "class" - -[[weeks.days]] -activity = "lab" - -[[weeks.days]] -activity = "exam" -+++ - -Hi. -` - -func checkError(t *testing.T, err error, expected string) { - if err == nil { - t.Fatalf("err is nil. Expected: %s", expected) - } - if !strings.Contains(err.Error(), expected) { - t.Errorf("err.Error() returned: '%s'. Expected: '%s'", err.Error(), expected) - } -} - -func TestDegenerateEmptyPageZeroLengthName(t *testing.T) { - t.Parallel() - s := newTestSite(t) - _, err := s.NewPage("") - if err == nil { - t.Fatalf("A zero length page name must return an error") - } - - checkError(t, err, "Zero length page name") -} - -func TestDegenerateEmptyPage(t *testing.T) { - t.Parallel() - s := newTestSite(t) - _, err := s.newPageFrom(strings.NewReader(emptyPage), "test") - if err != nil { - t.Fatalf("Empty files should not trigger an error. Should be able to touch a file while watching without erroring out.") - } -} - -func checkPageTitle(t *testing.T, page *Page, title string) { - if page.title != title { - t.Fatalf("Page title is: %s. Expected %s", page.title, title) +func checkPageTitle(t *testing.T, page page.Page, title string) { + if page.Title() != title { + t.Fatalf("Page title is: %s. Expected %s", page.Title(), title) } } -func checkPageContent(t *testing.T, page *Page, content string, msg ...interface{}) { - a := normalizeContent(content) - b := normalizeContent(string(page.content())) +func checkPageContent(t *testing.T, page page.Page, expected string, msg ...interface{}) { + a := normalizeContent(expected) + b := normalizeContent(content(page)) if a != b { - t.Log(trace()) + t.Log(stackTrace()) t.Fatalf("Page content is:\n%q\nExpected:\n%q (%q)", b, a, msg) } } @@ -499,42 +298,29 @@ func normalizeContent(c string) string { return strings.TrimSpace(norm) } -func checkPageTOC(t *testing.T, page *Page, toc string) { - if page.TableOfContents != template.HTML(toc) { - t.Fatalf("Page TableOfContents is: %q.\nExpected %q", page.TableOfContents, toc) +func checkPageTOC(t *testing.T, page page.Page, toc string) { + if page.TableOfContents() != template.HTML(toc) { + t.Fatalf("Page TableOfContents is: %q.\nExpected %q", page.TableOfContents(), toc) } } -func checkPageSummary(t *testing.T, page *Page, summary string, msg ...interface{}) { - a := normalizeContent(string(page.summary)) +func checkPageSummary(t *testing.T, page page.Page, summary string, msg ...interface{}) { + a := normalizeContent(string(page.Summary())) b := normalizeContent(summary) if a != b { t.Fatalf("Page summary is:\n%q.\nExpected\n%q (%q)", a, b, msg) } } -func checkPageType(t *testing.T, page *Page, pageType string) { +func checkPageType(t *testing.T, page page.Page, pageType string) { if page.Type() != pageType { t.Fatalf("Page type is: %s. Expected: %s", page.Type(), pageType) } } -func checkPageDate(t *testing.T, page *Page, time time.Time) { - if page.Date != time { - t.Fatalf("Page date is: %s. Expected: %s", page.Date, time) - } -} - -func checkTruncation(t *testing.T, page *Page, shouldBe bool, msg string) { - if page.Summary() == "" { - t.Fatal("page has no summary, can not check truncation") - } - if page.truncated != shouldBe { - if shouldBe { - t.Fatalf("page wasn't truncated: %s", msg) - } else { - t.Fatalf("page was truncated: %s", msg) - } +func checkPageDate(t *testing.T, page page.Page, time time.Time) { + if page.Date() != time { + t.Fatalf("Page date is: %s. Expected: %s", page.Date(), time) } } @@ -562,7 +348,7 @@ func normalizeExpected(ext, str string) string { } func testAllMarkdownEnginesForPages(t *testing.T, - assertFunc func(t *testing.T, ext string, pages Pages), settings map[string]interface{}, pageSources ...string) { + assertFunc func(t *testing.T, ext string, pages page.Pages), settings map[string]interface{}, pageSources ...string) { engines := []struct { ext string @@ -607,33 +393,93 @@ func testAllMarkdownEnginesForPages(t *testing.T, s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) - require.Len(t, s.RegularPages, len(pageSources)) + require.Len(t, s.RegularPages(), len(pageSources)) - assertFunc(t, e.ext, s.RegularPages) + assertFunc(t, e.ext, s.RegularPages()) home, err := s.Info.Home() require.NoError(t, err) require.NotNil(t, home) - require.Equal(t, homePath, home.Path()) - require.Contains(t, home.content(), "Home Page Content") + require.Equal(t, homePath, home.File().Path()) + require.Contains(t, content(home), "Home Page Content") + + } +} + +// Issue #1076 +func TestPageWithDelimiterForMarkdownThatCrossesBorder(t *testing.T) { + t.Parallel() + cfg, fs := newTestCfg() + + writeSource(t, fs, filepath.Join("content", "simple.md"), simplePageWithSummaryDelimiterAndMarkdownThatCrossesBorder) + + s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) + + require.Len(t, s.RegularPages(), 1) + + p := s.RegularPages()[0] + + if p.Summary() != template.HTML( + "<p>The <a href=\"http://gohugo.io/\">best static site generator</a>.<sup class=\"footnote-ref\" id=\"fnref:1\"><a href=\"#fn:1\">1</a></sup></p>") { + t.Fatalf("Got summary:\n%q", p.Summary()) + } + + c := content(p) + if c != "<p>The <a href=\"http://gohugo.io/\">best static site generator</a>.<sup class=\"footnote-ref\" id=\"fnref:1\"><a href=\"#fn:1\">1</a></sup></p>\n\n<div class=\"footnotes\">\n\n<hr />\n\n<ol>\n<li id=\"fn:1\">Many people say so.\n <a class=\"footnote-return\" href=\"#fnref:1\"><sup>[return]</sup></a></li>\n</ol>\n</div>" { + t.Fatalf("Got content:\n%q", c) + } +} + +func TestPageDatesAllKinds(t *testing.T) { + t.Parallel() + assert := assert.New(t) + + pageContent := ` +--- +title: Page +date: 2017-01-15 +tags: ["hugo"] +categories: ["cool stuff"] +--- +` + + b := newTestSitesBuilder(t) + b.WithSimpleConfigFile().WithContent("page.md", pageContent) + b.WithSimpleConfigFile().WithContent("blog/page.md", pageContent) + + b.CreateSites().Build(BuildCfg{}) + + assert.Equal(1, len(b.H.Sites)) + s := b.H.Sites[0] + + checkDate := func(t time.Time, msg string) { + assert.Equal(2017, t.Year(), msg) + } + + checkDated := func(d resource.Dated, msg string) { + checkDate(d.Date(), "date: "+msg) + checkDate(d.Lastmod(), "lastmod: "+msg) } + for _, p := range s.Pages() { + checkDated(p, p.Kind()) + } + checkDate(s.Info.LastChange(), "site") } func TestCreateNewPage(t *testing.T) { t.Parallel() - assertFunc := func(t *testing.T, ext string, pages Pages) { + assertFunc := func(t *testing.T, ext string, pages page.Pages) { p := pages[0] // issue #2290: Path is relative to the content dir and will continue to be so. - require.Equal(t, filepath.FromSlash(fmt.Sprintf("p0.%s", ext)), p.Path()) + require.Equal(t, filepath.FromSlash(fmt.Sprintf("p0.%s", ext)), p.File().Path()) assert.False(t, p.IsHome()) checkPageTitle(t, p, "Simple") checkPageContent(t, p, normalizeExpected(ext, "<p>Simple Page</p>\n")) checkPageSummary(t, p, "Simple Page") checkPageType(t, p, "page") - checkTruncation(t, p, false, "simple short page") } settings := map[string]interface{}{ @@ -645,43 +491,17 @@ func TestCreateNewPage(t *testing.T) { func TestPageWithDelimiter(t *testing.T) { t.Parallel() - assertFunc := func(t *testing.T, ext string, pages Pages) { + assertFunc := func(t *testing.T, ext string, pages page.Pages) { p := pages[0] checkPageTitle(t, p, "Simple") checkPageContent(t, p, normalizeExpected(ext, "<p>Summary Next Line</p>\n\n<p>Some more text</p>\n"), ext) checkPageSummary(t, p, normalizeExpected(ext, "<p>Summary Next Line</p>"), ext) checkPageType(t, p, "page") - checkTruncation(t, p, true, "page with summary delimiter") } testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithSummaryDelimiter) } -// Issue #1076 -func TestPageWithDelimiterForMarkdownThatCrossesBorder(t *testing.T) { - t.Parallel() - cfg, fs := newTestCfg() - - writeSource(t, fs, filepath.Join("content", "simple.md"), simplePageWithSummaryDelimiterAndMarkdownThatCrossesBorder) - - s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) - - require.Len(t, s.RegularPages, 1) - - p := s.RegularPages[0] - - if p.Summary() != template.HTML( - "<p>The <a href=\"http://gohugo.io/\">best static site generator</a>.<sup class=\"footnote-ref\" id=\"fnref:1\"><a href=\"#fn:1\">1</a></sup></p>") { - t.Fatalf("Got summary:\n%q", p.Summary()) - } - - if p.content() != template.HTML( - "<p>The <a href=\"http://gohugo.io/\">best static site generator</a>.<sup class=\"footnote-ref\" id=\"fnref:1\"><a href=\"#fn:1\">1</a></sup></p>\n\n<div class=\"footnotes\">\n\n<hr />\n\n<ol>\n<li id=\"fn:1\">Many people say so.\n <a class=\"footnote-return\" href=\"#fnref:1\"><sup>[return]</sup></a></li>\n</ol>\n</div>") { - - t.Fatalf("Got content:\n%q", p.content()) - } -} - // Issue #3854 // Also see https://github.com/gohugoio/hugo/issues/3977 func TestPageWithDateFields(t *testing.T) { @@ -693,8 +513,8 @@ weight: %d --- Simple Page With Some Date` - hasDate := func(p *Page) bool { - return p.Date.Year() == 2017 + hasDate := func(p page.Page) bool { + return p.Date().Year() == 2017 } datePage := func(field string, weight int) string { @@ -702,7 +522,7 @@ Simple Page With Some Date` } t.Parallel() - assertFunc := func(t *testing.T, ext string, pages Pages) { + assertFunc := func(t *testing.T, ext string, pages page.Pages) { assert.True(len(pages) > 0) for _, p := range pages { assert.True(hasDate(p)) @@ -733,8 +553,8 @@ title: Raw s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) - require.Len(t, s.RegularPages, 1) - p := s.RegularPages[0] + require.Len(t, s.RegularPages(), 1) + p := s.RegularPages()[0] require.Equal(t, p.RawContent(), "**Raw**") @@ -742,7 +562,7 @@ title: Raw func TestPageWithShortCodeInSummary(t *testing.T) { t.Parallel() - assertFunc := func(t *testing.T, ext string, pages Pages) { + assertFunc := func(t *testing.T, ext string, pages page.Pages) { p := pages[0] checkPageTitle(t, p, "Simple") checkPageContent(t, p, normalizeExpected(ext, "<p>Summary Next Line. <figure> <img src=\"/not/real\"/> </figure> . More text here.</p><p>Some more text</p>")) @@ -755,7 +575,7 @@ func TestPageWithShortCodeInSummary(t *testing.T) { func TestPageWithEmbeddedScriptTag(t *testing.T) { t.Parallel() - assertFunc := func(t *testing.T, ext string, pages Pages) { + assertFunc := func(t *testing.T, ext string, pages page.Pages) { p := pages[0] if ext == "ad" || ext == "rst" { // TOD(bep) @@ -775,9 +595,9 @@ func TestPageWithAdditionalExtension(t *testing.T) { s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) - require.Len(t, s.RegularPages, 1) + require.Len(t, s.RegularPages(), 1) - p := s.RegularPages[0] + p := s.RegularPages()[0] checkPageContent(t, p, "<p>first line.<br />\nsecond line.</p>\n\n<p>fourth line.</p>\n") } @@ -790,9 +610,9 @@ func TestTableOfContents(t *testing.T) { s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) - require.Len(t, s.RegularPages, 1) + require.Len(t, s.RegularPages(), 1) - p := s.RegularPages[0] + p := s.RegularPages()[0] checkPageContent(t, p, "\n\n<p>For some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.</p>\n\n<h2 id=\"aa\">AA</h2>\n\n<p>I have no idea, of course, how long it took me to reach the limit of the plain,\nbut at last I entered the foothills, following a pretty little canyon upward\ntoward the mountains. Beside me frolicked a laughing brooklet, hurrying upon\nits noisy way down to the silent sea. In its quieter pools I discovered many\nsmall fish, of four-or five-pound weight I should imagine. In appearance,\nexcept as to size and color, they were not unlike the whale of our own seas. As\nI watched them playing about I discovered, not only that they suckled their\nyoung, but that at intervals they rose to the surface to breathe as well as to\nfeed upon certain grasses and a strange, scarlet lichen which grew upon the\nrocks just above the water line.</p>\n\n<h3 id=\"aaa\">AAA</h3>\n\n<p>I remember I felt an extraordinary persuasion that I was being played with,\nthat presently, when I was upon the very verge of safety, this mysterious\ndeath–as swift as the passage of light–would leap after me from the pit about\nthe cylinder and strike me down. ## BB</p>\n\n<h3 id=\"bbb\">BBB</h3>\n\n<p>“You’re a great Granser,” he cried delightedly, “always making believe them little marks mean something.”</p>\n") checkPageTOC(t, p, "<nav id=\"TableOfContents\">\n<ul>\n<li>\n<ul>\n<li><a href=\"#aa\">AA</a>\n<ul>\n<li><a href=\"#aaa\">AAA</a></li>\n<li><a href=\"#bbb\">BBB</a></li>\n</ul></li>\n</ul></li>\n</ul>\n</nav>") @@ -800,7 +620,7 @@ func TestTableOfContents(t *testing.T) { func TestPageWithMoreTag(t *testing.T) { t.Parallel() - assertFunc := func(t *testing.T, ext string, pages Pages) { + assertFunc := func(t *testing.T, ext string, pages page.Pages) { p := pages[0] checkPageTitle(t, p, "Simple") checkPageContent(t, p, normalizeExpected(ext, "<p>Summary Same Line</p>\n\n<p>Some more text</p>\n")) @@ -812,20 +632,10 @@ func TestPageWithMoreTag(t *testing.T) { testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithSummaryDelimiterSameLine) } -func TestPageWithMoreTagOnlySummary(t *testing.T) { - - assertFunc := func(t *testing.T, ext string, pages Pages) { - p := pages[0] - checkTruncation(t, p, false, "page with summary delimiter at end") - } - - testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithSummaryDelimiterOnlySummary) -} - // #2973 func TestSummaryWithHTMLTagsOnNextLine(t *testing.T) { - assertFunc := func(t *testing.T, ext string, pages Pages) { + assertFunc := func(t *testing.T, ext string, pages page.Pages) { p := pages[0] require.Contains(t, p.Summary(), "Happy new year everyone!") require.NotContains(t, p.Summary(), "User interface") @@ -853,9 +663,9 @@ func TestPageWithDate(t *testing.T) { s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) - require.Len(t, s.RegularPages, 1) + require.Len(t, s.RegularPages(), 1) - p := s.RegularPages[0] + p := s.RegularPages()[0] d, _ := time.Parse(time.RFC3339, "2013-05-17T16:59:30Z") checkPageDate(t, p, d) @@ -905,16 +715,16 @@ func TestPageWithLastmodFromGitInfo(t *testing.T) { require.NoError(t, h.Build(BuildCfg{SkipRender: true})) enSite := h.Sites[0] - assrt.Len(enSite.RegularPages, 1) + assrt.Len(enSite.RegularPages(), 1) // 2018-03-11 is the Git author date for testsite/content/first-post.md - assrt.Equal("2018-03-11", enSite.RegularPages[0].Lastmod.Format("2006-01-02")) + assrt.Equal("2018-03-11", enSite.RegularPages()[0].Lastmod().Format("2006-01-02")) nnSite := h.Sites[1] - assrt.Len(nnSite.RegularPages, 1) + assrt.Len(nnSite.RegularPages(), 1) // 2018-08-11 is the Git author date for testsite/content_nn/first-post.md - assrt.Equal("2018-08-11", nnSite.RegularPages[0].Lastmod.Format("2006-01-02")) + assrt.Equal("2018-08-11", nnSite.RegularPages()[0].Lastmod().Format("2006-01-02")) } @@ -953,28 +763,28 @@ Content s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) - assrt.Len(s.RegularPages, 2) + assrt.Len(s.RegularPages(), 2) - noSlug := s.RegularPages[0] - slug := s.RegularPages[1] + noSlug := s.RegularPages()[0] + slug := s.RegularPages()[1] - assrt.Equal(28, noSlug.Lastmod.Day()) + assrt.Equal(28, noSlug.Lastmod().Day()) switch strings.ToLower(dateHandler) { case ":filename": - assrt.False(noSlug.Date.IsZero()) - assrt.False(slug.Date.IsZero()) - assrt.Equal(2012, noSlug.Date.Year()) - assrt.Equal(2012, slug.Date.Year()) - assrt.Equal("noslug", noSlug.Slug) - assrt.Equal("aslug", slug.Slug) + assrt.False(noSlug.Date().IsZero()) + assrt.False(slug.Date().IsZero()) + assrt.Equal(2012, noSlug.Date().Year()) + assrt.Equal(2012, slug.Date().Year()) + assrt.Equal("noslug", noSlug.Slug()) + assrt.Equal("aslug", slug.Slug()) case ":filemodtime": - assrt.Equal(c1fi.ModTime().Year(), noSlug.Date.Year()) - assrt.Equal(c2fi.ModTime().Year(), slug.Date.Year()) + assrt.Equal(c1fi.ModTime().Year(), noSlug.Date().Year()) + assrt.Equal(c2fi.ModTime().Year(), slug.Date().Year()) fallthrough default: - assrt.Equal("", noSlug.Slug) - assrt.Equal("aslug", slug.Slug) + assrt.Equal("", noSlug.Slug()) + assrt.Equal("aslug", slug.Slug()) } }) @@ -984,10 +794,10 @@ Content func TestWordCountWithAllCJKRunesWithoutHasCJKLanguage(t *testing.T) { t.Parallel() - assertFunc := func(t *testing.T, ext string, pages Pages) { + assertFunc := func(t *testing.T, ext string, pages page.Pages) { p := pages[0] if p.WordCount() != 8 { - t.Fatalf("[%s] incorrect word count for content '%s'. expected %v, got %v", ext, p.plain, 8, p.WordCount()) + t.Fatalf("[%s] incorrect word count. expected %v, got %v", ext, 8, p.WordCount()) } } @@ -998,10 +808,10 @@ func TestWordCountWithAllCJKRunesHasCJKLanguage(t *testing.T) { t.Parallel() settings := map[string]interface{}{"hasCJKLanguage": true} - assertFunc := func(t *testing.T, ext string, pages Pages) { + assertFunc := func(t *testing.T, ext string, pages page.Pages) { p := pages[0] if p.WordCount() != 15 { - t.Fatalf("[%s] incorrect word count for content '%s'. expected %v, got %v", ext, p.plain, 15, p.WordCount()) + t.Fatalf("[%s] incorrect word count, expected %v, got %v", ext, 15, p.WordCount()) } } testAllMarkdownEnginesForPages(t, assertFunc, settings, simplePageWithAllCJKRunes) @@ -1011,15 +821,15 @@ func TestWordCountWithMainEnglishWithCJKRunes(t *testing.T) { t.Parallel() settings := map[string]interface{}{"hasCJKLanguage": true} - assertFunc := func(t *testing.T, ext string, pages Pages) { + assertFunc := func(t *testing.T, ext string, pages page.Pages) { p := pages[0] if p.WordCount() != 74 { - t.Fatalf("[%s] incorrect word count for content '%s'. expected %v, got %v", ext, p.plain, 74, p.WordCount()) + t.Fatalf("[%s] incorrect word count, expected %v, got %v", ext, 74, p.WordCount()) } - if p.summary != simplePageWithMainEnglishWithCJKRunesSummary { - t.Fatalf("[%s] incorrect Summary for content '%s'. expected %v, got %v", ext, p.plain, - simplePageWithMainEnglishWithCJKRunesSummary, p.summary) + if p.Summary() != simplePageWithMainEnglishWithCJKRunesSummary { + t.Fatalf("[%s] incorrect Summary for content '%s'. expected %v, got %v", ext, p.Plain(), + simplePageWithMainEnglishWithCJKRunesSummary, p.Summary()) } } @@ -1032,15 +842,15 @@ func TestWordCountWithIsCJKLanguageFalse(t *testing.T) { "hasCJKLanguage": true, } - assertFunc := func(t *testing.T, ext string, pages Pages) { + assertFunc := func(t *testing.T, ext string, pages page.Pages) { p := pages[0] if p.WordCount() != 75 { - t.Fatalf("[%s] incorrect word count for content '%s'. expected %v, got %v", ext, p.plain, 74, p.WordCount()) + t.Fatalf("[%s] incorrect word count for content '%s'. expected %v, got %v", ext, p.Plain(), 74, p.WordCount()) } - if p.summary != simplePageWithIsCJKLanguageFalseSummary { - t.Fatalf("[%s] incorrect Summary for content '%s'. expected %v, got %v", ext, p.plain, - simplePageWithIsCJKLanguageFalseSummary, p.summary) + if p.Summary() != simplePageWithIsCJKLanguageFalseSummary { + t.Fatalf("[%s] incorrect Summary for content '%s'. expected %v, got %v", ext, p.Plain(), + simplePageWithIsCJKLanguageFalseSummary, p.Summary()) } } @@ -1050,7 +860,7 @@ func TestWordCountWithIsCJKLanguageFalse(t *testing.T) { func TestWordCount(t *testing.T) { t.Parallel() - assertFunc := func(t *testing.T, ext string, pages Pages) { + assertFunc := func(t *testing.T, ext string, pages page.Pages) { p := pages[0] if p.WordCount() != 483 { t.Fatalf("[%s] incorrect word count. expected %v, got %v", ext, 483, p.WordCount()) @@ -1064,163 +874,11 @@ func TestWordCount(t *testing.T) { t.Fatalf("[%s] incorrect min read. expected %v, got %v", ext, 3, p.ReadingTime()) } - checkTruncation(t, p, true, "long page") } testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithLongContent) } -func TestCreatePage(t *testing.T) { - t.Parallel() - var tests = []struct { - r string - }{ - {simplePageJSON}, - {simplePageJSONMultiple}, - //{strings.NewReader(SIMPLE_PAGE_JSON_COMPACT)}, - } - - for i, test := range tests { - s := newTestSite(t) - p, _ := s.NewPage("page") - if _, err := p.ReadFrom(strings.NewReader(test.r)); err != nil { - t.Fatalf("[%d] Unable to parse page: %s", i, err) - } - } -} - -func TestDegenerateInvalidFrontMatterShortDelim(t *testing.T) { - t.Parallel() - var tests = []struct { - r string - err string - }{ - {invalidFrontmatterShortDelimEnding, "EOF looking for end YAML front matter delimiter"}, - } - for _, test := range tests { - s := newTestSite(t) - p, _ := s.NewPage("invalid/front/matter/short/delim") - _, err := p.ReadFrom(strings.NewReader(test.r)) - checkError(t, err, test.err) - } -} - -func TestShouldRenderContent(t *testing.T) { - t.Parallel() - assert := require.New(t) - - var tests = []struct { - text string - render bool - }{ - {contentNoFrontmatter, true}, - {renderNoFrontmatter, false}, - {contentWithCommentedFrontmatter, true}, - {contentWithCommentedTextFrontmatter, true}, - {contentWithCommentedLongFrontmatter, true}, - {contentWithCommentedLong2Frontmatter, true}, - } - - for i, test := range tests { - s := newTestSite(t) - p, _ := s.NewPage("render/front/matter") - _, err := p.ReadFrom(strings.NewReader(test.text)) - msg := fmt.Sprintf("test %d", i) - assert.NoError(err, msg) - assert.Equal(test.render, p.IsRenderable(), msg) - } -} - -// Issue #768 -func TestCalendarParamsVariants(t *tes |