strip trailing baseurl slash. Added a new template function "sanitizeurl" which ensures no double slashes. Fixed #221

This commit is contained in:
spf13 2014-04-07 22:02:08 -04:00
parent a6170154cf
commit ad34be9d77
3 changed files with 32 additions and 19 deletions

View file

@ -16,8 +16,18 @@ package helpers
import ( import (
"net/url" "net/url"
"path" "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 // Similar to MakePath, but with Unicode handling
// Example: // Example:
// uri: Vim (text editor) // uri: Vim (text editor)
@ -55,6 +65,7 @@ func MakePermalink(host, plink string) *url.URL {
} }
func UrlPrep(ugly bool, in string) string { func UrlPrep(ugly bool, in string) string {
in = SanitizeUrl(in)
if ugly { if ugly {
return Uglify(in) return Uglify(in)
} else { } else {

View file

@ -227,7 +227,7 @@ func (s *Site) initializeSiteInfo() {
} }
s.Info = SiteInfo{ s.Info = SiteInfo{
BaseUrl: template.URL(viper.GetString("BaseUrl")), BaseUrl: template.URL(helpers.SanitizeUrl(viper.GetString("BaseUrl"))),
Title: viper.GetString("Title"), Title: viper.GetString("Title"),
Recent: &s.Pages, Recent: &s.Pages,
Params: params, Params: params,

View file

@ -2,8 +2,6 @@ package bundle
import ( import (
"errors" "errors"
"github.com/eknkc/amber"
"github.com/spf13/hugo/helpers"
"html" "html"
"html/template" "html/template"
"io" "io"
@ -13,6 +11,9 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
"github.com/eknkc/amber"
"github.com/spf13/hugo/helpers"
) )
func Gt(a interface{}, b interface{}) bool { func Gt(a interface{}, b interface{}) bool {
@ -160,6 +161,7 @@ func NewTemplate() Template {
funcMap := template.FuncMap{ funcMap := template.FuncMap{
"urlize": helpers.Urlize, "urlize": helpers.Urlize,
"sanitizeurl": helpers.SanitizeUrl,
"gt": Gt, "gt": Gt,
"isset": IsSet, "isset": IsSet,
"echoParam": ReturnWhenSet, "echoParam": ReturnWhenSet,