summaryrefslogtreecommitdiffstats
path: root/output/layout_base.go
diff options
context:
space:
mode:
Diffstat (limited to 'output/layout_base.go')
-rw-r--r--output/layout_base.go91
1 files changed, 19 insertions, 72 deletions
diff --git a/output/layout_base.go b/output/layout_base.go
index 49ae1d64e..31e1194f4 100644
--- a/output/layout_base.go
+++ b/output/layout_base.go
@@ -40,26 +40,16 @@ type TemplateNames struct {
}
type TemplateLookupDescriptor struct {
- // TemplateDir is the project or theme root of the current template.
- // This will be the same as WorkingDir for non-theme templates.
- TemplateDir string
-
// The full path to the site root.
WorkingDir string
- // Main project layout dir, defaults to "layouts"
- LayoutDir string
-
// The path to the template relative the the base.
// I.e. shortcodes/youtube.html
RelPath string
- // The template name prefix to look for, i.e. "theme".
+ // The template name prefix to look for.
Prefix string
- // The theme dir if theme active.
- ThemeDir string
-
// All the output formats in play. This is used to decide if text/template or
// html/template.
OutputFormats Formats
@@ -71,6 +61,7 @@ type TemplateLookupDescriptor struct {
func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) {
name := filepath.ToSlash(d.RelPath)
+ name = strings.TrimPrefix(name, "/")
if d.Prefix != "" {
name = strings.Trim(d.Prefix, "/") + "/" + name
@@ -78,22 +69,8 @@ func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) {
var (
id TemplateNames
-
- // This is the path to the actual template in process. This may
- // be in the theme's or the project's /layouts.
- baseLayoutDir = filepath.Join(d.TemplateDir, d.LayoutDir)
- fullPath = filepath.Join(baseLayoutDir, d.RelPath)
-
- // This is always the project's layout dir.
- baseWorkLayoutDir = filepath.Join(d.WorkingDir, d.LayoutDir)
-
- baseThemeLayoutDir string
)
- if d.ThemeDir != "" {
- baseThemeLayoutDir = filepath.Join(d.ThemeDir, "layouts")
- }
-
// The filename will have a suffix with an optional type indicator.
// Examples:
// index.html
@@ -119,7 +96,7 @@ func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) {
filenameNoSuffix := parts[0]
- id.OverlayFilename = fullPath
+ id.OverlayFilename = d.RelPath
id.Name = name
if isPlainText {
@@ -127,7 +104,7 @@ func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) {
}
// Ace and Go templates may have both a base and inner template.
- pathDir := filepath.Dir(fullPath)
+ pathDir := filepath.Dir(d.RelPath)
if ext == "amber" || strings.HasSuffix(pathDir, "partials") || strings.HasSuffix(pathDir, "shortcodes") {
// No base template support
@@ -150,7 +127,7 @@ func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) {
// This may be a view that shouldn't have base template
// Have to look inside it to make sure
- needsBase, err := d.ContainsAny(fullPath, innerMarkers)
+ needsBase, err := d.ContainsAny(d.RelPath, innerMarkers)
if err != nil {
return id, err
}
@@ -158,21 +135,14 @@ func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) {
if needsBase {
currBaseFilename := fmt.Sprintf("%s-%s", filenameNoSuffix, baseFilename)
- templateDir := filepath.Dir(fullPath)
-
- // Find the base, e.g. "_default".
- baseTemplatedDir := strings.TrimPrefix(templateDir, baseLayoutDir)
- baseTemplatedDir = strings.TrimPrefix(baseTemplatedDir, helpers.FilePathSeparator)
-
// Look for base template in the follwing order:
// 1. <current-path>/<template-name>-baseof.<outputFormat>(optional).<suffix>, e.g. list-baseof.<outputFormat>(optional).<suffix>.
// 2. <current-path>/baseof.<outputFormat>(optional).<suffix>
// 3. _default/<template-name>-baseof.<outputFormat>(optional).<suffix>, e.g. list-baseof.<outputFormat>(optional).<suffix>.
// 4. _default/baseof.<outputFormat>(optional).<suffix>
- // For each of the steps above, it will first look in the project, then, if theme is set,
- // in the theme's layouts folder.
- // Also note that the <current-path> may be both the project's layout folder and the theme's.
- pairsToCheck := createPairsToCheck(baseTemplatedDir, baseFilename, currBaseFilename)
+ //
+ // The filesystem it looks in a a composite of the project and potential theme(s).
+ pathsToCheck := createPathsToCheck(pathDir, baseFilename, currBaseFilename)
// We may have language code and/or "terms" in the template name. We want the most specific,
// but need to fall back to the baseof.html or baseof.ace if needed.
@@ -183,20 +153,15 @@ func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) {
if len(p1) > 0 && len(p1) == len(p2) {
for i := len(p1); i > 0; i-- {
v1, v2 := strings.Join(p1[:i], ".")+"."+ext, strings.Join(p2[:i], ".")+"."+ext
- pairsToCheck = append(pairsToCheck, createPairsToCheck(baseTemplatedDir, v1, v2)...)
+ pathsToCheck = append(pathsToCheck, createPathsToCheck(pathDir, v1, v2)...)
}
}
- Loop:
- for _, pair := range pairsToCheck {
- pathsToCheck := basePathsToCheck(pair, baseLayoutDir, baseWorkLayoutDir, baseThemeLayoutDir)
-
- for _, pathToCheck := range pathsToCheck {
- if ok, err := d.FileExists(pathToCheck); err == nil && ok {
- id.MasterFilename = pathToCheck
- break Loop
- }
+ for _, p := range pathsToCheck {
+ if ok, err := d.FileExists(p); err == nil && ok {
+ id.MasterFilename = p
+ break
}
}
}
@@ -205,29 +170,11 @@ func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) {
}
-func createPairsToCheck(baseTemplatedDir, baseFilename, currBaseFilename string) [][]string {
- return [][]string{
- {baseTemplatedDir, currBaseFilename},
- {baseTemplatedDir, baseFilename},
- {"_default", currBaseFilename},
- {"_default", baseFilename},
- }
-}
-
-func basePathsToCheck(path []string, layoutDir, workLayoutDir, themeLayoutDir string) []string {
- // workLayoutDir will always be the most specific, so start there.
- pathsToCheck := []string{filepath.Join((append([]string{workLayoutDir}, path...))...)}
-
- if layoutDir != "" && layoutDir != workLayoutDir {
- pathsToCheck = append(pathsToCheck, filepath.Join((append([]string{layoutDir}, path...))...))
+func createPathsToCheck(baseTemplatedDir, baseFilename, currBaseFilename string) []string {
+ return []string{
+ filepath.Join(baseTemplatedDir, currBaseFilename),
+ filepath.Join(baseTemplatedDir, baseFilename),
+ filepath.Join("_default", currBaseFilename),
+ filepath.Join("_default", baseFilename),
}
-
- // May have a theme
- if themeLayoutDir != "" && themeLayoutDir != layoutDir {
- pathsToCheck = append(pathsToCheck, filepath.Join((append([]string{themeLayoutDir}, path...))...))
-
- }
-
- return pathsToCheck
-
}