diff --git a/hugolib/handler_file.go b/hugolib/handler_file.go index 4e692ed9a..0d3a05ecf 100644 --- a/hugolib/handler_file.go +++ b/hugolib/handler_file.go @@ -22,6 +22,7 @@ import ( func init() { RegisterHandler(new(cssHandler)) + RegisterHandler(new(defaultHandler)) } type basicFileHandler Handle @@ -34,10 +35,16 @@ func (h basicFileHandler) PageConvert(*Page, tpl.Template) HandledResult { return HandledResult{} } -type cssHandler struct { - basicFileHandler +type defaultHandler struct{ basicFileHandler } + +func (h defaultHandler) Extensions() []string { return []string{"*"} } +func (h defaultHandler) FileConvert(f *source.File, s *Site) HandledResult { + s.WriteDestFile(f.Path(), f.Contents) + return HandledResult{file: f} } +type cssHandler struct{ basicFileHandler } + func (h cssHandler) Extensions() []string { return []string{"css"} } func (h cssHandler) FileConvert(f *source.File, s *Site) HandledResult { x := cssmin.Minify(f.Bytes()) diff --git a/hugolib/handler_meta.go b/hugolib/handler_meta.go index bdab3a019..1e0d760c5 100644 --- a/hugolib/handler_meta.go +++ b/hugolib/handler_meta.go @@ -17,6 +17,7 @@ import ( "errors" "fmt" + "github.com/spf13/hugo/source" ) @@ -82,6 +83,11 @@ func (mh *MetaHandle) Convert(i interface{}, s *Site, results HandleResults) { func (mh *MetaHandle) Handler() Handler { if mh.handler == nil { mh.handler = FindHandler(mh.ext) + + // if no handler found, use default handler + if mh.handler == nil { + mh.handler = FindHandler("*") + } } return mh.handler } diff --git a/hugolib/handler_test.go b/hugolib/handler_test.go new file mode 100644 index 000000000..d44a373b4 --- /dev/null +++ b/hugolib/handler_test.go @@ -0,0 +1,78 @@ +package hugolib + +import ( + "path/filepath" + "testing" + + "github.com/spf13/afero" + "github.com/spf13/hugo/helpers" + "github.com/spf13/hugo/hugofs" + "github.com/spf13/hugo/source" + "github.com/spf13/hugo/target" + "github.com/spf13/viper" +) + +func TestDefaultHandler(t *testing.T) { + viper.Reset() + defer viper.Reset() + + hugofs.DestinationFS = new(afero.MemMapFs) + sources := []source.ByteSource{ + {filepath.FromSlash("sect/doc1.html"), []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")}, + {filepath.FromSlash("sect/doc2.html"), []byte("more content")}, + {filepath.FromSlash("sect/doc3.md"), []byte("# doc3\n*some* content")}, + {filepath.FromSlash("sect/doc4.md"), []byte("---\ntitle: doc4\n---\n# doc4\n*some content*")}, + {filepath.FromSlash("sect/doc3/img1.png"), []byte("‰PNG  ��� IHDR����������:~›U��� IDATWcø��ZMoñ����IEND®B`‚")}, + {filepath.FromSlash("sect/img2.gif"), []byte("GIF89a��€��ÿÿÿ���,�������D�;")}, + {filepath.FromSlash("sect/img2.spf"), []byte("****FAKE-FILETYPE****")}, + {filepath.FromSlash("doc7.html"), []byte("doc7 content")}, + {filepath.FromSlash("sect/doc8.html"), []byte("---\nmarkup: md\n---\n# title\nsome *content*")}, + } + + viper.Set("DefaultExtension", "html") + viper.Set("verbose", true) + + s := &Site{ + Source: &source.InMemorySource{ByteSource: sources}, + Targets: targetList{Page: &target.PagePub{UglyURLs: true}}, + } + + s.initializeSiteInfo() + // From site_test.go + templatePrep(s) + + must(s.addTemplate("_default/single.html", "{{.Content}}")) + must(s.addTemplate("head", "")) + must(s.addTemplate("head_abs", "")) + + // From site_test.go + createAndRenderPages(t, s) + + tests := []struct { + doc string + expected string + }{ + {filepath.FromSlash("sect/doc1.html"), "\n\n

title

\n\n

some content

\n"}, + {filepath.FromSlash("sect/doc2.html"), "more content"}, + {filepath.FromSlash("sect/doc3.html"), "\n\n

doc3

\n\n

some content

\n"}, + {filepath.FromSlash("sect/doc3/img1.png"), string([]byte("‰PNG  ��� IHDR����������:~›U��� IDATWcø��ZMoñ����IEND®B`‚"))}, + {filepath.FromSlash("sect/img2.gif"), string([]byte("GIF89a��€��ÿÿÿ���,�������D�;"))}, + {filepath.FromSlash("sect/img2.spf"), string([]byte("****FAKE-FILETYPE****"))}, + {filepath.FromSlash("doc7.html"), "doc7 content"}, + {filepath.FromSlash("sect/doc8.html"), "\n\n

title

\n\n

some content

\n"}, + } + + for _, test := range tests { + file, err := hugofs.DestinationFS.Open(test.doc) + if err != nil { + t.Fatalf("Did not find %s in target.", test.doc) + } + + content := helpers.ReaderToString(file) + + if content != test.expected { + t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, content) + } + } + +}