Fix various Windows-issues

File handling was broken on Windows. This commit contains a revision of the path handling with separation of file paths and urls where needed.

There may be remaining issues and there may be better ways to do this, but it is easier to start that refactoring job with a set of passing tests.

Fixes #687
Fixes #660
This commit is contained in:
bep 2014-12-07 19:48:00 +01:00 committed by spf13
parent 3a8c12418a
commit 9f77f93071
19 changed files with 205 additions and 137 deletions

View file

@ -15,11 +15,10 @@ package helpers
import (
"fmt"
"net/url"
"path/filepath"
"strings"
"github.com/PuerkitoBio/purell"
"net/url"
"path"
"strings"
)
func SanitizeUrl(in string) string {
@ -68,7 +67,7 @@ func MakePermalink(host, plink string) *url.URL {
panic(fmt.Errorf("Can't make permalink from absolute link %q", plink))
}
base.Path = filepath.Join(base.Path, p.Path)
base.Path = path.Join(base.Path, p.Path)
// path.Join will strip off the last /, so put it back if it was there.
if strings.HasSuffix(p.Path, "/") && !strings.HasSuffix(base.Path, "/") {
@ -84,7 +83,7 @@ func UrlPrep(ugly bool, in string) string {
return x
} else {
x := PrettifyUrl(SanitizeUrl(in))
if filepath.Ext(x) == ".xml" {
if path.Ext(x) == ".xml" {
return x
}
url, err := purell.NormalizeURLString(x, purell.FlagAddTrailingSlash)
@ -98,10 +97,10 @@ func UrlPrep(ugly bool, in string) string {
// Don't Return /index.html portion.
func PrettifyUrl(in string) string {
x := PrettifyPath(in)
x := PrettifyUrlPath(in)
if filepath.Base(x) == "index.html" {
return filepath.Dir(x)
if path.Base(x) == "index.html" {
return path.Dir(x)
}
if in == "" {
@ -111,21 +110,43 @@ func PrettifyUrl(in string) string {
return x
}
// /section/name.html -> /section/name/index.html
// /section/name/ -> /section/name/index.html
// /section/name/index.html -> /section/name/index.html
func PrettifyUrlPath(in string) string {
if path.Ext(in) == "" {
// /section/name/ -> /section/name/index.html
if len(in) < 2 {
return "/"
}
return path.Join(path.Clean(in), "index.html")
} else {
name, ext := ResourceAndExt(in)
if name == "index" {
// /section/name/index.html -> /section/name/index.html
return path.Clean(in)
} else {
// /section/name.html -> /section/name/index.html
return path.Join(path.Dir(in), name, "index"+ext)
}
}
}
// /section/name/index.html -> /section/name.html
// /section/name/ -> /section/name.html
// /section/name.html -> /section/name.html
func Uglify(in string) string {
if filepath.Ext(in) == "" {
if path.Ext(in) == "" {
if len(in) < 2 {
return "/"
}
// /section/name/ -> /section/name.html
return filepath.Clean(in) + ".html"
return path.Clean(in) + ".html"
} else {
name, ext := FileAndExt(in)
name, ext := ResourceAndExt(in)
if name == "index" {
// /section/name/index.html -> /section/name.html
d := filepath.Dir(in)
d := path.Dir(in)
if len(d) > 1 {
return d + ext
} else {
@ -133,7 +154,15 @@ func Uglify(in string) string {
}
} else {
// /section/name.html -> /section/name.html
return filepath.Clean(in)
return path.Clean(in)
}
}
}
// Same as FileAndExt, but for Urls
func ResourceAndExt(in string) (name string, ext string) {
ext = path.Ext(in)
base := path.Base(in)
return FileAndExtSep(in, ext, base, "/"), ext
}