// pluralize returns the plural form of a single word. func pluralize(in interface{}) (string, error) { word, err := cast.ToStringE(in) if err != nil { return "", err } return inflect.Pluralize(word), nil }
func plural(qty int, singular string) string { s := singular if qty != 1 { s = inflect.Pluralize(s) } return fmt.Sprintf("%d %s", qty, s) }
func doTestSectionNaming(t *testing.T, canonify, uglify, pluralize bool) { hugofs.InitMemFs() 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() s.prepTemplates( "_default/single.html", "{{.Content}}", "_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.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) } } }