Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Commit

Permalink
Normalize paths within hugo
Browse files Browse the repository at this point in the history
filepath was used inconsistently throughout the hugolib.  With the
introduction of source and target modules, all path are normalized to
"/".  This simplifies the processing of paths.  It does mean that
contributors need to be aware of using path/filepath in any module other
than source or target is not recommended.  The current exception is
hugolib/config.go
  • Loading branch information
noahcampbell committed Sep 12, 2013
1 parent 998b2f7 commit 74b55fc
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 43 deletions.
9 changes: 5 additions & 4 deletions hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (c *Config) readInConfig() {

func (c *Config) setPath(p string) {
if p == "" {
path, err := FindPath()
path, err := findPath()
if err != nil {
fmt.Printf("Error finding path: %s", err)
}
Expand All @@ -124,7 +124,7 @@ func (c *Config) GetPath() string {
return c.Path
}

func FindPath() (string, error) {
func findPath() (string, error) {
serverFile, err := filepath.Abs(os.Args[0])

if err != nil {
Expand All @@ -147,13 +147,14 @@ func FindPath() (string, error) {
return path, nil
}

// GetAbsPath return the absolute path for a given path with the internal slashes
// properly converted.
func (c *Config) GetAbsPath(name string) string {
if filepath.IsAbs(name) {
return name
}

p := filepath.Join(c.GetPath(), name)
return p
return filepath.ToSlash(filepath.Join(c.GetPath(), name))
}

func (c *Config) findConfigFile(configFileName string) (string, error) {
Expand Down
23 changes: 10 additions & 13 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@ import (
"html/template"
"io"
"launchpad.net/goyaml"
"os"
"path/filepath"
"path"
"sort"
"strings"
"time"
"unicode"
)

var _ = filepath.Base("")

type Page struct {
Status string
Images []string
Expand Down Expand Up @@ -135,7 +132,7 @@ func StripHTML(s string) string {

func (p *Page) guessSection() {
if p.Section == "" {
x := strings.Split(p.FileName, string(os.PathSeparator))
x := strings.Split(p.FileName, "/")
if len(x) > 1 {
if section := x[len(x)-2]; section != "content" {
p.Section = section
Expand Down Expand Up @@ -223,26 +220,26 @@ func (p *Page) Permalink() template.HTML {
section := strings.TrimSpace(p.Section)
pSlug := strings.TrimSpace(p.Slug)
pUrl := strings.TrimSpace(p.Url)
var path string
var permalink string
if len(pSlug) > 0 {
if p.Site.Config.UglyUrls {
path = section + "/" + p.Slug + "." + p.Extension
permalink = section + "/" + p.Slug + "." + p.Extension
} else {
path = section + "/" + p.Slug + "/"
permalink = section + "/" + p.Slug + "/"
}
} else if len(pUrl) > 2 {
path = pUrl
permalink = pUrl
} else {
_, t := filepath.Split(p.FileName)
_, t := path.Split(p.FileName)
if p.Site.Config.UglyUrls {
x := replaceExtension(strings.TrimSpace(t), p.Extension)
path = section + "/" + x
permalink = section + "/" + x
} else {
file, _ := fileExt(strings.TrimSpace(t))
path = section + "/" + file
permalink = section + "/" + file
}
}
return template.HTML(MakePermalink(baseUrl, path))
return template.HTML(MakePermalink(baseUrl, permalink))
}

func (page *Page) handleTomlMetaData(datum []byte) (interface{}, error) {
Expand Down
14 changes: 7 additions & 7 deletions hugolib/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package hugolib

import (
"html/template"
"path/filepath"
"path"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -260,19 +260,19 @@ func TestDegenerateInvalidFrontMatterLeadingWhitespace(t *testing.T) {
}

func TestSectionEvaluation(t *testing.T) {
page, _ := ReadFrom(strings.NewReader(SIMPLE_PAGE), filepath.FromSlash("blue/file1.md"))
page, _ := ReadFrom(strings.NewReader(SIMPLE_PAGE), "blue/file1.md")
if page.Section != "blue" {
t.Errorf("Section should be %s, got: %s", "blue", page.Section)
}
}

func TestLayoutOverride(t *testing.T) {
var (
path_content_one_dir = filepath.Join("content", "gub", "file1.md")
path_content_two_dir = filepath.Join("content", "dub", "sub", "file1.md")
path_content_no_dir = filepath.Join("content", "file1")
path_one_directory = filepath.Join("fub", "file1.md")
path_no_directory = filepath.Join("file1.md")
path_content_one_dir = path.Join("content", "gub", "file1.md")
path_content_two_dir = path.Join("content", "dub", "sub", "file1.md")
path_content_no_dir = path.Join("content", "file1")
path_one_directory = path.Join("fub", "file1.md")
path_no_directory = path.Join("file1.md")
)
tests := []struct {
content string
Expand Down
10 changes: 5 additions & 5 deletions hugolib/path_seperators_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hugolib

import (
"path/filepath"
"path"
"strings"
"testing"
)
Expand All @@ -13,7 +13,7 @@ Sample Text
`

func TestDegenerateMissingFolderInPageFilename(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_YAML), filepath.Join("foobar"))
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_YAML), path.Join("foobar"))
if err != nil {
t.Fatalf("Error in ReadFrom")
}
Expand All @@ -28,9 +28,9 @@ func TestNewPageWithFilePath(t *testing.T) {
section string
layout string
}{
{filepath.Join("sub", "foobar.html"), "sub", "sub/single.html"},
{filepath.Join("content", "sub", "foobar.html"), "sub", "sub/single.html"},
{filepath.Join("content", "dub", "sub", "foobar.html"), "sub", "sub/single.html"},
{path.Join("sub", "foobar.html"), "sub", "sub/single.html"},
{path.Join("content", "sub", "foobar.html"), "sub", "sub/single.html"},
{path.Join("content", "dub", "sub", "foobar.html"), "sub", "sub/single.html"},
}

for _, el := range toCheck {
Expand Down
20 changes: 11 additions & 9 deletions hugolib/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/spf13/nitro"
"html/template"
"os"
"path/filepath"
"path"
"strings"
"time"
)
Expand Down Expand Up @@ -264,7 +264,9 @@ func (s *Site) CreatePages() (err error) {
}
page.Site = s.Info
page.Tmpl = s.Tmpl
_ = s.setUrlPath(page)
if err = s.setUrlPath(page); err != nil {
return err
}
s.setOutFile(page)
if s.Config.BuildDrafts || !page.Draft {
s.Pages = append(s.Pages, page)
Expand All @@ -288,15 +290,15 @@ 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))
y := strings.TrimPrefix(p.FileName, s.absContentDir())
x := strings.Split(y, "/")

if len(x) <= 1 {
return fmt.Errorf("Zero length page name")
}

p.Section = strings.Trim(x[1], "/\\")
p.Path = strings.Trim(strings.Join(x[:len(x)-1], string(os.PathSeparator)), "/\\")
p.Section = strings.Trim(x[1], "/")
p.Path = path.Join(x[:len(x)-1]...)
return nil
}

Expand All @@ -320,11 +322,11 @@ func (s *Site) setOutFile(p *Page) {
outfile = strings.TrimSpace(p.Slug) + "." + p.Extension
} else {
// Fall back to filename
_, t := filepath.Split(p.FileName)
_, t := path.Split(p.FileName)
outfile = replaceExtension(strings.TrimSpace(t), p.Extension)
}

p.OutFile = p.Path + string(os.PathSeparator) + strings.TrimSpace(outfile)
p.OutFile = p.Path + "/" + strings.TrimSpace(outfile)
}

func (s *Site) BuildSiteMeta() (err error) {
Expand Down Expand Up @@ -524,7 +526,7 @@ func (s *Site) RenderLists() error {
if err != nil {
return err
}
err = s.WritePublic(section+"/index.html", content.Bytes())
err = s.WritePublic(section, content.Bytes())
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions hugolib/site_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package hugolib
import (
"bytes"
"io"
"path/filepath"
"strings"
"testing"
)
Expand Down Expand Up @@ -49,8 +48,8 @@ func TestPageCount(t *testing.T) {
s := &Site{Target: target}
s.prepTemplates()
must(s.addTemplate("indexes/blue.html", INDEX_TEMPLATE))
s.Pages = append(s.Pages, mustReturn(ReadFrom(strings.NewReader(SLUG_DOC_1), filepath.FromSlash("content/blue/doc1.md"))))
s.Pages = append(s.Pages, mustReturn(ReadFrom(strings.NewReader(SLUG_DOC_2), filepath.FromSlash("content/blue/doc2.md"))))
s.Pages = append(s.Pages, mustReturn(ReadFrom(strings.NewReader(SLUG_DOC_1), "content/blue/doc1.md")))
s.Pages = append(s.Pages, mustReturn(ReadFrom(strings.NewReader(SLUG_DOC_2), "content/blue/doc2.md")))

if err := s.BuildSiteMeta(); err != nil {
t.Errorf("Unable to build site metadata: %s", err)
Expand All @@ -60,7 +59,7 @@ func TestPageCount(t *testing.T) {
t.Errorf("Unable to render site lists: %s", err)
}

blueIndex := target.files["blue/index.html"]
blueIndex := target.files["blue"]
if blueIndex == nil {
t.Errorf("No indexed rendered. %v", target.files)
}
Expand Down
1 change: 1 addition & 0 deletions source/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (f *Filesystem) Files() []*File {
}

func (f *Filesystem) add(name string, reader io.Reader) {
name = filepath.ToSlash(name)
f.files = append(f.files, &File{Name: name, Contents: reader})
}

Expand Down
5 changes: 4 additions & 1 deletion target/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func TestFileTranslator(t *testing.T) {
{"foo", "foo/index.html"},
{"foo.html", "foo/index.html"},
{"foo.xhtml", "foo/index.xhtml"},
{"section", "section/index.html"},
{"section/", "section/index.html"},
{"section/foo", "section/foo/index.html"},
{"section/foo.html", "section/foo/index.html"},
{"section/foo.rss", "section/foo/index.rss"},
Expand All @@ -34,7 +36,7 @@ func TestFileTranslator(t *testing.T) {

func TestFileTranslatorBase(t *testing.T) {
tests := []struct {
content string
content string
expected string
}{
{"/", "a/base/index.html"},
Expand Down Expand Up @@ -64,6 +66,7 @@ func TestTranslateUglyUrls(t *testing.T) {
}{
{"foo.html", "foo.html"},
{"/", "index.html"},
{"section", "section.html"},
{"index.html", "index.html"},
}

Expand Down

0 comments on commit 74b55fc

Please sign in to comment.