1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 03:34:46 +01:00
gojekyll/pages/permalinks.go

92 lines
2.7 KiB
Go
Raw Normal View History

2017-06-22 17:02:32 +02:00
package pages
2017-06-15 13:31:52 +02:00
import (
"fmt"
"path/filepath"
2017-06-15 16:17:21 +02:00
"regexp"
2017-06-23 02:39:53 +02:00
"strconv"
"strings"
2017-06-17 01:17:22 +02:00
2017-07-01 05:56:29 +02:00
"github.com/osteele/gojekyll/templates"
2017-07-09 22:17:20 +02:00
"github.com/osteele/gojekyll/utils"
2017-06-15 13:31:52 +02:00
)
2017-07-03 16:39:55 +02:00
// DefaultPermalinkPattern is the default permalink pattern for pages that aren't in a collection
const DefaultPermalinkPattern = "/:path:output_ext"
2017-06-15 13:31:52 +02:00
// PermalinkStyles defines built-in styles from https://jekyllrb.com/docs/permalinks/#builtinpermalinkstyles
var PermalinkStyles = map[string]string{
"date": "/:categories/:year/:month/:day/:title.html",
"pretty": "/:categories/:year/:month/:day/:title/",
"ordinal": "/:categories/:year/:y_day/:title.html",
"none": "/:categories/:title.html",
}
2017-06-15 15:01:42 +02:00
// 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",
}
2017-06-15 16:17:21 +02:00
var templateVariableMatcher = regexp.MustCompile(`:\w+\b`)
2017-06-15 15:01:42 +02:00
// See https://jekyllrb.com/docs/permalinks/#template-variables
2017-07-03 15:37:14 +02:00
func (f *file) permalinkVariables() map[string]string {
2017-06-15 13:31:52 +02:00
var (
2017-07-10 19:23:51 +02:00
relpath = f.relpath
2017-07-09 22:17:20 +02:00
root = utils.TrimExt(relpath)
2017-07-03 15:37:14 +02:00
name = filepath.Base(root)
fm = f.frontMatter
bindings = templates.VariableMap(fm)
2017-07-09 22:17:20 +02:00
slug = bindings.String("slug", utils.Slugify(name))
2017-06-15 13:31:52 +02:00
)
2017-07-03 15:37:14 +02:00
vars := map[string]string{
"categories": strings.Join(f.Categories(), "/"),
2017-07-01 05:56:29 +02:00
"collection": bindings.String("collection", ""),
2017-07-09 22:17:20 +02:00
"name": utils.Slugify(name),
2017-07-03 15:37:14 +02:00
"path": "/" + root, // TODO are we removing and then adding this?
"slug": slug,
"title": slug,
// The following aren't documented, but are evident
"output_ext": f.OutputExt(),
"y_day": strconv.Itoa(f.fileModTime.YearDay()),
2017-06-15 15:01:42 +02:00
}
2017-07-03 15:37:14 +02:00
for k, v := range permalinkDateVariables {
vars[k] = f.fileModTime.Format(v)
2017-06-15 13:31:52 +02:00
}
2017-07-03 15:37:14 +02:00
return vars
2017-06-15 13:31:52 +02:00
}
2017-07-03 15:37:14 +02:00
func (f *file) computePermalink(vars map[string]string) (src string, err error) {
2017-07-03 16:39:55 +02:00
pattern := templates.VariableMap(f.frontMatter).String("permalink", DefaultPermalinkPattern)
2017-06-15 13:31:52 +02:00
if p, found := PermalinkStyles[pattern]; found {
pattern = p
}
2017-07-03 15:37:14 +02:00
templateVariables := f.permalinkVariables()
2017-07-09 22:17:20 +02:00
s, err := utils.SafeReplaceAllStringFunc(templateVariableMatcher, pattern, func(m string) (string, error) {
2017-06-15 13:31:52 +02:00
varname := m[1:]
value, found := templateVariables[varname]
if !found {
2017-07-03 15:37:14 +02:00
return "", fmt.Errorf("unknown variable %q in permalink template %q", varname, pattern)
2017-06-15 13:31:52 +02:00
}
2017-07-03 15:37:14 +02:00
return value, nil
2017-06-15 13:31:52 +02:00
})
2017-07-03 15:37:14 +02:00
if err != nil {
return "", err
}
2017-07-09 22:17:20 +02:00
return utils.URLPathClean("/" + s), nil
2017-06-15 13:31:52 +02:00
}
2017-07-03 15:37:14 +02:00
func (f *file) setPermalink() (err error) {
f.permalink, err = f.computePermalink(f.permalinkVariables())
return
}