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 {
s := ""
if n.Section != "" {
s = n.Section
}
return s
return n.Section
}

View file

@ -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
}

View file

@ -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 = `

View file

@ -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) {
}
}
}

View file

@ -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)