func TestRenderThingOrDefault(t *testing.T) { tests := []struct { content string missing bool template string expected string }{ {PAGE_SIMPLE_TITLE, true, TEMPLATE_TITLE, HTML("simple template")}, {PAGE_SIMPLE_TITLE, true, TEMPLATE_FUNC, HTML("simple-template")}, {PAGE_SIMPLE_TITLE, false, TEMPLATE_TITLE, HTML("simple template")}, {PAGE_SIMPLE_TITLE, false, TEMPLATE_FUNC, HTML("simple-template")}, } hugofs.DestinationFS = new(afero.MemMapFs) s := &Site{} templatePrep(s) for i, test := range tests { p, err := NewPageFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md") if err != nil { t.Fatalf("Error parsing buffer: %s", err) } templateName := fmt.Sprintf("default%d", i) err = s.addTemplate(templateName, test.template) if err != nil { t.Fatalf("Unable to add template") } var err2 error var b io.Reader if test.missing { b, err2 = s.renderPage("name", p, "missing", templateName) } else { b, err2 = s.renderPage("name", p, templateName, "missing_default") } if err2 != nil { t.Errorf("Unable to render html: %s", err) } if err2 := s.WriteDestPage("out", b); err2 != nil { t.Errorf("Unable to write html: %s", err) } file, err := hugofs.DestinationFS.Open(filepath.FromSlash("out/index.html")) if err != nil { t.Errorf("Unable to open html: %s", err) } if helpers.ReaderToString(file) != test.expected { t.Errorf("Content does not match. Expected '%s', got '%s'", test.expected, helpers.ReaderToString(file)) } } }
func TestDefaultHandler(t *testing.T) { testCommonResetState() hugofs.InitMemFs() sources := []source.ByteSource{ {Name: filepath.FromSlash("sect/doc1.html"), Content: []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")}, {Name: filepath.FromSlash("sect/doc2.html"), Content: []byte("<!doctype html><html><body>more content</body></html>")}, {Name: filepath.FromSlash("sect/doc3.md"), Content: []byte("# doc3\n*some* content")}, {Name: filepath.FromSlash("sect/doc4.md"), Content: []byte("---\ntitle: doc4\n---\n# doc4\n*some content*")}, {Name: filepath.FromSlash("sect/doc3/img1.png"), Content: []byte("‰PNG ��� IHDR����������:~›U��� IDATWcø��ZMoñ����IEND®B`‚")}, {Name: filepath.FromSlash("sect/img2.gif"), Content: []byte("GIF89a��€��ÿÿÿ���,�������D�;")}, {Name: filepath.FromSlash("sect/img2.spf"), Content: []byte("****FAKE-FILETYPE****")}, {Name: filepath.FromSlash("doc7.html"), Content: []byte("<html><body>doc7 content</body></html>")}, {Name: filepath.FromSlash("sect/doc8.html"), Content: []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, PublishDir: "public"}}, Language: helpers.NewLanguage("en"), } if err := buildAndRenderSite(s, "_default/single.html", "{{.Content}}", "head", "<head><script src=\"script.js\"></script></head>", "head_abs", "<head><script src=\"/script.js\"></script></head>"); err != nil { t.Fatalf("Failed to render site: %s", err) } tests := []struct { doc string expected string }{ {filepath.FromSlash("public/sect/doc1.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"}, {filepath.FromSlash("public/sect/doc2.html"), "<!doctype html><html><body>more content</body></html>"}, {filepath.FromSlash("public/sect/doc3.html"), "\n\n<h1 id=\"doc3\">doc3</h1>\n\n<p><em>some</em> content</p>\n"}, {filepath.FromSlash("public/sect/doc3/img1.png"), string([]byte("‰PNG ��� IHDR����������:~›U��� IDATWcø��ZMoñ����IEND®B`‚"))}, {filepath.FromSlash("public/sect/img2.gif"), string([]byte("GIF89a��€��ÿÿÿ���,�������D�;"))}, {filepath.FromSlash("public/sect/img2.spf"), string([]byte("****FAKE-FILETYPE****"))}, {filepath.FromSlash("public/doc7.html"), "<html><body>doc7 content</body></html>"}, {filepath.FromSlash("public/sect/doc8.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"}, } for _, test := range tests { file, err := hugofs.Destination().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) } } }
func TestRenderThingOrDefault(t *testing.T) { tests := []struct { missing bool template string expected string }{ {true, templateTitle, HTML("simple template")}, {true, templateFunc, HTML("simple-template")}, {false, templateTitle, HTML("simple template")}, {false, templateFunc, HTML("simple-template")}, } hugofs.InitMemFs() for i, test := range tests { s := &Site{} p, err := NewPageFrom(strings.NewReader(pageSimpleTitle), "content/a/file.md") if err != nil { t.Fatalf("Error parsing buffer: %s", err) } templateName := fmt.Sprintf("default%d", i) s.prepTemplates(templateName, test.template) var err2 error if test.missing { err2 = s.renderAndWritePage("name", "out", p, "missing", templateName) } else { err2 = s.renderAndWritePage("name", "out", p, templateName, "missing_default") } if err2 != nil { t.Errorf("Unable to render html: %s", err) } file, err := hugofs.Destination().Open(filepath.FromSlash("out/index.html")) if err != nil { t.Errorf("Unable to open html: %s", err) } if helpers.ReaderToString(file) != test.expected { t.Errorf("Content does not match. Expected '%s', got '%s'", test.expected, helpers.ReaderToString(file)) } } }
func TestSkipRender(t *testing.T) { testCommonResetState() hugofs.InitMemFs() sources := []source.ByteSource{ {Name: filepath.FromSlash("sect/doc1.html"), Content: []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")}, {Name: filepath.FromSlash("sect/doc2.html"), Content: []byte("<!doctype html><html><body>more content</body></html>")}, {Name: filepath.FromSlash("sect/doc3.md"), Content: []byte("# doc3\n*some* content")}, {Name: filepath.FromSlash("sect/doc4.md"), Content: []byte("---\ntitle: doc4\n---\n# doc4\n*some content*")}, {Name: filepath.FromSlash("sect/doc5.html"), Content: []byte("<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>")}, {Name: filepath.FromSlash("sect/doc6.html"), Content: []byte("<!doctype html><html>{{ template \"head_abs\" }}<body>body5</body></html>")}, {Name: filepath.FromSlash("doc7.html"), Content: []byte("<html><body>doc7 content</body></html>")}, {Name: filepath.FromSlash("sect/doc8.html"), Content: []byte("---\nmarkup: md\n---\n# title\nsome *content*")}, } viper.Set("defaultExtension", "html") viper.Set("verbose", true) viper.Set("canonifyURLs", true) viper.Set("baseURL", "http://auth/bub") s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, targets: targetList{page: &target.PagePub{UglyURLs: true}}, Language: helpers.NewDefaultLanguage(), } if err := buildAndRenderSite(s, "_default/single.html", "{{.Content}}", "head", "<head><script src=\"script.js\"></script></head>", "head_abs", "<head><script src=\"/script.js\"></script></head>"); err != nil { t.Fatalf("Failed to build site: %s", err) } tests := []struct { doc string expected string }{ {filepath.FromSlash("sect/doc1.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"}, {filepath.FromSlash("sect/doc2.html"), "<!doctype html><html><body>more content</body></html>"}, {filepath.FromSlash("sect/doc3.html"), "\n\n<h1 id=\"doc3\">doc3</h1>\n\n<p><em>some</em> content</p>\n"}, {filepath.FromSlash("sect/doc4.html"), "\n\n<h1 id=\"doc4\">doc4</h1>\n\n<p><em>some content</em></p>\n"}, {filepath.FromSlash("sect/doc5.html"), "<!doctype html><html><head><script src=\"script.js\"></script></head><body>body5</body></html>"}, {filepath.FromSlash("sect/doc6.html"), "<!doctype html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"}, {filepath.FromSlash("doc7.html"), "<html><body>doc7 content</body></html>"}, {filepath.FromSlash("sect/doc8.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"}, } for _, test := range tests { file, err := hugofs.Destination().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) } } }
func TestAbsURLify(t *testing.T) { testCommonResetState() viper.Set("defaultExtension", "html") hugofs.InitMemFs() sources := []source.ByteSource{ {Name: filepath.FromSlash("sect/doc1.html"), Content: []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>")}, {Name: filepath.FromSlash("blue/doc2.html"), Content: []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>")}, } for _, baseURL := range []string{"http://auth/bub", "http://base", "//base"} { for _, canonify := range []bool{true, false} { viper.Set("canonifyURLs", canonify) viper.Set("baseURL", baseURL) s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, targets: targetList{page: &target.PagePub{UglyURLs: true}}, Language: helpers.NewDefaultLanguage(), } t.Logf("Rendering with baseURL %q and canonifyURLs set %v", viper.GetString("baseURL"), canonify) if err := buildAndRenderSite(s, "blue/single.html", templateWithURLAbs); err != nil { t.Fatalf("Failed to build site: %s", err) } tests := []struct { file, expected string }{ {"blue/doc2.html", "<a href=\"%s/foobar.jpg\">Going</a>"}, {"sect/doc1.html", "<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"}, } for _, test := range tests { file, err := hugofs.Destination().Open(filepath.FromSlash(test.file)) if err != nil { t.Fatalf("Unable to locate rendered content: %s", test.file) } content := helpers.ReaderToString(file) expected := test.expected if strings.Contains(expected, "%s") { expected = fmt.Sprintf(expected, baseURL) } if !canonify { expected = strings.Replace(expected, baseURL, "", -1) } if content != expected { t.Errorf("AbsURLify with baseURL %q content expected:\n%q\ngot\n%q", baseURL, expected, content) } } } } }
func TestAbsUrlify(t *testing.T) { hugofs.DestinationFS = new(afero.MemMapFs) sources := []source.ByteSource{ {filepath.FromSlash("sect/doc1.html"), []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>")}, {filepath.FromSlash("content/blue/doc2.html"), []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>")}, } for _, canonify := range []bool{true, false} { viper.Set("CanonifyURLs", canonify) viper.Set("BaseURL", "http://auth/bub") s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, Targets: targetList{Page: &target.PagePub{UglyURLs: true}}, } t.Logf("Rendering with BaseURL %q and CanonifyURLs set %v", viper.GetString("baseURL"), canonify) s.initializeSiteInfo() templatePrep(s) must(s.addTemplate("blue/single.html", TEMPLATE_WITH_URL_ABS)) 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 { file, expected string }{ {"content/blue/doc2.html", "<a href=\"http://auth/bub/foobar.jpg\">Going</a>"}, {"sect/doc1.html", "<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"}, } for _, test := range tests { file, err := hugofs.DestinationFS.Open(filepath.FromSlash(test.file)) if err != nil { t.Fatalf("Unable to locate rendered content: %s", test.file) } content := helpers.ReaderToString(file) expected := test.expected if !canonify { expected = strings.Replace(expected, viper.GetString("baseurl"), "", -1) } if content != expected { t.Errorf("AbsUrlify content expected:\n%q\ngot\n%q", expected, content) } } } }
func TestSkipRender(t *testing.T) { 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("<!doctype html><html><body>more content</body></html>")}, {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/doc5.html"), []byte("<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>")}, {filepath.FromSlash("sect/doc6.html"), []byte("<!doctype html><html>{{ template \"head_abs\" }}<body>body5</body></html>")}, {filepath.FromSlash("doc7.html"), []byte("<html><body>doc7 content</body></html>")}, {filepath.FromSlash("sect/doc8.html"), []byte("---\nmarkup: md\n---\n# title\nsome *content*")}, } viper.Set("verbose", true) viper.Set("CanonifyURLs", true) viper.Set("baseurl", "http://auth/bub") s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, Targets: targetList{Page: &target.PagePub{UglyURLs: true}}, } s.initializeSiteInfo() templatePrep(s) must(s.addTemplate("_default/single.html", "{{.Content}}")) must(s.addTemplate("head", "<head><script src=\"script.js\"></script></head>")) must(s.addTemplate("head_abs", "<head><script src=\"/script.js\"></script></head>")) createAndRenderPages(t, s) tests := []struct { doc string expected string }{ {filepath.FromSlash("sect/doc1.html"), "\n\n<h1 id=\"title:5d74edbb89ef198cd37882b687940cda\">title</h1>\n\n<p>some <em>content</em></p>\n"}, {filepath.FromSlash("sect/doc2.html"), "<!doctype html><html><body>more content</body></html>"}, {filepath.FromSlash("sect/doc3.html"), "\n\n<h1 id=\"doc3:28c75a9e2162b8eccda73a1ab9ce80b4\">doc3</h1>\n\n<p><em>some</em> content</p>\n"}, {filepath.FromSlash("sect/doc4.html"), "\n\n<h1 id=\"doc4:f8e6806123f341b8975509637645a4d3\">doc4</h1>\n\n<p><em>some content</em></p>\n"}, {filepath.FromSlash("sect/doc5.html"), "<!doctype html><html><head><script src=\"script.js\"></script></head><body>body5</body></html>"}, {filepath.FromSlash("sect/doc6.html"), "<!doctype html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"}, {filepath.FromSlash("doc7.html"), "<html><body>doc7 content</body></html>"}, {filepath.FromSlash("sect/doc8.html"), "\n\n<h1 id=\"title:0ae308ad73e2f37bd09874105281b5d8\">title</h1>\n\n<p>some <em>content</em></p>\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) } } }
func doTest404ShouldAlwaysHaveUglyURLs(t *testing.T, uglyURLs bool) { viper.Reset() defer viper.Reset() viper.Set("DefaultExtension", "html") viper.Set("verbose", true) viper.Set("baseurl", "http://auth/bub") viper.Set("DisableSitemap", false) viper.Set("DisableRSS", false) viper.Set("RSSUri", "index.xml") viper.Set("UglyURLs", uglyURLs) sources := []source.ByteSource{ {filepath.FromSlash("sect/doc1.html"), []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")}, } s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, Targets: targetList{Page: &target.PagePub{UglyURLs: uglyURLs}}, } s.initializeSiteInfo() templatePrep(s) must(s.addTemplate("index.html", "Home Sweet Home. IsHome={{ .IsHome }}")) must(s.addTemplate("_default/single.html", "{{.Content}} IsHome={{ .IsHome }}")) must(s.addTemplate("404.html", "Page Not Found. IsHome={{ .IsHome }}")) // make sure the XML files also end up with ugly urls must(s.addTemplate("rss.xml", "<root>RSS</root>")) must(s.addTemplate("sitemap.xml", "<root>SITEMAP</root>")) createAndRenderPages(t, s) s.RenderHomePage() s.RenderSitemap() var expectedPagePath string if uglyURLs { expectedPagePath = "sect/doc1.html" } else { expectedPagePath = "sect/doc1/index.html" } tests := []struct { doc string expected string }{ {filepath.FromSlash("index.html"), "Home Sweet Home. IsHome=true"}, {filepath.FromSlash(expectedPagePath), "\n\n<h1 id=\"title:5d74edbb89ef198cd37882b687940cda\">title</h1>\n\n<p>some <em>content</em></p>\n IsHome=false"}, {filepath.FromSlash("404.html"), "Page Not Found. IsHome=false"}, {filepath.FromSlash("index.xml"), "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n<root>RSS</root>"}, {filepath.FromSlash("sitemap.xml"), "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n<root>SITEMAP</root>"}, } for _, p := range s.Pages { assert.False(t, p.IsHome) } for _, test := range tests { file, err := hugofs.DestinationFS.Open(test.doc) if err != nil { t.Fatalf("Did not find %s in target: %s", test.doc, err) } content := helpers.ReaderToString(file) if content != test.expected { t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, content) } } }
func doTestCrossrefs(t *testing.T, relative, uglyURLs bool) { viper.Reset() defer viper.Reset() baseURL := "http://foo/bar" viper.Set("DefaultExtension", "html") viper.Set("baseurl", baseURL) viper.Set("UglyURLs", uglyURLs) viper.Set("verbose", true) var refShortcode string var expectedBase string var expectedURLSuffix string var expectedPathSuffix string if relative { refShortcode = "relref" expectedBase = "/bar" } else { refShortcode = "ref" expectedBase = baseURL } if uglyURLs { expectedURLSuffix = ".html" expectedPathSuffix = ".html" } else { expectedURLSuffix = "/" expectedPathSuffix = "/index.html" } sources := []source.ByteSource{ {filepath.FromSlash("sect/doc1.md"), []byte(fmt.Sprintf(`Ref 2: {{< %s "sect/doc2.md" >}}`, refShortcode))}, // Issue #1148: Make sure that no P-tags is added around shortcodes. {filepath.FromSlash("sect/doc2.md"), []byte(fmt.Sprintf(`**Ref 1:** {{< %s "sect/doc1.md" >}} THE END.`, refShortcode))}, } s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, Targets: targetList{Page: &target.PagePub{UglyURLs: uglyURLs}}, } s.initializeSiteInfo() templatePrep(s) must(s.addTemplate("_default/single.html", "{{.Content}}")) createAndRenderPages(t, s) tests := []struct { doc string expected string }{ {filepath.FromSlash(fmt.Sprintf("sect/doc1%s", expectedPathSuffix)), fmt.Sprintf("<p>Ref 2: %s/sect/doc2%s</p>\n", expectedBase, expectedURLSuffix)}, {filepath.FromSlash(fmt.Sprintf("sect/doc2%s", expectedPathSuffix)), fmt.Sprintf("<p><strong>Ref 1:</strong></p>\n\n%s/sect/doc1%s\n\n<p>THE END.</p>\n", expectedBase, expectedURLSuffix)}, } for _, test := range tests { file, err := hugofs.DestinationFS.Open(test.doc) if err != nil { t.Fatalf("Did not find %s in target: %s", test.doc, err) } content := helpers.ReaderToString(file) if content != test.expected { t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, content) } } }
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("<!doctype html><html><body>more content</body></html>")}, {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("<html><body>doc7 content</body></html>")}, {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", "<head><script src=\"script.js\"></script></head>")) must(s.addTemplate("head_abs", "<head><script src=\"/script.js\"></script></head>")) // From site_test.go createAndRenderPages(t, s) tests := []struct { doc string expected string }{ {filepath.FromSlash("sect/doc1.html"), "\n\n<h1 id=\"title:5d74edbb89ef198cd37882b687940cda\">title</h1>\n\n<p>some <em>content</em></p>\n"}, {filepath.FromSlash("sect/doc2.html"), "<!doctype html><html><body>more content</body></html>"}, {filepath.FromSlash("sect/doc3.html"), "\n\n<h1 id=\"doc3:28c75a9e2162b8eccda73a1ab9ce80b4\">doc3</h1>\n\n<p><em>some</em> content</p>\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"), "<html><body>doc7 content</body></html>"}, {filepath.FromSlash("sect/doc8.html"), "\n\n<h1 id=\"title:0ae308ad73e2f37bd09874105281b5d8\">title</h1>\n\n<p>some <em>content</em></p>\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) } } }
func doTestShouldAlwaysHaveUglyURLs(t *testing.T, uglyURLs bool) { viper.Reset() defer viper.Reset() viper.Set("DefaultExtension", "html") viper.Set("verbose", true) viper.Set("baseurl", "http://auth/bub") viper.Set("DisableSitemap", false) viper.Set("DisableRSS", false) viper.Set("RSSUri", "index.xml") viper.Set("blackfriday", map[string]interface{}{ "plainIDAnchors": true}) viper.Set("UglyURLs", uglyURLs) sources := []source.ByteSource{ {filepath.FromSlash("sect/doc1.md"), []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")}, {filepath.FromSlash("sect/doc2.md"), []byte("---\nurl: /ugly.html\nmarkup: markdown\n---\n# title\ndoc2 *content*")}, } s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, targets: targetList{page: &target.PagePub{UglyURLs: uglyURLs}}, } s.initializeSiteInfo() s.prepTemplates( "index.html", "Home Sweet {{ if.IsHome }}Home{{ end }}.", "_default/single.html", "{{.Content}}{{ if.IsHome }}This is not home!{{ end }}", "404.html", "Page Not Found.{{ if.IsHome }}This is not home!{{ end }}", "rss.xml", "<root>RSS</root>", "sitemap.xml", "<root>SITEMAP</root>") createAndRenderPages(t, s) s.RenderHomePage() s.RenderSitemap() var expectedPagePath string if uglyURLs { expectedPagePath = "sect/doc1.html" } else { expectedPagePath = "sect/doc1/index.html" } tests := []struct { doc string expected string }{ {filepath.FromSlash("index.html"), "Home Sweet Home."}, {filepath.FromSlash(expectedPagePath), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"}, {filepath.FromSlash("404.html"), "Page Not Found."}, {filepath.FromSlash("index.xml"), "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n<root>RSS</root>"}, {filepath.FromSlash("sitemap.xml"), "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n<root>SITEMAP</root>"}, // Issue #1923 {filepath.FromSlash("ugly.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>doc2 <em>content</em></p>\n"}, } for _, p := range s.Pages { assert.False(t, p.IsHome) } for _, test := range tests { file, err := hugofs.DestinationFS.Open(test.doc) if err != nil { t.Fatalf("Did not find %s in target: %s", test.doc, err) } content := helpers.ReaderToString(file) if content != test.expected { t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, content) } } }
func doTestSectionNaming(t *testing.T, canonify, uglify, pluralize bool) { hugofs.InitMemFs() testCommonResetState() viper.Set("baseURL", "http://auth/sub/") viper.Set("defaultExtension", "html") viper.Set("uglyURLs", uglify) viper.Set("pluralizeListTitles", pluralize) viper.Set("canonifyURLs", canonify) var expectedPathSuffix string if uglify { expectedPathSuffix = ".html" } else { expectedPathSuffix = "/index.html" } sources := []source.ByteSource{ {Name: filepath.FromSlash("sect/doc1.html"), Content: []byte("doc1")}, {Name: filepath.FromSlash("Fish and Chips/doc2.html"), Content: []byte("doc2")}, {Name: filepath.FromSlash("ラーメン/doc3.html"), Content: []byte("doc3")}, } s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, targets: targetList{page: &target.PagePub{UglyURLs: uglify}}, Language: helpers.NewDefaultLanguage(), } if err := buildAndRenderSite(s, "_default/single.html", "{{.Content}}", "_default/list.html", "{{ .Title }}"); err != nil { t.Fatalf("Failed to build site: %s", err) } tests := []struct { doc string pluralAware bool expected string }{ {filepath.FromSlash(fmt.Sprintf("sect/doc1%s", expectedPathSuffix)), false, "doc1"}, {filepath.FromSlash(fmt.Sprintf("sect%s", expectedPathSuffix)), true, "Sect"}, {filepath.FromSlash(fmt.Sprintf("fish-and-chips/doc2%s", expectedPathSuffix)), false, "doc2"}, {filepath.FromSlash(fmt.Sprintf("fish-and-chips%s", expectedPathSuffix)), true, "Fish and Chips"}, {filepath.FromSlash(fmt.Sprintf("ラーメン/doc3%s", expectedPathSuffix)), false, "doc3"}, {filepath.FromSlash(fmt.Sprintf("ラーメン%s", expectedPathSuffix)), true, "ラーメン"}, } for _, test := range tests { file, err := hugofs.Destination().Open(test.doc) if err != nil { t.Fatalf("Did not find %s in target: %s", test.doc, err) } content := helpers.ReaderToString(file) if test.pluralAware && pluralize { test.expected = inflect.Pluralize(test.expected) } if content != test.expected { t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, content) } } }
func TestShortcodesInSite(t *testing.T) { viper.Reset() defer viper.Reset() baseURL := "http://foo/bar" viper.Set("DefaultExtension", "html") viper.Set("baseurl", baseURL) viper.Set("UglyURLs", false) viper.Set("verbose", true) tests := []struct { contentPath string content string outFile string expected string }{ {"sect/doc1.md", `a{{< b >}}c`, filepath.FromSlash("sect/doc1/index.html"), "<p>abc</p>\n"}, // Issue #1642: Multiple shortcodes wrapped in P // Deliberately forced to pass even if they maybe shouldn't. {"sect/doc2.md", `a {{< b >}} {{< c >}} {{< d >}} e`, filepath.FromSlash("sect/doc2/index.html"), "<p>a</p>\n\n<p>b<br />\nc\nd</p>\n\n<p>e</p>\n"}, {"sect/doc3.md", `a {{< b >}} {{< c >}} {{< d >}} e`, filepath.FromSlash("sect/doc3/index.html"), "<p>a</p>\n\n<p>b<br />\nc</p>\n\nd\n\n<p>e</p>\n"}, {"sect/doc4.md", `a {{< b >}} {{< b >}} {{< b >}} {{< b >}} {{< b >}} `, filepath.FromSlash("sect/doc4/index.html"), "<p>a\nb\nb\nb\nb\nb</p>\n"}, } sources := make([]source.ByteSource, len(tests)) for i, test := range tests { sources[i] = source.ByteSource{filepath.FromSlash(test.contentPath), []byte(test.content)} } s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, targets: targetList{page: &target.PagePub{UglyURLs: false}}, } s.initializeSiteInfo() s.loadTemplates() s.Tmpl.AddTemplate("_default/single.html", "{{.Content}}") s.Tmpl.AddInternalShortcode("b.html", `b`) s.Tmpl.AddInternalShortcode("c.html", `c`) s.Tmpl.AddInternalShortcode("d.html", `d`) s.Tmpl.MarkReady() createAndRenderPages(t, s) for _, test := range tests { file, err := hugofs.DestinationFS.Open(test.outFile) if err != nil { t.Fatalf("Did not find %s in target: %s", test.outFile, err) } content := helpers.ReaderToString(file) if content != test.expected { t.Errorf("%s content expected:\n%q\ngot:\n%q", test.outFile, test.expected, content) } } }
func (f *File) String() string { return helpers.ReaderToString(f.Contents) }
func TestShortcodesInSite(t *testing.T) { testCommonResetState() baseURL := "http://foo/bar" viper.Set("DefaultExtension", "html") viper.Set("DefaultContentLanguage", "en") viper.Set("baseurl", baseURL) viper.Set("UglyURLs", false) viper.Set("verbose", true) viper.Set("pygmentsuseclasses", true) viper.Set("pygmentscodefences", true) tests := []struct { contentPath string content string outFile string expected string }{ {"sect/doc1.md", `a{{< b >}}c`, filepath.FromSlash("sect/doc1/index.html"), "<p>abc</p>\n"}, // Issue #1642: Multiple shortcodes wrapped in P // Deliberately forced to pass even if they maybe shouldn't. {"sect/doc2.md", `a {{< b >}} {{< c >}} {{< d >}} e`, filepath.FromSlash("sect/doc2/index.html"), "<p>a</p>\n\n<p>b<br />\nc\nd</p>\n\n<p>e</p>\n"}, {"sect/doc3.md", `a {{< b >}} {{< c >}} {{< d >}} e`, filepath.FromSlash("sect/doc3/index.html"), "<p>a</p>\n\n<p>b<br />\nc</p>\n\nd\n\n<p>e</p>\n"}, {"sect/doc4.md", `a {{< b >}} {{< b >}} {{< b >}} {{< b >}} {{< b >}} `, filepath.FromSlash("sect/doc4/index.html"), "<p>a\nb\nb\nb\nb\nb</p>\n"}, // #2192 #2209: Shortcodes in markdown headers {"sect/doc5.md", `# {{< b >}} ## {{% c %}}`, filepath.FromSlash("sect/doc5/index.html"), "\n\n<h1 id=\"hahahugoshortcode-1hbhb\">b</h1>\n\n<h2 id=\"hahahugoshortcode-2hbhb\">c</h2>\n"}, // #2223 pygments {"sect/doc6.md", "\n```bash\nb: {{< b >}} c: {{% c %}}\n```\n", filepath.FromSlash("sect/doc6/index.html"), "b: b c: c\n</code></pre></div>\n"}, // #2249 {"sect/doc7.ad", `_Shortcodes:_ *b: {{< b >}} c: {{% c %}}*`, filepath.FromSlash("sect/doc7/index.html"), "<div class=\"paragraph\">\n<p><em>Shortcodes:</em> <strong>b: b c: c</strong></p>\n</div>\n"}, {"sect/doc8.rst", `**Shortcodes:** *b: {{< b >}} c: {{% c %}}*`, filepath.FromSlash("sect/doc8/index.html"), "<div class=\"document\">\n\n\n<p><strong>Shortcodes:</strong> <em>b: b c: c</em></p>\n</div>"}, {"sect/doc9.mmark", ` --- menu: main: parent: 'parent' --- **Shortcodes:** *b: {{< b >}} c: {{% c %}}*`, filepath.FromSlash("sect/doc9/index.html"), "<p><strong>Shortcodes:</strong> <em>b: b c: c</em></p>\n"}, // Issue #1229: Menus not available in shortcode. {"sect/doc10.md", `--- menu: main: identifier: 'parent' tags: - Menu --- **Menus:** {{< menu >}}`, filepath.FromSlash("sect/doc10/index.html"), "<p><strong>Menus:</strong> 1</p>\n"}, // Issue #2323: Taxonomies not available in shortcode. {"sect/doc11.md", `--- tags: - Bugs --- **Tags:** {{< tags >}}`, filepath.FromSlash("sect/doc11/index.html"), "<p><strong>Tags:</strong> 2</p>\n"}, } sources := make([]source.ByteSource, len(tests)) for i, test := range tests { sources[i] = source.ByteSource{Name: filepath.FromSlash(test.contentPath), Content: []byte(test.content)} } s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, targets: targetList{page: &target.PagePub{UglyURLs: false}}, Language: helpers.NewDefaultLanguage(), } addTemplates := func(templ tpl.Template) error { templ.AddTemplate("_default/single.html", "{{.Content}}") templ.AddInternalShortcode("b.html", `b`) templ.AddInternalShortcode("c.html", `c`) templ.AddInternalShortcode("d.html", `d`) templ.AddInternalShortcode("menu.html", `{{ len (index .Page.Menus "main").Children }}`) templ.AddInternalShortcode("tags.html", `{{ len .Page.Site.Taxonomies.tags }}`) return nil } sites, err := newHugoSites(s) if err != nil { t.Fatalf("Failed to build site: %s", err) } if err = sites.Build(BuildCfg{withTemplate: addTemplates}); err != nil { t.Fatalf("Failed to build site: %s", err) } for _, test := range tests { if strings.HasSuffix(test.contentPath, ".ad") && !helpers.HasAsciidoc() { fmt.Println("Skip Asciidoc test case as no Asciidoc present.") continue } else if strings.HasSuffix(test.contentPath, ".rst") && !helpers.HasRst() { fmt.Println("Skip Rst test case as no rst2html present.") continue } else if strings.Contains(test.expected, "code") && !helpers.HasPygments() { fmt.Println("Skip Pygments test case as no pygments present.") continue } file, err := hugofs.Destination().Open(test.outFile) if err != nil { t.Fatalf("Did not find %s in target: %s", test.outFile, err) } content := helpers.ReaderToString(file) if !strings.Contains(content, test.expected) { t.Fatalf("%s content expected:\n%q\ngot:\n%q", test.outFile, test.expected, content) } } }
func doTestSectionNaming(t *testing.T, canonify, uglify, pluralize bool) { hugofs.DestinationFS = new(afero.MemMapFs) viper.Reset() defer viper.Reset() viper.Set("baseurl", "http://auth/sub/") viper.Set("DefaultExtension", "html") viper.Set("UglyURLs", uglify) viper.Set("PluralizeListTitles", pluralize) viper.Set("CanonifyURLs", canonify) var expectedPathSuffix string if uglify { expectedPathSuffix = ".html" } else { expectedPathSuffix = "/index.html" } sources := []source.ByteSource{ {filepath.FromSlash("sect/doc1.html"), []byte("doc1")}, {filepath.FromSlash("Fish and Chips/doc2.html"), []byte("doc2")}, {filepath.FromSlash("ラーメン/doc3.html"), []byte("doc3")}, } s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, Targets: targetList{Page: &target.PagePub{UglyURLs: uglify}}, } s.initializeSiteInfo() templatePrep(s) must(s.addTemplate("_default/single.html", "{{.Content}}")) must(s.addTemplate("_default/list.html", "{{ .Title }}")) createAndRenderPages(t, s) s.RenderSectionLists() tests := []struct { doc string pluralAware bool expected string }{ {filepath.FromSlash(fmt.Sprintf("sect/doc1%s", expectedPathSuffix)), false, "doc1"}, {filepath.FromSlash(fmt.Sprintf("sect%s", expectedPathSuffix)), true, "Sect"}, {filepath.FromSlash(fmt.Sprintf("fish-and-chips/doc2%s", expectedPathSuffix)), false, "doc2"}, {filepath.FromSlash(fmt.Sprintf("fish-and-chips%s", expectedPathSuffix)), true, "Fish and Chips"}, {filepath.FromSlash(fmt.Sprintf("ラーメン/doc3%s", expectedPathSuffix)), false, "doc3"}, {filepath.FromSlash(fmt.Sprintf("ラーメン%s", expectedPathSuffix)), true, "ラーメン"}, } for _, test := range tests { file, err := hugofs.DestinationFS.Open(test.doc) if err != nil { t.Fatalf("Did not find %s in target: %s", test.doc, err) } content := helpers.ReaderToString(file) if test.pluralAware && pluralize { test.expected = inflect.Pluralize(test.expected) } if content != test.expected { t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, content) } } }
func TestShortcodesInSite(t *testing.T) { viper.Reset() defer viper.Reset() baseURL := "http://foo/bar" viper.Set("DefaultExtension", "html") viper.Set("baseurl", baseURL) viper.Set("UglyURLs", false) viper.Set("verbose", true) tests := []struct { contentPath string content string outFile string expected string }{ {"sect/doc1.md", `a{{< b >}}c`, filepath.FromSlash("sect/doc1/index.html"), "<p>abc</p>\n"}, // Issue #1642: Multiple shortcodes wrapped in P // Deliberately forced to pass even if they maybe shouldn't. {"sect/doc2.md", `a {{< b >}} {{< c >}} {{< d >}} e`, filepath.FromSlash("sect/doc2/index.html"), "<p>a</p>\n\n<p>b<br />\nc\nd</p>\n\n<p>e</p>\n"}, {"sect/doc3.md", `a {{< b >}} {{< c >}} {{< d >}} e`, filepath.FromSlash("sect/doc3/index.html"), "<p>a</p>\n\n<p>b<br />\nc</p>\n\nd\n\n<p>e</p>\n"}, {"sect/doc4.md", `a {{< b >}} {{< b >}} {{< b >}} {{< b >}} {{< b >}} `, filepath.FromSlash("sect/doc4/index.html"), "<p>a\nb\nb\nb\nb\nb</p>\n"}, // #2192 #2209: Shortcodes in markdown headers {"sect/doc5.md", `# {{< b >}} ## {{% c %}}`, filepath.FromSlash("sect/doc5/index.html"), "\n\n<h1 id=\"hugoshortcode-1\">b</h1>\n\n<h2 id=\"hugoshortcode-2\">c</h2>\n"}, {"sect/doc6.md", "\n```bash\n{{< b >}}\n{{% c %}}\n```\n", filepath.FromSlash("sect/doc6/index.html"), "<pre><code class=\"language-bash\">b\nc\n</code></pre>\n"}, // #2249 {"sect/doc7.ad", `_Shortcodes:_ *b: {{< b >}} c: {{% c %}}*`, filepath.FromSlash("sect/doc7/index.html"), "<div class=\"paragraph\">\n<p><em>Shortcodes:</em> <strong>b: b c: c</strong></p>\n</div>\n"}, } sources := make([]source.ByteSource, len(tests)) for i, test := range tests { sources[i] = source.ByteSource{Name: filepath.FromSlash(test.contentPath), Content: []byte(test.content)} } s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, targets: targetList{page: &target.PagePub{UglyURLs: false}}, } s.initializeSiteInfo() s.loadTemplates() s.Tmpl.AddTemplate("_default/single.html", "{{.Content}}") s.Tmpl.AddInternalShortcode("b.html", `b`) s.Tmpl.AddInternalShortcode("c.html", `c`) s.Tmpl.AddInternalShortcode("d.html", `d`) s.Tmpl.MarkReady() createAndRenderPages(t, s) for _, test := range tests { if strings.HasSuffix(test.contentPath, ".ad") && !helpers.HasAsciidoc() { fmt.Println("Skip Asciidoc test case as no Asciidoc present.") continue } file, err := hugofs.Destination().Open(test.outFile) if err != nil { t.Fatalf("Did not find %s in target: %s", test.outFile, err) } content := helpers.ReaderToString(file) if content != test.expected { t.Errorf("%s content expected:\n%q\ngot:\n%q", test.outFile, test.expected, content) } } }
func doTestCrossrefs(t *testing.T, relative, uglyURLs bool) { testCommonResetState() baseURL := "http://foo/bar" viper.Set("defaultExtension", "html") viper.Set("baseURL", baseURL) viper.Set("uglyURLs", uglyURLs) viper.Set("verbose", true) var refShortcode string var expectedBase string var expectedURLSuffix string var expectedPathSuffix string if relative { refShortcode = "relref" expectedBase = "/bar" } else { refShortcode = "ref" expectedBase = baseURL } if uglyURLs { expectedURLSuffix = ".html" expectedPathSuffix = ".html" } else { expectedURLSuffix = "/" expectedPathSuffix = "/index.html" } sources := []source.ByteSource{ { Name: filepath.FromSlash("sect/doc1.md"), Content: []byte(fmt.Sprintf(`Ref 2: {{< %s "sect/doc2.md" >}}`, refShortcode)), }, // Issue #1148: Make sure that no P-tags is added around shortcodes. { Name: filepath.FromSlash("sect/doc2.md"), Content: []byte(fmt.Sprintf(`**Ref 1:** {{< %s "sect/doc1.md" >}} THE END.`, refShortcode)), }, // Issue #1753: Should not add a trailing newline after shortcode. { Name: filepath.FromSlash("sect/doc3.md"), Content: []byte(fmt.Sprintf(`**Ref 1:**{{< %s "sect/doc3.md" >}}.`, refShortcode)), }, } s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, targets: targetList{page: &target.PagePub{UglyURLs: uglyURLs}}, Language: helpers.NewDefaultLanguage(), } if err := buildAndRenderSite(s, "_default/single.html", "{{.Content}}"); err != nil { t.Fatalf("Failed to build site: %s", err) } if len(s.AllPages) != 3 { t.Fatalf("Expected 3 got %d pages", len(s.AllPages)) } tests := []struct { doc string expected string }{ {filepath.FromSlash(fmt.Sprintf("sect/doc1%s", expectedPathSuffix)), fmt.Sprintf("<p>Ref 2: %s/sect/doc2%s</p>\n", expectedBase, expectedURLSuffix)}, {filepath.FromSlash(fmt.Sprintf("sect/doc2%s", expectedPathSuffix)), fmt.Sprintf("<p><strong>Ref 1:</strong></p>\n\n%s/sect/doc1%s\n\n<p>THE END.</p>\n", expectedBase, expectedURLSuffix)}, {filepath.FromSlash(fmt.Sprintf("sect/doc3%s", expectedPathSuffix)), fmt.Sprintf("<p><strong>Ref 1:</strong>%s/sect/doc3%s.</p>\n", expectedBase, expectedURLSuffix)}, } for _, test := range tests { file, err := hugofs.Destination().Open(test.doc) if err != nil { t.Fatalf("Did not find %s in target: %s", test.doc, err) } content := helpers.ReaderToString(file) if content != test.expected { t.Fatalf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, content) } } }