2017-03-06 05:20:30 -05:00
|
|
|
// Copyright 2017-present The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2017-03-18 11:46:10 -04:00
|
|
|
"sync"
|
2018-01-25 03:37:05 -05:00
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2017-03-06 05:20:30 -05:00
|
|
|
)
|
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
// These may be used as content sections with potential conflicts. Avoid that.
|
|
|
|
var reservedSections = map[string]bool{
|
|
|
|
"shortcodes": true,
|
|
|
|
"partials": true,
|
|
|
|
}
|
|
|
|
|
2017-03-16 03:58:50 -04:00
|
|
|
// LayoutDescriptor describes how a layout should be chosen. This is
|
|
|
|
// typically built from a Page.
|
|
|
|
type LayoutDescriptor struct {
|
|
|
|
Type string
|
|
|
|
Section string
|
|
|
|
Kind string
|
2017-07-02 04:46:28 -04:00
|
|
|
Lang string
|
2017-03-16 03:58:50 -04:00
|
|
|
Layout string
|
2018-01-13 11:21:42 -05:00
|
|
|
// LayoutOverride indicates what we should only look for the above layout.
|
|
|
|
LayoutOverride bool
|
2017-03-06 05:20:30 -05:00
|
|
|
}
|
|
|
|
|
2017-08-02 08:25:05 -04:00
|
|
|
// LayoutHandler calculates the layout template to use to render a given output type.
|
2017-03-06 07:18:33 -05:00
|
|
|
type LayoutHandler struct {
|
2017-03-18 11:46:10 -04:00
|
|
|
mu sync.RWMutex
|
|
|
|
cache map[layoutCacheKey][]string
|
|
|
|
}
|
|
|
|
|
|
|
|
type layoutCacheKey struct {
|
2018-01-13 11:21:42 -05:00
|
|
|
d LayoutDescriptor
|
2018-07-10 05:55:22 -04:00
|
|
|
f string
|
2017-03-06 13:54:46 -05:00
|
|
|
}
|
|
|
|
|
2017-08-02 08:25:05 -04:00
|
|
|
// NewLayoutHandler creates a new LayoutHandler.
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
func NewLayoutHandler() *LayoutHandler {
|
|
|
|
return &LayoutHandler{cache: make(map[layoutCacheKey][]string)}
|
2017-03-06 05:20:30 -05:00
|
|
|
}
|
|
|
|
|
2017-08-02 08:25:05 -04:00
|
|
|
// For returns a layout for the given LayoutDescriptor and options.
|
|
|
|
// Layouts are rendered and cached internally.
|
2018-01-13 11:21:42 -05:00
|
|
|
func (l *LayoutHandler) For(d LayoutDescriptor, f Format) ([]string, error) {
|
2017-03-18 11:46:10 -04:00
|
|
|
|
|
|
|
// We will get lots of requests for the same layouts, so avoid recalculations.
|
2018-07-10 05:55:22 -04:00
|
|
|
key := layoutCacheKey{d, f.Name}
|
2017-03-18 11:46:10 -04:00
|
|
|
l.mu.RLock()
|
|
|
|
if cacheVal, found := l.cache[key]; found {
|
|
|
|
l.mu.RUnlock()
|
2017-03-26 13:34:30 -04:00
|
|
|
return cacheVal, nil
|
2017-03-18 11:46:10 -04:00
|
|
|
}
|
|
|
|
l.mu.RUnlock()
|
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
layouts := resolvePageTemplate(d, f)
|
2017-03-06 05:20:30 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
layouts = prependTextPrefixIfNeeded(f, layouts...)
|
2018-01-25 03:37:05 -05:00
|
|
|
layouts = helpers.UniqueStrings(layouts)
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2017-03-18 11:46:10 -04:00
|
|
|
l.mu.Lock()
|
|
|
|
l.cache[key] = layouts
|
|
|
|
l.mu.Unlock()
|
|
|
|
|
2017-03-26 13:34:30 -04:00
|
|
|
return layouts, nil
|
2017-03-06 05:20:30 -05:00
|
|
|
}
|
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
type layoutBuilder struct {
|
|
|
|
layoutVariations []string
|
|
|
|
typeVariations []string
|
|
|
|
d LayoutDescriptor
|
|
|
|
f Format
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *layoutBuilder) addLayoutVariations(vars ...string) {
|
|
|
|
for _, layoutVar := range vars {
|
|
|
|
if l.d.LayoutOverride && layoutVar != l.d.Layout {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
l.layoutVariations = append(l.layoutVariations, layoutVar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *layoutBuilder) addTypeVariations(vars ...string) {
|
|
|
|
for _, typeVar := range vars {
|
|
|
|
if !reservedSections[typeVar] {
|
|
|
|
l.typeVariations = append(l.typeVariations, typeVar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *layoutBuilder) addSectionType() {
|
|
|
|
if l.d.Section != "" {
|
|
|
|
l.addTypeVariations(l.d.Section)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *layoutBuilder) addKind() {
|
|
|
|
l.addLayoutVariations(l.d.Kind)
|
|
|
|
l.addTypeVariations(l.d.Kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resolvePageTemplate(d LayoutDescriptor, f Format) []string {
|
|
|
|
|
|
|
|
b := &layoutBuilder{d: d, f: f}
|
|
|
|
|
|
|
|
if d.Layout != "" {
|
|
|
|
b.addLayoutVariations(d.Layout)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Type != "" {
|
|
|
|
b.addTypeVariations(d.Type)
|
|
|
|
}
|
2017-03-25 12:46:09 -04:00
|
|
|
|
|
|
|
switch d.Kind {
|
2018-01-13 11:21:42 -05:00
|
|
|
case "page":
|
|
|
|
b.addLayoutVariations("single")
|
|
|
|
b.addSectionType()
|
2017-03-25 12:46:09 -04:00
|
|
|
case "home":
|
2018-01-13 11:21:42 -05:00
|
|
|
b.addLayoutVariations("index", "home")
|
|
|
|
// Also look in the root
|
|
|
|
b.addTypeVariations("")
|
2017-03-25 12:46:09 -04:00
|
|
|
case "section":
|
2018-01-13 11:21:42 -05:00
|
|
|
if d.Section != "" {
|
|
|
|
b.addLayoutVariations(d.Section)
|
|
|
|
}
|
|
|
|
b.addSectionType()
|
|
|
|
b.addKind()
|
2017-03-25 12:46:09 -04:00
|
|
|
case "taxonomy":
|
2018-01-13 11:21:42 -05:00
|
|
|
if d.Section != "" {
|
|
|
|
b.addLayoutVariations(d.Section)
|
|
|
|
}
|
|
|
|
b.addKind()
|
|
|
|
b.addSectionType()
|
|
|
|
|
2017-03-25 12:46:09 -04:00
|
|
|
case "taxonomyTerm":
|
2018-01-13 11:21:42 -05:00
|
|
|
if d.Section != "" {
|
|
|
|
b.addLayoutVariations(d.Section + ".terms")
|
|
|
|
}
|
|
|
|
b.addTypeVariations("taxonomy")
|
|
|
|
b.addSectionType()
|
|
|
|
b.addLayoutVariations("terms")
|
2017-03-25 12:46:09 -04:00
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
}
|
2017-07-02 04:46:28 -04:00
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
isRSS := f.Name == RSSFormat.Name
|
|
|
|
if isRSS {
|
|
|
|
// The historic and common rss.xml case
|
|
|
|
b.addLayoutVariations("")
|
|
|
|
}
|
2017-07-02 04:46:28 -04:00
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
// All have _default in their lookup path
|
|
|
|
b.addTypeVariations("_default")
|
2017-07-02 04:46:28 -04:00
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
if d.Kind != "page" {
|
|
|
|
// Add the common list type
|
|
|
|
b.addLayoutVariations("list")
|
2017-07-02 04:46:28 -04:00
|
|
|
}
|
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
layouts := b.resolveVariations()
|
2017-07-02 04:46:28 -04:00
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
if isRSS {
|
|
|
|
layouts = append(layouts, "_internal/_default/rss.xml")
|
2017-07-02 04:46:28 -04:00
|
|
|
}
|
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
return layouts
|
2017-07-02 04:46:28 -04:00
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *layoutBuilder) resolveVariations() []string {
|
2017-07-02 04:46:28 -04:00
|
|
|
|
|
|
|
var layouts []string
|
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
var variations []string
|
|
|
|
name := strings.ToLower(l.f.Name)
|
|
|
|
|
|
|
|
if l.d.Lang != "" {
|
|
|
|
// We prefer the most specific type before language.
|
|
|
|
variations = append(variations, []string{fmt.Sprintf("%s.%s", l.d.Lang, name), name, l.d.Lang}...)
|
|
|
|
} else {
|
|
|
|
variations = append(variations, name)
|
|
|
|
}
|
2017-07-02 04:46:28 -04:00
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
variations = append(variations, "")
|
|
|
|
|
|
|
|
for _, typeVar := range l.typeVariations {
|
|
|
|
for _, variation := range variations {
|
|
|
|
for _, layoutVar := range l.layoutVariations {
|
|
|
|
if variation == "" && layoutVar == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
template := layoutTemplate(typeVar, layoutVar)
|
|
|
|
layouts = append(layouts, replaceKeyValues(template,
|
|
|
|
"TYPE", typeVar,
|
|
|
|
"LAYOUT", layoutVar,
|
|
|
|
"VARIATIONS", variation,
|
2018-07-10 05:55:22 -04:00
|
|
|
"EXTENSION", l.f.MediaType.Suffix(),
|
2018-01-13 11:21:42 -05:00
|
|
|
))
|
|
|
|
}
|
2017-07-02 04:46:28 -04:00
|
|
|
}
|
2018-01-13 11:21:42 -05:00
|
|
|
|
2017-06-20 02:45:52 -04:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2017-06-20 02:45:52 -04:00
|
|
|
return filterDotLess(layouts)
|
|
|
|
}
|
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
func layoutTemplate(typeVar, layoutVar string) string {
|
|
|
|
|
|
|
|
var l string
|
|
|
|
|
|
|
|
if typeVar != "" {
|
|
|
|
l = "TYPE/"
|
|
|
|
}
|
|
|
|
|
|
|
|
if layoutVar != "" {
|
|
|
|
l += "LAYOUT.VARIATIONS.EXTENSION"
|
|
|
|
} else {
|
|
|
|
l += "VARIATIONS.EXTENSION"
|
|
|
|
}
|
|
|
|
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2017-06-20 02:45:52 -04:00
|
|
|
func filterDotLess(layouts []string) []string {
|
|
|
|
var filteredLayouts []string
|
|
|
|
|
|
|
|
for _, l := range layouts {
|
2018-01-13 11:21:42 -05:00
|
|
|
l = strings.Replace(l, "..", ".", -1)
|
2017-07-02 04:46:28 -04:00
|
|
|
l = strings.Trim(l, ".")
|
2017-06-20 02:45:52 -04:00
|
|
|
// If media type has no suffix, we have "index" type of layouts in this list, which
|
|
|
|
// doesn't make much sense.
|
|
|
|
if strings.Contains(l, ".") {
|
|
|
|
filteredLayouts = append(filteredLayouts, l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filteredLayouts
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func prependTextPrefixIfNeeded(f Format, layouts ...string) []string {
|
|
|
|
if !f.IsPlainText {
|
|
|
|
return layouts
|
|
|
|
}
|
|
|
|
|
|
|
|
newLayouts := make([]string, len(layouts))
|
|
|
|
|
|
|
|
for i, l := range layouts {
|
|
|
|
newLayouts[i] = "_text/" + l
|
|
|
|
}
|
|
|
|
|
|
|
|
return newLayouts
|
2017-03-07 11:26:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func replaceKeyValues(s string, oldNew ...string) string {
|
|
|
|
replacer := strings.NewReplacer(oldNew...)
|
|
|
|
return replacer.Replace(s)
|
|
|
|
}
|