mirror of
https://github.com/danog/gojekyll.git
synced 2025-01-23 03:41:14 +01:00
75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// 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",
|
|
}
|
|
|
|
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
|
|
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{
|
|
"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
|
|
}
|
|
}
|
|
|
|
func expandPermalinkPattern(pattern string, path string, frontMatter VariableMap) (s string, err error) {
|
|
if p, found := PermalinkStyles[pattern]; found {
|
|
pattern = p
|
|
}
|
|
templateVariables := permalinkTemplateVariables(path, frontMatter)
|
|
// The ReplaceAllStringFunc callback signals errors via panic.
|
|
// Turn them into return values.
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
if e, ok := r.(error); ok {
|
|
err = e
|
|
} else {
|
|
panic(r)
|
|
}
|
|
}
|
|
}()
|
|
s = templateVariableMatcher.ReplaceAllStringFunc(pattern, func(m string) string {
|
|
varname := m[1:]
|
|
value, found := templateVariables[varname]
|
|
if !found {
|
|
panic(fmt.Errorf("unknown variable %s in permalink template %s", varname, pattern))
|
|
}
|
|
return value
|
|
})
|
|
return
|
|
}
|