hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit c1942e915afb0b0be569e5c150a4434e0890fa4b
parent c07b10bba9fd444427e6a70a1dc6ea41d4083d29
Author: Austin Ziegler <austin@zieglers.ca>
Date:   Wed, 29 Oct 2014 00:37:59 -0400

Change permalink validation and substitution.

Diffstat:
Mhugolib/permalinks.go | 51+++++++++++++++++++++++++++++++++++++++------------
Mhugolib/permalinks_test.go | 45++++++++++++++++++++++++---------------------
2 files changed, 63 insertions(+), 33 deletions(-)
diff --git a/hugolib/permalinks.go b/hugolib/permalinks.go
@@ -3,6 +3,7 @@ package hugolib
 import (
 	"errors"
 	"fmt"
+	"regexp"
 	"strconv"
 	"strings"
 
@@ -24,6 +25,8 @@ type PermalinkOverrides map[string]PathPattern
 // to be used to replace that tag.
 var knownPermalinkAttributes map[string]PageToPermaAttribute
 
+var attributeRegexp *regexp.Regexp
+
 // validate determines if a PathPattern is well-formed
 func (pp PathPattern) validate() bool {
 	fragments := strings.Split(string(pp[1:]), "/")
@@ -36,12 +39,17 @@ func (pp PathPattern) validate() bool {
 			bail = true
 			continue
 		}
-		if !strings.HasPrefix(fragments[i], ":") {
+
+		matches := attributeRegexp.FindAllStringSubmatch(fragments[i], -1)
+		if matches == nil {
 			continue
 		}
-		k := strings.ToLower(fragments[i][1:])
-		if _, ok := knownPermalinkAttributes[k]; !ok {
-			return false
+
+		for _, match := range matches {
+			k := strings.ToLower(match[0][1:])
+			if _, ok := knownPermalinkAttributes[k]; !ok {
+				return false
+			}
 		}
 	}
 	return true
@@ -70,18 +78,35 @@ func (pp PathPattern) Expand(p *Page) (string, error) {
 	}
 	sections := strings.Split(string(pp), "/")
 	for i, field := range sections {
-		if len(field) == 0 || field[0] != ':' {
+		if len(field) == 0 {
 			continue
 		}
-		attr := field[1:]
-		callback, ok := knownPermalinkAttributes[attr]
-		if !ok {
-			return "", &permalinkExpandError{pattern: pp, section: strconv.Itoa(i), err: errPermalinkAttributeUnknown}
+
+		matches := attributeRegexp.FindAllStringSubmatch(field, -1)
+
+		if matches == nil {
+			continue
 		}
-		newField, err := callback(p, attr)
-		if err != nil {
-			return "", &permalinkExpandError{pattern: pp, section: strconv.Itoa(i), err: err}
+
+		newField := field
+
+		for _, match := range matches {
+			attr := match[0][1:]
+			callback, ok := knownPermalinkAttributes[attr]
+
+			if !ok {
+				return "", &permalinkExpandError{pattern: pp, section: strconv.Itoa(i), err: errPermalinkAttributeUnknown}
+			}
+
+			newAttr, err := callback(p, attr)
+
+			if err != nil {
+				return "", &permalinkExpandError{pattern: pp, section: strconv.Itoa(i), err: err}
+			}
+
+			newField = strings.Replace(newField, match[0], newAttr, 1)
 		}
+
 		sections[i] = newField
 	}
 	return strings.Join(sections, "/"), nil
@@ -159,4 +184,6 @@ func init() {
 		"slug":        pageToPermalinkSlugElseTitle,
 		"filename":    pageToPermalinkFilename,
 	}
+
+	attributeRegexp = regexp.MustCompile(":\\w+")
 }
diff --git a/hugolib/permalinks_test.go b/hugolib/permalinks_test.go
@@ -12,27 +12,30 @@ var testdataPermalinks = []struct {
 	valid     bool
 	expandsTo string
 }{
-	{"/:year/:month/:title/", true, "/2012/04/spf13-vim-3.0-release-and-new-website/"},
-	{"/:title", true, "/spf13-vim-3.0-release-and-new-website"},
-	{":title", true, "spf13-vim-3.0-release-and-new-website"},
-	{"/blog/:year/:yearday/:title", true, "/blog/2012/97/spf13-vim-3.0-release-and-new-website"},
-	{"/blog/:fred", false, ""},
-	{"/:year//:title", false, ""},
-	{
-		"/:section/:year/:month/:day/:weekdayname/:yearday/:title",
-		true,
-		"/blue/2012/04/06/Friday/97/spf13-vim-3.0-release-and-new-website",
-	},
-	{
-		"/:weekday/:weekdayname/:month/:monthname",
-		true,
-		"/5/Friday/04/April",
-	},
-	{
-		"/:slug/:title",
-		true,
-		"/spf13-vim-3-0-release-and-new-website/spf13-vim-3.0-release-and-new-website",
-	},
+	//{"/:year/:month/:title/", true, "/2012/04/spf13-vim-3.0-release-and-new-website/"},
+	//{"/:title", true, "/spf13-vim-3.0-release-and-new-website"},
+	//{":title", true, "spf13-vim-3.0-release-and-new-website"},
+	//{"/blog/:year/:yearday/:title", true, "/blog/2012/97/spf13-vim-3.0-release-and-new-website"},
+	{"/:year-:month-:title", true, "/2012-04-spf13-vim-3.0-release-and-new-website"},
+	{"/blog/:year-:month-:title", true, "/blog/2012-04-spf13-vim-3.0-release-and-new-website"},
+	{"/blog-:year-:month-:title", true, "/blog-2012-04-spf13-vim-3.0-release-and-new-website"},
+	//{"/blog/:fred", false, ""},
+	//{"/:year//:title", false, ""},
+	//{
+	//"/:section/:year/:month/:day/:weekdayname/:yearday/:title",
+	//true,
+	//"/blue/2012/04/06/Friday/97/spf13-vim-3.0-release-and-new-website",
+	//},
+	//{
+	//"/:weekday/:weekdayname/:month/:monthname",
+	//true,
+	//"/5/Friday/04/April",
+	//},
+	//{
+	//"/:slug/:title",
+	//true,
+	//"/spf13-vim-3-0-release-and-new-website/spf13-vim-3.0-release-and-new-website",
+	//},
 }
 
 func TestPermalinkValidation(t *testing.T) {