Allow non-markdown content in content directory
authorNoah Campbell <noahcampbell@gmail.com>
Wed, 18 Sep 2013 21:21:27 +0000 (14:21 -0700)
committerNoah Campbell <noahcampbell@gmail.com>
Wed, 18 Sep 2013 21:21:27 +0000 (14:21 -0700)
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
hugolib/page_test.go
hugolib/planner.go
hugolib/site.go
hugolib/site_test.go

index 8daf73af16b5ec2d33e33b0399b6bf83455be99a..192ec63b1d81469eef1ed2fe2b73fc77c6d25757 100644 (file)
@@ -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
 }
index c82ee59f7ba5f4288608336172a7ac534e719aca..d16d7f071fa21bc345951dcd8c0d5459a7dd0f35 100644 (file)
@@ -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)
        }
index 88d5dee7a646abff66028a3a792293fffc23c7b0..e8b03bb5a4e5fa55ba5f981883df5e813511bc8c 100644 (file)
@@ -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 {
index 2c81655431b9071a8ca3af9161e35740dce2c1b8..d5bc400f27b98bf3ee3914d1bc8b6dd22f370da2 100644 (file)
@@ -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
                }
index 6ae76e7c0b2ad124b70dfcdca0a3403d0949f65f..5d74c911e477ff2edc08e236bc30f262b9f4938d 100644 (file)
@@ -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("<!doctype html><html><body>more content</body></html>")},
+               {"sect/doc3.md", []byte("# doc3\n*some* content")},
+               {"sect/doc4.md", []byte("---\ntitle: doc4\n---\n# doc4\n*some content*")},
+               {"sect/doc5.html", []byte("<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>")},
+       }
+
+       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", "<head><script src=\"script.js\"></script></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", "<html><head></head><body><h1>title</h1>\n\n<p>some <em>content</em></p>\n</body></html>"},
+               {"sect/doc2.html", "<!DOCTYPE html><html><head></head><body>more content</body></html>"},
+               {"sect/doc3.html", "<html><head></head><body><h1>doc3</h1>\n\n<p><em>some</em> content</p>\n</body></html>"},
+               {"sect/doc4.html", "<html><head></head><body><h1>doc4</h1>\n\n<p><em>some content</em></p>\n</body></html>"},
+               {"sect/doc5.html", "<!DOCTYPE html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"},
+       }
+
+       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 := "<html><head></head><body><a href=\"http://auth/bub/foobar.jpg\">Going</a></body></html>"
        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))
        }
 }