2019-01-02 06:33:26 -05:00
// Copyright 2019 The Hugo Authors. All rights reserved.
2015-12-10 17:19:38 -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.
2014-02-25 23:57:31 -05:00
package hugolib
import (
2023-02-11 10:20:24 -05:00
"context"
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
"fmt"
2016-01-09 10:11:38 -05:00
"path/filepath"
2019-01-02 06:33:26 -05:00
"reflect"
2020-12-02 07:23:25 -05:00
"strings"
"testing"
2019-01-02 06:33:26 -05:00
2021-06-09 04:58:18 -04:00
"github.com/gohugoio/hugo/config"
2023-07-28 04:53:47 -04:00
"github.com/gohugoio/hugo/resources/kinds"
2019-08-16 09:55:03 -04:00
2019-01-02 06:33:26 -05:00
"github.com/gohugoio/hugo/parser/pageparser"
2019-08-10 15:05:17 -04:00
qt "github.com/frankban/quicktest"
2014-02-25 23:57:31 -05:00
)
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
func TestExtractShortcodes ( t * testing . T ) {
2019-01-02 06:33:26 -05:00
b := newTestSitesBuilder ( t ) . WithSimpleConfigFile ( )
b . WithTemplates (
"default/single.html" , ` EMPTY ` ,
"_internal/shortcodes/tag.html" , ` tag ` ,
"_internal/shortcodes/legacytag.html" , ` {{ $_hugo_config := "{ \"version\": 1 }" }} tag ` ,
"_internal/shortcodes/sc1.html" , ` sc1 ` ,
"_internal/shortcodes/sc2.html" , ` sc2 ` ,
"_internal/shortcodes/inner.html" , ` {{ with .Inner }} {{ . }} {{ end }} ` ,
"_internal/shortcodes/inner2.html" , ` {{ .Inner }} ` ,
"_internal/shortcodes/inner3.html" , ` {{ .Inner }} ` ,
) . WithContent ( "page.md" , ` -- -
title : "Shortcodes Galore!"
-- -
` )
2018-10-18 04:21:23 -04:00
2019-01-02 06:33:26 -05:00
b . CreateSites ( ) . Build ( BuildCfg { } )
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
2019-01-02 06:33:26 -05:00
s := b . H . Sites [ 0 ]
2018-10-18 04:21:23 -04:00
2019-01-02 06:33:26 -05:00
// Make it more regexp friendly
strReplacer := strings . NewReplacer ( "[" , "{" , "]" , "}" )
2017-01-09 19:36:59 -05:00
2019-01-02 06:33:26 -05:00
str := func ( s * shortcode ) string {
if s == nil {
return "<nil>"
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
}
2019-11-27 07:42:36 -05:00
var version int
if s . info != nil {
version = s . info . ParseInfo ( ) . Config . Version
}
2019-01-02 06:33:26 -05:00
return strReplacer . Replace ( fmt . Sprintf ( "%s;inline:%t;closing:%t;inner:%v;params:%v;ordinal:%d;markup:%t;version:%d;pos:%d" ,
2019-11-27 07:42:36 -05:00
s . name , s . isInline , s . isClosing , s . inner , s . params , s . ordinal , s . doMarkup , version , s . pos ) )
2019-01-02 06:33:26 -05:00
}
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
2019-08-10 15:05:17 -04:00
regexpCheck := func ( re string ) func ( c * qt . C , shortcode * shortcode , err error ) {
return func ( c * qt . C , shortcode * shortcode , err error ) {
c . Assert ( err , qt . IsNil )
c . Assert ( str ( shortcode ) , qt . Matches , ".*" + re + ".*" )
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
}
2019-01-02 06:33:26 -05:00
}
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
2019-01-02 06:33:26 -05:00
for _ , test := range [ ] struct {
name string
input string
2019-08-10 15:05:17 -04:00
check func ( c * qt . C , shortcode * shortcode , err error )
2019-01-02 06:33:26 -05:00
} {
{ "one shortcode, no markup" , "{{< tag >}}" , regexpCheck ( "tag.*closing:false.*markup:false" ) } ,
{ "one shortcode, markup" , "{{% tag %}}" , regexpCheck ( "tag.*closing:false.*markup:true;version:2" ) } ,
{ "one shortcode, markup, legacy" , "{{% legacytag %}}" , regexpCheck ( "tag.*closing:false.*markup:true;version:1" ) } ,
{ "outer shortcode markup" , "{{% inner %}}{{< tag >}}{{% /inner %}}" , regexpCheck ( "inner.*closing:true.*markup:true" ) } ,
{ "inner shortcode markup" , "{{< inner >}}{{% tag %}}{{< /inner >}}" , regexpCheck ( "inner.*closing:true.*;markup:false;version:2" ) } ,
{ "one pos param" , "{{% tag param1 %}}" , regexpCheck ( "tag.*params:{param1}" ) } ,
{ "two pos params" , "{{< tag param1 param2>}}" , regexpCheck ( "tag.*params:{param1 param2}" ) } ,
{ "one named param" , ` {{ % tag param1 = "value" % }} ` , regexpCheck ( "tag.*params:map{param1:value}" ) } ,
{ "two named params" , ` {{ < tag param1 = "value1" param2 = "value2" > }} ` , regexpCheck ( "tag.*params:map{param\\d:value\\d param\\d:value\\d}" ) } ,
{ "inner" , ` {{ < inner > }} Inner Content {{ < / inner > }} ` , regexpCheck ( "inner;inline:false;closing:true;inner:{Inner Content};" ) } ,
// issue #934
{ "inner self-closing" , ` {{ < inner / > }} ` , regexpCheck ( "inner;.*inner:{}" ) } ,
2020-12-02 07:23:25 -05:00
{
"nested inner" , ` {{ < inner > }} Inner Content-> {{ % inner2 param1 % }} inner2txt {{ % / inner2 % }} Inner close-> {{ < / inner > }} ` ,
regexpCheck ( "inner;.*inner:{Inner Content->.*Inner close->}" ) ,
} ,
{
"nested, nested inner" , ` {{ < inner > }} inner2-> {{ % inner2 param1 % }} inner2txt->inner3 {{ < inner3 > }} inner3txt {{ < / inner3 > }} {{ % / inner2 % }} final close-> {{ < / inner > }} ` ,
regexpCheck ( "inner:{inner2-> inner2.*{{inner2txt->inner3.*final close->}" ) ,
} ,
2019-01-02 06:33:26 -05:00
{ "closed without content" , ` {{ < inner param1 > }} {{ < / inner > }} ` , regexpCheck ( "inner.*inner:{}" ) } ,
{ "inline" , ` {{ < my .inline > }} Hi {{ < / my .inline > }} ` , regexpCheck ( "my.inline;inline:true;closing:true;inner:{Hi};" ) } ,
} {
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
test := test
2019-01-02 06:33:26 -05:00
t . Run ( test . name , func ( t * testing . T ) {
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
t . Parallel ( )
2019-08-10 15:05:17 -04:00
c := qt . New ( t )
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
2019-01-02 06:33:26 -05:00
p , err := pageparser . ParseMain ( strings . NewReader ( test . input ) , pageparser . Config { } )
2019-08-10 15:05:17 -04:00
c . Assert ( err , qt . IsNil )
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
handler := newShortcodeHandler ( "" , s )
2019-01-02 06:33:26 -05:00
iter := p . Iterator ( )
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
2022-07-07 10:11:47 -04:00
short , err := handler . extractShortcode ( 0 , 0 , p . Input ( ) , iter )
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
2019-08-10 15:05:17 -04:00
test . check ( c , short , err )
2019-01-02 06:33:26 -05:00
} )
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
}
}
2017-05-06 14:15:28 -04:00
func TestShortcodeMultipleOutputFormats ( t * testing . T ) {
t . Parallel ( )
siteConfig := `
baseURL = "http://example.com/blog"
2020-06-16 09:43:50 -04:00
disableKinds = [ "section" , "term" , "taxonomy" , "RSS" , "sitemap" , "robotsTXT" , "404" ]
2017-05-06 14:15:28 -04:00
2024-09-28 16:15:53 -04:00
[ pagination ]
pagerSize = 1
2017-05-06 14:15:28 -04:00
[ outputs ]
home = [ "HTML" , "AMP" , "Calendar" ]
page = [ "HTML" , "AMP" , "JSON" ]
`
pageTemplate := ` -- -
title : "%s"
-- -
# Doc
{ { < myShort > } }
{ { < noExt > } }
{ { % % onlyHTML % % } }
{ { < myInner > } } { { < myShort > } } { { < / myInner > } }
`
pageTemplateCSVOnly := ` -- -
title : "%s"
outputs : [ "CSV" ]
-- -
# Doc
CSV : { { < myShort > } }
`
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
b := newTestSitesBuilder ( t ) . WithConfigFile ( "toml" , siteConfig )
b . WithTemplates (
2017-05-06 14:15:28 -04:00
"layouts/_default/single.html" , ` Single HTML: {{ .Title }} | {{ .Content }} ` ,
"layouts/_default/single.json" , ` Single JSON: {{ .Title }} | {{ .Content }} ` ,
"layouts/_default/single.csv" , ` Single CSV: {{ .Title }} | {{ .Content }} ` ,
"layouts/index.html" , ` Home HTML: {{ .Title }} | {{ .Content }} ` ,
"layouts/index.amp.html" , ` Home AMP: {{ .Title }} | {{ .Content }} ` ,
"layouts/index.ics" , ` Home Calendar: {{ .Title }} | {{ .Content }} ` ,
"layouts/shortcodes/myShort.html" , ` ShortHTML ` ,
"layouts/shortcodes/myShort.amp.html" , ` ShortAMP ` ,
"layouts/shortcodes/myShort.csv" , ` ShortCSV ` ,
"layouts/shortcodes/myShort.ics" , ` ShortCalendar ` ,
"layouts/shortcodes/myShort.json" , ` ShortJSON ` ,
"layouts/shortcodes/noExt" , ` ShortNoExt ` ,
"layouts/shortcodes/onlyHTML.html" , ` ShortOnlyHTML ` ,
"layouts/shortcodes/myInner.html" , ` myInner:-- {{- .Inner -}} -- ` ,
)
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
b . WithContent ( "_index.md" , fmt . Sprintf ( pageTemplate , "Home" ) ,
"sect/mypage.md" , fmt . Sprintf ( pageTemplate , "Single" ) ,
"sect/mycsvpage.md" , fmt . Sprintf ( pageTemplateCSVOnly , "Single CSV" ) ,
)
2017-05-06 14:15:28 -04:00
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
b . Build ( BuildCfg { } )
h := b . H
2019-08-10 15:05:17 -04:00
b . Assert ( len ( h . Sites ) , qt . Equals , 1 )
2017-05-06 14:15:28 -04:00
s := h . Sites [ 0 ]
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
home := s . getPageOldVersion ( kinds . KindHome )
2019-08-10 15:05:17 -04:00
b . Assert ( home , qt . Not ( qt . IsNil ) )
b . Assert ( len ( home . OutputFormats ( ) ) , qt . Equals , 3 )
2017-05-06 14:15:28 -04:00
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
b . AssertFileContent ( "public/index.html" ,
2017-05-06 14:15:28 -04:00
"Home HTML" ,
"ShortHTML" ,
"ShortNoExt" ,
"ShortOnlyHTML" ,
"myInner:--ShortHTML--" ,
)
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
b . AssertFileContent ( "public/amp/index.html" ,
2017-05-06 14:15:28 -04:00
"Home AMP" ,
"ShortAMP" ,
"ShortNoExt" ,
"ShortOnlyHTML" ,
"myInner:--ShortAMP--" ,
)
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
b . AssertFileContent ( "public/index.ics" ,
2017-05-06 14:15:28 -04:00
"Home Calendar" ,
"ShortCalendar" ,
"ShortNoExt" ,
"ShortOnlyHTML" ,
"myInner:--ShortCalendar--" ,
)
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
b . AssertFileContent ( "public/sect/mypage/index.html" ,
2017-05-06 14:15:28 -04:00
"Single HTML" ,
"ShortHTML" ,
"ShortNoExt" ,
"ShortOnlyHTML" ,
"myInner:--ShortHTML--" ,
)
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
b . AssertFileContent ( "public/sect/mypage/index.json" ,
2017-05-06 14:15:28 -04:00
"Single JSON" ,
"ShortJSON" ,
"ShortNoExt" ,
"ShortOnlyHTML" ,
"myInner:--ShortJSON--" ,
)
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
b . AssertFileContent ( "public/amp/sect/mypage/index.html" ,
2017-05-06 14:15:28 -04:00
// No special AMP template
"Single HTML" ,
"ShortAMP" ,
"ShortNoExt" ,
"ShortOnlyHTML" ,
"myInner:--ShortAMP--" ,
)
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
b . AssertFileContent ( "public/sect/mycsvpage/index.csv" ,
2017-05-06 14:15:28 -04:00
"Single CSV" ,
"ShortCSV" ,
)
}
2015-05-19 16:00:48 -04:00
func BenchmarkReplaceShortcodeTokens ( b * testing . B ) {
2015-11-13 15:21:03 -05:00
type input struct {
in [ ] byte
2023-02-11 10:20:24 -05:00
tokenHandler func ( ctx context . Context , token string ) ( [ ] byte , error )
2015-11-13 15:21:03 -05:00
expect [ ] byte
}
2015-05-19 16:00:48 -04:00
data := [ ] struct {
input string
replacements map [ string ] string
2015-11-13 15:21:03 -05:00
expect [ ] byte
2015-05-19 16:00:48 -04:00
} {
2016-09-08 15:23:01 -04:00
{ "Hello HAHAHUGOSHORTCODE-1HBHB." , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "World" } , [ ] byte ( "Hello World." ) } ,
{ strings . Repeat ( "A" , 100 ) + " HAHAHUGOSHORTCODE-1HBHB." , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "Hello World" } , [ ] byte ( strings . Repeat ( "A" , 100 ) + " Hello World." ) } ,
{ strings . Repeat ( "A" , 500 ) + " HAHAHUGOSHORTCODE-1HBHB." , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "Hello World" } , [ ] byte ( strings . Repeat ( "A" , 500 ) + " Hello World." ) } ,
{ strings . Repeat ( "ABCD " , 500 ) + " HAHAHUGOSHORTCODE-1HBHB." , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "Hello World" } , [ ] byte ( strings . Repeat ( "ABCD " , 500 ) + " Hello World." ) } ,
{ strings . Repeat ( "A " , 3000 ) + " HAHAHUGOSHORTCODE-1HBHB." + strings . Repeat ( "BC " , 1000 ) + " HAHAHUGOSHORTCODE-1HBHB." , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "Hello World" } , [ ] byte ( strings . Repeat ( "A " , 3000 ) + " Hello World." + strings . Repeat ( "BC " , 1000 ) + " Hello World." ) } ,
2015-11-13 15:21:03 -05:00
}
2020-12-02 07:23:25 -05:00
cnt := 0
2023-02-11 10:20:24 -05:00
in := make ( [ ] input , b . N * len ( data ) )
2015-11-13 15:21:03 -05:00
for i := 0 ; i < b . N ; i ++ {
for _ , this := range data {
2023-02-11 10:20:24 -05:00
replacements := make ( map [ string ] shortcodeRenderer )
for k , v := range this . replacements {
replacements [ k ] = prerenderedShortcode { s : v }
}
tokenHandler := func ( ctx context . Context , token string ) ( [ ] byte , error ) {
return [ ] byte ( this . replacements [ token ] ) , nil
}
in [ cnt ] = input { [ ] byte ( this . input ) , tokenHandler , this . expect }
2015-11-13 15:21:03 -05:00
cnt ++
}
2015-05-19 16:00:48 -04:00
}
2015-11-13 15:21:03 -05:00
2015-05-19 16:00:48 -04:00
b . ResetTimer ( )
2015-11-13 15:21:03 -05:00
cnt = 0
2023-02-11 10:20:24 -05:00
ctx := context . Background ( )
2015-05-19 16:00:48 -04:00
for i := 0 ; i < b . N ; i ++ {
2015-11-13 15:21:03 -05:00
for j := range data {
currIn := in [ cnt ]
cnt ++
2023-02-11 10:20:24 -05:00
results , err := expandShortcodeTokens ( ctx , currIn . in , currIn . tokenHandler )
2015-11-13 15:21:03 -05:00
if err != nil {
b . Fatalf ( "[%d] failed: %s" , i , err )
continue
}
if len ( results ) != len ( currIn . expect ) {
b . Fatalf ( "[%d] replaceShortcodeTokens, got \n%q but expected \n%q" , j , results , currIn . expect )
2015-05-19 16:00:48 -04:00
}
}
}
}
2022-05-29 09:14:32 -04:00
func BenchmarkShortcodesInSite ( b * testing . B ) {
files := `
-- config . toml --
-- layouts / shortcodes / mark1 . md --
{ { . Inner } }
-- layouts / shortcodes / mark2 . md --
1. Item Mark2 1
1. Item Mark2 2
1. Item Mark2 2 - 1
1. Item Mark2 3
-- layouts / _default / single . html --
{ { . Content } }
`
content := `
-- -
title : "Markdown Shortcode"
-- -
# # List
1. List 1
{ { § mark1 § } }
1. Item Mark1 1
1. Item Mark1 2
{ { § mark2 § } }
{ { § / mark1 § } }
`
for i := 1 ; i < 100 ; i ++ {
files += fmt . Sprintf ( "\n-- content/posts/p%d.md --\n" + content , i + 1 )
}
files = strings . ReplaceAll ( files , "§" , "%" )
cfg := IntegrationTestConfig {
T : b ,
TxtarString : files ,
}
builders := make ( [ ] * IntegrationTestBuilder , b . N )
for i := range builders {
builders [ i ] = NewIntegrationTestBuilder ( cfg )
}
b . ResetTimer ( )
for i := 0 ; i < b . N ; i ++ {
builders [ i ] . Build ( )
}
}
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
func TestReplaceShortcodeTokens ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
for i , this := range [ ] struct {
2015-01-28 22:39:59 -05:00
input string
2015-01-28 22:11:41 -05:00
prefix string
replacements map [ string ] string
2022-03-17 17:03:27 -04:00
expect any
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
} {
2019-01-02 06:33:26 -05:00
{ "Hello HAHAHUGOSHORTCODE-1HBHB." , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "World" } , "Hello World." } ,
{ "Hello HAHAHUGOSHORTCODE-1@}@." , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "World" } , false } ,
{ "HAHAHUGOSHORTCODE2-1HBHB" , "PREFIX2" , map [ string ] string { "HAHAHUGOSHORTCODE2-1HBHB" : "World" } , "World" } ,
2015-10-20 14:35:12 -04:00
{ "Hello World!" , "PREFIX2" , map [ string ] string { } , "Hello World!" } ,
2019-01-02 06:33:26 -05:00
{ "!HAHAHUGOSHORTCODE-1HBHB" , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "World" } , "!World" } ,
{ "HAHAHUGOSHORTCODE-1HBHB!" , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "World" } , "World!" } ,
{ "!HAHAHUGOSHORTCODE-1HBHB!" , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "World" } , "!World!" } ,
{ "_{_PREFIX-1HBHB" , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "World" } , "_{_PREFIX-1HBHB" } ,
{ "Hello HAHAHUGOSHORTCODE-1HBHB." , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "To You My Old Friend Who Told Me This Fantastic Story" } , "Hello To You My Old Friend Who Told Me This Fantastic Story." } ,
{ "A HAHAHUGOSHORTCODE-1HBHB asdf HAHAHUGOSHORTCODE-2HBHB." , "A" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "v1" , "HAHAHUGOSHORTCODE-2HBHB" : "v2" } , "A v1 asdf v2." } ,
{ "Hello HAHAHUGOSHORTCODE2-1HBHB. Go HAHAHUGOSHORTCODE2-2HBHB, Go, Go HAHAHUGOSHORTCODE2-3HBHB Go Go!." , "PREFIX2" , map [ string ] string { "HAHAHUGOSHORTCODE2-1HBHB" : "Europe" , "HAHAHUGOSHORTCODE2-2HBHB" : "Jonny" , "HAHAHUGOSHORTCODE2-3HBHB" : "Johnny" } , "Hello Europe. Go Jonny, Go, Go Johnny Go Go!." } ,
{ "A HAHAHUGOSHORTCODE-2HBHB HAHAHUGOSHORTCODE-1HBHB." , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "A" , "HAHAHUGOSHORTCODE-2HBHB" : "B" } , "A B A." } ,
{ "A HAHAHUGOSHORTCODE-1HBHB HAHAHUGOSHORTCODE-2" , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "A" } , false } ,
{ "A HAHAHUGOSHORTCODE-1HBHB but not the second." , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "A" , "HAHAHUGOSHORTCODE-2HBHB" : "B" } , "A A but not the second." } ,
{ "An HAHAHUGOSHORTCODE-1HBHB." , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "A" , "HAHAHUGOSHORTCODE-2HBHB" : "B" } , "An A." } ,
{ "An HAHAHUGOSHORTCODE-1HBHB HAHAHUGOSHORTCODE-2HBHB." , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "A" , "HAHAHUGOSHORTCODE-2HBHB" : "B" } , "An A B." } ,
{ "A HAHAHUGOSHORTCODE-1HBHB HAHAHUGOSHORTCODE-2HBHB HAHAHUGOSHORTCODE-3HBHB HAHAHUGOSHORTCODE-1HBHB HAHAHUGOSHORTCODE-3HBHB." , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "A" , "HAHAHUGOSHORTCODE-2HBHB" : "B" , "HAHAHUGOSHORTCODE-3HBHB" : "C" } , "A A B C A C." } ,
{ "A HAHAHUGOSHORTCODE-1HBHB HAHAHUGOSHORTCODE-2HBHB HAHAHUGOSHORTCODE-3HBHB HAHAHUGOSHORTCODE-1HBHB HAHAHUGOSHORTCODE-3HBHB." , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "A" , "HAHAHUGOSHORTCODE-2HBHB" : "B" , "HAHAHUGOSHORTCODE-3HBHB" : "C" } , "A A B C A C." } ,
2015-06-21 07:08:30 -04:00
// Issue #1148 remove p-tags 10 =>
2019-01-02 06:33:26 -05:00
{ "Hello <p>HAHAHUGOSHORTCODE-1HBHB</p>. END." , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "World" } , "Hello World. END." } ,
{ "Hello <p>HAHAHUGOSHORTCODE-1HBHB</p>. <p>HAHAHUGOSHORTCODE-2HBHB</p> END." , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "World" , "HAHAHUGOSHORTCODE-2HBHB" : "THE" } , "Hello World. THE END." } ,
{ "Hello <p>HAHAHUGOSHORTCODE-1HBHB. END</p>." , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "World" } , "Hello <p>World. END</p>." } ,
{ "<p>Hello HAHAHUGOSHORTCODE-1HBHB</p>. END." , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "World" } , "<p>Hello World</p>. END." } ,
{ "Hello <p>HAHAHUGOSHORTCODE-1HBHB12" , "PREFIX" , map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : "World" } , "Hello <p>World12" } ,
2020-12-02 07:23:25 -05:00
{
"Hello HAHAHUGOSHORTCODE-1HBHB. HAHAHUGOSHORTCODE-1HBHB-HAHAHUGOSHORTCODE-1HBHB HAHAHUGOSHORTCODE-1HBHB HAHAHUGOSHORTCODE-1HBHB HAHAHUGOSHORTCODE-1HBHB END" , "P" ,
map [ string ] string { "HAHAHUGOSHORTCODE-1HBHB" : strings . Repeat ( "BC" , 100 ) } ,
2015-06-21 07:08:30 -04:00
fmt . Sprintf ( "Hello %s. %s-%s %s %s %s END" ,
2020-12-02 07:23:25 -05:00
strings . Repeat ( "BC" , 100 ) , strings . Repeat ( "BC" , 100 ) , strings . Repeat ( "BC" , 100 ) , strings . Repeat ( "BC" , 100 ) , strings . Repeat ( "BC" , 100 ) , strings . Repeat ( "BC" , 100 ) ) ,
} ,
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
} {
2015-10-20 14:35:12 -04:00
2023-02-11 10:20:24 -05:00
replacements := make ( map [ string ] shortcodeRenderer )
for k , v := range this . replacements {
replacements [ k ] = prerenderedShortcode { s : v }
}
tokenHandler := func ( ctx context . Context , token string ) ( [ ] byte , error ) {
return [ ] byte ( this . replacements [ token ] ) , nil
}
ctx := context . Background ( )
results , err := expandShortcodeTokens ( ctx , [ ] byte ( this . input ) , tokenHandler )
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
if b , ok := this . expect . ( bool ) ; ok && ! b {
if err == nil {
t . Errorf ( "[%d] replaceShortcodeTokens didn't return an expected error" , i )
}
} else {
if err != nil {
t . Errorf ( "[%d] failed: %s" , i , err )
continue
}
2015-01-28 22:39:59 -05:00
if ! reflect . DeepEqual ( results , [ ] byte ( this . expect . ( string ) ) ) {
2015-06-21 07:08:30 -04:00
t . Errorf ( "[%d] replaceShortcodeTokens, got \n%q but expected \n%q" , i , results , this . expect )
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
}
}
}
2014-02-25 23:57:31 -05:00
}
2017-05-06 14:15:28 -04:00
2018-04-23 23:57:33 -04:00
func TestShortcodeGetContent ( t * testing . T ) {
t . Parallel ( )
contentShortcode := `
{ { - $ t := . Get 0 - } }
{ { - $ p := . Get 1 - } }
{ { - $ k := . Get 2 - } }
{ { - $ page := $ . Page . Site . GetPage "page" $ p - } }
{ { if $ page } }
{ { - if eq $ t "bundle" - } }
{ { - . Scratch . Set "p" ( $ page . Resources . GetMatch ( printf "%s*" $ k ) ) - } }
{ { - else - } }
{ { - $ . Scratch . Set "p" $ page - } }
{ { - end - } } P1 : { { . Page . Content } } | P2 : { { $ p := ( $ . Scratch . Get "p" ) } } { { $ p . Title } } / { { $ p . Content } } |
{ { - else - } }
{ { - errorf "Page %s is nil" $ p - } }
{ { - end - } }
`
var templates [ ] string
var content [ ] string
contentWithShortcodeTemplate := ` -- -
title : doc % s
weight : % d
-- -
Logo : { { < c "bundle" "b1" "logo.png" > } } : P1 : { { < c "page" "section1/p1" "" > } } : BP1 : { { < c "bundle" "b1" "bp1" > } } `
simpleContentTemplate := ` -- -
title : doc % s
weight : % d
-- -
C - % s `
templates = append ( templates , [ ] string { "shortcodes/c.html" , contentShortcode } ... )
templates = append ( templates , [ ] string { "_default/single.html" , "Single Content: {{ .Content }}" } ... )
templates = append ( templates , [ ] string { "_default/list.html" , "List Content: {{ .Content }}" } ... )
content = append ( content , [ ] string { "b1/index.md" , fmt . Sprintf ( contentWithShortcodeTemplate , "b1" , 1 ) } ... )
content = append ( content , [ ] string { "b1/logo.png" , "PNG logo" } ... )
content = append ( content , [ ] string { "b1/bp1.md" , fmt . Sprintf ( simpleContentTemplate , "bp1" , 1 , "bp1" ) } ... )
content = append ( content , [ ] string { "section1/_index.md" , fmt . Sprintf ( contentWithShortcodeTemplate , "s1" , 2 ) } ... )
content = append ( content , [ ] string { "section1/p1.md" , fmt . Sprintf ( simpleContentTemplate , "s1p1" , 2 , "s1p1" ) } ... )
content = append ( content , [ ] string { "section2/_index.md" , fmt . Sprintf ( simpleContentTemplate , "b1" , 1 , "b1" ) } ... )
content = append ( content , [ ] string { "section2/s2p1.md" , fmt . Sprintf ( contentWithShortcodeTemplate , "bp1" , 1 ) } ... )
builder := newTestSitesBuilder ( t ) . WithDefaultMultiSiteConfig ( )
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
builder . WithContent ( content ... ) . WithTemplates ( templates ... ) . CreateSites ( ) . Build ( BuildCfg { } )
2018-04-23 23:57:33 -04:00
s := builder . H . Sites [ 0 ]
2019-08-10 15:05:17 -04:00
builder . Assert ( len ( s . RegularPages ( ) ) , qt . Equals , 3 )
2018-04-23 23:57:33 -04:00
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
builder . AssertFileContent ( "public/en/section1/index.html" ,
2018-04-23 23:57:33 -04:00
"List Content: <p>Logo:P1:|P2:logo.png/PNG logo|:P1: P1:|P2:docs1p1/<p>C-s1p1</p>\n|" ,
"BP1:P1:|P2:docbp1/<p>C-bp1</p>" ,
)
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
builder . AssertFileContent ( "public/en/b1/index.html" ,
2018-04-23 23:57:33 -04:00
"Single Content: <p>Logo:P1:|P2:logo.png/PNG logo|:P1: P1:|P2:docs1p1/<p>C-s1p1</p>\n|" ,
"P2:docbp1/<p>C-bp1</p>" ,
)
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
builder . AssertFileContent ( "public/en/section2/s2p1/index.html" ,
2018-04-23 23:57:33 -04:00
"Single Content: <p>Logo:P1:|P2:logo.png/PNG logo|:P1: P1:|P2:docs1p1/<p>C-s1p1</p>\n|" ,
"P2:docbp1/<p>C-bp1</p>" ,
)
}
2019-04-12 04:26:10 -04:00
// https://github.com/gohugoio/hugo/issues/5833
2019-04-12 04:44:21 -04:00
func TestShortcodeParentResourcesOnRebuild ( t * testing . T ) {
2019-04-12 04:26:10 -04:00
t . Parallel ( )
2022-09-27 05:42:25 -04:00
b := newTestSitesBuilder ( t ) . Running ( ) . WithSimpleConfigFile ( )
b . WithTemplatesAdded (
"index.html" , `
{ { $ b := . Site . GetPage "b1" } }
b1 Content : { { $ b . Content } }
{ { $ p := $ b . Resources . GetMatch "p1*" } }
Content : { { $ p . Content } }
{ { $ article := . Site . GetPage "blog/article" } }
Article Content : { { $ article . Content } }
` ,
"shortcodes/c.html" , `
{ { range . Page . Parent . Resources } }
* Parent resource : { { . Name } } : { { . RelPermalink } }
{ { end } }
` )
2019-04-12 04:26:10 -04:00
2022-09-27 05:42:25 -04:00
pageContent := `
2019-04-12 04:26:10 -04:00
-- -
title : MyPage
-- -
2019-04-12 04:44:21 -04:00
SHORTCODE : { { < c > } }
2019-04-12 04:26:10 -04:00
2022-09-27 05:42:25 -04:00
`
2019-04-12 04:44:21 -04:00
2022-09-27 05:42:25 -04:00
b . WithContent ( "b1/index.md" , pageContent ,
"b1/logo.png" , "PNG logo" ,
"b1/p1.md" , pageContent ,
"blog/_index.md" , pageContent ,
"blog/logo-article.png" , "PNG logo" ,
"blog/article.md" , pageContent ,
)
2019-04-12 04:26:10 -04:00
2022-09-27 05:42:25 -04:00
b . Build ( BuildCfg { } )
2019-04-12 04:26:10 -04:00
2019-04-15 06:06:12 -04:00
assert := func ( matchers ... string ) {
2022-09-27 05:42:25 -04:00
allMatchers := append ( matchers , "Parent resource: logo.png: /b1/logo.png" ,
"Article Content: <p>SHORTCODE: \n\n* Parent resource: logo-article.png: /blog/logo-article.png" ,
)
2019-04-15 06:06:12 -04:00
b . AssertFileContent ( "public/index.html" ,
2022-09-27 05:42:25 -04:00
allMatchers ... ,
2019-04-15 06:06:12 -04:00
)
2019-04-12 04:44:21 -04:00
}
assert ( )
2022-09-27 05:42:25 -04:00
b . EditFiles ( "content/b1/index.md" , pageContent + " Edit." )
2019-04-12 04:44:21 -04:00
2022-09-27 05:42:25 -04:00
b . Build ( BuildCfg { } )
2019-04-12 04:44:21 -04:00
2022-09-27 05:42:25 -04:00
assert ( "Edit." )
2019-04-12 04:26:10 -04:00
}
2018-04-23 23:57:33 -04:00
func TestShortcodePreserveOrder ( t * testing . T ) {
2018-04-22 08:07:29 -04:00
t . Parallel ( )
2019-08-10 15:05:17 -04:00
c := qt . New ( t )
2018-04-22 08:07:29 -04:00
contentTemplate := ` -- -
title : doc % d
weight : % d
-- -
# doc
2018-04-23 02:09:56 -04:00
{ { < s1 > } } { { < s2 > } } { { < s3 > } } { { < s4 > } } { { < s5 > } }
{ { < nested > } }
2018-04-23 23:57:33 -04:00
{ { < ordinal > } } { { < scratch > } }
{ { < ordinal > } } { { < scratch > } }
{ { < ordinal > } } { { < scratch > } }
2018-04-23 02:09:56 -04:00
{ { < / nested > } }
2018-04-22 08:07:29 -04:00
`
2018-04-23 23:57:33 -04:00
ordinalShortcodeTemplate := ` ordinal: {{ .Ordinal }} {{ .Page .Scratch .Set "ordinal" .Ordinal }} `
2018-04-23 02:09:56 -04:00
nestedShortcode := ` outer ordinal: {{ .Ordinal }} inner: {{ .Inner }} `
2018-04-23 23:57:33 -04:00
scratchGetShortcode := ` scratch ordinal: {{ .Ordinal }} scratch get ordinal: {{ .Page .Scratch .Get "ordinal" }} `
shortcodeTemplate := ` v%d: {{ .Ordinal }} sgo: {{ .Page .Scratch .Get "o2" }} {{ .Page .Scratch .Set "o2" .Ordinal }} | `
2018-04-22 08:07:29 -04:00
var shortcodes [ ] string
var content [ ] string
2018-04-23 02:09:56 -04:00
shortcodes = append ( shortcodes , [ ] string { "shortcodes/nested.html" , nestedShortcode } ... )
shortcodes = append ( shortcodes , [ ] string { "shortcodes/ordinal.html" , ordinalShortcodeTemplate } ... )
2018-04-23 23:57:33 -04:00
shortcodes = append ( shortcodes , [ ] string { "shortcodes/scratch.html" , scratchGetShortcode } ... )
2018-04-22 08:07:29 -04:00
for i := 1 ; i <= 5 ; i ++ {
2018-04-23 23:57:33 -04:00
sc := fmt . Sprintf ( shortcodeTemplate , i )
sc = strings . Replace ( sc , "%%" , "%" , - 1 )
shortcodes = append ( shortcodes , [ ] string { fmt . Sprintf ( "shortcodes/s%d.html" , i ) , sc } ... )
2018-04-22 08:07:29 -04:00
}
for i := 1 ; i <= 3 ; i ++ {
content = append ( content , [ ] string { fmt . Sprintf ( "p%d.md" , i ) , fmt . Sprintf ( contentTemplate , i , i ) } ... )
}
builder := newTestSitesBuilder ( t ) . WithDefaultMultiSiteConfig ( )
builder . WithContent ( content ... ) . WithTemplatesAdded ( shortcodes ... ) . CreateSites ( ) . Build ( BuildCfg { } )
s := builder . H . Sites [ 0 ]
2019-08-10 15:05:17 -04:00
c . Assert ( len ( s . RegularPages ( ) ) , qt . Equals , 3 )
2018-04-22 08:07:29 -04:00
2018-04-23 23:57:33 -04:00
builder . AssertFileContent ( "public/en/p1/index.html" , ` v1: 0 sgo: |v2: 1 sgo: 0|v3: 2 sgo: 1|v4: 3 sgo: 2|v5: 4 sgo: 3 ` )
2023-10-24 06:04:13 -04:00
builder . AssertFileContent ( "public/en/p1/index.html" , ` outer ordinal : 5 inner :
2018-04-23 23:57:33 -04:00
ordinal : 0 scratch ordinal : 1 scratch get ordinal : 0
ordinal : 2 scratch ordinal : 3 scratch get ordinal : 2
ordinal : 4 scratch ordinal : 5 scratch get ordinal : 4 ` )
2018-04-22 08:07:29 -04:00
}
2018-11-01 05:39:44 -04:00
2018-12-21 03:51:15 -05:00
func TestShortcodeVariables ( t * testing . T ) {
2018-11-01 05:39:44 -04:00
t . Parallel ( )
2019-08-10 15:05:17 -04:00
c := qt . New ( t )
2018-11-01 05:39:44 -04:00
builder := newTestSitesBuilder ( t ) . WithSimpleConfigFile ( )
builder . WithContent ( "page.md" , ` -- -
title : "Hugo Rocks!"
-- -
# doc
{ { < s1 > } }
` ).WithTemplatesAdded("layouts/shortcodes/s1.html", `
2018-12-21 03:51:15 -05:00
Name : { { . Name } }
2018-11-01 05:39:44 -04:00
{ { with . Position } }
File : { { . Filename } }
Offset : { { . Offset } }
Line : { { . LineNumber } }
Column : { { . ColumnNumber } }
String : { { . | safeHTML } }
{ { end } }
` ) . CreateSites ( ) . Build ( BuildCfg { } )
s := builder . H . Sites [ 0 ]
2019-08-10 15:05:17 -04:00
c . Assert ( len ( s . RegularPages ( ) ) , qt . Equals , 1 )
2018-11-01 05:39:44 -04:00
builder . AssertFileContent ( "public/page/index.html" ,
2018-11-01 06:28:30 -04:00
filepath . FromSlash ( "File: content/page.md" ) ,
2018-11-01 05:39:44 -04:00
"Line: 7" , "Column: 4" , "Offset: 40" ,
2018-11-01 06:28:30 -04:00
filepath . FromSlash ( "String: \"content/page.md:7:4\"" ) ,
2018-12-21 03:51:15 -05:00
"Name: s1" ,
2018-11-01 05:39:44 -04:00
)
}
2018-11-26 05:01:27 -05:00
func TestInlineShortcodes ( t * testing . T ) {
for _ , enableInlineShortcodes := range [ ] bool { true , false } {
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
enableInlineShortcodes := enableInlineShortcodes
2018-11-26 05:01:27 -05:00
t . Run ( fmt . Sprintf ( "enableInlineShortcodes=%t" , enableInlineShortcodes ) ,
func ( t * testing . T ) {
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
t . Parallel ( )
2018-11-26 05:01:27 -05:00
conf := fmt . Sprintf ( `
baseURL = "https://example.com"
enableInlineShortcodes = % t
` , enableInlineShortcodes )
b := newTestSitesBuilder ( t )
b . WithConfigFile ( "toml" , conf )
2019-01-31 05:53:21 -05:00
shortcodeContent := ` FIRST : { { < myshort . inline "first" > } }
2018-11-26 05:01:27 -05:00
Page : { { . Page . Title } }
Seq : { { seq 3 } }
Param : { { . Get 0 } }
{ { < / myshort . inline > } } : END :
SECOND : { { < myshort . inline "second" / > } } : END
2019-01-31 05:53:21 -05:00
NEW INLINE : { { < n1 . inline "5" > } } W1 : { { seq ( . Get 0 ) } } { { < / n1 . inline > } } : END :
INLINE IN INNER : { { < outer > } } { { < n2 . inline > } } W2 : { { seq 4 } } { { < / n2 . inline > } } { { < / outer > } } : END :
REUSED INLINE IN INNER : { { < outer > } } { { < n1 . inline "3" / > } } { { < / outer > } } : END :
2019-12-28 06:07:23 -05:00
# # MARKDOWN DELIMITER : { { % mymarkdown . inline % } } * * Hugo Rocks ! * * { { % / mymarkdown . inline % } }
2019-01-31 05:53:21 -05:00
`
2018-11-26 05:01:27 -05:00
2019-01-31 05:53:21 -05:00
b . WithContent ( "page-md-shortcode.md" , ` -- -
title : "Hugo"
-- -
` + shortcodeContent )
b . WithContent ( "_index.md" , ` -- -
title : "Hugo Home"
-- -
` + shortcodeContent )
2018-11-26 05:01:27 -05:00
b . WithTemplatesAdded ( "layouts/_default/single.html" , `
CONTENT : { { . Content } }
2019-12-28 06:07:23 -05:00
TOC : { { . TableOfContents } }
2018-11-26 05:01:27 -05:00
` )
2019-01-31 05:53:21 -05:00
b . WithTemplatesAdded ( "layouts/index.html" , `
CONTENT : { { . Content } }
2019-12-28 06:07:23 -05:00
TOC : { { . TableOfContents } }
2019-01-31 05:53:21 -05:00
` )
b . WithTemplatesAdded ( "layouts/shortcodes/outer.html" , ` Inner: {{ .Inner }} ` )
2018-11-26 05:01:27 -05:00
b . CreateSites ( ) . Build ( BuildCfg { } )
2019-01-31 05:53:21 -05:00
shouldContain := [ ] string {
"Seq: [1 2 3]" ,
"Param: first" ,
"Param: second" ,
"NEW INLINE: W1: [1 2 3 4 5]" ,
"INLINE IN INNER: Inner: W2: [1 2 3 4]" ,
"REUSED INLINE IN INNER: Inner: W1: [1 2 3]" ,
2020-02-22 12:06:30 -05:00
` <li><a href="#markdown-delimiter-hugo-rocks">MARKDOWN DELIMITER: <strong>Hugo Rocks!</strong></a></li> ` ,
2019-01-31 05:53:21 -05:00
}
2018-11-26 05:01:27 -05:00
if enableInlineShortcodes {
b . AssertFileContent ( "public/page-md-shortcode/index.html" ,
2019-01-31 05:53:21 -05:00
shouldContain ... ,
)
b . AssertFileContent ( "public/index.html" ,
shouldContain ... ,
2018-11-26 05:01:27 -05:00
)
} else {
b . AssertFileContent ( "public/page-md-shortcode/index.html" ,
"FIRST::END" ,
"SECOND::END" ,
2019-01-31 05:53:21 -05:00
"NEW INLINE: :END" ,
"INLINE IN INNER: Inner: :END:" ,
"REUSED INLINE IN INNER: Inner: :END:" ,
2018-11-26 05:01:27 -05:00
)
}
} )
}
}
2019-04-15 09:17:46 -04:00
// https://github.com/gohugoio/hugo/issues/5863
func TestShortcodeNamespaced ( t * testing . T ) {
t . Parallel ( )
2019-08-10 15:05:17 -04:00
c := qt . New ( t )
2019-04-15 09:17:46 -04:00
builder := newTestSitesBuilder ( t ) . WithSimpleConfigFile ( )
builder . WithContent ( "page.md" , ` -- -
title : "Hugo Rocks!"
-- -
# doc
hello : { { < hello > } }
test / hello : { { < test / hello > } }
` ) . WithTemplatesAdded (
"layouts/shortcodes/hello.html" , ` hello ` ,
"layouts/shortcodes/test/hello.html" , ` test/hello ` ) . CreateSites ( ) . Build ( BuildCfg { } )
s := builder . H . Sites [ 0 ]
2019-08-10 15:05:17 -04:00
c . Assert ( len ( s . RegularPages ( ) ) , qt . Equals , 1 )
2019-04-15 09:17:46 -04:00
builder . AssertFileContent ( "public/page/index.html" ,
"hello: hello" ,
"test/hello: test/hello" ,
)
}
2019-09-29 08:51:51 -04:00
2022-07-07 10:11:47 -04:00
func TestShortcodeParams ( t * testing . T ) {
2019-09-29 08:51:51 -04:00
t . Parallel ( )
2024-08-13 09:49:56 -04:00
files := `
-- hugo . toml --
baseURL = "https://example.org"
-- layouts / shortcodes / hello . html --
{ { range $ i , $ v := . Params } } { { printf "- %v: %v (%T) " $ i $ v $ v - } } { { end } }
-- content / page . md --
2019-09-29 08:51:51 -04:00
title : "Hugo Rocks!"
2024-08-13 09:49:56 -04:00
summary : "Foo"
2019-09-29 08:51:51 -04:00
-- -
# doc
types positional : { { < hello true false 33 3.14 > } }
types named : { { < hello b1 = true b2 = false i1 = 33 f1 = 3.14 > } }
types string : { { < hello "true" trues "33" "3.14" > } }
2022-07-07 10:11:47 -04:00
escaped quoute : { { < hello "hello \"world\"." > } }
2024-08-13 09:49:56 -04:00
-- layouts / _default / single . html --
Content : { { . Content } } |
`
2019-09-29 08:51:51 -04:00
2024-08-13 09:49:56 -04:00
b := Test ( t , files )
2019-09-29 08:51:51 -04:00
2024-08-13 09:49:56 -04:00
b . AssertFileContent ( "public/page/index.html" ,
2019-09-29 08:51:51 -04:00
"types positional: - 0: true (bool) - 1: false (bool) - 2: 33 (int) - 3: 3.14 (float64)" ,
2024-08-13 09:49:56 -04:00
"types named: - b1: true (bool) - b2: false (bool) - f1: 3.14 (float64) - i1: 33 (int)" ,
2019-09-29 08:51:51 -04:00
"types string: - 0: true (string) - 1: trues (string) - 2: 33 (string) - 3: 3.14 (string) " ,
2022-07-07 10:11:47 -04:00
"hello "world". (string)" ,
2019-09-29 08:51:51 -04:00
)
}
2019-11-06 03:20:59 -05:00
func TestShortcodeRef ( t * testing . T ) {
2022-05-28 05:01:47 -04:00
t . Parallel ( )
2019-11-06 03:20:59 -05:00
2023-01-04 12:24:36 -05:00
v := config . New ( )
2022-05-28 05:01:47 -04:00
v . Set ( "baseURL" , "https://example.org" )
2019-11-06 03:20:59 -05:00
2022-05-28 05:01:47 -04:00
builder := newTestSitesBuilder ( t ) . WithViper ( v )
2019-11-06 03:20:59 -05:00
2022-05-28 05:01:47 -04:00
for i := 1 ; i <= 2 ; i ++ {
builder . WithContent ( fmt . Sprintf ( "page%d.md" , i ) , ` -- -
2019-11-06 03:20:59 -05:00
title : "Hugo Rocks!"
-- -
[ Page 1 ] ( { { < ref "page1.md" > } } )
[ Page 1 with anchor ] ( { { < relref "page1.md#doc" > } } )
[ Page 2 ] ( { { < ref "page2.md" > } } )
[ Page 2 with anchor ] ( { { < relref "page2.md#doc" > } } )
# # Doc
` )
2022-05-28 05:01:47 -04:00
}
2019-11-06 03:20:59 -05:00
2022-05-28 05:01:47 -04:00
builder . Build ( BuildCfg { } )
2019-11-06 03:20:59 -05:00
2022-05-28 05:01:47 -04:00
builder . AssertFileContent ( "public/page2/index.html" , `
2019-11-06 03:20:59 -05:00
< a href = "/page1/#doc" > Page 1 with anchor < / a >
< a href = "https://example.org/page2/" > Page 2 < / a >
< a href = "/page2/#doc" > Page 2 with anchor < / a > < / p >
< h2 id = "doc" > Doc < / h2 >
` ,
2022-05-28 05:01:47 -04:00
)
2019-11-06 03:20:59 -05:00
}
2020-06-14 12:16:45 -04:00
// https://github.com/gohugoio/hugo/issues/6857
func TestShortcodeNoInner ( t * testing . T ) {
t . Parallel ( )
b := newTestSitesBuilder ( t )
2022-05-02 10:07:52 -04:00
b . WithContent ( "mypage.md" , ` -- -
2020-06-14 12:16:45 -04:00
title : "No Inner!"
-- -
{ { < noinner > } } { { < / noinner > } }
` ) . WithTemplatesAdded (
"layouts/shortcodes/noinner.html" , ` No inner here. ` )
err := b . BuildE ( BuildCfg { } )
2023-02-23 02:38:51 -05:00
b . Assert ( err . Error ( ) , qt . Contains , filepath . FromSlash ( ` "content/mypage.md:4:16": failed to extract shortcode: shortcode "noinner" does not evaluate .Inner or .InnerDeindent, yet a closing tag was provided ` ) )
2020-06-14 12:16:45 -04:00
}
2021-04-23 07:40:05 -04:00
func TestShortcodeStableOutputFormatTemplates ( t * testing . T ) {
t . Parallel ( )
for i := 0 ; i < 5 ; i ++ {
b := newTestSitesBuilder ( t )
const numPages = 10
for i := 0 ; i < numPages ; i ++ {
b . WithContent ( fmt . Sprintf ( "page%d.md" , i ) , ` -- -
title : "Page"
outputs : [ "html" , "css" , "csv" , "json" ]
-- -
{ { < myshort > } }
` )
}
b . WithTemplates (
"_default/single.html" , "{{ .Content }}" ,
"_default/single.css" , "{{ .Content }}" ,
"_default/single.csv" , "{{ .Content }}" ,
"_default/single.json" , "{{ .Content }}" ,
"shortcodes/myshort.html" , ` Short-HTML ` ,
"shortcodes/myshort.csv" , ` Short-CSV ` ,
)
b . Build ( BuildCfg { } )
2022-01-04 07:26:23 -05:00
// helpers.PrintFs(b.Fs.Destination, "public", os.Stdout)
2021-04-23 07:40:05 -04:00
for i := 0 ; i < numPages ; i ++ {
b . AssertFileContent ( fmt . Sprintf ( "public/page%d/index.html" , i ) , "Short-HTML" )
b . AssertFileContent ( fmt . Sprintf ( "public/page%d/index.csv" , i ) , "Short-CSV" )
b . AssertFileContent ( fmt . Sprintf ( "public/page%d/index.json" , i ) , "Short-HTML" )
}
for i := 0 ; i < numPages ; i ++ {
b . AssertFileContent ( fmt . Sprintf ( "public/page%d/styles.css" , i ) , "Short-HTML" )
}
}
}
2022-05-27 09:19:02 -04:00
// #9821
func TestShortcodeMarkdownOutputFormat ( t * testing . T ) {
t . Parallel ( )
files := `
-- config . toml --
-- content / p1 . md --
-- -
title : "p1"
-- -
{ { < foo > } }
2023-01-04 12:24:36 -05:00
# The below would have failed using the HTML template parser .
2022-05-27 09:19:02 -04:00
-- layouts / shortcodes / foo . md --
§ § §
< x
§ § §
-- layouts / _default / single . html --
{ { . Content } }
`
2024-01-28 16:11:05 -05:00
b := Test ( t , files )
2022-05-27 09:19:02 -04:00
b . AssertFileContent ( "public/p1/index.html" , `
< x
` )
}
2022-05-28 07:18:50 -04:00
func TestShortcodePreserveIndentation ( t * testing . T ) {
t . Parallel ( )
files := `
-- config . toml --
-- content / p1 . md --
-- -
title : "p1"
-- -
# # List With Indented Shortcodes
1. List 1
{ { % mark1 % } }
1. Item Mark1 1
1. Item Mark1 2
{ { % mark2 % } }
{ { % / mark1 % } }
-- layouts / shortcodes / mark1 . md --
{ { . Inner } }
-- layouts / shortcodes / mark2 . md --
1. Item Mark2 1
1. Item Mark2 2
1. Item Mark2 2 - 1
1. Item Mark2 3
-- layouts / _default / single . html --
{ { . Content } }
`
2024-01-28 16:11:05 -05:00
b := Test ( t , files )
2022-05-28 07:18:50 -04:00
b . AssertFileContent ( "public/p1/index.html" , "<ol>\n<li>\n<p>List 1</p>\n<ol>\n<li>Item Mark1 1</li>\n<li>Item Mark1 2</li>\n<li>Item Mark2 1</li>\n<li>Item Mark2 2\n<ol>\n<li>Item Mark2 2-1</li>\n</ol>\n</li>\n<li>Item Mark2 3</li>\n</ol>\n</li>\n</ol>" )
}
func TestShortcodeCodeblockIndent ( t * testing . T ) {
t . Parallel ( )
files := `
-- config . toml --
-- content / p1 . md --
-- -
title : "p1"
-- -
# # Code block
{ { % code % } }
-- layouts / shortcodes / code . md --
echo "foo" ;
-- layouts / _default / single . html --
{ { . Content } }
`
2024-01-28 16:11:05 -05:00
b := Test ( t , files )
2022-05-28 07:18:50 -04:00
b . AssertFileContent ( "public/p1/index.html" , "<pre><code>echo "foo";\n</code></pre>" )
}
2022-05-30 14:42:46 -04:00
func TestShortcodeHighlightDeindent ( t * testing . T ) {
t . Parallel ( )
files := `
-- config . toml --
[ markup ]
[ markup . highlight ]
codeFences = true
noClasses = false
-- content / p1 . md --
-- -
title : "p1"
-- -
# # Indent 5 Spaces
{ { < highlight bash > } }
line 1 ;
line 2 ;
line 3 ;
{ { < / highlight > } }
-- layouts / _default / single . html --
{ { . Content } }
`
2024-01-28 16:11:05 -05:00
b := Test ( t , files )
2022-05-30 14:42:46 -04:00
b . AssertFileContent ( "public/p1/index.html" , `
< pre > < code > < div class = "highlight" > < pre tabindex = "0" class = "chroma" > < code class = "language-bash" data - lang = "bash" > < span class = "line" > < span class = "cl" > line 1 < span class = "p" > ; < / span >
< / span > < / span > < span class = "line" > < span class = "cl" > line 2 < span class = "p" > ; < / span >
< / span > < / span > < span class = "line" > < span class = "cl" > line 3 < span class = "p" > ; < / span > < / span > < / span > < / code > < / pre > < / div >
< / code > < / pre >
` )
}
2022-09-01 03:26:27 -04:00
// Issue 10236.
func TestShortcodeParamEscapedQuote ( t * testing . T ) {
t . Parallel ( )
files := `
-- config . toml --
-- content / p1 . md --
-- -
title : "p1"
-- -
{ { < figure src = "/media/spf13.jpg" title = "Steve \"Francia\"." > } }
-- layouts / shortcodes / figure . html --
Title : { { . Get "title" | safeHTML } }
-- layouts / _default / single . html --
{ { . Content } }
`
b := NewIntegrationTestBuilder (
IntegrationTestConfig {
T : t ,
TxtarString : files ,
2023-08-01 12:12:36 -04:00
Verbose : true ,
2022-09-01 03:26:27 -04:00
} ,
) . Build ( )
b . AssertFileContent ( "public/p1/index.html" , ` Title: Steve "Francia". ` )
}
2022-10-25 11:33:25 -04:00
// Issue 10391.
func TestNestedShortcodeCustomOutputFormat ( t * testing . T ) {
t . Parallel ( )
files := `
-- config . toml --
[ outputFormats . Foobar ]
baseName = "foobar"
isPlainText = true
mediaType = "application/json"
notAlternative = true
[ languages . en ]
languageName = "English"
[ languages . en . outputs ]
home = [ "HTML" , "RSS" , "Foobar" ]
[ languages . fr ]
languageName = "Français"
[ [ module . mounts ] ]
source = "content/en"
target = "content"
lang = "en"
[ [ module . mounts ] ]
source = "content/fr"
target = "content"
lang = "fr"
-- layouts / _default / list . foobar . json --
{ { - $ . Scratch . Add "data" slice - } }
{ { - range ( where . Site . AllPages "Kind" "!=" "home" ) - } }
{ { - $ . Scratch . Add "data" ( dict "content" ( . Plain | truncate 10000 ) "type" . Type "full_url" . Permalink ) - } }
{ { - end - } }
{ { - $ . Scratch . Get "data" | jsonify - } }
-- content / en / p1 . md --
-- -
title : "p1"
-- -
# # # More information
{ { < tabs > } }
{ { % tab "Test" % } }
It ' s a test
{ { % / tab % } }
{ { < / tabs > } }
-- content / fr / p2 . md --
-- -
title : Test
-- -
# # # Plus d ' informations
{ { < tabs > } }
{ { % tab "Test" % } }
C ' est un test
{ { % / tab % } }
{ { < / tabs > } }
-- layouts / shortcodes / tabs . html --
< div >
< div class = "tab-content" > { { . Inner } } < / div >
< / div >
-- layouts / shortcodes / tab . html --
< div > { { . Inner } } < / div >
-- layouts / _default / single . html --
{ { . Content } }
`
b := NewIntegrationTestBuilder (
IntegrationTestConfig {
T : t ,
TxtarString : files ,
2023-08-01 12:12:36 -04:00
Verbose : true ,
2022-10-25 11:33:25 -04:00
} ,
) . Build ( )
b . AssertFileContent ( "public/fr/p2/index.html" , ` plus-dinformations ` )
}
2023-01-31 03:01:43 -05:00
// Issue 10671.
func TestShortcodeInnerShouldBeEmptyWhenNotClosed ( t * testing . T ) {
t . Parallel ( )
files := `
-- config . toml --
disableKinds = [ "home" , "taxonomy" , "term" ]
-- content / p1 . md --
-- -
title : "p1"
-- -
{ { < sc "self-closing" / > } }
Text .
{ { < sc "closing-no-newline" > } } { { < / sc > } }
-- layouts / shortcodes / sc . html --
Inner : { { . Get 0 } } : { { len . Inner } }
InnerDeindent : { { . Get 0 } } : { { len . InnerDeindent } }
-- layouts / _default / single . html --
{ { . Content } }
`
b := NewIntegrationTestBuilder (
IntegrationTestConfig {
T : t ,
TxtarString : files ,
2023-08-01 12:12:36 -04:00
Verbose : true ,
2023-01-31 03:01:43 -05:00
} ,
) . Build ( )
b . AssertFileContent ( "public/p1/index.html" , `
Inner : self - closing : 0
InnerDeindent : self - closing : 0
Inner : closing - no - newline : 0
InnerDeindent : closing - no - newline : 0
` )
}
2023-02-23 02:08:17 -05:00
// Issue 10675.
func TestShortcodeErrorWhenItShouldBeClosed ( t * testing . T ) {
t . Parallel ( )
files := `
-- config . toml --
disableKinds = [ "home" , "taxonomy" , "term" ]
-- content / p1 . md --
-- -
title : "p1"
-- -
{ { < sc > } }
Text .
-- layouts / shortcodes / sc . html --
Inner : { { . Get 0 } } : { { len . Inner } }
-- layouts / _default / single . html --
{ { . Content } }
`
b , err := NewIntegrationTestBuilder (
IntegrationTestConfig {
T : t ,
TxtarString : files ,
2023-08-01 12:12:36 -04:00
Verbose : true ,
2023-02-23 02:08:17 -05:00
} ,
) . BuildE ( )
b . Assert ( err , qt . Not ( qt . IsNil ) )
2023-03-10 12:41:17 -05:00
b . Assert ( err . Error ( ) , qt . Contains , ` p1.md:5:1": failed to extract shortcode: shortcode "sc" must be closed or self-closed ` )
2023-02-23 02:08:17 -05:00
}
2023-03-12 05:50:16 -04:00
// Issue 10819.
func TestShortcodeInCodeFenceHyphen ( t * testing . T ) {
t . Parallel ( )
files := `
-- config . toml --
disableKinds = [ "home" , "taxonomy" , "term" ]
-- content / p1 . md --
-- -
title : "p1"
-- -
§ § § go
{ { < sc > } }
§ § §
Text .
-- layouts / shortcodes / sc . html --
Hello .
-- layouts / _default / single . html --
{ { . Content } }
`
b := NewIntegrationTestBuilder (
IntegrationTestConfig {
T : t ,
TxtarString : files ,
2023-08-01 12:12:36 -04:00
Verbose : true ,
2023-03-12 05:50:16 -04:00
} ,
) . Build ( )
b . AssertFileContent ( "public/p1/index.html" , "<span style=\"color:#a6e22e\">Hello.</span>" )
}