mirror of
https://github.com/danog/gojekyll.git
synced 2024-11-30 10:08:58 +01:00
Test permalinks
This commit is contained in:
parent
ba1e46bcf5
commit
35309aca2e
@ -8,6 +8,7 @@ import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/osteele/gojekyll/helpers"
|
||||
@ -148,7 +149,7 @@ func (p *pageFields) categories() []string {
|
||||
if v, found := p.frontMatter["categories"]; found {
|
||||
switch v := v.(type) {
|
||||
case string:
|
||||
return regexp.MustCompile(`\s+`).Split(v, -1)
|
||||
return strings.Fields(v)
|
||||
case []interface{}:
|
||||
sl := make([]string, len(v))
|
||||
for i, s := range v {
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/osteele/gojekyll/helpers"
|
||||
@ -38,7 +39,6 @@ var templateVariableMatcher = regexp.MustCompile(`:\w+\b`)
|
||||
// See https://jekyllrb.com/docs/permalinks/#template-variables
|
||||
func (p *pageFields) permalinkTemplateVariables() map[string]string {
|
||||
var (
|
||||
collection string
|
||||
relpath = strings.TrimPrefix(p.relpath, p.container.PathPrefix())
|
||||
root = helpers.TrimExt(relpath)
|
||||
name = filepath.Base(root)
|
||||
@ -48,13 +48,14 @@ func (p *pageFields) permalinkTemplateVariables() map[string]string {
|
||||
// TODO recognize category; list
|
||||
vs := map[string]string{
|
||||
"categories": strings.Join(categories, "/"),
|
||||
"collection": collection,
|
||||
"collection": p.frontMatter.String("collection", ""),
|
||||
"name": helpers.Slugify(name),
|
||||
"path": "/" + root,
|
||||
"slug": p.frontMatter.String("slug", helpers.Slugify(name)),
|
||||
"title": p.frontMatter.String("slug", helpers.Slugify(name)),
|
||||
// The following isn't documented, but is evident
|
||||
// The following aren't documented, but is evident
|
||||
"output_ext": p.OutputExt(),
|
||||
"y_day": strconv.Itoa(p.modTime.YearDay()),
|
||||
// TODO categories
|
||||
}
|
||||
for name, f := range permalinkDateVariables {
|
||||
@ -88,7 +89,11 @@ func (p *pageFields) expandPermalink() (s string, err error) {
|
||||
}
|
||||
return value
|
||||
})
|
||||
return path.Clean(filepath.ToSlash(s)), nil
|
||||
finalSlash := ""
|
||||
if strings.HasSuffix(filepath.ToSlash(s), "/") {
|
||||
finalSlash = "/"
|
||||
}
|
||||
return path.Clean(filepath.ToSlash(s)) + finalSlash, nil
|
||||
}
|
||||
|
||||
// The permalink is computed once instead of on demand, so that subsequent
|
||||
|
@ -1,7 +1,9 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/osteele/gojekyll/templates"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -13,57 +15,71 @@ func (c containerMock) Output() bool { return true }
|
||||
func (c containerMock) PathPrefix() string { return c.pathPrefix }
|
||||
func (c containerMock) DefaultPermalink() string { return "/:path:output_ext" }
|
||||
|
||||
type pathTest struct{ path, pattern, out string }
|
||||
|
||||
var tests = []pathTest{
|
||||
{"/a/b/base.html", "/out:output_ext", "/out.html"},
|
||||
{"/a/b/base.md", "/out:output_ext", "/out.html"},
|
||||
{"/a/b/base.markdown", "/out:output_ext", "/out.html"},
|
||||
{"/a/b/base.html", "/:path/out:output_ext", "/a/b/base/out.html"},
|
||||
{"/a/b/base.html", "/prefix/:name", "/prefix/base"},
|
||||
{"/a/b/base.html", "/prefix/:path/post", "/prefix/a/b/base/post"},
|
||||
{"/a/b/base.html", "/prefix/:title", "/prefix/base"},
|
||||
{"/a/b/base.html", "/prefix/:slug", "/prefix/base"},
|
||||
{"base", "/:categories/:name:output_ext", "/a/b/base"},
|
||||
|
||||
{"base", "date", "/a/b/2006/02/03/base.html"},
|
||||
{"base", "pretty", "/a/b/2006/02/03/base/"},
|
||||
{"base", "ordinal", "/a/b/2006/34/base.html"},
|
||||
{"base", "none", "/a/b/base.html"},
|
||||
}
|
||||
|
||||
var collectionTests = []pathTest{
|
||||
{"_c/a/b/c.d", "/prefix/:collection/post", "/prefix/c/post"},
|
||||
{"_c/a/b/c.d", "/prefix:path/post", "/prefix/a/b/c/post"},
|
||||
}
|
||||
|
||||
func TestExpandPermalinkPattern(t *testing.T) {
|
||||
var (
|
||||
c = containerMock{}
|
||||
d = templates.VariableMap{}
|
||||
path = "/a/b/base.html"
|
||||
c = containerMock{}
|
||||
d = templates.VariableMap{
|
||||
"categories": "b a",
|
||||
}
|
||||
)
|
||||
|
||||
testPermalinkPattern := func(pattern, path string, data templates.VariableMap) (string, error) {
|
||||
vs := templates.MergeVariableMaps(data, templates.VariableMap{"permalink": pattern})
|
||||
p := pageFields{container: c, relpath: path, frontMatter: vs}
|
||||
ext := filepath.Ext(path)
|
||||
switch ext {
|
||||
case ".md", ".markdown":
|
||||
ext = ".html"
|
||||
}
|
||||
p := pageFields{container: c, relpath: path, frontMatter: vs, outputExt: ext}
|
||||
t0, err := time.Parse(time.RFC3339, "2006-02-03T15:04:05Z")
|
||||
require.NoError(t, err)
|
||||
p.modTime = t0
|
||||
return p.expandPermalink()
|
||||
}
|
||||
|
||||
// t.Run(":output_ext", func(t *testing.T) {
|
||||
// p, err := testPermalinkPattern("/base:output_ext", path, d)
|
||||
// require.NoError(t, err)
|
||||
// require.Equal(t, "/base.html", p)
|
||||
// })
|
||||
// t.Run(":output_ext renames markdown to .html", func(t *testing.T) {
|
||||
// p, err := testPermalinkPattern("/base:output_ext", "/a/b/base.md", d)
|
||||
// require.NoError(t, err)
|
||||
// require.Equal(t, "/base.html", p)
|
||||
// p, err = testPermalinkPattern("/base:output_ext", "/a/b/base.markdown", d)
|
||||
// require.NoError(t, err)
|
||||
// require.Equal(t, "/base.html", p)
|
||||
// })
|
||||
t.Run(":name", func(t *testing.T) {
|
||||
p, err := testPermalinkPattern("/name/:name", path, d)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "/name/base", p)
|
||||
})
|
||||
t.Run(":path", func(t *testing.T) {
|
||||
p, err := testPermalinkPattern("/prefix:path/post", path, d)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "/prefix/a/b/base/post", p)
|
||||
})
|
||||
t.Run(":title", func(t *testing.T) {
|
||||
p, err := testPermalinkPattern("/title/:title.html", path, d)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "/title/base.html", p)
|
||||
})
|
||||
t.Run("invalid template variable", func(t *testing.T) {
|
||||
_, err := testPermalinkPattern("/:invalid", path, d)
|
||||
require.Error(t, err)
|
||||
})
|
||||
runTests := func(tests []pathTest) {
|
||||
for i, test := range tests {
|
||||
t.Run(test.pattern, func(t *testing.T) {
|
||||
p, err := testPermalinkPattern(test.pattern, test.path, d)
|
||||
require.NoError(t, err)
|
||||
require.Equalf(t, test.out, p, "%d: pattern=%s", i+1, test.pattern)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
runTests(tests)
|
||||
|
||||
c = containerMock{"_c/"}
|
||||
path = "_c/a/b/c.d"
|
||||
t.Run(":path", func(t *testing.T) {
|
||||
p, err := testPermalinkPattern("/prefix:path/post", path, d)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "/prefix/a/b/c/post", p)
|
||||
d["collection"] = "c"
|
||||
runTests(collectionTests)
|
||||
|
||||
t.Run("invalid template variable", func(t *testing.T) {
|
||||
p, err := testPermalinkPattern("/:invalid", "/a/b/base.html", d)
|
||||
require.Error(t, err)
|
||||
require.Zero(t, p)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user