mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
hugolib: make Page implement the ReaderFrom interface
This commit is contained in:
parent
bea42218b6
commit
be01d04870
4 changed files with 22 additions and 22 deletions
|
@ -35,7 +35,7 @@ func (b basicPageHandler) Read(f *source.File, s *Site) HandledResult {
|
||||||
return HandledResult{file: f, err: err}
|
return HandledResult{file: f, err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := page.ReadFrom(f.Contents); err != nil {
|
if _, err := page.ReadFrom(f.Contents); err != nil {
|
||||||
return HandledResult{file: f, err: err}
|
return HandledResult{file: f, err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -308,7 +308,7 @@ func NewPageFrom(buf io.Reader, name string) (*Page, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return p, err
|
return p, err
|
||||||
}
|
}
|
||||||
err = p.ReadFrom(buf)
|
_, err = p.ReadFrom(buf)
|
||||||
|
|
||||||
return p, err
|
return p, err
|
||||||
}
|
}
|
||||||
|
@ -324,14 +324,14 @@ func NewPage(name string) (*Page, error) {
|
||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Page) ReadFrom(buf io.Reader) (err error) {
|
func (p *Page) ReadFrom(buf io.Reader) (int64, error) {
|
||||||
// Parse for metadata & body
|
// Parse for metadata & body
|
||||||
if err = p.parse(buf); err != nil {
|
if err := p.parse(buf); err != nil {
|
||||||
jww.ERROR.Print(err)
|
jww.ERROR.Print(err)
|
||||||
return
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return int64(len(p.rawContent)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Page) analyzePage() {
|
func (p *Page) analyzePage() {
|
||||||
|
|
|
@ -51,7 +51,7 @@ func TestParseTaxonomies(t *testing.T) {
|
||||||
} {
|
} {
|
||||||
|
|
||||||
p, _ := NewPage("page/with/taxonomy")
|
p, _ := NewPage("page/with/taxonomy")
|
||||||
err := p.ReadFrom(strings.NewReader(test))
|
_, err := p.ReadFrom(strings.NewReader(test))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed parsing %q: %s", test, err)
|
t.Fatalf("Failed parsing %q: %s", test, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -320,7 +320,7 @@ func checkTruncation(t *testing.T, page *Page, shouldBe bool, msg string) {
|
||||||
|
|
||||||
func TestCreateNewPage(t *testing.T) {
|
func TestCreateNewPage(t *testing.T) {
|
||||||
p, _ := NewPage("simple.md")
|
p, _ := NewPage("simple.md")
|
||||||
err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE))
|
_, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE))
|
||||||
p.Convert()
|
p.Convert()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -336,7 +336,7 @@ func TestCreateNewPage(t *testing.T) {
|
||||||
|
|
||||||
func TestPageWithDelimiter(t *testing.T) {
|
func TestPageWithDelimiter(t *testing.T) {
|
||||||
p, _ := NewPage("simple.md")
|
p, _ := NewPage("simple.md")
|
||||||
err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER))
|
_, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER))
|
||||||
p.Convert()
|
p.Convert()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
|
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
|
||||||
|
@ -353,7 +353,7 @@ func TestPageWithShortCodeInSummary(t *testing.T) {
|
||||||
s := new(Site)
|
s := new(Site)
|
||||||
s.prepTemplates()
|
s.prepTemplates()
|
||||||
p, _ := NewPage("simple.md")
|
p, _ := NewPage("simple.md")
|
||||||
err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SHORTCODE_IN_SUMMARY))
|
_, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SHORTCODE_IN_SUMMARY))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
|
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -368,7 +368,7 @@ func TestPageWithShortCodeInSummary(t *testing.T) {
|
||||||
|
|
||||||
func TestPageWithEmbeddedScriptTag(t *testing.T) {
|
func TestPageWithEmbeddedScriptTag(t *testing.T) {
|
||||||
p, _ := NewPage("simple.md")
|
p, _ := NewPage("simple.md")
|
||||||
err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_EMBEDDED_SCRIPT))
|
_, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_EMBEDDED_SCRIPT))
|
||||||
p.Convert()
|
p.Convert()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
|
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
|
||||||
|
@ -378,7 +378,7 @@ func TestPageWithEmbeddedScriptTag(t *testing.T) {
|
||||||
|
|
||||||
func TestPageWithAdditionalExtension(t *testing.T) {
|
func TestPageWithAdditionalExtension(t *testing.T) {
|
||||||
p, _ := NewPage("simple.md")
|
p, _ := NewPage("simple.md")
|
||||||
err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_ADDITIONAL_EXTENSION))
|
_, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_ADDITIONAL_EXTENSION))
|
||||||
p.Convert()
|
p.Convert()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
|
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
|
||||||
|
@ -388,7 +388,7 @@ func TestPageWithAdditionalExtension(t *testing.T) {
|
||||||
|
|
||||||
func TestTableOfContents(t *testing.T) {
|
func TestTableOfContents(t *testing.T) {
|
||||||
p, _ := NewPage("tocpage.md")
|
p, _ := NewPage("tocpage.md")
|
||||||
err := p.ReadFrom(strings.NewReader(PAGE_WITH_TOC))
|
_, err := p.ReadFrom(strings.NewReader(PAGE_WITH_TOC))
|
||||||
p.Convert()
|
p.Convert()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
|
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
|
||||||
|
@ -399,7 +399,7 @@ func TestTableOfContents(t *testing.T) {
|
||||||
|
|
||||||
func TestPageWithMoreTag(t *testing.T) {
|
func TestPageWithMoreTag(t *testing.T) {
|
||||||
p, _ := NewPage("simple.md")
|
p, _ := NewPage("simple.md")
|
||||||
err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER_SAME_LINE))
|
_, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER_SAME_LINE))
|
||||||
p.Convert()
|
p.Convert()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
|
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
|
||||||
|
@ -413,7 +413,7 @@ func TestPageWithMoreTag(t *testing.T) {
|
||||||
|
|
||||||
func TestPageWithDate(t *testing.T) {
|
func TestPageWithDate(t *testing.T) {
|
||||||
p, _ := NewPage("simple.md")
|
p, _ := NewPage("simple.md")
|
||||||
err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_RFC3339_DATE))
|
_, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_RFC3339_DATE))
|
||||||
p.Convert()
|
p.Convert()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
|
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
|
||||||
|
@ -427,7 +427,7 @@ func TestPageWithDate(t *testing.T) {
|
||||||
|
|
||||||
func TestWordCount(t *testing.T) {
|
func TestWordCount(t *testing.T) {
|
||||||
p, _ := NewPage("simple.md")
|
p, _ := NewPage("simple.md")
|
||||||
err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_LONG_CONTENT))
|
_, err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_LONG_CONTENT))
|
||||||
p.Convert()
|
p.Convert()
|
||||||
p.analyzePage()
|
p.analyzePage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -461,7 +461,7 @@ func TestCreatePage(t *testing.T) {
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
p, _ := NewPage("page")
|
p, _ := NewPage("page")
|
||||||
if err := p.ReadFrom(strings.NewReader(test.r)); err != nil {
|
if _, err := p.ReadFrom(strings.NewReader(test.r)); err != nil {
|
||||||
t.Errorf("Unable to parse page: %s", err)
|
t.Errorf("Unable to parse page: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -477,7 +477,7 @@ func TestDegenerateInvalidFrontMatterShortDelim(t *testing.T) {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|
||||||
p, _ := NewPage("invalid/front/matter/short/delim")
|
p, _ := NewPage("invalid/front/matter/short/delim")
|
||||||
err := p.ReadFrom(strings.NewReader(test.r))
|
_, err := p.ReadFrom(strings.NewReader(test.r))
|
||||||
checkError(t, err, test.err)
|
checkError(t, err, test.err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -496,7 +496,7 @@ func TestShouldRenderContent(t *testing.T) {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|
||||||
p, _ := NewPage("render/front/matter")
|
p, _ := NewPage("render/front/matter")
|
||||||
err := p.ReadFrom(strings.NewReader(test.text))
|
_, err := p.ReadFrom(strings.NewReader(test.text))
|
||||||
p = pageMust(p, err)
|
p = pageMust(p, err)
|
||||||
if p.IsRenderable() != test.render {
|
if p.IsRenderable() != test.render {
|
||||||
t.Errorf("expected p.IsRenderable() == %t, got %t", test.render, p.IsRenderable())
|
t.Errorf("expected p.IsRenderable() == %t, got %t", test.render, p.IsRenderable())
|
||||||
|
@ -506,7 +506,7 @@ func TestShouldRenderContent(t *testing.T) {
|
||||||
|
|
||||||
func TestDifferentFrontMatterVarTypes(t *testing.T) {
|
func TestDifferentFrontMatterVarTypes(t *testing.T) {
|
||||||
page, _ := NewPage("test/file1.md")
|
page, _ := NewPage("test/file1.md")
|
||||||
_ = page.ReadFrom(strings.NewReader(PAGE_WITH_VARIOUS_FRONTMATTER_TYPES))
|
_, _ = page.ReadFrom(strings.NewReader(PAGE_WITH_VARIOUS_FRONTMATTER_TYPES))
|
||||||
|
|
||||||
dateval, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
|
dateval, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
|
||||||
if page.GetParam("a_string") != "bar" {
|
if page.GetParam("a_string") != "bar" {
|
||||||
|
@ -535,7 +535,7 @@ func TestDifferentFrontMatterVarTypes(t *testing.T) {
|
||||||
|
|
||||||
func TestDegenerateInvalidFrontMatterLeadingWhitespace(t *testing.T) {
|
func TestDegenerateInvalidFrontMatterLeadingWhitespace(t *testing.T) {
|
||||||
p, _ := NewPage("invalid/front/matter/leading/ws")
|
p, _ := NewPage("invalid/front/matter/leading/ws")
|
||||||
err := p.ReadFrom(strings.NewReader(INVALID_FRONT_MATTER_LEADING_WS))
|
_, err := p.ReadFrom(strings.NewReader(INVALID_FRONT_MATTER_LEADING_WS))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to parse front matter given leading whitespace: %s", err)
|
t.Fatalf("Unable to parse front matter given leading whitespace: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -588,7 +588,7 @@ func TestLayoutOverride(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
p, _ := NewPage(test.path)
|
p, _ := NewPage(test.path)
|
||||||
err := p.ReadFrom(strings.NewReader(test.content))
|
_, err := p.ReadFrom(strings.NewReader(test.content))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to parse content:\n%s\n", test.content)
|
t.Fatalf("Unable to parse content:\n%s\n", test.content)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue