mirror of
https://github.com/danog/gojekyll.git
synced 2024-11-27 08:34:42 +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.
|
// TemplateObject returns the attributes of the template page object.
|
||||||
func (p *DynamicPage) TemplateObject() VariableMap {
|
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{
|
data := VariableMap{
|
||||||
|
"path": p.path,
|
||||||
"url": p.Permalink(),
|
"url": p.Permalink(),
|
||||||
"path": p.Source(),
|
// TODO content output
|
||||||
// TODO content title excerpt date id categories tags next previous
|
|
||||||
// TODO Posts should get date, category, categories, tags
|
// not documented, but present in both collection and non-collection pages
|
||||||
// TODO only do the following if it's a collection document?
|
"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(),
|
"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 {
|
for k, v := range p.frontMatter {
|
||||||
switch k {
|
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:
|
default:
|
||||||
data[k] = v
|
data[k] = v
|
||||||
}
|
}
|
||||||
@ -96,6 +124,7 @@ func (p *DynamicPage) TemplateObject() VariableMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TemplateObject returns the attributes of the template page object.
|
// TemplateObject returns the attributes of the template page object.
|
||||||
|
// See https://jekyllrb.com/docs/variables/#page-variables
|
||||||
func (p *pageFields) TemplateObject() VariableMap {
|
func (p *pageFields) TemplateObject() VariableMap {
|
||||||
var (
|
var (
|
||||||
path = "/" + p.path
|
path = "/" + p.path
|
||||||
|
@ -3,7 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PermalinkStyles defines built-in styles from https://jekyllrb.com/docs/permalinks/#builtinpermalinkstyles
|
// 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",
|
"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 {
|
func permalinkTemplateVariables(path string, frontMatter VariableMap) map[string]string {
|
||||||
var (
|
var (
|
||||||
collectionName string
|
collectionName string
|
||||||
localPath = path
|
localPath = path
|
||||||
ext = filepath.Ext(path)
|
ext = filepath.Ext(path)
|
||||||
root = path[:len(path)-len(ext)]
|
|
||||||
outputExt = ext
|
outputExt = ext
|
||||||
|
root = path[:len(path)-len(ext)]
|
||||||
name = filepath.Base(root)
|
name = filepath.Base(root)
|
||||||
title = frontMatter.String("title", name)
|
title = frontMatter.String("title", name)
|
||||||
)
|
)
|
||||||
|
|
||||||
if isMarkdown(path) {
|
if isMarkdown(path) {
|
||||||
outputExt = ".html"
|
outputExt = ".html"
|
||||||
}
|
}
|
||||||
|
|
||||||
if val, found := frontMatter["collection"]; found {
|
if val, found := frontMatter["collection"]; found {
|
||||||
collectionName = val.(string)
|
collectionName = val.(string)
|
||||||
prefix := "_" + collectionName + "/"
|
prefix := "_" + collectionName + "/"
|
||||||
localPath = localPath[len(prefix):]
|
localPath = localPath[len(prefix):]
|
||||||
}
|
}
|
||||||
|
vs := map[string]string{
|
||||||
return map[string]string{
|
|
||||||
"collection": collectionName,
|
"collection": collectionName,
|
||||||
"ext": strings.TrimLeft(ext, "."),
|
|
||||||
"name": hyphenateNonAlphaSequence(name),
|
"name": hyphenateNonAlphaSequence(name),
|
||||||
"output_ext": strings.TrimLeft(outputExt, "."),
|
|
||||||
"path": localPath,
|
"path": localPath,
|
||||||
"title": hyphenateNonAlphaSequence(title),
|
"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) {
|
func expandPermalinkPattern(pattern string, path string, frontMatter VariableMap) (s string, err error) {
|
||||||
|
@ -9,37 +9,35 @@ import (
|
|||||||
func TestExpandPermalinkPattern(t *testing.T) {
|
func TestExpandPermalinkPattern(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
d = VariableMap{}
|
d = VariableMap{}
|
||||||
path = "/a/b/c.d"
|
path = "/a/b/base.html"
|
||||||
mdPath = "/a/b/c.md"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
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) {
|
t.Run(":output_ext", func(t *testing.T) {
|
||||||
p, _ := expandPermalinkPattern("/ext/:output_ext", path, d)
|
p, _ := expandPermalinkPattern("/base:output_ext", path, d)
|
||||||
assert.Equal(t, "/ext/d", p)
|
assert.Equal(t, "/base.html", p)
|
||||||
})
|
})
|
||||||
t.Run(":output_ext", func(t *testing.T) {
|
t.Run(":output_ext renames markdown to .html", func(t *testing.T) {
|
||||||
p, _ := expandPermalinkPattern("/ext/:output_ext", mdPath, d)
|
p, _ := expandPermalinkPattern("/base:output_ext", "/a/b/base.md", d)
|
||||||
assert.Equal(t, "/ext/html", p)
|
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) {
|
t.Run(":name", func(t *testing.T) {
|
||||||
p, _ := expandPermalinkPattern("/name/:name", path, d)
|
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) {
|
t.Run(":path", func(t *testing.T) {
|
||||||
p, _ := expandPermalinkPattern("/prefix:path/post", path, d)
|
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) {
|
t.Run(":title", func(t *testing.T) {
|
||||||
p, _ := expandPermalinkPattern("/title/:title.html", path, d)
|
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"
|
d["collection"] = "c"
|
||||||
|
Loading…
Reference in New Issue
Block a user