shithub: hugo

Download patch

ref: 311e10222301d47422f970c460383879ad78f681
parent: 5374242ff7b6cc1677baf9a1dd2b835d8ee6d18f
author: Noah Campbell <[email protected]>
date: Wed Sep 18 10:21:27 EDT 2013

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.

--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -90,8 +90,7 @@
 	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 @@
 	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 @@
 		}
 	}
 
-	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
 }
--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -170,7 +170,7 @@
 }
 
 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 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 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)
 	}
--- a/hugolib/planner.go
+++ b/hugolib/planner.go
@@ -18,8 +18,8 @@
 			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 {
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -286,7 +286,7 @@
 	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 @@
 	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
 		}
--- a/hugolib/site_test.go
+++ b/hugolib/site_test.go
@@ -188,6 +188,63 @@
 	}
 }
 
+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 @@
 
 	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))
 	}
 }