summaryrefslogtreecommitdiffstats
path: root/helpers/url.go
diff options
context:
space:
mode:
authorbep <bjorn.erik.pedersen@gmail.com>2015-01-27 11:44:41 +0100
committerbep <bjorn.erik.pedersen@gmail.com>2015-01-27 11:44:41 +0100
commitbedc2d848803bfcc88de876f1a2d848ff8c57303 (patch)
tree257ebe61ca382043dac89f3ddb42dca279fce3f7 /helpers/url.go
parent5f9596e68c55830d046de3faa88ab0a8f68fcf76 (diff)
Introduce FilepathPathBridge
This commit introduces the new interface FilepathPathBridge to remove some code that differs only in their use of either the path or filepath package.
Diffstat (limited to 'helpers/url.go')
-rw-r--r--helpers/url.go56
1 files changed, 31 insertions, 25 deletions
diff --git a/helpers/url.go b/helpers/url.go
index 558ed9c97..e4db6ceb7 100644
--- a/helpers/url.go
+++ b/helpers/url.go
@@ -22,6 +22,35 @@ import (
"strings"
)
+type PathBridge struct {
+}
+
+func (PathBridge) Base(in string) string {
+ return path.Base(in)
+}
+
+func (PathBridge) Clean(in string) string {
+ return path.Clean(in)
+}
+
+func (PathBridge) Dir(in string) string {
+ return path.Dir(in)
+}
+
+func (PathBridge) Ext(in string) string {
+ return path.Ext(in)
+}
+
+func (PathBridge) Join(elem ...string) string {
+ return path.Join(elem...)
+}
+
+func (PathBridge) Separator() string {
+ return "/"
+}
+
+var pathBridge PathBridge
+
// SanitizeUrl sanitizes the input URL string.
func SanitizeUrl(in string) string {
url, err := purell.NormalizeURLString(in, purell.FlagsSafe|purell.FlagRemoveTrailingSlash|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator)
@@ -141,22 +170,7 @@ func PrettifyUrl(in string) string {
// /section/name/ becomes /section/name/index.html
// /section/name/index.html becomes /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)
- }
- }
+ return PrettiyPath(in, pathBridge)
}
// Uglify does the opposite of PrettifyUrlPath().
@@ -171,7 +185,7 @@ func Uglify(in string) string {
// /section/name/ -> /section/name.html
return path.Clean(in) + ".html"
} else {
- name, ext := ResourceAndExt(in)
+ name, ext := FileAndExt(in, pathBridge)
if name == "index" {
// /section/name/index.html -> /section/name.html
d := path.Dir(in)
@@ -186,11 +200,3 @@ func Uglify(in string) string {
}
}
}
-
-// Same as FileAndExt, but for URLs.
-func ResourceAndExt(in string) (name string, ext string) {
- ext = path.Ext(in)
- base := path.Base(in)
-
- return extractFilename(in, ext, base, "/"), ext
-}