strip trailing baseurl slash. Added a new template function "sanitizeurl" which ensur...
authorspf13 <steve.francia@gmail.com>
Tue, 8 Apr 2014 02:02:08 +0000 (22:02 -0400)
committerspf13 <steve.francia@gmail.com>
Tue, 8 Apr 2014 02:02:08 +0000 (22:02 -0400)
helpers/url.go
hugolib/site.go
template/bundle/template.go

index ffafe7af0386697382f0893b39131df1e20f84e1..d7f99f4f52e1d14e714b5e9ec462fd5847ae31d2 100644 (file)
@@ -16,8 +16,18 @@ package helpers
 import (
        "net/url"
        "path"
+
+       "github.com/PuerkitoBio/purell"
 )
 
+func SanitizeUrl(in string) string {
+       url, err := purell.NormalizeURLString(in, purell.FlagsUsuallySafeGreedy|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator)
+       if err != nil {
+               return in
+       }
+       return url
+}
+
 // Similar to MakePath, but with Unicode handling
 // Example:
 //     uri: Vim (text editor)
@@ -55,6 +65,7 @@ func MakePermalink(host, plink string) *url.URL {
 }
 
 func UrlPrep(ugly bool, in string) string {
+       in = SanitizeUrl(in)
        if ugly {
                return Uglify(in)
        } else {
index f653a2441e23001a8b12f2b250c4be5df710d650..cd3109fd7fceaa5ac59029c5cd8a7d6a7564f626 100644 (file)
@@ -227,7 +227,7 @@ func (s *Site) initializeSiteInfo() {
        }
 
        s.Info = SiteInfo{
-               BaseUrl:    template.URL(viper.GetString("BaseUrl")),
+               BaseUrl:    template.URL(helpers.SanitizeUrl(viper.GetString("BaseUrl"))),
                Title:      viper.GetString("Title"),
                Recent:     &s.Pages,
                Params:     params,
index d7e135b89308f901b88d4bb40ecf925bd81a372f..5d34bbf808be8c2a275148e94b1093db9c14fdef 100644 (file)
@@ -2,8 +2,6 @@ package bundle
 
 import (
        "errors"
-       "github.com/eknkc/amber"
-       "github.com/spf13/hugo/helpers"
        "html"
        "html/template"
        "io"
@@ -13,6 +11,9 @@ import (
        "reflect"
        "strconv"
        "strings"
+
+       "github.com/eknkc/amber"
+       "github.com/spf13/hugo/helpers"
 )
 
 func Gt(a interface{}, b interface{}) bool {
@@ -159,22 +160,23 @@ func NewTemplate() Template {
        }
 
        funcMap := template.FuncMap{
-               "urlize":    helpers.Urlize,
-               "gt":        Gt,
-               "isset":     IsSet,
-               "echoParam": ReturnWhenSet,
-               "safeHtml":  SafeHtml,
-               "first":     First,
-               "highlight": Highlight,
-               "add":       func(a, b int) int { return a + b },
-               "sub":       func(a, b int) int { return a - b },
-               "div":       func(a, b int) int { return a / b },
-               "mod":       func(a, b int) int { return a % b },
-               "mul":       func(a, b int) int { return a * b },
-               "modBool":   func(a, b int) bool { return a%b == 0 },
-               "lower":     func(a string) string { return strings.ToLower(a) },
-               "upper":     func(a string) string { return strings.ToUpper(a) },
-               "title":     func(a string) string { return strings.Title(a) },
+               "urlize":      helpers.Urlize,
+               "sanitizeurl": helpers.SanitizeUrl,
+               "gt":          Gt,
+               "isset":       IsSet,
+               "echoParam":   ReturnWhenSet,
+               "safeHtml":    SafeHtml,
+               "first":       First,
+               "highlight":   Highlight,
+               "add":         func(a, b int) int { return a + b },
+               "sub":         func(a, b int) int { return a - b },
+               "div":         func(a, b int) int { return a / b },
+               "mod":         func(a, b int) int { return a % b },
+               "mul":         func(a, b int) int { return a * b },
+               "modBool":     func(a, b int) bool { return a%b == 0 },
+               "lower":       func(a string) string { return strings.ToLower(a) },
+               "upper":       func(a string) string { return strings.ToUpper(a) },
+               "title":       func(a string) string { return strings.Title(a) },
        }
 
        templates.Funcs(funcMap)