diff --git a/hugolib/node.go b/hugolib/node.go index 2d323aa4f..a04a34f25 100644 --- a/hugolib/node.go +++ b/hugolib/node.go @@ -39,10 +39,5 @@ type UrlPath struct { } func (n *Node) GetSection() string { - s := "" - if n.Section != "" { - s = n.Section - } - - return s + return n.Section } diff --git a/hugolib/page.go b/hugolib/page.go index 7da47bd0e..aca0bc203 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -94,11 +94,7 @@ func NewPage(filename string) *Page { } func (page *Page) Initalize() error { - err := page.setUrlPath() - if err != nil { - return err - } - err = page.buildPageFromFile() + err := page.buildPageFromFile() if err != nil { return err } @@ -106,57 +102,22 @@ func (page *Page) Initalize() error { return nil } -func (p *Page) setUrlPath() error { - y := strings.TrimPrefix(p.FileName, p.Site.Config.GetAbsPath(p.Site.Config.ContentDir)) - x := strings.Split(y, string(os.PathSeparator)) - - if len(x) <= 1 { - return errors.New("Zero length page name") - } - - p.Section = strings.Trim(x[1], "/\\") - p.Path = strings.Trim(strings.Join(x[:len(x)-1], string(os.PathSeparator)), "/\\") - return nil -} - -// If Url is provided it is assumed to be the complete relative path -// and will override everything -// Otherwise path + slug is used if provided -// Lastly path + filename is used if provided -func (p *Page) setOutFile() { - // Always use Url if it's specified - if len(strings.TrimSpace(p.Url)) > 2 { - p.OutFile = strings.TrimSpace(p.Url) - return - } - - var outfile string - if len(strings.TrimSpace(p.Slug)) > 0 { - // Use Slug if provided - if p.Site.Config.UglyUrls { - outfile = p.Slug + "." + p.Extension - } else { - outfile = p.Slug + slash + "index." + p.Extension - } - } else { - // Fall back to filename - _, t := filepath.Split(p.FileName) - if p.Site.Config.UglyUrls { - outfile = replaceExtension(strings.TrimSpace(t), p.Extension) - } else { - file, _ := fileExt(strings.TrimSpace(t)) - outfile = file + slash + "index." + p.Extension +func (p *Page) guessSection() { + if p.Section == "" { + x := strings.Split(p.FileName, string(os.PathSeparator)) + if len(x) > 1 { + if section := x[len(x)-2]; section != "content" { + p.Section = section + } } } - - p.OutFile = p.Path + string(os.PathSeparator) + strings.TrimSpace(outfile) } func (page *Page) Type() string { if page.contentType != "" { return page.contentType } - page.setUrlPath() + page.guessSection() if x := page.GetSection(); x != "" { return x } diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 363ee1941..48bf1498d 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -32,7 +32,7 @@ Short Delim ` var INVALID_FRONT_MATTER_LEADING_WS = ` - + --- title: Leading WS --- @@ -52,7 +52,7 @@ var SIMPLE_PAGE_JSON = ` "slug": "spf13-vim-3-0-release-and-new-website" } -Content of the file goes Here +Content of the file goes Here ` var SIMPLE_PAGE_JSON_MULTIPLE = ` diff --git a/hugolib/path_seperators_test.go b/hugolib/path_seperators_test.go index f13eea1ef..4f9e7cdfd 100644 --- a/hugolib/path_seperators_test.go +++ b/hugolib/path_seperators_test.go @@ -23,10 +23,10 @@ func TestDegenerateMissingFolderInPageFilename(t *testing.T) { } func TestNewPageWithFilePath(t *testing.T) { - toCheck := []struct{ - input string + toCheck := []struct { + input string section string - layout string + layout string }{ {filepath.Join("sub", "foobar.html"), "sub", "sub/single.html"}, {filepath.Join("content", "sub", "foobar.html"), "sub", "sub/single.html"}, @@ -35,6 +35,7 @@ func TestNewPageWithFilePath(t *testing.T) { for _, el := range toCheck { p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_YAML), el.input) + p.guessSection() if err != nil { t.Fatalf("Reading from SIMPLE_PAGE_YAML resulted in an error: %s", err) } @@ -47,5 +48,3 @@ func TestNewPageWithFilePath(t *testing.T) { } } } - - diff --git a/hugolib/site.go b/hugolib/site.go index 770a14fc1..ad2171f8d 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -16,6 +16,7 @@ package hugolib import ( "bitbucket.org/pkg/inflect" "bytes" + "errors" "fmt" "github.com/spf13/nitro" "html/template" @@ -263,8 +264,9 @@ func (s *Site) CreatePages() { page := NewPage(fileName) page.Site = s.Info page.Tmpl = s.Tmpl + _ = s.setUrlPath(page) page.Initalize() - page.setOutFile() + s.setOutFile(page) if s.Config.BuildDrafts || !page.Draft { s.Pages = append(s.Pages, page) } @@ -285,6 +287,52 @@ func (s *Site) setupPrevNext() { } } +func (s *Site) setUrlPath(p *Page) error { + y := strings.TrimPrefix(p.FileName, s.Config.GetAbsPath(s.Config.ContentDir)) + x := strings.Split(y, string(os.PathSeparator)) + + if len(x) <= 1 { + return errors.New("Zero length page name") + } + + p.Section = strings.Trim(x[1], "/\\") + p.Path = strings.Trim(strings.Join(x[:len(x)-1], string(os.PathSeparator)), "/\\") + return nil +} + +// If Url is provided it is assumed to be the complete relative path +// and will override everything +// Otherwise path + slug is used if provided +// Lastly path + filename is used if provided +func (s *Site) setOutFile(p *Page) { + // Always use Url if it's specified + if len(strings.TrimSpace(p.Url)) > 2 { + p.OutFile = strings.TrimSpace(p.Url) + return + } + + var outfile string + if len(strings.TrimSpace(p.Slug)) > 0 { + // Use Slug if provided + if s.Config.UglyUrls { + outfile = p.Slug + "." + p.Extension + } else { + outfile = p.Slug + slash + "index." + p.Extension + } + } else { + // Fall back to filename + _, t := filepath.Split(p.FileName) + if s.Config.UglyUrls { + outfile = replaceExtension(strings.TrimSpace(t), p.Extension) + } else { + file, _ := fileExt(strings.TrimSpace(t)) + outfile = file + slash + "index." + p.Extension + } + } + + p.OutFile = p.Path + string(os.PathSeparator) + strings.TrimSpace(outfile) +} + func (s *Site) BuildSiteMeta() (err error) { s.Indexes = make(IndexList) s.Sections = make(Index)