2017-05-01 03:06:42 -04:00
|
|
|
// Copyright 2017-present The Hugo Authors. All rights reserved.
|
2015-04-05 15:03:16 -04:00
|
|
|
//
|
2016-03-10 14:59:41 -05:00
|
|
|
// Portions Copyright The Go Authors.
|
|
|
|
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2015-04-05 15:03:16 -04:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-04-05 15:03:16 -04:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-02-17 07:30:50 -05:00
|
|
|
package tplimpl
|
2015-04-05 15:03:16 -04:00
|
|
|
|
|
|
|
import (
|
2022-02-17 10:51:19 -05:00
|
|
|
"context"
|
2019-12-10 13:56:44 -05:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
2022-03-08 04:06:12 -05:00
|
|
|
"github.com/gohugoio/hugo/common/hreflect"
|
2019-12-10 13:56:44 -05:00
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
2022-03-08 04:06:12 -05:00
|
|
|
"github.com/gohugoio/hugo/tpl"
|
2019-12-10 13:56:44 -05:00
|
|
|
|
|
|
|
template "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
|
|
|
|
texttemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate"
|
2015-05-26 06:33:32 -04: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
|
|
|
"github.com/gohugoio/hugo/deps"
|
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/tpl/internal"
|
2017-04-30 05:34:45 -04:00
|
|
|
|
|
|
|
// Init the namespaces
|
2017-06-13 12:42:45 -04:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/cast"
|
|
|
|
_ "github.com/gohugoio/hugo/tpl/collections"
|
|
|
|
_ "github.com/gohugoio/hugo/tpl/compare"
|
|
|
|
_ "github.com/gohugoio/hugo/tpl/crypto"
|
2023-02-21 12:32:09 -05:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/css"
|
2017-06-13 12:42:45 -04:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/data"
|
2020-07-02 03:04:46 -04:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/debug"
|
2022-02-17 07:04:00 -05:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/diagrams"
|
2017-06-13 12:42:45 -04:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/encoding"
|
|
|
|
_ "github.com/gohugoio/hugo/tpl/fmt"
|
2018-11-26 04:11:22 -05:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/hugo"
|
2017-06-13 12:42:45 -04:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/images"
|
|
|
|
_ "github.com/gohugoio/hugo/tpl/inflect"
|
2020-07-02 12:16:32 -04:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/js"
|
2017-06-13 12:42:45 -04:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/lang"
|
|
|
|
_ "github.com/gohugoio/hugo/tpl/math"
|
2020-06-30 10:11:05 -04:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/openapi/openapi3"
|
2017-06-13 12:42:45 -04:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/os"
|
|
|
|
_ "github.com/gohugoio/hugo/tpl/partials"
|
2018-04-15 14:54:22 -04:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/path"
|
2018-12-28 04:41:46 -05:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/reflect"
|
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
|
|
|
_ "github.com/gohugoio/hugo/tpl/resources"
|
2017-06-13 12:42:45 -04:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/safe"
|
2018-11-26 04:11:22 -05:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/site"
|
2017-06-13 12:42:45 -04:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/strings"
|
2018-07-31 05:35:38 -04:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/templates"
|
2017-06-13 12:42:45 -04:00
|
|
|
_ "github.com/gohugoio/hugo/tpl/time"
|
|
|
|
_ "github.com/gohugoio/hugo/tpl/transform"
|
|
|
|
_ "github.com/gohugoio/hugo/tpl/urls"
|
2017-03-13 18:55:02 -04:00
|
|
|
)
|
2016-10-10 18:03:30 -04:00
|
|
|
|
2020-12-02 07:23:25 -05:00
|
|
|
var (
|
2022-02-17 10:51:19 -05:00
|
|
|
_ texttemplate.ExecHelper = (*templateExecHelper)(nil)
|
|
|
|
zero reflect.Value
|
|
|
|
contextInterface = reflect.TypeOf((*context.Context)(nil)).Elem()
|
2020-12-02 07:23:25 -05:00
|
|
|
)
|
2019-12-10 13:56:44 -05:00
|
|
|
|
|
|
|
type templateExecHelper struct {
|
2020-09-07 09:07:10 -04:00
|
|
|
running bool // whether we're in server mode.
|
|
|
|
funcs map[string]reflect.Value
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
2022-02-17 10:51:19 -05:00
|
|
|
func (t *templateExecHelper) GetFunc(ctx context.Context, tmpl texttemplate.Preparer, name string) (fn reflect.Value, firstArg reflect.Value, found bool) {
|
2019-12-10 13:56:44 -05:00
|
|
|
if fn, found := t.funcs[name]; found {
|
2022-02-17 10:51:19 -05:00
|
|
|
if fn.Type().NumIn() > 0 {
|
|
|
|
first := fn.Type().In(0)
|
|
|
|
if first.Implements(contextInterface) {
|
|
|
|
// TODO(bep) check if we can void this conversion every time -- and if that matters.
|
|
|
|
// The first argument may be context.Context. This is never provided by the end user, but it's used to pass down
|
|
|
|
// contextual information, e.g. the top level data context (e.g. Page).
|
|
|
|
return fn, reflect.ValueOf(ctx), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn, zero, true
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
2022-02-17 10:51:19 -05:00
|
|
|
return zero, zero, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *templateExecHelper) Init(ctx context.Context, tmpl texttemplate.Preparer) {
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
2022-02-17 10:51:19 -05:00
|
|
|
func (t *templateExecHelper) GetMapValue(ctx context.Context, tmpl texttemplate.Preparer, receiver, key reflect.Value) (reflect.Value, bool) {
|
2019-12-10 13:56:44 -05:00
|
|
|
if params, ok := receiver.Interface().(maps.Params); ok {
|
|
|
|
// Case insensitive.
|
|
|
|
keystr := strings.ToLower(key.String())
|
|
|
|
v, found := params[keystr]
|
|
|
|
if !found {
|
|
|
|
return zero, false
|
|
|
|
}
|
|
|
|
return reflect.ValueOf(v), true
|
|
|
|
}
|
|
|
|
|
|
|
|
v := receiver.MapIndex(key)
|
|
|
|
|
|
|
|
return v, v.IsValid()
|
|
|
|
}
|
|
|
|
|
2022-02-17 10:51:19 -05:00
|
|
|
func (t *templateExecHelper) GetMethod(ctx context.Context, tmpl texttemplate.Preparer, receiver reflect.Value, name string) (method reflect.Value, firstArg reflect.Value) {
|
2020-09-07 09:07:10 -04:00
|
|
|
if t.running {
|
|
|
|
switch name {
|
|
|
|
case "GetPage", "Render":
|
|
|
|
if info, ok := tmpl.(tpl.Info); ok {
|
|
|
|
if m := receiver.MethodByName(name + "WithTemplateInfo"); m.IsValid() {
|
|
|
|
return m, reflect.ValueOf(info)
|
|
|
|
}
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 04:06:12 -05:00
|
|
|
fn := hreflect.GetMethodByName(receiver, name)
|
2022-02-17 10:51:19 -05:00
|
|
|
if !fn.IsValid() {
|
|
|
|
return zero, zero
|
|
|
|
}
|
|
|
|
|
|
|
|
if fn.Type().NumIn() > 0 {
|
|
|
|
first := fn.Type().In(0)
|
|
|
|
if first.Implements(contextInterface) {
|
|
|
|
// The first argument may be context.Context. This is never provided by the end user, but it's used to pass down
|
|
|
|
// contextual information, e.g. the top level data context (e.g. Page).
|
|
|
|
return fn, reflect.ValueOf(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn, zero
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func newTemplateExecuter(d *deps.Deps) (texttemplate.Executer, map[string]reflect.Value) {
|
|
|
|
funcs := createFuncMap(d)
|
|
|
|
funcsv := make(map[string]reflect.Value)
|
|
|
|
|
|
|
|
for k, v := range funcs {
|
2020-01-07 04:23:24 -05:00
|
|
|
vv := reflect.ValueOf(v)
|
|
|
|
funcsv[k] = vv
|
|
|
|
}
|
|
|
|
|
|
|
|
// Duplicate Go's internal funcs here for faster lookups.
|
|
|
|
for k, v := range template.GoFuncs {
|
|
|
|
if _, exists := funcsv[k]; !exists {
|
|
|
|
vv, ok := v.(reflect.Value)
|
|
|
|
if !ok {
|
|
|
|
vv = reflect.ValueOf(v)
|
|
|
|
}
|
|
|
|
funcsv[k] = vv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range texttemplate.GoFuncs {
|
|
|
|
if _, exists := funcsv[k]; !exists {
|
|
|
|
funcsv[k] = v
|
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
exeHelper := &templateExecHelper{
|
2020-09-07 09:07:10 -04:00
|
|
|
running: d.Running,
|
|
|
|
funcs: funcsv,
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return texttemplate.NewExecuter(
|
|
|
|
exeHelper,
|
|
|
|
), funcsv
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func createFuncMap(d *deps.Deps) map[string]any {
|
2017-05-01 03:06:42 -04:00
|
|
|
funcMap := template.FuncMap{}
|
2017-04-30 05:34:45 -04:00
|
|
|
|
|
|
|
// Merge the namespace funcs
|
|
|
|
for _, nsf := range internal.TemplateFuncsNamespaceRegistry {
|
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
|
|
|
ns := nsf(d)
|
2017-05-01 03:06:42 -04:00
|
|
|
if _, exists := funcMap[ns.Name]; exists {
|
|
|
|
panic(ns.Name + " is a duplicate template func")
|
|
|
|
}
|
2017-04-30 05:34:45 -04:00
|
|
|
funcMap[ns.Name] = ns.Context
|
2017-05-01 12:40:34 -04:00
|
|
|
for _, mm := range ns.MethodMappings {
|
|
|
|
for _, alias := range mm.Aliases {
|
|
|
|
if _, exists := funcMap[alias]; exists {
|
|
|
|
panic(alias + " is a duplicate template func")
|
|
|
|
}
|
|
|
|
funcMap[alias] = mm.Method
|
|
|
|
}
|
2017-04-30 05:34:45 -04:00
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
2017-01-09 19:36:59 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
if d.OverloadedTemplateFuncs != nil {
|
|
|
|
for k, v := range d.OverloadedTemplateFuncs {
|
|
|
|
funcMap[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 funcMap
|
|
|
|
}
|