Further work on path/section stuff. Tests passing now.

This commit is contained in:
spf13 2013-08-14 08:57:14 -04:00
parent b4bcc591e4
commit 480e01eb15
5 changed files with 65 additions and 62 deletions

View file

@ -39,10 +39,5 @@ type UrlPath struct {
} }
func (n *Node) GetSection() string { func (n *Node) GetSection() string {
s := "" return n.Section
if n.Section != "" {
s = n.Section
}
return s
} }

View file

@ -94,11 +94,7 @@ func NewPage(filename string) *Page {
} }
func (page *Page) Initalize() error { func (page *Page) Initalize() error {
err := page.setUrlPath() err := page.buildPageFromFile()
if err != nil {
return err
}
err = page.buildPageFromFile()
if err != nil { if err != nil {
return err return err
} }
@ -106,57 +102,22 @@ func (page *Page) Initalize() error {
return nil return nil
} }
func (p *Page) setUrlPath() error { func (p *Page) guessSection() {
y := strings.TrimPrefix(p.FileName, p.Site.Config.GetAbsPath(p.Site.Config.ContentDir)) if p.Section == "" {
x := strings.Split(y, string(os.PathSeparator)) x := strings.Split(p.FileName, string(os.PathSeparator))
if len(x) > 1 {
if len(x) <= 1 { if section := x[len(x)-2]; section != "content" {
return errors.New("Zero length page name") p.Section = section
} }
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
} }
} }
p.OutFile = p.Path + string(os.PathSeparator) + strings.TrimSpace(outfile)
} }
func (page *Page) Type() string { func (page *Page) Type() string {
if page.contentType != "" { if page.contentType != "" {
return page.contentType return page.contentType
} }
page.setUrlPath() page.guessSection()
if x := page.GetSection(); x != "" { if x := page.GetSection(); x != "" {
return x return x
} }

View file

@ -35,6 +35,7 @@ func TestNewPageWithFilePath(t *testing.T) {
for _, el := range toCheck { for _, el := range toCheck {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_YAML), el.input) p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_YAML), el.input)
p.guessSection()
if err != nil { if err != nil {
t.Fatalf("Reading from SIMPLE_PAGE_YAML resulted in an error: %s", err) t.Fatalf("Reading from SIMPLE_PAGE_YAML resulted in an error: %s", err)
} }
@ -47,5 +48,3 @@ func TestNewPageWithFilePath(t *testing.T) {
} }
} }
} }

View file

@ -16,6 +16,7 @@ package hugolib
import ( import (
"bitbucket.org/pkg/inflect" "bitbucket.org/pkg/inflect"
"bytes" "bytes"
"errors"
"fmt" "fmt"
"github.com/spf13/nitro" "github.com/spf13/nitro"
"html/template" "html/template"
@ -263,8 +264,9 @@ func (s *Site) CreatePages() {
page := NewPage(fileName) page := NewPage(fileName)
page.Site = s.Info page.Site = s.Info
page.Tmpl = s.Tmpl page.Tmpl = s.Tmpl
_ = s.setUrlPath(page)
page.Initalize() page.Initalize()
page.setOutFile() s.setOutFile(page)
if s.Config.BuildDrafts || !page.Draft { if s.Config.BuildDrafts || !page.Draft {
s.Pages = append(s.Pages, page) 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) { func (s *Site) BuildSiteMeta() (err error) {
s.Indexes = make(IndexList) s.Indexes = make(IndexList)
s.Sections = make(Index) s.Sections = make(Index)