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 (
|
|
|
|
"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
|
2019-11-27 07:42:36 -05:00
|
|
|
|
|
|
|
RenderingHook bool
|
2020-01-15 09:59:56 -05:00
|
|
|
Baseof bool
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d LayoutDescriptor) isList() bool {
|
2020-01-27 06:30:31 -05:00
|
|
|
return !d.RenderingHook && d.Kind != "page" && d.Kind != "404"
|
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
|
|
|
|
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 03:16:58 -04:00
|
|
|
layouts = helpers.UniqueStringsReuse(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 {
|
2020-01-15 09:59:56 -05: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 11:21:42 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
l.layoutVariations = append(l.layoutVariations, layoutVar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *layoutBuilder) addTypeVariations(vars ...string) {
|
|
|
|
for _, typeVar := range vars {
|
|
|
|
if !reservedSections[typeVar] {
|
2019-11-27 07:42:36 -05:00
|
|
|
if l.d.RenderingHook {
|
|
|
|
typeVar = typeVar + renderingHookRoot
|
|
|
|
}
|
2018-01-13 11:21:42 -05: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 07:42:36 -05:00
|
|
|
const renderingHookRoot = "/_markup"
|
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
func resolvePageTemplate(d LayoutDescriptor, f Format) []string {
|
|
|
|
b := &layoutBuilder{d: d, f: f}
|
|
|
|
|
2020-06-02 12:08:17 -04:00
|
|
|
if !d.RenderingHook && d.Layout != "" {
|
|
|
|
b.addLayoutVariations(d.Layout)
|
|
|
|
}
|
|
|
|
if d.Type != "" {
|
|
|
|
b.addTypeVariations(d.Type)
|
|
|
|
}
|
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
if d.RenderingHook {
|
|
|
|
b.addLayoutVariations(d.Kind)
|
2020-06-02 12:08:17 -04:00
|
|
|
b.addSectionType()
|
2018-01-13 11:21:42 -05:00
|
|
|
}
|
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()
|
2020-06-16 09:43:50 -04:00
|
|
|
case "term":
|
|
|
|
b.addKind()
|
2018-01-13 11:21:42 -05:00
|
|
|
if d.Section != "" {
|
|
|
|
b.addLayoutVariations(d.Section)
|
|
|
|
}
|
2020-06-16 09:43:50 -04:00
|
|
|
b.addLayoutVariations("taxonomy")
|
|
|
|
b.addTypeVariations("taxonomy")
|
2018-01-13 11:21:42 -05:00
|
|
|
b.addSectionType()
|
2020-06-16 09:43:50 -04:00
|
|
|
case "taxonomy":
|
2018-01-13 11:21:42 -05:00
|
|
|
if d.Section != "" {
|
|
|
|
b.addLayoutVariations(d.Section + ".terms")
|
|
|
|
}
|
|
|
|
b.addSectionType()
|
|
|
|
b.addLayoutVariations("terms")
|
2020-06-16 09:43:50 -04:00
|
|
|
// For legacy reasons this is deliberately put last.
|
|
|
|
b.addKind()
|
2020-01-27 06:30:31 -05:00
|
|
|
case "404":
|
|
|
|
b.addLayoutVariations("404")
|
|
|
|
b.addTypeVariations("")
|
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
|
2020-01-15 09:59:56 -05:00
|
|
|
if !d.RenderingHook && !d.Baseof && isRSS {
|
2018-01-13 11:21:42 -05:00
|
|
|
// The historic and common rss.xml case
|
|
|
|
b.addLayoutVariations("")
|
|
|
|
}
|
2017-07-02 04:46:28 -04:00
|
|
|
|
2020-01-27 06:30:31 -05:00
|
|
|
if d.Baseof || d.Kind != "404" {
|
|
|
|
// Most have _default in their lookup path
|
|
|
|
b.addTypeVariations("_default")
|
|
|
|
}
|
2017-07-02 04:46:28 -04:00
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
if d.isList() {
|
2018-01-13 11:21:42 -05:00
|
|
|
// Add the common list type
|
|
|
|
b.addLayoutVariations("list")
|
2017-07-02 04:46:28 -04:00
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
if d.Baseof {
|
|
|
|
b.addLayoutVariations("baseof")
|
|
|
|
}
|
|
|
|
|
2018-01-13 11:21:42 -05:00
|
|
|
layouts := b.resolveVariations()
|
2017-07-02 04:46:28 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
if !d.RenderingHook && !d.Baseof && isRSS {
|
2018-01-13 11:21:42 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2020-10-15 14:54:47 -04:00
|
|
|
variations = append(variations, []string{l.d.Lang + "." + name, name, l.d.Lang}...)
|
2018-01-13 11:21:42 -05:00
|
|
|
} 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
|
|
|
|
}
|
2020-10-15 14:54:47 -04:00
|
|
|
|
|
|
|
s := constructLayoutPath(typeVar, layoutVar, variation, l.f.MediaType.Suffix())
|
|
|
|
if s != "" {
|
|
|
|
layouts = append(layouts, s)
|
|
|
|
}
|
2018-01-13 11:21:42 -05:00
|
|
|
}
|
2017-07-02 04:46:28 -04:00
|
|
|
}
|
2017-06-20 02:45:52 -04:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2020-10-15 14:54:47 -04:00
|
|
|
return layouts
|
2017-06-20 02:45:52 -04:00
|
|
|
}
|
|
|
|
|
2020-10-15 14:54:47 -04: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 11:21:42 -05:00
|
|
|
|
2020-10-15 14:54:47 -04:00
|
|
|
var (
|
|
|
|
p strings.Builder
|
|
|
|
needDot bool
|
|
|
|
)
|
2018-01-13 11:21:42 -05:00
|
|
|
|
2020-10-15 14:54:47 -04:00
|
|
|
if typ != "" {
|
|
|
|
p.WriteString(typ)
|
|
|
|
p.WriteString("/")
|
2018-01-13 11:21:42 -05:00
|
|
|
}
|
|
|
|
|
2020-10-15 14:54:47 -04:00
|
|
|
if layout != "" {
|
|
|
|
p.WriteString(layout)
|
|
|
|
needDot = true
|
2018-01-13 11:21:42 -05:00
|
|
|
}
|
|
|
|
|
2020-10-15 14:54:47 -04:00
|
|
|
if variations != "" {
|
|
|
|
if needDot {
|
|
|
|
p.WriteString(".")
|
2017-06-20 02:45:52 -04:00
|
|
|
}
|
2020-10-15 14:54:47 -04:00
|
|
|
p.WriteString(variations)
|
|
|
|
needDot = true
|
2017-06-20 02:45:52 -04:00
|
|
|
}
|
|
|
|
|
2020-10-15 14:54:47 -04:00
|
|
|
if extension != "" {
|
|
|
|
if needDot {
|
|
|
|
p.WriteString(".")
|
|
|
|
}
|
|
|
|
p.WriteString(extension)
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2020-10-15 14:54:47 -04:00
|
|
|
return p.String()
|
2017-03-07 11:26:22 -05:00
|
|
|
}
|