Skip to content

Commit

Permalink
common/paths: Remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jan 16, 2022
1 parent 55a9bc1 commit 6f07bdb
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 134 deletions.
58 changes: 1 addition & 57 deletions common/paths/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package paths
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"regexp"
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
24 changes: 0 additions & 24 deletions common/paths/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
24 changes: 1 addition & 23 deletions common/paths/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
30 changes: 0 additions & 30 deletions common/paths/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6f07bdb

Please sign in to comment.