Skip to content

Commit

Permalink
Simplify the content map key structure to ease finding stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Nov 22, 2021
1 parent 5e0947c commit 4e6d17b
Show file tree
Hide file tree
Showing 88 changed files with 4,151 additions and 3,956 deletions.
37 changes: 0 additions & 37 deletions bench.sh

This file was deleted.

12 changes: 0 additions & 12 deletions benchSite.sh

This file was deleted.

2 changes: 1 addition & 1 deletion benchbep.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
gobench -package=./hugolib -bench="BenchmarkSiteNew/Deep_content_tree"
gobench --package ./hugolib --bench "BenchmarkSiteNew/Regular_Deep" -base v0.89.4
1 change: 0 additions & 1 deletion bepdock.sh

This file was deleted.

2 changes: 1 addition & 1 deletion commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ func (c *commandeer) serve(s *serverCmd) error {
mu, serverURL, endpoint, err := srv.createEndpoint(i)

if doLiveReload {
u, err := url.Parse(helpers.SanitizeURL(baseURLs[i]))
u, err := url.Parse(baseURLs[i])
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions common/herrors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type ErrorSender interface {
// Recover is a helper function that can be used to capture panics.
// Put this at the top of a method/function that crashes in a template:
// defer herrors.Recover()
// TODO1 check usage
func Recover(args ...interface{}) {
if r := recover(); r != nil {
fmt.Println("ERR:", r)
Expand Down
60 changes: 21 additions & 39 deletions common/paths/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package paths
import (
"errors"
"fmt"
"net/url"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -83,15 +84,6 @@ func ReplaceExtension(path string, newExt string) string {
return f + "." + newExt
}

func makePathRelative(inPath string, possibleDirectories ...string) (string, error) {
for _, currentPath := range possibleDirectories {
if strings.HasPrefix(inPath, currentPath) {
return strings.TrimPrefix(inPath, currentPath), nil
}
}
return inPath, errors.New("can't extract relative path, unknown prefix")
}

// Should be good enough for Hugo.
var isFileRe = regexp.MustCompile(`.*\..{1,6}$`)

Expand Down Expand Up @@ -233,38 +225,15 @@ 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)
var slashFunc = func(r rune) bool {
return r == '/'
}

func prettifyPath(in string, b filepathPathBridge) string {
if filepath.Ext(in) == "" {
// /section/name/ -> /section/name/index.html
if len(in) < 2 {
return b.Separator()
}
return b.Join(in, "index.html")
}
name, ext := fileAndExt(in, b)
if name == "index" {
// /section/name/index.html -> /section/name/index.html
return b.Clean(in)
}
// /section/name.html -> /section/name/index.html
return b.Join(b.Dir(in), name, "index"+ext)
// FieldsSlash cuts s into fields separated with '/'.
// TODO1 add some tests, consider leading/trailing slashes.
func FieldsSlash(s string) []string {
f := strings.FieldsFunc(s, slashFunc)
return f
}

type NamedSlice struct {
Expand Down Expand Up @@ -310,3 +279,16 @@ func AddTrailingSlash(path string) string {
}
return path
}

// PathEscape escapes unicode letters in pth.
// Use URLEscape to escape full URLs including scheme, query etc.
// This is slightly faster for the common case.
// Note, there is a url.PathEscape function, but that also
// escapes /.
func PathEscape(pth string) string {
u, err := url.Parse(pth)
if err != nil {
panic(err)
}
return u.EscapedPath()
}
23 changes: 0 additions & 23 deletions common/paths/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,6 @@ func TestGetRelativePath(t *testing.T) {
}
}

func TestMakePathRelative(t *testing.T) {
type test struct {
inPath, path1, path2, output string
}

data := []test{
{"/abc/bcd/ab.css", "/abc/bcd", "/bbc/bcd", "/ab.css"},
{"/abc/bcd/ab.css", "/abcd/bcd", "/abc/bcd", "/ab.css"},
}

for i, d := range data {
output, _ := makePathRelative(d.inPath, d.path1, d.path2)
if d.output != output {
t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output)
}
}
_, error := makePathRelative("a/b/c.ss", "/a/c", "/d/c", "/e/f")

if error == nil {
t.Errorf("Test failed, expected error")
}
}

func TestGetDottedRelativePath(t *testing.T) {
// on Windows this will receive both kinds, both country and western ...
for _, f := range []func(string) string{filepath.FromSlash, func(s string) string { return s }} {
Expand Down
107 changes: 7 additions & 100 deletions common/paths/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"net/url"
"path"
"strings"

"github.com/PuerkitoBio/purell"
)

type pathBridge struct {
Expand Down Expand Up @@ -51,51 +49,6 @@ func (pathBridge) Separator() string {

var pb pathBridge

func sanitizeURLWithFlags(in string, f purell.NormalizationFlags) string {
s, err := purell.NormalizeURLString(in, f)
if err != nil {
return in
}

// Temporary workaround for the bug fix and resulting
// behavioral change in purell.NormalizeURLString():
// a leading '/' was inadvertently added to relative links,
// but no longer, see #878.
//
// I think the real solution is to allow Hugo to
// make relative URL with relative path,
// e.g. "../../post/hello-again/", as wished by users
// in issues #157, #622, etc., without forcing
// relative URLs to begin with '/'.
// Once the fixes are in, let's remove this kludge
// and restore SanitizeURL() to the way it was.
// -- @anthonyfok, 2015-02-16
//
// Begin temporary kludge
u, err := url.Parse(s)
if err != nil {
panic(err)
}
if len(u.Path) > 0 && !strings.HasPrefix(u.Path, "/") {
u.Path = "/" + u.Path
}
return u.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.
// Example
// base: http://spf13.com/
Expand Down Expand Up @@ -155,58 +108,12 @@ func AddContextRoot(baseURL, relativePath string) string {
return newPath
}

// URLizeAn

// PrettifyURL takes a URL string and returns a semantic, clean URL.
func PrettifyURL(in string) string {
x := PrettifyURLPath(in)

if path.Base(x) == "index.html" {
return path.Dir(x)
}

if in == "" {
return "/"
}

return x
}

// PrettifyURLPath takes a URL path to a content and converts it
// to enable pretty URLs.
// /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 PrettifyURLPath(in string) string {
return prettifyPath(in, pb)
}

// Uglify does the opposite of PrettifyURLPath().
// /section/name/index.html becomes /section/name.html
// /section/name/ becomes /section/name.html
// /section/name.html becomes /section/name.html
func Uglify(in string) string {
if path.Ext(in) == "" {
if len(in) < 2 {
return "/"
}
// /section/name/ -> /section/name.html
return path.Clean(in) + ".html"
}

name, ext := fileAndExt(in, pb)
if name == "index" {
// /section/name/index.html -> /section/name.html
d := path.Dir(in)
if len(d) > 1 {
return d + ext
}
return in
}
// /.xml -> /index.xml
if name == "" {
return path.Dir(in) + "index" + ext
// URLEscape escapes unicode letters.
func URLEscape(uri string) string {
// escape unicode letters
u, err := url.Parse(uri)
if err != nil {
panic(err)
}
// /section/name.html -> /section/name.html
return path.Clean(in)
return u.String()
}
64 changes: 0 additions & 64 deletions common/paths/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,9 @@
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 Expand Up @@ -95,35 +63,3 @@ func TestAddContextRoot(t *testing.T) {
}
}
}

func TestPretty(t *testing.T) {
c := qt.New(t)
c.Assert("/section/name/index.html", qt.Equals, PrettifyURLPath("/section/name.html"))
c.Assert("/section/sub/name/index.html", qt.Equals, PrettifyURLPath("/section/sub/name.html"))
c.Assert("/section/name/index.html", qt.Equals, PrettifyURLPath("/section/name/"))
c.Assert("/section/name/index.html", qt.Equals, PrettifyURLPath("/section/name/index.html"))
c.Assert("/index.html", qt.Equals, PrettifyURLPath("/index.html"))
c.Assert("/name/index.xml", qt.Equals, PrettifyURLPath("/name.xml"))
c.Assert("/", qt.Equals, PrettifyURLPath("/"))
c.Assert("/", qt.Equals, PrettifyURLPath(""))
c.Assert("/section/name", qt.Equals, PrettifyURL("/section/name.html"))
c.Assert("/section/sub/name", qt.Equals, PrettifyURL("/section/sub/name.html"))
c.Assert("/section/name", qt.Equals, PrettifyURL("/section/name/"))
c.Assert("/section/name", qt.Equals, PrettifyURL("/section/name/index.html"))
c.Assert("/", qt.Equals, PrettifyURL("/index.html"))
c.Assert("/name/index.xml", qt.Equals, PrettifyURL("/name.xml"))
c.Assert("/", qt.Equals, PrettifyURL("/"))
c.Assert("/", qt.Equals, PrettifyURL(""))
}

func TestUgly(t *testing.T) {
c := qt.New(t)
c.Assert("/section/name.html", qt.Equals, Uglify("/section/name.html"))
c.Assert("/section/sub/name.html", qt.Equals, Uglify("/section/sub/name.html"))
c.Assert("/section/name.html", qt.Equals, Uglify("/section/name/"))
c.Assert("/section/name.html", qt.Equals, Uglify("/section/name/index.html"))
c.Assert("/index.html", qt.Equals, Uglify("/index.html"))
c.Assert("/name.xml", qt.Equals, Uglify("/name.xml"))
c.Assert("/", qt.Equals, Uglify("/"))
c.Assert("/", qt.Equals, Uglify(""))
}
Loading

0 comments on commit 4e6d17b

Please sign in to comment.