diff options
Diffstat (limited to 'common/paths')
-rw-r--r-- | common/paths/path.go | 58 | ||||
-rw-r--r-- | common/paths/path_test.go | 24 | ||||
-rw-r--r-- | common/paths/url.go | 24 | ||||
-rw-r--r-- | common/paths/url_test.go | 30 |
4 files changed, 2 insertions, 134 deletions
diff --git a/common/paths/path.go b/common/paths/path.go index 0237dd9f2..63e831ff6 100644 --- a/common/paths/path.go +++ b/common/paths/path.go @@ -16,7 +16,6 @@ package paths import ( "errors" "fmt" - "os" "path" "path/filepath" "regexp" @@ -36,8 +35,7 @@ type filepathPathBridge interface { Separator() string } -type filepathBridge struct { -} +type filepathBridge struct{} func (filepathBridge) Base(in string) string { return filepath.Base(in) @@ -65,11 +63,6 @@ func (filepathBridge) Separator() string { var fpb filepathBridge -// ToSlashTrimLeading is just a filepath.ToSlash with an added / prefix trimmer. -func ToSlashTrimLeading(s string) string { - return strings.TrimPrefix(filepath.ToSlash(s), "/") -} - // MakeTitle converts the path given to a suitable title, trimming whitespace // and replacing hyphens with whitespace. func MakeTitle(inpath string) string { @@ -233,23 +226,6 @@ func GetRelativePath(path, base string) (final string, err error) { return name, nil } -// PathPrep prepares the path using the uglify setting to create paths on -// either the form /section/name/index.html or /section/name.html. -func PathPrep(ugly bool, in string) string { - if ugly { - return Uglify(in) - } - return PrettifyPath(in) -} - -// PrettifyPath is the same as PrettifyURLPath but for file paths. -// /section/name.html becomes /section/name/index.html -// /section/name/ becomes /section/name/index.html -// /section/name/index.html becomes /section/name/index.html -func PrettifyPath(in string) string { - return prettifyPath(in, fpb) -} - func prettifyPath(in string, b filepathPathBridge) string { if filepath.Ext(in) == "" { // /section/name/ -> /section/name/index.html @@ -278,35 +254,3 @@ func (n NamedSlice) String() string { } return fmt.Sprintf("%s%s{%s}", n.Name, FilePathSeparator, strings.Join(n.Slice, ",")) } - -// FindCWD returns the current working directory from where the Hugo -// executable is run. -func FindCWD() (string, error) { - serverFile, err := filepath.Abs(os.Args[0]) - if err != nil { - return "", fmt.Errorf("can't get absolute path for executable: %v", err) - } - - path := filepath.Dir(serverFile) - realFile, err := filepath.EvalSymlinks(serverFile) - if err != nil { - if _, err = os.Stat(serverFile + ".exe"); err == nil { - realFile = filepath.Clean(serverFile + ".exe") - } - } - - if err == nil && realFile != serverFile { - path = filepath.Dir(realFile) - } - - return path, nil -} - -// AddTrailingSlash adds a trailing Unix styled slash (/) if not already -// there. -func AddTrailingSlash(path string) string { - if !strings.HasSuffix(path, "/") { - path += "/" - } - return path -} diff --git a/common/paths/path_test.go b/common/paths/path_test.go index e55493c7d..d1917a359 100644 --- a/common/paths/path_test.go +++ b/common/paths/path_test.go @@ -226,27 +226,3 @@ func TestFileAndExt(t *testing.T) { } } } - -func TestFindCWD(t *testing.T) { - type test struct { - expectedDir string - expectedErr error - } - - // cwd, _ := os.Getwd() - data := []test{ - //{cwd, nil}, - // Commenting this out. It doesn't work properly. - // There's a good reason why we don't use os.Getwd(), it doesn't actually work the way we want it to. - // I really don't know a better way to test this function. - SPF 2014.11.04 - } - for i, d := range data { - dir, err := FindCWD() - if d.expectedDir != dir { - t.Errorf("Test %d failed. Expected %q but got %q", i, d.expectedDir, dir) - } - if d.expectedErr != err { - t.Errorf("Test %d failed. Expected %q but got %q", i, d.expectedErr, err) - } - } -} diff --git a/common/paths/url.go b/common/paths/url.go index 600e8d22d..c50f7f865 100644 --- a/common/paths/url.go +++ b/common/paths/url.go @@ -22,8 +22,7 @@ import ( "github.com/PuerkitoBio/purell" ) -type pathBridge struct { -} +type pathBridge struct{} func (pathBridge) Base(in string) string { return path.Base(in) @@ -83,17 +82,6 @@ func sanitizeURLWithFlags(in string, f purell.NormalizationFlags) string { // End temporary kludge // return s - -} - -// SanitizeURL sanitizes the input URL string. -func SanitizeURL(in string) string { - return sanitizeURLWithFlags(in, purell.FlagsSafe|purell.FlagRemoveTrailingSlash|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator) -} - -// SanitizeURLKeepTrailingSlash is the same as SanitizeURL, but will keep any trailing slash. -func SanitizeURLKeepTrailingSlash(in string) string { - return sanitizeURLWithFlags(in, purell.FlagsSafe|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator) } // MakePermalink combines base URL with content path to create full URL paths. @@ -127,16 +115,6 @@ func MakePermalink(host, plink string) *url.URL { return base } -// IsAbsURL determines whether the given path points to an absolute URL. -func IsAbsURL(path string) bool { - url, err := url.Parse(path) - if err != nil { - return false - } - - return url.IsAbs() || strings.HasPrefix(path, "//") -} - // AddContextRoot adds the context root to an URL if it's not already set. // For relative URL entries on sites with a base url with a context root set (i.e. http://example.com/mysite), // relative URLs must not include the context root if canonifyURLs is enabled. But if it's disabled, it must be set. diff --git a/common/paths/url_test.go b/common/paths/url_test.go index 3e8391ef5..4e5f73053 100644 --- a/common/paths/url_test.go +++ b/common/paths/url_test.go @@ -14,41 +14,11 @@ package paths import ( - "strings" "testing" qt "github.com/frankban/quicktest" ) -func TestSanitizeURL(t *testing.T) { - tests := []struct { - input string - expected string - }{ - {"http://foo.bar/", "http://foo.bar"}, - {"http://foo.bar", "http://foo.bar"}, // issue #1105 - {"http://foo.bar/zoo/", "http://foo.bar/zoo"}, // issue #931 - } - - for i, test := range tests { - o1 := SanitizeURL(test.input) - o2 := SanitizeURLKeepTrailingSlash(test.input) - - expected2 := test.expected - - if strings.HasSuffix(test.input, "/") && !strings.HasSuffix(expected2, "/") { - expected2 += "/" - } - - if o1 != test.expected { - t.Errorf("[%d] 1: Expected %#v, got %#v\n", i, test.expected, o1) - } - if o2 != expected2 { - t.Errorf("[%d] 2: Expected %#v, got %#v\n", i, expected2, o2) - } - } -} - func TestMakePermalink(t *testing.T) { type test struct { host, link, output string |