Allow URL with extension in frontmatter

Fixes #1923
This commit is contained in:
Bjørn Erik Pedersen 2016-03-05 20:18:17 +01:00
parent 3c100cc32c
commit 27f8d8f963
2 changed files with 42 additions and 12 deletions

View file

@ -45,6 +45,7 @@ import (
"github.com/spf13/nitro"
"github.com/spf13/viper"
"gopkg.in/fsnotify.v1"
"path"
)
var _ = transform.AbsURL
@ -90,9 +91,10 @@ type Site struct {
}
type targetList struct {
Page target.Output
File target.Output
Alias target.AliasPublisher
Page target.Output
PageUgly target.Output
File target.Output
Alias target.AliasPublisher
}
type SiteInfo struct {
@ -1937,6 +1939,16 @@ func (s *Site) renderAndWritePage(name string, dest string, d interface{}, layou
outBuffer := bp.GetBuffer()
defer bp.PutBuffer(outBuffer)
var pageTarget target.Output
if p, ok := d.(*Page); ok && path.Ext(p.URL) != "" {
// user has explicitly set a URL with extension for this page
// make sure it sticks even if "ugly URLs" are turned off.
pageTarget = s.PageUglyTarget()
} else {
pageTarget = s.PageTarget()
}
transformLinks := transform.NewEmptyTransforms()
if viper.GetBool("RelativeURLs") || viper.GetBool("CanonifyURLs") {
@ -1950,7 +1962,7 @@ func (s *Site) renderAndWritePage(name string, dest string, d interface{}, layou
var path []byte
if viper.GetBool("RelativeURLs") {
translated, err := s.PageTarget().(target.OptionalTranslator).TranslateRelative(dest)
translated, err := pageTarget.(target.OptionalTranslator).TranslateRelative(dest)
if err != nil {
return err
}
@ -1981,7 +1993,7 @@ func (s *Site) renderAndWritePage(name string, dest string, d interface{}, layou
}
if err == nil {
if err = s.WriteDestPage(dest, outBuffer); err != nil {
if err = s.WriteDestPage(dest, pageTarget, outBuffer); err != nil {
return err
}
}
@ -2033,6 +2045,11 @@ func (s *Site) PageTarget() target.Output {
return s.Targets.Page
}
func (s *Site) PageUglyTarget() target.Output {
s.initTargetList()
return s.Targets.PageUgly
}
func (s *Site) FileTarget() target.Output {
s.initTargetList()
return s.Targets.File
@ -2051,6 +2068,12 @@ func (s *Site) initTargetList() {
UglyURLs: viper.GetBool("UglyURLs"),
}
}
if s.Targets.PageUgly == nil {
s.Targets.PageUgly = &target.PagePub{
PublishDir: s.absPublishDir(),
UglyURLs: true,
}
}
if s.Targets.File == nil {
s.Targets.File = &target.Filesystem{
PublishDir: s.absPublishDir(),
@ -2069,9 +2092,9 @@ func (s *Site) WriteDestFile(path string, reader io.Reader) (err error) {
return s.FileTarget().Publish(path, reader)
}
func (s *Site) WriteDestPage(path string, reader io.Reader) (err error) {
func (s *Site) WriteDestPage(path string, target target.Output, reader io.Reader) (err error) {
jww.DEBUG.Println("creating page:", path)
return s.PageTarget().Publish(path, reader)
return target.Publish(path, reader)
}
func (s *Site) WriteDestAlias(path string, permalink string) (err error) {

View file

@ -419,14 +419,15 @@ THE END.`, refShortcode))},
}
// Issue #939
func Test404ShouldAlwaysHaveUglyURLs(t *testing.T) {
// Issue #1923
func TestShouldAlwaysHaveUglyURLs(t *testing.T) {
hugofs.DestinationFS = new(afero.MemMapFs)
for _, uglyURLs := range []bool{true, false} {
doTest404ShouldAlwaysHaveUglyURLs(t, uglyURLs)
doTestShouldAlwaysHaveUglyURLs(t, uglyURLs)
}
}
func doTest404ShouldAlwaysHaveUglyURLs(t *testing.T, uglyURLs bool) {
func doTestShouldAlwaysHaveUglyURLs(t *testing.T, uglyURLs bool) {
viper.Reset()
defer viper.Reset()
@ -436,11 +437,15 @@ func doTest404ShouldAlwaysHaveUglyURLs(t *testing.T, uglyURLs bool) {
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.html"), []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")},
{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{
@ -475,10 +480,12 @@ func doTest404ShouldAlwaysHaveUglyURLs(t *testing.T, uglyURLs bool) {
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(expectedPagePath), "\n\n<h1 id=\"title\">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>"},
// Issue #1923
{filepath.FromSlash("ugly.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>doc2 <em>content</em></p>\n IsHome=false"},
}
for _, p := range s.Pages {