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.
This commit is contained in:
Noah Campbell 2013-09-18 14:21:27 -07:00
parent 5374242ff7
commit 311e102223
5 changed files with 97 additions and 13 deletions

View 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
}

View 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)
}

View 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 {

View 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
}

View 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))
}
}