summaryrefslogtreecommitdiffstats
path: root/helpers/path.go
diff options
context:
space:
mode:
authorMathias Biilmann <info@mathias-biilmann.net>2016-07-11 01:06:40 -0700
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-07-11 10:06:40 +0200
commit330639d2aefbcaef4106653617fe8b518eb0711e (patch)
tree95678c6a01aa08ce00221467c2a8abaa1e6062a2 /helpers/path.go
parent32d82a4496e5f53a723293175e72b5a57ea6e5a3 (diff)
Fix panic when using URLize
Using URLize on a string like '100%-true' would cause a panic
Diffstat (limited to 'helpers/path.go')
-rw-r--r--helpers/path.go19
1 files changed, 17 insertions, 2 deletions
diff --git a/helpers/path.go b/helpers/path.go
index a5b176568..5a8694f15 100644
--- a/helpers/path.go
+++ b/helpers/path.go
@@ -91,6 +91,19 @@ func MakeTitle(inpath string) string {
return strings.Replace(strings.TrimSpace(inpath), "-", " ", -1)
}
+// From https://golang.org/src/net/url/url.go
+func ishex(c rune) bool {
+ switch {
+ case '0' <= c && c <= '9':
+ return true
+ case 'a' <= c && c <= 'f':
+ return true
+ case 'A' <= c && c <= 'F':
+ return true
+ }
+ return false
+}
+
// UnicodeSanitize sanitizes string to be used in Hugo URL's, allowing only
// a predefined set of special Unicode characters.
// If RemovePathAccents configuration flag is enabled, Uniccode accents
@@ -99,8 +112,10 @@ func UnicodeSanitize(s string) string {
source := []rune(s)
target := make([]rune, 0, len(source))
- for _, r := range source {
- if unicode.IsLetter(r) || unicode.IsDigit(r) || unicode.IsMark(r) || r == '%' || r == '.' || r == '/' || r == '\\' || r == '_' || r == '-' || r == '#' || r == '+' {
+ for i, r := range source {
+ if r == '%' && i+2 < len(source) && ishex(source[i+1]) && ishex(source[i+2]) {
+ target = append(target, r)
+ } else if unicode.IsLetter(r) || unicode.IsDigit(r) || unicode.IsMark(r) || r == '.' || r == '/' || r == '\\' || r == '_' || r == '-' || r == '#' || r == '+' {
target = append(target, r)
}
}