2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2017-02-17 07:30:50 -05:00
|
|
|
//
|
|
|
|
// 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 tplimpl
|
|
|
|
|
|
|
|
import (
|
2017-05-06 14:15:28 -04:00
|
|
|
"fmt"
|
2019-12-10 13:56:44 -05:00
|
|
|
"io"
|
|
|
|
"reflect"
|
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/common/herrors"
|
2019-12-10 02:02:15 -05:00
|
|
|
|
2017-04-02 08:20:34 -04:00
|
|
|
"strings"
|
2019-12-10 02:02:15 -05:00
|
|
|
|
|
|
|
template "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
|
|
|
|
|
|
|
|
texttemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate"
|
|
|
|
"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
2018-05-04 11:53:56 -04:00
|
|
|
"github.com/gohugoio/hugo/tpl/tplimpl/embedded"
|
2018-10-03 08:58:09 -04:00
|
|
|
"github.com/pkg/errors"
|
2018-05-04 11:53:56 -04:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
"os"
|
2017-04-02 08:20:34 -04:00
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/output"
|
2017-03-27 14:43:49 -04:00
|
|
|
|
|
|
|
"path/filepath"
|
2017-02-17 07:30:50 -05:00
|
|
|
"sync"
|
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/deps"
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
"github.com/gohugoio/hugo/tpl"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/afero"
|
2017-03-27 14:43:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
textTmplNamePrefix = "_text/"
|
2017-02-17 07:30:50 -05:00
|
|
|
)
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
var (
|
2019-12-10 13:56:44 -05:00
|
|
|
_ tpl.TemplateManager = (*templateHandler)(nil)
|
|
|
|
_ tpl.TemplateHandler = (*templateHandler)(nil)
|
|
|
|
_ tpl.TemplateDebugger = (*templateHandler)(nil)
|
|
|
|
_ tpl.TemplateFuncGetter = (*templateHandler)(nil)
|
|
|
|
_ tpl.TemplateFinder = (*htmlTemplates)(nil)
|
|
|
|
_ tpl.TemplateFinder = (*textTemplates)(nil)
|
|
|
|
_ templateLoader = (*htmlTemplates)(nil)
|
|
|
|
_ templateLoader = (*textTemplates)(nil)
|
2017-03-27 14:43:49 -04:00
|
|
|
)
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2019-04-15 09:17:46 -04:00
|
|
|
const (
|
|
|
|
shortcodesPathPrefix = "shortcodes/"
|
|
|
|
internalPathPrefix = "_internal/"
|
|
|
|
)
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// The identifiers may be truncated in the log, e.g.
|
|
|
|
// "executing "main" at <$scaled.SRelPermalin...>: can't evaluate field SRelPermalink in type *resource.Image"
|
|
|
|
var identifiersRe = regexp.MustCompile(`at \<(.*?)(\.{3})?\>:`)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
var embeddedTemplatesAliases = map[string][]string{
|
|
|
|
"shortcodes/twitter.html": {"shortcodes/tweet.html"},
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
const baseFileBase = "baseof"
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func newTemplateAdapter(deps *deps.Deps) *templateHandler {
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
common := &templatesCommon{
|
|
|
|
nameBaseTemplateName: make(map[string]string),
|
2019-04-16 03:13:55 -04:00
|
|
|
transformNotFound: make(map[string]bool),
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
htmlT := &htmlTemplates{
|
2018-10-03 08:58:09 -04:00
|
|
|
t: template.New(""),
|
|
|
|
overlays: make(map[string]*template.Template),
|
|
|
|
templatesCommon: common,
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
textT := &textTemplates{
|
2018-10-03 08:58:09 -04:00
|
|
|
textTemplate: &textTemplate{t: texttemplate.New("")},
|
2019-11-26 03:44:31 -05:00
|
|
|
standalone: &textTemplate{t: texttemplate.New("")},
|
2018-10-03 08:58:09 -04:00
|
|
|
overlays: make(map[string]*texttemplate.Template),
|
|
|
|
templatesCommon: common,
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
h := &templateHandler{
|
2019-12-10 13:56:44 -05:00
|
|
|
Deps: deps,
|
|
|
|
layoutsFs: deps.BaseFs.Layouts.Fs,
|
|
|
|
templateHandlerCommon: &templateHandlerCommon{
|
|
|
|
shortcodes: make(map[string]*shortcodeTemplates),
|
|
|
|
templateInfo: make(map[string]tpl.Info),
|
|
|
|
html: htmlT,
|
|
|
|
text: textT,
|
|
|
|
},
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
common.handler = h
|
|
|
|
|
|
|
|
return h
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
type htmlTemplates struct {
|
|
|
|
*templatesCommon
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
t *template.Template
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
// This looks, and is, strange.
|
|
|
|
// The clone is used by non-renderable content pages, and these need to be
|
|
|
|
// re-parsed on content change, and to avoid the
|
|
|
|
// "cannot Parse after Execute" error, we need to re-clone it from the original clone.
|
|
|
|
clone *template.Template
|
|
|
|
cloneClone *template.Template
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
// a separate storage for the overlays created from cloned master templates.
|
|
|
|
// note: No mutex protection, so we add these in one Go routine, then just read.
|
|
|
|
overlays map[string]*template.Template
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
func (t *htmlTemplates) Lookup(name string) (tpl.Template, bool) {
|
2017-03-27 14:43:49 -04:00
|
|
|
templ := t.lookup(name)
|
|
|
|
if templ == nil {
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
return nil, false
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
return templ, true
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *htmlTemplates) LookupVariant(name string, variants tpl.TemplateVariants) (tpl.Template, bool, bool) {
|
|
|
|
return t.handler.LookupVariant(name, variants)
|
|
|
|
}
|
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
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *htmlTemplates) addLateTemplate(name, tpl string) error {
|
|
|
|
_, err := t.addTemplateIn(t.clone, name, tpl)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *htmlTemplates) addTemplate(name, tpl string) (*templateContext, error) {
|
|
|
|
return t.addTemplateIn(t.t, name, tpl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *htmlTemplates) addTemplateIn(tt *template.Template, name, tpl string) (*templateContext, error) {
|
|
|
|
templ, err := tt.New(name).Parse(tpl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
typ := resolveTemplateType(name)
|
|
|
|
|
|
|
|
c, err := applyTemplateTransformersToHMLTTemplate(typ, templ)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
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
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
for k := range c.notFound {
|
|
|
|
t.transformNotFound[k] = true
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
if typ == templateShortcode {
|
|
|
|
t.handler.addShortcodeVariant(name, c.Info, templ)
|
|
|
|
} else {
|
|
|
|
t.handler.templateInfo[name] = c.Info
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
return c, nil
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *htmlTemplates) handleMaster(name, overlayFilename, masterFilename string, onMissing func(filename string) (templateInfo, error)) error {
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
masterTpl := t.lookup(masterFilename)
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
if masterTpl == nil {
|
|
|
|
templ, err := onMissing(masterFilename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
masterTpl, err = t.t.New(overlayFilename).Parse(templ.template)
|
|
|
|
if err != nil {
|
|
|
|
return templ.errWithFileContext("parse master failed", err)
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
templ, err := onMissing(overlayFilename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
overlayTpl, err := template.Must(masterTpl.Clone()).Parse(templ.template)
|
|
|
|
if err != nil {
|
|
|
|
return templ.errWithFileContext("parse failed", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The extra lookup is a workaround, see
|
|
|
|
// * https://github.com/golang/go/issues/16101
|
|
|
|
// * https://github.com/gohugoio/hugo/issues/2549
|
|
|
|
overlayTpl = overlayTpl.Lookup(overlayTpl.Name())
|
|
|
|
if _, err := applyTemplateTransformersToHMLTTemplate(templateUndefined, overlayTpl); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
t.overlays[name] = overlayTpl
|
|
|
|
t.nameBaseTemplateName[name] = masterFilename
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *htmlTemplates) lookup(name string) *template.Template {
|
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
|
|
|
// Need to check in the overlay registry first as it will also be found below.
|
2017-02-17 07:30:50 -05:00
|
|
|
if t.overlays != nil {
|
|
|
|
if templ, ok := t.overlays[name]; ok {
|
|
|
|
return templ
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
if templ := t.t.Lookup(name); templ != nil {
|
|
|
|
return templ
|
|
|
|
}
|
|
|
|
|
2017-02-17 07:30:50 -05:00
|
|
|
if t.clone != nil {
|
2017-03-27 14:43:49 -04:00
|
|
|
return t.clone.Lookup(name)
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t htmlTemplates) withNewHandler(h *templateHandler) *htmlTemplates {
|
|
|
|
t.templatesCommon = t.templatesCommon.withNewHandler(h)
|
|
|
|
return &t
|
|
|
|
}
|
|
|
|
|
|
|
|
type nopLookupVariant int
|
|
|
|
|
|
|
|
func (l nopLookupVariant) LookupVariant(name string, variants tpl.TemplateVariants) (tpl.Template, bool, bool) {
|
|
|
|
return nil, false, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// templateHandler holds the templates in play.
|
|
|
|
// It implements the templateLoader and tpl.TemplateHandler interfaces.
|
|
|
|
// There is one templateHandler created per Site.
|
|
|
|
type templateHandler struct {
|
|
|
|
executor texttemplate.Executer
|
|
|
|
funcs map[string]reflect.Value
|
|
|
|
|
|
|
|
// This is the filesystem to load the templates from. All the templates are
|
|
|
|
// stored in the root of this filesystem.
|
|
|
|
layoutsFs afero.Fs
|
|
|
|
|
|
|
|
*deps.Deps
|
|
|
|
|
|
|
|
*templateHandlerCommon
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddLateTemplate is used to add a template late, i.e. after the
|
|
|
|
// regular templates have started its execution.
|
|
|
|
func (t *templateHandler) AddLateTemplate(name, tpl string) error {
|
|
|
|
h := t.getTemplateHandler(name)
|
|
|
|
if err := h.addLateTemplate(name, tpl); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// AddTemplate parses and adds a template to the collection.
|
|
|
|
// Templates with name prefixed with "_text" will be handled as plain
|
|
|
|
// text templates.
|
|
|
|
// TODO(bep) clean up these addTemplate variants
|
|
|
|
func (t *templateHandler) AddTemplate(name, tpl string) error {
|
|
|
|
h := t.getTemplateHandler(name)
|
|
|
|
_, err := h.addTemplate(name, tpl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2017-02-21 07:55:08 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) Debug() {
|
|
|
|
fmt.Println("HTML templates:\n", t.html.t.DefinedTemplates())
|
|
|
|
fmt.Println("\n\nText templates:\n", t.text.t.DefinedTemplates())
|
2017-04-30 15:52:56 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) Execute(templ tpl.Template, wr io.Writer, data interface{}) error {
|
|
|
|
if t.Metrics != nil {
|
|
|
|
defer t.Metrics.MeasureSince(templ.Name(), time.Now())
|
|
|
|
}
|
|
|
|
|
|
|
|
execErr := t.executor.Execute(templ, wr, data)
|
|
|
|
if execErr != nil {
|
|
|
|
execErr = t.addFileContext(templ.Name(), execErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return execErr
|
|
|
|
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) GetFunc(name string) (reflect.Value, bool) {
|
|
|
|
v, found := t.funcs[name]
|
|
|
|
return v, found
|
|
|
|
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
// LoadTemplates loads the templates from the layouts filesystem.
|
2017-03-27 14:43:49 -04:00
|
|
|
// A prefix can be given to indicate a template namespace to load the templates
|
|
|
|
// into, i.e. "_internal" etc.
|
2018-10-03 08:58:09 -04:00
|
|
|
func (t *templateHandler) LoadTemplates(prefix string) error {
|
|
|
|
return t.loadTemplates(prefix)
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2017-04-02 08:20:34 -04:00
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// Lookup tries to find a template with the given name in both template
|
|
|
|
// collections: First HTML, then the plain text template collection.
|
|
|
|
func (t *templateHandler) Lookup(name string) (tpl.Template, bool) {
|
|
|
|
if strings.HasPrefix(name, textTmplNamePrefix) {
|
|
|
|
// The caller has explicitly asked for a text template, so only look
|
|
|
|
// in the text template collection.
|
|
|
|
// The templates are stored without the prefix identificator.
|
|
|
|
name = strings.TrimPrefix(name, textTmplNamePrefix)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
return t.applyTemplateInfo(t.text.Lookup(name))
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// Look in both
|
|
|
|
if te, found := t.html.Lookup(name); found {
|
|
|
|
return t.applyTemplateInfo(te, true)
|
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
return t.applyTemplateInfo(t.text.Lookup(name))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// This currently only applies to shortcodes and what we get here is the
|
|
|
|
// shortcode name.
|
|
|
|
func (t *templateHandler) LookupVariant(name string, variants tpl.TemplateVariants) (tpl.Template, bool, bool) {
|
|
|
|
name = templateBaseName(templateShortcode, name)
|
|
|
|
s, found := t.shortcodes[name]
|
|
|
|
if !found {
|
|
|
|
return nil, false, false
|
2019-04-16 03:13:55 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
sv, found := s.fromVariants(variants)
|
|
|
|
if !found {
|
|
|
|
return nil, false, false
|
2017-04-02 08:20:34 -04:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
more := len(s.variants) > 1
|
|
|
|
|
|
|
|
return &tpl.TemplateInfo{
|
|
|
|
Template: sv.templ,
|
|
|
|
Info: sv.info,
|
|
|
|
}, true, more
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkReady marks the templates as "ready for execution". No changes allowed
|
|
|
|
// after this is set.
|
|
|
|
// TODO(bep) if this proves to be resource heavy, we could detect
|
|
|
|
// earlier if we really need this, or make it lazy.
|
|
|
|
func (t *templateHandler) MarkReady() error {
|
|
|
|
if err := t.postTransform(); err != nil {
|
|
|
|
return err
|
2017-05-06 14:15:28 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
if t.html.clone == nil {
|
|
|
|
t.html.clone = template.Must(t.html.t.Clone())
|
|
|
|
t.html.cloneClone = template.Must(t.html.clone.Clone())
|
|
|
|
}
|
|
|
|
if t.text.clone == nil {
|
|
|
|
t.text.clone = texttemplate.Must(t.text.t.Clone())
|
|
|
|
t.text.cloneClone = texttemplate.Must(t.text.clone.Clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// RebuildClone rebuilds the cloned templates. Used for live-reloads.
|
|
|
|
func (t *templateHandler) RebuildClone() {
|
|
|
|
if t.html != nil && t.html.cloneClone != nil {
|
|
|
|
t.html.clone = template.Must(t.html.cloneClone.Clone())
|
|
|
|
}
|
|
|
|
if t.text != nil && t.text.cloneClone != nil {
|
|
|
|
t.text.clone = texttemplate.Must(t.text.cloneClone.Clone())
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (h *templateHandler) initTemplateExecuter() {
|
|
|
|
exec, funcs := newTemplateExecuter(h.Deps)
|
|
|
|
h.executor = exec
|
|
|
|
h.funcs = funcs
|
|
|
|
funcMap := make(map[string]interface{})
|
|
|
|
for k, v := range funcs {
|
|
|
|
funcMap[k] = v.Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that these funcs are not the ones getting called
|
|
|
|
// on execution, but they are needed at parse time.
|
|
|
|
h.text.textTemplate.t.Funcs(funcMap)
|
|
|
|
h.text.standalone.t.Funcs(funcMap)
|
|
|
|
h.html.t.Funcs(funcMap)
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) getTemplateHandler(name string) templateLoader {
|
|
|
|
if strings.HasPrefix(name, textTmplNamePrefix) {
|
|
|
|
return t.text
|
|
|
|
}
|
|
|
|
return t.html
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) addFileContext(name string, inerr error) error {
|
|
|
|
if strings.HasPrefix(name, "_internal") {
|
|
|
|
return inerr
|
|
|
|
}
|
|
|
|
|
|
|
|
f, realFilename, err := t.fileAndFilename(name)
|
|
|
|
if err != nil {
|
|
|
|
return inerr
|
|
|
|
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
master, hasMaster := t.html.nameBaseTemplateName[name]
|
|
|
|
|
|
|
|
ferr := errors.Wrap(inerr, "execute of template failed")
|
|
|
|
|
|
|
|
// Since this can be a composite of multiple template files (single.html + baseof.html etc.)
|
|
|
|
// we potentially need to look in both -- and cannot rely on line number alone.
|
|
|
|
lineMatcher := func(m herrors.LineMatcher) bool {
|
|
|
|
if m.Position.LineNumber != m.LineNumber {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !hasMaster {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
identifiers := t.extractIdentifiers(m.Error.Error())
|
|
|
|
|
|
|
|
for _, id := range identifiers {
|
|
|
|
if strings.Contains(m.Line, id) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fe, ok := herrors.WithFileContext(ferr, realFilename, f, lineMatcher)
|
|
|
|
if ok || !hasMaster {
|
|
|
|
return fe
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the base template if relevant
|
|
|
|
f, realFilename, err = t.fileAndFilename(master)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
fe, ok = herrors.WithFileContext(ferr, realFilename, f, lineMatcher)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
// Return the most specific.
|
|
|
|
return ferr
|
|
|
|
|
|
|
|
}
|
|
|
|
return fe
|
|
|
|
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) addInternalTemplate(name, tpl string) error {
|
|
|
|
return t.AddTemplate("_internal/"+name, tpl)
|
|
|
|
}
|
2019-01-31 05:53:21 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) addShortcodeVariant(name string, info tpl.Info, templ tpl.Template) {
|
|
|
|
base := templateBaseName(templateShortcode, name)
|
|
|
|
|
|
|
|
shortcodename, variants := templateNameAndVariants(base)
|
|
|
|
|
|
|
|
templs, found := t.shortcodes[shortcodename]
|
|
|
|
if !found {
|
|
|
|
templs = &shortcodeTemplates{}
|
|
|
|
t.shortcodes[shortcodename] = templs
|
|
|
|
}
|
|
|
|
|
|
|
|
sv := shortcodeVariant{variants: variants, info: info, templ: templ}
|
|
|
|
|
|
|
|
i := templs.indexOf(variants)
|
|
|
|
|
|
|
|
if i != -1 {
|
|
|
|
// Only replace if it's an override of an internal template.
|
|
|
|
if !isInternal(name) {
|
|
|
|
templs.variants[i] = sv
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
templs.variants = append(templs.variants, sv)
|
|
|
|
}
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) addTemplateFile(name, baseTemplatePath, path string) error {
|
|
|
|
t.checkState()
|
2019-01-31 05:53:21 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
t.Log.DEBUG.Printf("Add template file: name %q, baseTemplatePath %q, path %q", name, baseTemplatePath, path)
|
|
|
|
|
|
|
|
getTemplate := func(filename string) (templateInfo, error) {
|
|
|
|
fs := t.Layouts.Fs
|
|
|
|
b, err := afero.ReadFile(fs, filename)
|
|
|
|
if err != nil {
|
|
|
|
return templateInfo{filename: filename, fs: fs}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := removeLeadingBOM(string(b))
|
|
|
|
|
|
|
|
realFilename := filename
|
|
|
|
if fi, err := fs.Stat(filename); err == nil {
|
|
|
|
if fim, ok := fi.(hugofs.FileMetaInfo); ok {
|
|
|
|
realFilename = fim.Meta().Filename()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return templateInfo{template: s, filename: filename, realFilename: realFilename, fs: fs}, nil
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// get the suffix and switch on that
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
switch ext {
|
|
|
|
case ".amber":
|
|
|
|
helpers.Deprecated("Amber templates are no longer supported.", "Use Go templates or a Hugo version <= 0.60.", true)
|
|
|
|
return nil
|
|
|
|
case ".ace":
|
|
|
|
helpers.Deprecated("ACE templates are no longer supported.", "Use Go templates or a Hugo version <= 0.60.", true)
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
|
|
|
|
if baseTemplatePath != "" {
|
|
|
|
return t.handleMaster(name, path, baseTemplatePath, getTemplate)
|
|
|
|
}
|
|
|
|
|
|
|
|
templ, err := getTemplate(path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.AddTemplate(name, templ.template)
|
|
|
|
if err != nil {
|
|
|
|
return templ.errWithFileContext("parse failed", err)
|
|
|
|
}
|
|
|
|
return nil
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) applyTemplateInfo(templ tpl.Template, found bool) (tpl.Template, bool) {
|
|
|
|
if adapter, ok := templ.(*tpl.TemplateInfo); ok {
|
|
|
|
if adapter.Info.IsZero() {
|
|
|
|
if info, found := t.templateInfo[templ.Name()]; found {
|
|
|
|
adapter.Info = info
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if templ != nil {
|
|
|
|
if info, found := t.templateInfo[templ.Name()]; found {
|
|
|
|
return &tpl.TemplateInfo{
|
|
|
|
Template: templ,
|
|
|
|
Info: info,
|
|
|
|
}, true
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
return templ, found
|
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) checkState() {
|
|
|
|
if t.html.clone != nil || t.text.clone != nil {
|
|
|
|
panic("template is cloned and cannot be modfified")
|
2019-04-16 03:13:55 -04:00
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
2019-04-16 03:13:55 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) clone(d *deps.Deps) *templateHandler {
|
|
|
|
c := &templateHandler{
|
|
|
|
Deps: d,
|
|
|
|
layoutsFs: d.BaseFs.Layouts.Fs,
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
c.templateHandlerCommon = t.templateHandlerCommon.withNewHandler(c)
|
|
|
|
d.Tmpl = c
|
|
|
|
d.TextTmpl = c.wrapTextTemplate(c.text.standalone)
|
|
|
|
c.executor, c.funcs = newTemplateExecuter(d)
|
|
|
|
|
|
|
|
return c
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *templateHandler) extractIdentifiers(line string) []string {
|
|
|
|
m := identifiersRe.FindAllStringSubmatch(line, -1)
|
|
|
|
identifiers := make([]string, len(m))
|
|
|
|
for i := 0; i < len(m); i++ {
|
|
|
|
identifiers[i] = m[i][1]
|
2017-05-06 14:15:28 -04:00
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
return identifiers
|
|
|
|
}
|
2017-05-06 14:15:28 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) fileAndFilename(name string) (afero.File, string, error) {
|
|
|
|
fs := t.layoutsFs
|
|
|
|
filename := filepath.FromSlash(name)
|
|
|
|
|
|
|
|
fi, err := fs.Stat(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
fim := fi.(hugofs.FileMetaInfo)
|
|
|
|
meta := fim.Meta()
|
|
|
|
|
|
|
|
f, err := meta.Open()
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", errors.Wrapf(err, "failed to open template file %q:", filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f, meta.Filename(), nil
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) handleMaster(name, overlayFilename, masterFilename string, onMissing func(filename string) (templateInfo, error)) error {
|
|
|
|
h := t.getTemplateHandler(name)
|
|
|
|
return h.handleMaster(name, overlayFilename, masterFilename, onMissing)
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) loadEmbedded() error {
|
|
|
|
for _, kv := range embedded.EmbeddedTemplates {
|
|
|
|
name, templ := kv[0], kv[1]
|
|
|
|
if err := t.addInternalTemplate(name, templ); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if aliases, found := embeddedTemplatesAliases[name]; found {
|
|
|
|
for _, alias := range aliases {
|
|
|
|
if err := t.addInternalTemplate(alias, templ); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) loadTemplates(prefix string) error {
|
|
|
|
|
|
|
|
walker := func(path string, fi hugofs.FileMetaInfo, err error) error {
|
|
|
|
if err != nil || fi.IsDir() {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if isDotFile(path) || isBackupFile(path) || isBaseTemplate(path) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
workingDir := t.PathSpec.WorkingDir
|
|
|
|
|
|
|
|
descriptor := output.TemplateLookupDescriptor{
|
|
|
|
WorkingDir: workingDir,
|
|
|
|
RelPath: path,
|
|
|
|
Prefix: prefix,
|
|
|
|
OutputFormats: t.OutputFormatsConfig,
|
|
|
|
FileExists: func(filename string) (bool, error) {
|
|
|
|
return helpers.Exists(filename, t.Layouts.Fs)
|
|
|
|
},
|
|
|
|
ContainsAny: func(filename string, subslices [][]byte) (bool, error) {
|
|
|
|
return helpers.FileContainsAny(filename, subslices, t.Layouts.Fs)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tplID, err := output.CreateTemplateNames(descriptor)
|
|
|
|
if err != nil {
|
|
|
|
t.Log.ERROR.Printf("Failed to resolve template in path %q: %s", path, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.addTemplateFile(tplID.Name, tplID.MasterFilename, tplID.OverlayFilename); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := helpers.SymbolicWalk(t.Layouts.Fs, "", walker); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2019-04-16 03:13:55 -04:00
|
|
|
func (t *templateHandler) postTransform() error {
|
|
|
|
if len(t.html.transformNotFound) == 0 && len(t.text.transformNotFound) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
t.text.transformNotFound = make(map[string]bool)
|
|
|
|
t.html.transformNotFound = make(map[string]bool)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for _, s := range []struct {
|
|
|
|
lookup func(name string) *parse.Tree
|
|
|
|
transformNotFound map[string]bool
|
|
|
|
}{
|
|
|
|
// html templates
|
|
|
|
{func(name string) *parse.Tree {
|
|
|
|
templ := t.html.lookup(name)
|
|
|
|
if templ == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return templ.Tree
|
|
|
|
}, t.html.transformNotFound},
|
|
|
|
// text templates
|
|
|
|
{func(name string) *parse.Tree {
|
|
|
|
templT := t.text.lookup(name)
|
|
|
|
if templT == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return templT.Tree
|
|
|
|
}, t.text.transformNotFound},
|
|
|
|
} {
|
2019-08-02 10:37:28 -04:00
|
|
|
for name := range s.transformNotFound {
|
2019-04-16 03:13:55 -04:00
|
|
|
templ := s.lookup(name)
|
|
|
|
if templ != nil {
|
|
|
|
_, err := applyTemplateTransformers(templateUndefined, templ, s.lookup)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) wrapTextTemplate(tt *textTemplate) tpl.TemplateParseFinder {
|
|
|
|
return struct {
|
|
|
|
tpl.TemplateParser
|
|
|
|
tpl.TemplateLookup
|
|
|
|
tpl.TemplateLookupVariant
|
|
|
|
}{
|
|
|
|
tt,
|
|
|
|
tt,
|
|
|
|
new(nopLookupVariant),
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
type templateHandlerCommon struct {
|
|
|
|
// shortcodes maps shortcode name to template variants
|
|
|
|
// (language, output format etc.) of that shortcode.
|
|
|
|
shortcodes map[string]*shortcodeTemplates
|
2019-04-16 03:13:55 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// templateInfo maps template name to some additional information about that template.
|
|
|
|
// Note that for shortcodes that same information is embedded in the
|
|
|
|
// shortcodeTemplates type.
|
|
|
|
templateInfo map[string]tpl.Info
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// text holds all the pure text templates.
|
|
|
|
text *textTemplates
|
|
|
|
html *htmlTemplates
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t templateHandlerCommon) withNewHandler(h *templateHandler) *templateHandlerCommon {
|
|
|
|
t.text = t.text.withNewHandler(h)
|
|
|
|
t.html = t.html.withNewHandler(h)
|
|
|
|
return &t
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
type templateLoader interface {
|
|
|
|
addLateTemplate(name, tpl string) error
|
|
|
|
addTemplate(name, tpl string) (*templateContext, error)
|
|
|
|
handleMaster(name, overlayFilename, masterFilename string, onMissing func(filename string) (templateInfo, error)) error
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// Shared by both HTML and text templates.
|
|
|
|
type templatesCommon struct {
|
|
|
|
handler *templateHandler
|
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
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// Used to get proper filenames in errors
|
|
|
|
nameBaseTemplateName map[string]string
|
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
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// Holds names of the templates not found during the first AST transformation
|
|
|
|
// pass.
|
|
|
|
transformNotFound map[string]bool
|
|
|
|
}
|
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
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t templatesCommon) withNewHandler(h *templateHandler) *templatesCommon {
|
|
|
|
t.handler = h
|
|
|
|
return &t
|
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
type textTemplate struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
t *texttemplate.Template
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *textTemplate) Lookup(name string) (tpl.Template, bool) {
|
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
tpl := t.t.Lookup(name)
|
|
|
|
return tpl, tpl != nil
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *textTemplate) Parse(name, tpl string) (tpl.Template, error) {
|
|
|
|
return t.parseIn(t.t, name, tpl)
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *textTemplate) parseIn(tt *texttemplate.Template, name, tpl string) (*texttemplate.Template, error) {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
templ, err := tt.New(name).Parse(tpl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
if _, err := applyTemplateTransformersToTextTemplate(templateUndefined, templ); err != nil {
|
|
|
|
return nil, err
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
return templ, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type textTemplates struct {
|
|
|
|
*templatesCommon
|
|
|
|
*textTemplate
|
|
|
|
standalone *textTemplate
|
|
|
|
clone *texttemplate.Template
|
|
|
|
cloneClone *texttemplate.Template
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
overlays map[string]*texttemplate.Template
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *textTemplates) Lookup(name string) (tpl.Template, bool) {
|
|
|
|
templ := t.lookup(name)
|
|
|
|
if templ == nil {
|
|
|
|
return nil, false
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
return templ, true
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *textTemplates) LookupVariant(name string, variants tpl.TemplateVariants) (tpl.Template, bool, bool) {
|
|
|
|
return t.handler.LookupVariant(name, variants)
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *textTemplates) addLateTemplate(name, tpl string) error {
|
|
|
|
_, err := t.addTemplateIn(t.clone, name, tpl)
|
|
|
|
return err
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *textTemplates) addTemplate(name, tpl string) (*templateContext, error) {
|
|
|
|
return t.addTemplateIn(t.t, name, tpl)
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *textTemplates) addTemplateIn(tt *texttemplate.Template, name, tpl string) (*templateContext, error) {
|
|
|
|
name = strings.TrimPrefix(name, textTmplNamePrefix)
|
|
|
|
templ, err := t.parseIn(tt, name, tpl)
|
2017-02-17 07:30:50 -05:00
|
|
|
if err != nil {
|
2019-12-10 13:56:44 -05:00
|
|
|
return nil, err
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
typ := resolveTemplateType(name)
|
|
|
|
|
|
|
|
c, err := applyTemplateTransformersToTextTemplate(typ, templ)
|
2017-02-17 07:30:50 -05:00
|
|
|
if err != nil {
|
2019-12-10 13:56:44 -05:00
|
|
|
return nil, err
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
for k := range c.notFound {
|
|
|
|
t.transformNotFound[k] = true
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
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
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
if typ == templateShortcode {
|
|
|
|
t.handler.addShortcodeVariant(name, c.Info, templ)
|
|
|
|
} else {
|
|
|
|
t.handler.templateInfo[name] = c.Info
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
return c, nil
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
func (t *textTemplates) handleMaster(name, overlayFilename, masterFilename string, onMissing func(filename string) (templateInfo, error)) error {
|
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
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
name = strings.TrimPrefix(name, textTmplNamePrefix)
|
|
|
|
masterTpl := t.lookup(masterFilename)
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
if masterTpl == nil {
|
|
|
|
templ, err := onMissing(masterFilename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
masterTpl, err = t.t.New(masterFilename).Parse(templ.template)
|
2017-03-27 14:43:49 -04:00
|
|
|
if err != nil {
|
2018-10-03 08:58:09 -04:00
|
|
|
return errors.Wrapf(err, "failed to parse %q:", templ.filename)
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
t.nameBaseTemplateName[masterFilename] = templ.filename
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
|
|
|
templ, err := onMissing(overlayFilename)
|
2017-02-17 07:30:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
overlayTpl, err := texttemplate.Must(masterTpl.Clone()).Parse(templ.template)
|
2017-02-17 07:30:50 -05:00
|
|
|
if err != nil {
|
2018-10-03 08:58:09 -04:00
|
|
|
return errors.Wrapf(err, "failed to parse %q:", templ.filename)
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
|
|
|
overlayTpl = overlayTpl.Lookup(overlayTpl.Name())
|
2019-04-02 04:30:24 -04:00
|
|
|
if _, err := applyTemplateTransformersToTextTemplate(templateUndefined, overlayTpl); err != nil {
|
2017-03-27 14:43:49 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.overlays[name] = overlayTpl
|
2018-10-03 08:58:09 -04:00
|
|
|
t.nameBaseTemplateName[name] = templ.filename
|
2017-03-27 14:43:49 -04:00
|
|
|
|
|
|
|
return err
|
|
|
|
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *textTemplates) lookup(name string) *texttemplate.Template {
|
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
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// Need to check in the overlay registry first as it will also be found below.
|
|
|
|
if t.overlays != nil {
|
|
|
|
if templ, ok := t.overlays[name]; ok {
|
|
|
|
return templ
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
if templ := t.t.Lookup(name); templ != nil {
|
|
|
|
return templ
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2018-05-24 05:24:38 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
if t.clone != nil {
|
|
|
|
return t.clone.Lookup(name)
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
2018-05-04 11:53:56 -04:00
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
return nil
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t textTemplates) withNewHandler(h *templateHandler) *textTemplates {
|
|
|
|
t.templatesCommon = t.templatesCommon.withNewHandler(h)
|
|
|
|
return &t
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func isBackupFile(path string) bool {
|
|
|
|
return path[len(path)-1] == '~'
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func isBaseTemplate(path string) bool {
|
|
|
|
return strings.Contains(filepath.Base(path), baseFileBase)
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func isDotFile(path string) bool {
|
|
|
|
return filepath.Base(path)[0] == '.'
|
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func removeLeadingBOM(s string) string {
|
|
|
|
const bom = '\ufeff'
|
|
|
|
|
|
|
|
for i, r := range s {
|
|
|
|
if i == 0 && r != bom {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
if i > 0 {
|
|
|
|
return s[i:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// resolves _internal/shortcodes/param.html => param.html etc.
|
|
|
|
func templateBaseName(typ templateType, name string) string {
|
|
|
|
name = strings.TrimPrefix(name, internalPathPrefix)
|
|
|
|
switch typ {
|
|
|
|
case templateShortcode:
|
|
|
|
return strings.TrimPrefix(name, shortcodesPathPrefix)
|
|
|
|
default:
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
|
|
|
}
|