Make Page.Content a method that returns interface{}

To prepare for a `Resource.Content` method.

See #4622
This commit is contained in:
Bjørn Erik Pedersen 2018-04-15 16:42:54 +02:00
parent 94c8b29c39
commit 417c5e2b67
8 changed files with 28 additions and 24 deletions

View file

@ -69,7 +69,7 @@ func doTestShortcodeCrossrefs(t *testing.T, relative bool) {
require.Len(t, s.RegularPages, 1)
output := string(s.RegularPages[0].Content)
output := string(s.RegularPages[0].content)
if !strings.Contains(output, expected) {
t.Errorf("Got\n%q\nExpected\n%q", output, expected)

View file

@ -378,9 +378,9 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) {
b.AssertFileContent("public/en/tags/tag1/index.html", "Tag1|Hello|http://example.com/blog/en/tags/tag1/")
// Check Blackfriday config
require.True(t, strings.Contains(string(doc1fr.Content), "«"), string(doc1fr.Content))
require.False(t, strings.Contains(string(doc1en.Content), "«"), string(doc1en.Content))
require.True(t, strings.Contains(string(doc1en.Content), "“"), string(doc1en.Content))
require.True(t, strings.Contains(string(doc1fr.content), "«"), string(doc1fr.content))
require.False(t, strings.Contains(string(doc1en.content), "«"), string(doc1en.content))
require.True(t, strings.Contains(string(doc1en.content), "“"), string(doc1en.content))
// Check that the drafts etc. are not built/processed/rendered.
assertShouldNotBuild(t, b.H)
@ -630,9 +630,9 @@ func assertShouldNotBuild(t *testing.T, sites *HugoSites) {
for _, p := range s.rawAllPages {
// No HTML when not processed
require.Equal(t, p.shouldBuild(), bytes.Contains(p.workContent, []byte("</")), p.BaseFileName()+": "+string(p.workContent))
require.Equal(t, p.shouldBuild(), p.Content != "", p.BaseFileName())
require.Equal(t, p.shouldBuild(), p.content != "", p.BaseFileName())
require.Equal(t, p.shouldBuild(), p.Content != "", p.BaseFileName())
require.Equal(t, p.shouldBuild(), p.content != "", p.BaseFileName())
}
}

View file

@ -128,7 +128,7 @@ type Page struct {
params map[string]interface{}
// Content sections
Content template.HTML
content template.HTML
Summary template.HTML
TableOfContents template.HTML
@ -263,6 +263,10 @@ type Page struct {
targetPathDescriptorPrototype *targetPathDescriptor
}
func (p *Page) Content() (interface{}, error) {
return p.content, nil
}
// Sites is a convenience method to get all the Hugo sites/languages configured.
func (p *Page) Sites() SiteInfos {
infos := make(SiteInfos, len(p.s.owner.Sites))
@ -462,7 +466,7 @@ func (p *Page) PlainWords() []string {
func (p *Page) initPlain() {
p.plainInit.Do(func() {
p.plain = helpers.StripHTML(string(p.Content))
p.plain = helpers.StripHTML(string(p.content))
return
})
}
@ -1098,7 +1102,7 @@ func (p *Page) prepareForRender(cfg *BuildCfg) error {
workContentCopy = summaryContent.content
}
p.Content = helpers.BytesToHTML(workContentCopy)
p.content = helpers.BytesToHTML(workContentCopy)
if summaryContent == nil {
if err := p.setAutoSummary(); err != nil {
@ -1107,7 +1111,7 @@ func (p *Page) prepareForRender(cfg *BuildCfg) error {
}
} else {
p.Content = helpers.BytesToHTML(workContentCopy)
p.content = helpers.BytesToHTML(workContentCopy)
}
//analyze for raw stats
@ -1720,7 +1724,7 @@ func (p *Page) prepareLayouts() error {
if p.Kind == KindPage {
if !p.IsRenderable() {
self := "__" + p.UniqueID()
err := p.s.TemplateHandler().AddLateTemplate(self, string(p.Content))
err := p.s.TemplateHandler().AddLateTemplate(self, string(p.content))
if err != nil {
return err
}

View file

@ -237,7 +237,7 @@ func (p Pages) ByLength() Pages {
key := "pageSort.ByLength"
length := func(p1, p2 *Page) bool {
return len(p1.Content) < len(p2.Content)
return len(p1.content) < len(p2.content)
}
pages, _ := spc.get(key, pageBy(length).Sort, p)

View file

@ -80,7 +80,7 @@ func TestSortByN(t *testing.T) {
{(Pages).ByPublishDate, func(p Pages) bool { return p[0].PublishDate == d4 }},
{(Pages).ByExpiryDate, func(p Pages) bool { return p[0].ExpiryDate == d4 }},
{(Pages).ByLastmod, func(p Pages) bool { return p[1].Lastmod == d3 }},
{(Pages).ByLength, func(p Pages) bool { return p[0].Content == "b_content" }},
{(Pages).ByLength, func(p Pages) bool { return p[0].content == "b_content" }},
} {
setSortVals([4]time.Time{d1, d2, d3, d4}, [4]string{"b", "ab", "cde", "fg"}, [4]int{0, 3, 2, 1}, p)
@ -168,7 +168,7 @@ func setSortVals(dates [4]time.Time, titles [4]string, weights [4]int, pages Pag
pages[len(dates)-1-i].linkTitle = pages[i].title + "l"
pages[len(dates)-1-i].PublishDate = dates[i]
pages[len(dates)-1-i].ExpiryDate = dates[i]
pages[len(dates)-1-i].Content = template.HTML(titles[i] + "_content")
pages[len(dates)-1-i].content = template.HTML(titles[i] + "_content")
}
lastLastMod := pages[2].Lastmod
pages[2].Lastmod = pages[1].Lastmod

View file

@ -87,7 +87,7 @@ func TestPageBundlerSiteRegular(t *testing.T) {
assert.Equal(singlePage, s.getPage("page", "a/1"))
assert.Equal(singlePage, s.getPage("page", "1"))
assert.Contains(singlePage.Content, "TheContent")
assert.Contains(singlePage.content, "TheContent")
if ugly {
assert.Equal("/a/1.html", singlePage.RelPermalink())
@ -129,7 +129,7 @@ func TestPageBundlerSiteRegular(t *testing.T) {
firstPage := pageResources[0].(*Page)
secondPage := pageResources[1].(*Page)
assert.Equal(filepath.FromSlash("b/my-bundle/1.md"), firstPage.pathOrTitle(), secondPage.pathOrTitle())
assert.Contains(firstPage.Content, "TheContent")
assert.Contains(firstPage.content, "TheContent")
assert.Equal(6, len(leafBundle1.Resources))
// https://github.com/gohugoio/hugo/issues/4582
@ -395,7 +395,7 @@ HEADLESS {{< myShort >}}
assert.Equal("Headless Bundle in Topless Bar", headless.Title())
assert.Equal("", headless.RelPermalink())
assert.Equal("", headless.Permalink())
assert.Contains(headless.Content, "HEADLESS SHORTCODE")
assert.Contains(headless.content, "HEADLESS SHORTCODE")
headlessResources := headless.Resources
assert.Equal(3, len(headlessResources))
@ -404,7 +404,7 @@ HEADLESS {{< myShort >}}
assert.NotNil(pageResource)
assert.IsType(&Page{}, pageResource)
p := pageResource.(*Page)
assert.Contains(p.Content, "SHORTCODE")
assert.Contains(p.content, "SHORTCODE")
assert.Equal("p1.md", p.Name())
th := testHelper{s.Cfg, s.Fs, t}

View file

@ -481,7 +481,7 @@ func checkPageTitle(t *testing.T, page *Page, title string) {
func checkPageContent(t *testing.T, page *Page, content string, msg ...interface{}) {
a := normalizeContent(content)
b := normalizeContent(string(page.Content))
b := normalizeContent(string(page.content))
if a != b {
t.Fatalf("Page content is:\n%q\nExpected:\n%q (%q)", b, a, msg)
}
@ -616,7 +616,7 @@ func testAllMarkdownEnginesForPages(t *testing.T,
require.NoError(t, err)
require.NotNil(t, home)
require.Equal(t, homePath, home.Path())
require.Contains(t, home.Content, "Home Page Content")
require.Contains(t, home.content, "Home Page Content")
}
@ -726,8 +726,8 @@ func TestPageWithDelimiterForMarkdownThatCrossesBorder(t *testing.T) {
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>\n</p>\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)
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>\n</p>\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)
}
}
@ -1511,7 +1511,7 @@ func TestPageSimpleMethods(t *testing.T) {
} {
p, _ := s.NewPage("Test")
p.Content = "<h1>Do Be Do Be Do</h1>"
p.content = "<h1>Do Be Do Be Do</h1>"
if !this.assertFunc(p) {
t.Errorf("[%d] Page method error", i)
}

View file

@ -87,7 +87,7 @@ title: "Title"
require.Len(t, h.Sites[0].RegularPages, 1)
output := strings.TrimSpace(string(h.Sites[0].RegularPages[0].Content))
output := strings.TrimSpace(string(h.Sites[0].RegularPages[0].content))
output = strings.TrimPrefix(output, "<p>")
output = strings.TrimSuffix(output, "</p>")