mirror of
https://github.com/danog/gojekyll.git
synced 2024-11-27 05:54:44 +01:00
More work on template variables
This commit is contained in:
parent
a3ff348d3c
commit
d57468e045
41
page.go
41
page.go
@ -76,18 +76,46 @@ func (p *StaticPage) TemplateObject() VariableMap {
|
||||
|
||||
// TemplateObject returns the attributes of the template page object.
|
||||
func (p *DynamicPage) TemplateObject() VariableMap {
|
||||
var (
|
||||
path = p.path
|
||||
ext = filepath.Ext(path)
|
||||
root = p.path[:len(path)-len(ext)]
|
||||
base = filepath.Base(root)
|
||||
)
|
||||
|
||||
data := VariableMap{
|
||||
"path": p.path,
|
||||
"url": p.Permalink(),
|
||||
"path": p.Source(),
|
||||
// TODO content title excerpt date id categories tags next previous
|
||||
// TODO Posts should get date, category, categories, tags
|
||||
// TODO only do the following if it's a collection document?
|
||||
// TODO content output
|
||||
|
||||
// not documented, but present in both collection and non-collection pages
|
||||
"permalink": p.Permalink(),
|
||||
|
||||
// TODO only in non-collection pages:
|
||||
// TODO dir
|
||||
// TODO name
|
||||
// TODO next previous
|
||||
|
||||
// TODO Documented as present in all pages, but de facto only defined for collection pages
|
||||
"id": base,
|
||||
"title": base, // TODO capitalize
|
||||
// TODO date (of the collection?) 2017-06-15 07:44:21 -0400
|
||||
// TODO excerpt category? categories tags
|
||||
// TODO slug
|
||||
|
||||
// TODO Only present in collection pages https://jekyllrb.com/docs/collections/#documents
|
||||
"relative_path": p.Path(),
|
||||
// TODO collections: output collection(name) date(of the collection)
|
||||
// TODO collection(name)
|
||||
|
||||
// TODO undocumented; only present in collection pages:
|
||||
"ext": ext,
|
||||
}
|
||||
for k, v := range p.frontMatter {
|
||||
switch k {
|
||||
case "layout", "permalink", "published":
|
||||
// doc implies these aren't present, but they appear to be present in a collection page:
|
||||
// case "layout", "published":
|
||||
case "permalink":
|
||||
// omit this, in order to use the value above
|
||||
default:
|
||||
data[k] = v
|
||||
}
|
||||
@ -96,6 +124,7 @@ func (p *DynamicPage) TemplateObject() VariableMap {
|
||||
}
|
||||
|
||||
// TemplateObject returns the attributes of the template page object.
|
||||
// See https://jekyllrb.com/docs/variables/#page-variables
|
||||
func (p *pageFields) TemplateObject() VariableMap {
|
||||
var (
|
||||
path = "/" + p.path
|
||||
|
@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// PermalinkStyles defines built-in styles from https://jekyllrb.com/docs/permalinks/#builtinpermalinkstyles
|
||||
@ -14,36 +14,53 @@ var PermalinkStyles = map[string]string{
|
||||
"none": "/:categories/:title.html",
|
||||
}
|
||||
|
||||
// permalinkDateVariables maps Jekyll permalink template variable names
|
||||
// to time.Format layout strings
|
||||
var permalinkDateVariables = map[string]string{
|
||||
"month": "01",
|
||||
"imonth": "1",
|
||||
"day": "02",
|
||||
"i_day": "2",
|
||||
"hour": "15",
|
||||
"minute": "04",
|
||||
"second": "05",
|
||||
"year": "2006",
|
||||
"short_year": "06",
|
||||
}
|
||||
|
||||
// See https://jekyllrb.com/docs/permalinks/#template-variables
|
||||
func permalinkTemplateVariables(path string, frontMatter VariableMap) map[string]string {
|
||||
var (
|
||||
collectionName string
|
||||
localPath = path
|
||||
ext = filepath.Ext(path)
|
||||
root = path[:len(path)-len(ext)]
|
||||
outputExt = ext
|
||||
root = path[:len(path)-len(ext)]
|
||||
name = filepath.Base(root)
|
||||
title = frontMatter.String("title", name)
|
||||
)
|
||||
|
||||
if isMarkdown(path) {
|
||||
outputExt = ".html"
|
||||
}
|
||||
|
||||
if val, found := frontMatter["collection"]; found {
|
||||
collectionName = val.(string)
|
||||
prefix := "_" + collectionName + "/"
|
||||
localPath = localPath[len(prefix):]
|
||||
}
|
||||
|
||||
return map[string]string{
|
||||
vs := map[string]string{
|
||||
"collection": collectionName,
|
||||
"ext": strings.TrimLeft(ext, "."),
|
||||
"name": hyphenateNonAlphaSequence(name),
|
||||
"output_ext": strings.TrimLeft(outputExt, "."),
|
||||
"path": localPath,
|
||||
"title": hyphenateNonAlphaSequence(title),
|
||||
// TODO year month imonth day i_day short_year hour minute second slug categories
|
||||
// TODO slug categories
|
||||
// The following aren't documented, but are evident
|
||||
"output_ext": outputExt,
|
||||
}
|
||||
d := time.Now() // TODO read from frontMatter or use file modtime
|
||||
for name, f := range permalinkDateVariables {
|
||||
vs[name] = d.Format(f)
|
||||
}
|
||||
return vs
|
||||
}
|
||||
|
||||
func expandPermalinkPattern(pattern string, path string, frontMatter VariableMap) (s string, err error) {
|
||||
|
@ -9,37 +9,35 @@ import (
|
||||
func TestExpandPermalinkPattern(t *testing.T) {
|
||||
var (
|
||||
d = VariableMap{}
|
||||
path = "/a/b/c.d"
|
||||
mdPath = "/a/b/c.md"
|
||||
path = "/a/b/base.html"
|
||||
)
|
||||
|
||||
t.Run(":ext", func(t *testing.T) {
|
||||
p, _ := expandPermalinkPattern("/ext/:ext", path, d)
|
||||
assert.Equal(t, "/ext/d", p)
|
||||
})
|
||||
t.Run(":ext", func(t *testing.T) {
|
||||
p, _ := expandPermalinkPattern("/ext/:ext", mdPath, d)
|
||||
assert.Equal(t, "/ext/md", p)
|
||||
})
|
||||
t.Run(":output_ext", func(t *testing.T) {
|
||||
p, _ := expandPermalinkPattern("/ext/:output_ext", path, d)
|
||||
assert.Equal(t, "/ext/d", p)
|
||||
p, _ := expandPermalinkPattern("/base:output_ext", path, d)
|
||||
assert.Equal(t, "/base.html", p)
|
||||
})
|
||||
t.Run(":output_ext", func(t *testing.T) {
|
||||
p, _ := expandPermalinkPattern("/ext/:output_ext", mdPath, d)
|
||||
assert.Equal(t, "/ext/html", p)
|
||||
t.Run(":output_ext renames markdown to .html", func(t *testing.T) {
|
||||
p, _ := expandPermalinkPattern("/base:output_ext", "/a/b/base.md", d)
|
||||
assert.Equal(t, "/base.html", p)
|
||||
p, _ = expandPermalinkPattern("/base:output_ext", "/a/b/base.markdown", d)
|
||||
assert.Equal(t, "/base.html", p)
|
||||
})
|
||||
t.Run(":name", func(t *testing.T) {
|
||||
p, _ := expandPermalinkPattern("/name/:name", path, d)
|
||||
assert.Equal(t, "/name/c", p)
|
||||
assert.Equal(t, "/name/base", p)
|
||||
})
|
||||
t.Run(":path", func(t *testing.T) {
|
||||
p, _ := expandPermalinkPattern("/prefix:path/post", path, d)
|
||||
assert.Equal(t, "/prefix/a/b/c.d/post", p)
|
||||
assert.Equal(t, "/prefix/a/b/base.html/post", p)
|
||||
})
|
||||
t.Run(":title", func(t *testing.T) {
|
||||
p, _ := expandPermalinkPattern("/title/:title.html", path, d)
|
||||
assert.Equal(t, "/title/c.html", p)
|
||||
assert.Equal(t, "/title/base.html", p)
|
||||
})
|
||||
t.Run("invalid template variable", func(t *testing.T) {
|
||||
_, err := expandPermalinkPattern("/:invalid", path, d)
|
||||
// assert.Equal(t, "/ext/d", p)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
d["collection"] = "c"
|
||||
|
Loading…
Reference in New Issue
Block a user