From 1c0cb4eb8d4c96d382b87e727478ea39d37c587a Mon Sep 17 00:00:00 2001 From: Oliver Steele Date: Tue, 13 Jun 2017 08:51:58 -0400 Subject: [PATCH] Add more permalink template variables --- page.go | 16 +++++++++------- permalinks_test.go | 26 +++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/page.go b/page.go index 35e4097..1d0f198 100644 --- a/page.go +++ b/page.go @@ -17,7 +17,7 @@ import ( var ( frontmatterMatcher = regexp.MustCompile(`(?s)^---\n(.+?\n)---\n`) - templateVariableMatcher = regexp.MustCompile(`:(?:collection|file_ext|name|path|title)\b`) + templateVariableMatcher = regexp.MustCompile(`:\w+\b`) nonAlphanumericSequenceMatcher = regexp.MustCompile(`[^[:alnum:]]+`) ) @@ -168,11 +168,12 @@ func expandPermalinkPattern(pattern string, data map[interface{}]interface{}, pa var ( collectionName string - ext = filepath.Ext(path) localPath = path + ext = filepath.Ext(path) + root = path[:len(path)-len(ext)] outputExt = ext - name = filepath.Base(localPath) - title = getString(data, "title", name[:len(name)-len(ext)]) + name = filepath.Base(root) + title = getString(data, "title", name) ) if ext == ".md" { @@ -182,8 +183,8 @@ func expandPermalinkPattern(pattern string, data map[interface{}]interface{}, pa if val, found := data["collection"]; found { collectionName = val.(string) - collectionPath := "_" + collectionName + "/" - localPath = localPath[len(collectionPath):] + prefix := "_" + collectionName + "/" + localPath = localPath[len(prefix):] } replaceNonalphumericsByHyphens := func(s string) string { @@ -192,8 +193,9 @@ func expandPermalinkPattern(pattern string, data map[interface{}]interface{}, pa templateVariables := map[string]string{ "collection": collectionName, + "ext": strings.TrimLeft(ext, "."), "name": replaceNonalphumericsByHyphens(name), - "output_ext": outputExt, + "output_ext": strings.TrimLeft(outputExt, "."), "path": localPath, "title": replaceNonalphumericsByHyphens(title), // TODO year month imonth day i_day short_year hour minute second slug categories diff --git a/permalinks_test.go b/permalinks_test.go index d455734..65a61f9 100644 --- a/permalinks_test.go +++ b/permalinks_test.go @@ -7,11 +7,31 @@ import ( ) func TestExpandPermalinkPattern(t *testing.T) { - d := map[interface{}]interface{}{} - path := "/a/b/c.d" + var ( + d = map[interface{}]interface{}{} + path = "/a/b/c.d" + mdPath = "/a/b/c.md" + ) + + t.Run(":ext", func(t *testing.T) { + p := expandPermalinkPattern("/ext/:ext", d, path) + assert.Equal(t, "/ext/d", p) + }) + t.Run(":ext", func(t *testing.T) { + p := expandPermalinkPattern("/ext/:ext", d, mdPath) + assert.Equal(t, "/ext/md", p) + }) + t.Run(":output_ext", func(t *testing.T) { + p := expandPermalinkPattern("/ext/:output_ext", d, path) + assert.Equal(t, "/ext/d", p) + }) + t.Run(":output_ext", func(t *testing.T) { + p := expandPermalinkPattern("/ext/:output_ext", d, mdPath) + assert.Equal(t, "/ext/html", p) + }) t.Run(":name", func(t *testing.T) { p := expandPermalinkPattern("/name/:name", d, path) - assert.Equal(t, "/name/c-d", p) + assert.Equal(t, "/name/c", p) }) t.Run(":path", func(t *testing.T) { p := expandPermalinkPattern("/prefix:path/post", d, path)