From 311e10222301d47422f970c460383879ad78f681 Mon Sep 17 00:00:00 2001 From: Noah Campbell Date: Wed, 18 Sep 2013 14:21:27 -0700 Subject: [PATCH] Allow non-markdown content in content directory Allow content that is not markdown and does not need to be rendered to exists in the content directory. Currently any valid html or xml document can exist. Templates are applied to these documents as well. If you need to have content that doesn't have templates or AbsUrlify like operations, then continue to put this content in static and it will be copied over. --- hugolib/page.go | 23 ++++++++++++++--- hugolib/page_test.go | 6 ++--- hugolib/planner.go | 4 +-- hugolib/site.go | 18 +++++++++++--- hugolib/site_test.go | 59 +++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 97 insertions(+), 13 deletions(-) diff --git a/hugolib/page.go b/hugolib/page.go index 8daf73af1..192ec63b1 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -90,8 +90,7 @@ func newPage(filename string) *Page { page := Page{contentType: "", File: File{FileName: filename, Extension: "html"}, Node: Node{Keywords: make([]string, 10, 30)}, - Params: make(map[string]interface{}), - Markup: "md"} + Params: make(map[string]interface{})} page.Date, _ = time.Parse("20060102", "20080101") page.guessSection() return &page @@ -361,6 +360,18 @@ func (p *Page) ExecuteTemplate(layout string) *bytes.Buffer { return buffer } +func (page *Page) guessMarkupType() string { + if page.Markup != "" { + return page.Markup + } + + if strings.HasSuffix(page.FileName, ".md") { + return "md" + } + + return "unknown" +} + func (page *Page) parse(reader io.Reader) error { p, err := parser.ReadFrom(reader) if err != nil { @@ -383,11 +394,15 @@ func (page *Page) parse(reader io.Reader) error { } } - switch page.Markup { - case "md": + switch page.guessMarkupType() { + case "md", "markdown", "mdown": page.convertMarkdown(bytes.NewReader(p.Content())) case "rst": page.convertRestructuredText(bytes.NewReader(p.Content())) + case "html": + fallthrough + default: + page.Content = template.HTML(p.Content()) } return nil } diff --git a/hugolib/page_test.go b/hugolib/page_test.go index c82ee59f7..d16d7f071 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -170,7 +170,7 @@ func checkPageDate(t *testing.T, page *Page, time time.Time) { } func TestCreateNewPage(t *testing.T) { - p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE), "simple") + p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE), "simple.md") if err != nil { t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) } @@ -182,7 +182,7 @@ func TestCreateNewPage(t *testing.T) { } func TestPageWithDelimiter(t *testing.T) { - p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER), "simple") + p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER), "simple.md") if err != nil { t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) } @@ -195,7 +195,7 @@ func TestPageWithDelimiter(t *testing.T) { } func TestPageWithMoreTag(t *testing.T) { - p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER_SAME_LINE), "simple") + p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER_SAME_LINE), "simple.md") if err != nil { t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) } diff --git a/hugolib/planner.go b/hugolib/planner.go index 88d5dee7a..e8b03bb5a 100644 --- a/hugolib/planner.go +++ b/hugolib/planner.go @@ -18,8 +18,8 @@ func (s *Site) ShowPlan(out io.Writer) (err error) { fmt.Fprintf(out, " (renderer: n/a)") } if s.Tmpl != nil { - fmt.Fprintf(out, " (layout: %s, exists: %t)", p.Layout(), s.Tmpl.Lookup(p.Layout()) != nil) - } + fmt.Fprintf(out, " (layout: %s, exists: %t)", p.Layout(), s.Tmpl.Lookup(p.Layout()) != nil) + } fmt.Fprintf(out, "\n") fmt.Fprintf(out, " canonical => ") if s.Target == nil { diff --git a/hugolib/site.go b/hugolib/site.go index 2c8165543..d5bc400f2 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -286,7 +286,7 @@ func (s *Site) setUrlPath(p *Page) error { x := strings.Split(y, "/") if len(x) <= 1 { - return fmt.Errorf("Zero length page name") + return fmt.Errorf("Zero length page name. filename: %s", y) } p.Section = strings.Trim(x[1], "/") @@ -400,9 +400,21 @@ func (s *Site) RenderAliases() error { return nil } -func (s *Site) RenderPages() error { +func (s *Site) RenderPages() (err error) { for _, p := range s.Pages { - content, err := s.RenderThingOrDefault(p, p.Layout(), "_default/single.html") + var layout string + + if !p.IsRenderable() { + layout = "__" + p.FileName + _, err := s.Tmpl.New(layout).Parse(string(p.Content)) + if err != nil { + return err + } + } else { + layout = p.Layout() + } + + content, err := s.RenderThingOrDefault(p, layout, "_default/single.html") if err != nil { return err } diff --git a/hugolib/site_test.go b/hugolib/site_test.go index 6ae76e7c0..5d74c911e 100644 --- a/hugolib/site_test.go +++ b/hugolib/site_test.go @@ -188,6 +188,63 @@ func TestSetOutFile(t *testing.T) { } } +func TestSkipRender(t *testing.T) { + files := make(map[string][]byte) + target := &InMemoryTarget{files: files} + sources := []byteSource{ + {"sect/doc1.html", []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")}, + {"sect/doc2.html", []byte("more content")}, + {"sect/doc3.md", []byte("# doc3\n*some* content")}, + {"sect/doc4.md", []byte("---\ntitle: doc4\n---\n# doc4\n*some content*")}, + {"sect/doc5.html", []byte("{{ template \"head\" }}body5")}, + } + + s := &Site{ + Target: target, + Config: Config{BaseUrl: "http://auth/bub/"}, + Source: &inMemorySource{sources}, + } + s.initializeSiteInfo() + s.prepTemplates() + + must(s.addTemplate("_default/single.html", "{{.Content}}")) + must(s.addTemplate("head", "")) + + if err := s.CreatePages(); err != nil { + t.Fatalf("Unable to create pages: %s", err) + } + + if err := s.BuildSiteMeta(); err != nil { + t.Fatalf("Unable to build site metadata: %s", err) + } + + if err := s.RenderPages(); err != nil { + t.Fatalf("Unable to render pages. %s", err) + } + + tests := []struct { + doc string + expected string + }{ + {"sect/doc1.html", "

title

\n\n

some content

\n"}, + {"sect/doc2.html", "more content"}, + {"sect/doc3.html", "

doc3

\n\n

some content

\n"}, + {"sect/doc4.html", "

doc4

\n\n

some content

\n"}, + {"sect/doc5.html", "body5"}, + } + + for _, test := range tests { + content, ok := target.files[test.doc] + if !ok { + t.Fatalf("Did not find %s in target. %v", test.doc, target.files) + } + + if !bytes.Equal(content, []byte(test.expected)) { + t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, string(content)) + } + } +} + func TestAbsUrlify(t *testing.T) { files := make(map[string][]byte) target := &InMemoryTarget{files: files} @@ -219,6 +276,6 @@ func TestAbsUrlify(t *testing.T) { expected := "Going" if string(content) != expected { - t.Errorf("Expected: %q, got: %q", expected, string(content)) + t.Errorf("AbsUrlify content expected:\n%q\ngot\n%q", expected, string(content)) } }