2017-03-06 10:20:30 +00: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 (
|
|
|
|
"strings"
|
2017-03-18 15:46:10 +00:00
|
|
|
"sync"
|
2018-01-25 08:37:05 +00:00
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2017-03-06 10:20:30 +00:00
|
|
|
)
|
|
|
|
|
2018-01-13 16:21:42 +00: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 07:58:50 +00:00
|
|
|
// LayoutDescriptor describes how a layout should be chosen. This is
|
|
|
|
// typically built from a Page.
|
|
|
|
type LayoutDescriptor struct {
|
|
|
|
Type string
|
|
|
|
Section string
|
2022-02-17 12:04:00 +00:00
|
|
|
|
|
|
|
// E.g. "page", but also used for the _markup render kinds, e.g. "render-image".
|
|
|
|
Kind string
|
|
|
|
|
|
|
|
// Comma-separated list of kind variants, e.g. "go,json" as variants which would find "render-codeblock-go.html"
|
|
|
|
KindVariants string
|
|
|
|
|
|
|
|
Lang string
|
|
|
|
Layout string
|
2018-01-13 16:21:42 +00:00
|
|
|
// LayoutOverride indicates what we should only look for the above layout.
|
|
|
|
LayoutOverride bool
|
2019-11-27 12:42:36 +00:00
|
|
|
|
|
|
|
RenderingHook bool
|
2020-01-15 14:59:56 +00:00
|
|
|
Baseof bool
|
2019-11-27 12:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d LayoutDescriptor) isList() bool {
|
2020-01-27 11:30:31 +00:00
|
|
|
return !d.RenderingHook && d.Kind != "page" && d.Kind != "404"
|
2017-03-06 10:20:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 12:25:05 +00:00
|
|
|
// LayoutHandler calculates the layout template to use to render a given output type.
|
2017-03-06 12:18:33 +00:00
|
|
|
type LayoutHandler struct {
|
2017-03-18 15:46:10 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
cache map[layoutCacheKey][]string
|
|
|
|
}
|
|
|
|
|
|
|
|
type layoutCacheKey struct {
|
2018-01-13 16:21:42 +00:00
|
|
|
d LayoutDescriptor
|
2018-07-10 09:55:22 +00:00
|
|
|
f string
|
2017-03-06 18:54:46 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 12:25:05 +00: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 14:01:25 +00:00
|
|
|
func NewLayoutHandler() *LayoutHandler {
|
|
|
|
return &LayoutHandler{cache: make(map[layoutCacheKey][]string)}
|
2017-03-06 10:20:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 12:25:05 +00:00
|
|
|
// For returns a layout for the given LayoutDescriptor and options.
|
|
|
|
// Layouts are rendered and cached internally.
|
2018-01-13 16:21:42 +00:00
|
|
|
func (l *LayoutHandler) For(d LayoutDescriptor, f Format) ([]string, error) {
|
2017-03-18 15:46:10 +00:00
|
|
|
// We will get lots of requests for the same layouts, so avoid recalculations.
|
2018-07-10 09:55:22 +00:00
|
|
|
key := layoutCacheKey{d, f.Name}
|
2017-03-18 15:46:10 +00:00
|
|
|
l.mu.RLock()
|
|
|
|
if cacheVal, found := l.cache[key]; found {
|
|
|
|
l.mu.RUnlock()
|
2017-03-26 17:34:30 +00:00
|
|
|
return cacheVal, nil
|
2017-03-18 15:46:10 +00:00
|
|
|
}
|
|
|
|
l.mu.RUnlock()
|
|
|
|
|
2018-01-13 16:21:42 +00:00
|
|
|
layouts := resolvePageTemplate(d, f)
|
2017-03-06 10:20:30 +00:00
|
|
|
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 07:16:58 +00:00
|
|
|
layouts = helpers.UniqueStringsReuse(layouts)
|
2017-03-27 18:43:49 +00:00
|
|
|
|
2017-03-18 15:46:10 +00:00
|
|
|
l.mu.Lock()
|
|
|
|
l.cache[key] = layouts
|
|
|
|
l.mu.Unlock()
|
|
|
|
|
2017-03-26 17:34:30 +00:00
|
|
|
return layouts, nil
|
2017-03-06 10:20:30 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 16:21:42 +00:00
|
|
|
type layoutBuilder struct {
|
|
|
|
layoutVariations []string
|
|
|
|
typeVariations []string
|
|
|
|
d LayoutDescriptor
|
|
|
|
f Format
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *layoutBuilder) addLayoutVariations(vars ...string) {
|
|
|
|
for _, layoutVar := range vars {
|
2020-01-15 14:59:56 +00:00
|
|
|
if l.d.Baseof && layoutVar != "baseof" {
|
|
|
|
l.layoutVariations = append(l.layoutVariations, layoutVar+"-baseof")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !l.d.RenderingHook && !l.d.Baseof && l.d.LayoutOverride && layoutVar != l.d.Layout {
|
2018-01-13 16:21:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
l.layoutVariations = append(l.layoutVariations, layoutVar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *layoutBuilder) addTypeVariations(vars ...string) {
|
|
|
|
for _, typeVar := range vars {
|
|
|
|
if !reservedSections[typeVar] {
|
2019-11-27 12:42:36 +00:00
|
|
|
if l.d.RenderingHook {
|
|
|
|
typeVar = typeVar + renderingHookRoot
|
|
|
|
}
|
2018-01-13 16:21:42 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
const renderingHookRoot = "/_markup"
|
|
|
|
|
2018-01-13 16:21:42 +00:00
|
|
|
func resolvePageTemplate(d LayoutDescriptor, f Format) []string {
|
|
|
|
b := &layoutBuilder{d: d, f: f}
|
|
|
|
|
2020-06-02 16:08:17 +00:00
|
|
|
if !d.RenderingHook && d.Layout != "" {
|
|
|
|
b.addLayoutVariations(d.Layout)
|
|
|
|
}
|
|
|
|
if d.Type != "" {
|
|
|
|
b.addTypeVariations(d.Type)
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
if d.RenderingHook {
|
2022-02-17 12:04:00 +00:00
|
|
|
if d.KindVariants != "" {
|
|
|
|
// Add the more specific variants first.
|
|
|
|
for _, variant := range strings.Split(d.KindVariants, ",") {
|
|
|
|
b.addLayoutVariations(d.Kind + "-" + variant)
|
|
|
|
}
|
|
|
|
}
|
2019-11-27 12:42:36 +00:00
|
|
|
b.addLayoutVariations(d.Kind)
|
2020-06-02 16:08:17 +00:00
|
|
|
b.addSectionType()
|
2018-01-13 16:21:42 +00:00
|
|
|
}
|
2017-03-25 16:46:09 +00:00
|
|
|
|
|
|
|
switch d.Kind {
|
2018-01-13 16:21:42 +00:00
|
|
|
case "page":
|
|
|
|
b.addLayoutVariations("single")
|
|
|
|
b.addSectionType()
|
2017-03-25 16:46:09 +00:00
|
|
|
case "home":
|
2018-01-13 16:21:42 +00:00
|
|
|
b.addLayoutVariations("index", "home")
|
|
|
|
// Also look in the root
|
|
|
|
b.addTypeVariations("")
|
2017-03-25 16:46:09 +00:00
|
|
|
case "section":
|
2018-01-13 16:21:42 +00:00
|
|
|
if d.Section != "" {
|
|
|
|
b.addLayoutVariations(d.Section)
|
|
|
|
}
|
|
|
|
b.addSectionType()
|
|
|
|
b.addKind()
|
2020-06-16 13:43:50 +00:00
|
|
|
case "term":
|
|
|
|
b.addKind()
|
2018-01-13 16:21:42 +00:00
|
|
|
if d.Section != "" {
|
|
|
|
b.addLayoutVariations(d.Section)
|
|
|
|
}
|
2020-06-16 13:43:50 +00:00
|
|
|
b.addLayoutVariations("taxonomy")
|
|
|
|
b.addTypeVariations("taxonomy")
|
2018-01-13 16:21:42 +00:00
|
|
|
b.addSectionType()
|
2020-06-16 13:43:50 +00:00
|
|
|
case "taxonomy":
|
2018-01-13 16:21:42 +00:00
|
|
|
if d.Section != "" {
|
|
|
|
b.addLayoutVariations(d.Section + ".terms")
|
|
|
|
}
|
|
|
|
b.addSectionType()
|
|
|
|
b.addLayoutVariations("terms")
|
2020-06-16 13:43:50 +00:00
|
|
|
// For legacy reasons this is deliberately put last.
|
|
|
|
b.addKind()
|
2020-01-27 11:30:31 +00:00
|
|
|
case "404":
|
|
|
|
b.addLayoutVariations("404")
|
|
|
|
b.addTypeVariations("")
|
2018-01-13 16:21:42 +00:00
|
|
|
}
|
2017-07-02 08:46:28 +00:00
|
|
|
|
2018-01-13 16:21:42 +00:00
|
|
|
isRSS := f.Name == RSSFormat.Name
|
2020-01-15 14:59:56 +00:00
|
|
|
if !d.RenderingHook && !d.Baseof && isRSS {
|
2018-01-13 16:21:42 +00:00
|
|
|
// The historic and common rss.xml case
|
|
|
|
b.addLayoutVariations("")
|
|
|
|
}
|
2017-07-02 08:46:28 +00:00
|
|
|
|
2020-01-27 11:30:31 +00:00
|
|
|
if d.Baseof || d.Kind != "404" {
|
|
|
|
// Most have _default in their lookup path
|
|
|
|
b.addTypeVariations("_default")
|
|
|
|
}
|
2017-07-02 08:46:28 +00:00
|
|
|
|
2019-11-27 12:42:36 +00:00
|
|
|
if d.isList() {
|
2018-01-13 16:21:42 +00:00
|
|
|
// Add the common list type
|
|
|
|
b.addLayoutVariations("list")
|
2017-07-02 08:46:28 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 14:59:56 +00:00
|
|
|
if d.Baseof {
|
|
|
|
b.addLayoutVariations("baseof")
|
|
|
|
}
|
|
|
|
|
2018-01-13 16:21:42 +00:00
|
|
|
layouts := b.resolveVariations()
|
2017-07-02 08:46:28 +00:00
|
|
|
|
2020-01-15 14:59:56 +00:00
|
|
|
if !d.RenderingHook && !d.Baseof && isRSS {
|
2018-01-13 16:21:42 +00:00
|
|
|
layouts = append(layouts, "_internal/_default/rss.xml")
|
2017-07-02 08:46:28 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 16:21:42 +00:00
|
|
|
return layouts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *layoutBuilder) resolveVariations() []string {
|
2017-07-02 08:46:28 +00:00
|
|
|
var layouts []string
|
|
|
|
|
2018-01-13 16:21:42 +00:00
|
|
|
var variations []string
|
|
|
|
name := strings.ToLower(l.f.Name)
|
|
|
|
|
|
|
|
if l.d.Lang != "" {
|
|
|
|
// We prefer the most specific type before language.
|
2020-10-15 18:54:47 +00:00
|
|
|
variations = append(variations, []string{l.d.Lang + "." + name, name, l.d.Lang}...)
|
2018-01-13 16:21:42 +00:00
|
|
|
} else {
|
|
|
|
variations = append(variations, name)
|
|
|
|
}
|
2017-07-02 08:46:28 +00:00
|
|
|
|
2018-01-13 16:21:42 +00:00
|
|
|
variations = append(variations, "")
|
|
|
|
|
|
|
|
for _, typeVar := range l.typeVariations {
|
|
|
|
for _, variation := range variations {
|
|
|
|
for _, layoutVar := range l.layoutVariations {
|
|
|
|
if variation == "" && layoutVar == "" {
|
|
|
|
continue
|
|
|
|
}
|
2020-10-15 18:54:47 +00:00
|
|
|
|
2021-03-11 08:18:01 +00:00
|
|
|
s := constructLayoutPath(typeVar, layoutVar, variation, l.f.MediaType.FirstSuffix.Suffix)
|
2020-10-15 18:54:47 +00:00
|
|
|
if s != "" {
|
|
|
|
layouts = append(layouts, s)
|
|
|
|
}
|
2018-01-13 16:21:42 +00:00
|
|
|
}
|
2017-07-02 08:46:28 +00:00
|
|
|
}
|
2017-06-20 06:45:52 +00:00
|
|
|
}
|
2017-03-27 18:43:49 +00:00
|
|
|
|
2020-10-15 18:54:47 +00:00
|
|
|
return layouts
|
2017-06-20 06:45:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 18:54:47 +00:00
|
|
|
// constructLayoutPath constructs a layout path given a type, layout,
|
|
|
|
// variations, and extension. The path constructed follows the pattern of
|
|
|
|
// type/layout.variations.extension. If any value is empty, it will be left out
|
|
|
|
// of the path construction.
|
|
|
|
//
|
|
|
|
// Path construction requires at least 2 of 3 out of layout, variations, and extension.
|
|
|
|
// If more than one of those is empty, an empty string is returned.
|
|
|
|
func constructLayoutPath(typ, layout, variations, extension string) string {
|
|
|
|
// we already know that layout and variations are not both empty because of
|
|
|
|
// checks in resolveVariants().
|
|
|
|
if extension == "" && (layout == "" || variations == "") {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commence valid path construction...
|
2018-01-13 16:21:42 +00:00
|
|
|
|
2020-10-15 18:54:47 +00:00
|
|
|
var (
|
|
|
|
p strings.Builder
|
|
|
|
needDot bool
|
|
|
|
)
|
2018-01-13 16:21:42 +00:00
|
|
|
|
2020-10-15 18:54:47 +00:00
|
|
|
if typ != "" {
|
|
|
|
p.WriteString(typ)
|
|
|
|
p.WriteString("/")
|
2018-01-13 16:21:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 18:54:47 +00:00
|
|
|
if layout != "" {
|
|
|
|
p.WriteString(layout)
|
|
|
|
needDot = true
|
2018-01-13 16:21:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 18:54:47 +00:00
|
|
|
if variations != "" {
|
|
|
|
if needDot {
|
|
|
|
p.WriteString(".")
|
2017-06-20 06:45:52 +00:00
|
|
|
}
|
2020-10-15 18:54:47 +00:00
|
|
|
p.WriteString(variations)
|
|
|
|
needDot = true
|
2017-06-20 06:45:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 18:54:47 +00:00
|
|
|
if extension != "" {
|
|
|
|
if needDot {
|
|
|
|
p.WriteString(".")
|
|
|
|
}
|
|
|
|
p.WriteString(extension)
|
|
|
|
}
|
2017-03-27 18:43:49 +00:00
|
|
|
|
2020-10-15 18:54:47 +00:00
|
|
|
return p.String()
|
2017-03-07 16:26:22 +00:00
|
|
|
}
|