shithub: hugo

Download patch

ref: e1340c060bc52c95116bf0d5105a6c0d44bb67e6
parent: a0447345418e735a4b0fd401c1610950f66ed778
author: bep <[email protected]>
date: Tue Mar 17 11:38:48 EDT 2015

Fix crossrefs on Windows

Have to convert path slashes to file path slashes before the URL path is compared to a file path.

Fixes #957

--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -172,7 +172,8 @@
 
 	if refURL.Path != "" {
 		for _, page := range []*Page(*s.Pages) {
-			if page.Source.Path() == refURL.Path || page.Source.LogicalName() == refURL.Path {
+			refPath := filepath.FromSlash(refURL.Path)
+			if page.Source.Path() == refPath || page.Source.LogicalName() == refPath {
 				target = page
 				break
 			}
--- a/hugolib/site_test.go
+++ b/hugolib/site_test.go
@@ -307,6 +307,83 @@
 	viper.Set("BuildFuture", false)
 }
 
+// Issue #957
+func TestCrossrefs(t *testing.T) {
+	for _, uglyUrls := range []bool{true, false} {
+		for _, relative := range []bool{true, false} {
+			doTestCrossrefs(t, relative, uglyUrls)
+		}
+	}
+}
+
+func doTestCrossrefs(t *testing.T, relative, uglyUrls bool) {
+	baseUrl := "http://foo/bar"
+	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))},
+		{filepath.FromSlash("sect/doc2.md"),
+			[]byte(fmt.Sprintf(`Ref 1: {{< %s "sect/doc1.md" >}}`, 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>Ref 1: %s/sect/doc1%s</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.ReaderToBytes(file)
+
+		if !bytes.Equal(content, []byte(test.expected)) {
+			t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, string(content))
+		}
+	}
+
+}
+
 // Issue #939
 func Test404ShouldAlwaysHaveUglyUrls(t *testing.T) {
 	for _, uglyURLs := range []bool{true, false} {