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 (
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"
2019-08-16 09:55:03 -04:00
"github.com/gohugoio/hugo/markup/asciidoc"
"github.com/gohugoio/hugo/markup/rst"
2019-11-06 03:20:59 -05:00
"github.com/spf13/viper"
2019-01-02 06:33:26 -05:00
"github.com/gohugoio/hugo/parser/pageparser"
"github.com/gohugoio/hugo/resources/page"
2014-02-25 23:57:31 -05:00
"strings"
"testing"
2014-11-20 12:32:21 -05:00
2017-06-13 12:42:45 -04:00
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/tpl"
2019-01-02 06:33:26 -05:00
"github.com/spf13/cast"
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
2019-08-10 15:05:17 -04:00
qt "github.com/frankban/quicktest"
2014-02-25 23:57:31 -05:00
)
2019-12-10 13:56:44 -05:00
func CheckShortCodeMatch ( t * testing . T , input , expected string , withTemplate func ( templ tpl . TemplateManager ) error ) {
2019-09-29 08:51:51 -04:00
t . Helper ( )
2016-08-07 08:03:03 -04:00
CheckShortCodeMatchAndError ( t , input , expected , withTemplate , false )
2015-08-07 14:08:23 -04:00
}
2019-12-10 13:56:44 -05:00
func CheckShortCodeMatchAndError ( t * testing . T , input , expected string , withTemplate func ( templ tpl . TemplateManager ) error , expectError bool ) {
2019-09-29 08:51:51 -04:00
t . Helper ( )
2017-02-04 22:20:06 -05:00
cfg , fs := newTestCfg ( )
2019-11-06 14:10:47 -05:00
cfg . Set ( "markup" , map [ string ] interface { } {
"defaultMarkdownHandler" : "blackfriday" , // TODO(bep)
} )
2019-08-10 15:05:17 -04:00
c := qt . New ( t )
2017-01-10 04:55:03 -05:00
2017-06-13 12:47:17 -04:00
// Need some front matter, see https://github.com/gohugoio/hugo/issues/2337
2016-08-07 08:03:03 -04:00
contentFile := ` -- -
title : "Title"
-- -
` + input
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , "content/simple.md" , contentFile )
2016-08-07 08:03:03 -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 := newTestSitesBuilderFromDepsCfg ( t , deps . DepsCfg { Fs : fs , Cfg : cfg , WithTemplate : withTemplate } ) . WithNothingAdded ( )
err := b . BuildE ( BuildCfg { } )
2015-04-29 13:08:34 -04:00
2015-08-07 14:08:23 -04:00
if err != nil && ! expectError {
2016-08-07 08:03:03 -04:00
t . Fatalf ( "Shortcode rendered error %s." , err )
2015-04-29 13:08:34 -04:00
}
2014-02-25 23:57:31 -05:00
2015-08-07 14:08:23 -04:00
if err == nil && expectError {
t . Fatalf ( "No error from shortcode" )
}
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
h := b . H
2019-08-10 15:05:17 -04:00
c . Assert ( len ( h . Sites ) , qt . Equals , 1 )
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
2019-08-10 15:05:17 -04:00
c . Assert ( len ( h . Sites [ 0 ] . RegularPages ( ) ) , qt . Equals , 1 )
2016-08-07 08:03:03 -04:00
2019-01-02 06:33:26 -05:00
output := strings . TrimSpace ( content ( h . Sites [ 0 ] . RegularPages ( ) [ 0 ] ) )
2016-11-23 12:28:14 -05:00
output = strings . TrimPrefix ( output , "<p>" )
output = strings . TrimSuffix ( output , "</p>" )
2016-08-07 08:03:03 -04:00
expected = strings . TrimSpace ( expected )
2014-02-25 23:57:31 -05:00
if output != expected {
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 . Fatalf ( "Shortcode render didn't match. got \n%q but expected \n%q" , output , expected )
2014-02-25 23:57:31 -05:00
}
}
func TestNonSC ( 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
// notice the syntax diff from 0.12, now comment delims must be added
2016-08-07 08:03:03 -04:00
CheckShortCodeMatch ( t , "{{%/* movie 47238zzb */%}}" , "{{% movie 47238zzb %}}" , nil )
2014-02-25 23:57:31 -05:00
}
2015-02-27 05:57:23 -05:00
// Issue #929
func TestHyphenatedSC ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2019-12-10 13:56:44 -05:00
wt := func ( tem tpl . TemplateManager ) error {
2017-03-27 14:43:49 -04:00
tem . AddTemplate ( "_internal/shortcodes/hyphenated-video.html" , ` Playing Video {{ .Get 0 }} ` )
2016-08-07 08:03:03 -04:00
return nil
}
2015-02-27 05:57:23 -05:00
2016-08-07 08:03:03 -04:00
CheckShortCodeMatch ( t , "{{< hyphenated-video 47238zzb >}}" , "Playing Video 47238zzb" , wt )
2015-02-27 05:57:23 -05:00
}
2016-01-04 17:48:05 -05:00
// Issue #1753
func TestNoTrailingNewline ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2019-12-10 13:56:44 -05:00
wt := func ( tem tpl . TemplateManager ) error {
2017-03-27 14:43:49 -04:00
tem . AddTemplate ( "_internal/shortcodes/a.html" , ` {{ .Get 0 }} ` )
2016-08-07 08:03:03 -04:00
return nil
}
2016-01-04 17:48:05 -05:00
2016-08-07 08:03:03 -04:00
CheckShortCodeMatch ( t , "ab{{< a c >}}d" , "abcd" , wt )
2016-01-04 17:48:05 -05:00
}
2014-02-25 23:57:31 -05:00
func TestPositionalParamSC ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2019-12-10 13:56:44 -05:00
wt := func ( tem tpl . TemplateManager ) error {
2017-03-27 14:43:49 -04:00
tem . AddTemplate ( "_internal/shortcodes/video.html" , ` Playing Video {{ .Get 0 }} ` )
2016-08-07 08:03:03 -04:00
return nil
}
2014-02-25 23:57:31 -05:00
2016-08-07 08:03:03 -04:00
CheckShortCodeMatch ( t , "{{< video 47238zzb >}}" , "Playing Video 47238zzb" , wt )
CheckShortCodeMatch ( t , "{{< video 47238zzb 132 >}}" , "Playing Video 47238zzb" , wt )
CheckShortCodeMatch ( t , "{{<video 47238zzb>}}" , "Playing Video 47238zzb" , wt )
CheckShortCodeMatch ( t , "{{<video 47238zzb >}}" , "Playing Video 47238zzb" , wt )
CheckShortCodeMatch ( t , "{{< video 47238zzb >}}" , "Playing Video 47238zzb" , wt )
2014-02-25 23:57:31 -05:00
}
2015-08-07 13:21:26 -04:00
func TestPositionalParamIndexOutOfBounds ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2019-12-10 13:56:44 -05:00
wt := func ( tem tpl . TemplateManager ) error {
2018-04-17 05:29:00 -04:00
tem . AddTemplate ( "_internal/shortcodes/video.html" , ` Playing Video {{ with .Get 1 }} {{ . }} {{ else }} Missing {{ end }} ` )
2016-08-07 08:03:03 -04:00
return nil
}
2018-04-17 05:29:00 -04:00
CheckShortCodeMatch ( t , "{{< video 47238zzb >}}" , "Playing Video Missing" , wt )
2015-08-07 13:21:26 -04:00
}
2018-08-14 12:11:36 -04:00
// #5071
func TestShortcodeRelated ( t * testing . T ) {
t . Parallel ( )
2019-12-10 13:56:44 -05:00
wt := func ( tem tpl . TemplateManager ) error {
2018-08-14 12:11:36 -04:00
tem . AddTemplate ( "_internal/shortcodes/a.html" , ` {{ len ( .Site .RegularPages .Related .Page ) }} ` )
return nil
}
CheckShortCodeMatch ( t , "{{< a >}}" , "0" , wt )
}
2019-01-02 06:33:26 -05:00
func TestShortcodeInnerMarkup ( t * testing . T ) {
t . Parallel ( )
2019-12-10 13:56:44 -05:00
wt := func ( tem tpl . TemplateManager ) error {
2019-01-02 06:33:26 -05:00
tem . AddTemplate ( "shortcodes/a.html" , ` <div> {{ .Inner }} </div> ` )
tem . AddTemplate ( "shortcodes/b.html" , ` **Bold**: <div> {{ .Inner }} </div> ` )
return nil
}
CheckShortCodeMatch ( t ,
"{{< a >}}B: <div>{{% b %}}**Bold**{{% /b %}}</div>{{< /a >}}" ,
// This assertion looks odd, but is correct: for inner shortcodes with
// the {{% we treats the .Inner content as markup, but not the shortcode
// itself.
"<div>B: <div>**Bold**: <div><strong>Bold</strong></div></div></div>" ,
wt )
CheckShortCodeMatch ( t ,
"{{% b %}}This is **B**: {{< b >}}This is B{{< /b>}}{{% /b %}}" ,
"<strong>Bold</strong>: <div>This is <strong>B</strong>: <strong>Bold</strong>: <div>This is B</div></div>" ,
wt )
}
2015-08-07 14:08:23 -04:00
// some repro issues for panics in Go Fuzz testing
2014-02-25 23:57:31 -05:00
func TestNamedParamSC ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2019-12-10 13:56:44 -05:00
wt := func ( tem tpl . TemplateManager ) error {
2017-03-27 14:43:49 -04:00
tem . AddTemplate ( "_internal/shortcodes/img.html" , ` <img {{ with .Get "src" }} src=" {{ . }} " {{ end }} {{ with .Get "class" }} class=" {{ . }} " {{ end }} > ` )
2016-08-07 08:03:03 -04:00
return nil
}
CheckShortCodeMatch ( t , ` {{ < img src = "one" > }} ` , ` <img src="one"> ` , wt )
CheckShortCodeMatch ( t , ` {{ < img class = "aspen" > }} ` , ` <img class="aspen"> ` , wt )
CheckShortCodeMatch ( t , ` {{ < img src = "one" > }} ` , ` <img src="one"> ` , wt )
CheckShortCodeMatch ( t , ` {{ < img src = "one" > }} ` , ` <img src="one"> ` , wt )
CheckShortCodeMatch ( t , ` {{ < img src = "one" > }} ` , ` <img src="one"> ` , wt )
CheckShortCodeMatch ( t , ` {{ < img src = "one" class = "aspen grove" > }} ` , ` <img src="one" class="aspen grove"> ` , wt )
2014-02-25 23:57:31 -05:00
}
2016-07-21 11:18:55 -04:00
// Issue #2294
func TestNestedNamedMissingParam ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2019-12-10 13:56:44 -05:00
wt := func ( tem tpl . TemplateManager ) error {
2017-03-27 14:43:49 -04:00
tem . AddTemplate ( "_internal/shortcodes/acc.html" , ` <div class="acc"> {{ .Inner }} </div> ` )
tem . AddTemplate ( "_internal/shortcodes/div.html" , ` <div {{ with .Get "class" }} class=" {{ . }} " {{ end }} > {{ .Inner }} </div> ` )
tem . AddTemplate ( "_internal/shortcodes/div2.html" , ` <div {{ with .Get 0 }} class=" {{ . }} " {{ end }} > {{ .Inner }} </div> ` )
2016-08-07 08:03:03 -04:00
return nil
}
2016-07-21 11:18:55 -04:00
CheckShortCodeMatch ( t ,
2016-07-21 15:03:44 -04:00
` {{ % acc % }} {{ % div % }} d1 {{ % / div % }} {{ % div2 % }} d2 {{ % / div2 % }} {{ % / acc % }} ` ,
2019-01-02 06:33:26 -05:00
"<div class=\"acc\"><div >d1</div><div >d2</div></div>" , wt )
2016-07-21 11:18:55 -04:00
}
2015-11-20 19:59:54 -05:00
func TestIsNamedParamsSC ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2019-12-10 13:56:44 -05:00
wt := func ( tem tpl . TemplateManager ) error {
2018-05-21 11:47:52 -04:00
tem . AddTemplate ( "_internal/shortcodes/bynameorposition.html" , ` {{ with .Get "id" }} Named: {{ . }} {{ else }} Pos: {{ .Get 0 }} {{ end }} ` )
2017-03-27 14:43:49 -04:00
tem . AddTemplate ( "_internal/shortcodes/ifnamedparams.html" , ` <div id=" {{ if .IsNamedParams }} {{ .Get "id" }} {{ else }} {{ .Get 0 }} {{ end }} "> ` )
2016-08-07 08:03:03 -04:00
return nil
}
CheckShortCodeMatch ( t , ` {{ < ifnamedparams id = "name" > }} ` , ` <div id="name"> ` , wt )
CheckShortCodeMatch ( t , ` {{ < ifnamedparams position > }} ` , ` <div id="position"> ` , wt )
2018-05-21 11:47:52 -04:00
CheckShortCodeMatch ( t , ` {{ < bynameorposition id = "name" > }} ` , ` Named: name ` , wt )
CheckShortCodeMatch ( t , ` {{ < bynameorposition position > }} ` , ` Pos: position ` , wt )
2015-11-20 19:59:54 -05:00
}
2014-02-25 23:57:31 -05:00
func TestInnerSC ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2019-12-10 13:56:44 -05:00
wt := func ( tem tpl . TemplateManager ) error {
2017-03-27 14:43:49 -04:00
tem . AddTemplate ( "_internal/shortcodes/inside.html" , ` <div {{ with .Get "class" }} class=" {{ . }} " {{ end }} > {{ .Inner }} </div> ` )
2016-08-07 08:03:03 -04:00
return nil
}
CheckShortCodeMatch ( t , ` {{ < inside class = "aspen" > }} ` , ` <div class="aspen"></div> ` , wt )
CheckShortCodeMatch ( t , ` {{ < inside class = "aspen" > }} More Here {{ < / inside > }} ` , "<div class=\"aspen\">More Here</div>" , wt )
CheckShortCodeMatch ( t , ` {{ < inside > }} More Here {{ < / inside > }} ` , "<div>More Here</div>" , wt )
2014-08-28 12:48:31 -04:00
}
func TestInnerSCWithMarkdown ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2019-12-10 13:56:44 -05:00
wt := func ( tem tpl . TemplateManager ) error {
2019-01-02 06:33:26 -05:00
// Note: In Hugo 0.55 we made it so any outer {{%'s inner content was rendered as part of the surrounding
// markup. This solved lots of problems, but it also meant that this test had to be adjusted.
tem . AddTemplate ( "_internal/shortcodes/wrapper.html" , ` <div {{ with .Get "class" }} class=" {{ . }} " {{ end }} > {{ .Inner }} </div> ` )
tem . AddTemplate ( "_internal/shortcodes/inside.html" , ` {{ .Inner }} ` )
2016-08-07 08:03:03 -04:00
return nil
}
2019-01-02 06:33:26 -05:00
CheckShortCodeMatch ( t , ` { { < wrapper > } } { { % inside % } }
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
# More Here
[ link ] ( http : //spf13.com) and text
2019-01-02 06:33:26 -05:00
{ { % / inside % } } { { < / wrapper > } } ` , "<div><h1 id=\"more-here\">More Here</h1>\n\n<p><a href=\"http://spf13.com\">link</a> and text</p>\n</div>" , wt )
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
func TestEmbeddedSC ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2018-10-02 13:56:31 -04:00
CheckShortCodeMatch ( t , ` {{ % figure src = "/found/here" class = "bananas orange" % }} ` , "<figure class=\"bananas orange\">\n <img src=\"/found/here\"/> \n</figure>" , nil )
CheckShortCodeMatch ( t , ` {{ % figure src = "/found/here" class = "bananas orange" caption = "This is a caption" % }} ` , "<figure class=\"bananas orange\">\n <img src=\"/found/here\"\n alt=\"This is a caption\"/> <figcaption>\n <p>This is a caption</p>\n </figcaption>\n</figure>" , nil )
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 TestNestedSC ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2019-12-10 13:56:44 -05:00
wt := func ( tem tpl . TemplateManager ) error {
2017-03-27 14:43:49 -04:00
tem . AddTemplate ( "_internal/shortcodes/scn1.html" , ` <div>Outer, inner is {{ .Inner }} </div> ` )
tem . AddTemplate ( "_internal/shortcodes/scn2.html" , ` <div>SC2</div> ` )
2016-08-07 08:03:03 -04:00
return nil
}
2019-01-02 06:33:26 -05:00
CheckShortCodeMatch ( t , ` {{ % scn1 % }} {{ % scn2 % }} {{ % / scn1 % }} ` , "<div>Outer, inner is <div>SC2</div></div>" , wt )
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
2016-08-07 08:03:03 -04:00
CheckShortCodeMatch ( t , ` {{ < scn1 > }} {{ % scn2 % }} {{ < / scn1 > }} ` , "<div>Outer, inner is <div>SC2</div></div>" , wt )
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 TestNestedComplexSC ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2019-12-10 13:56:44 -05:00
wt := func ( tem tpl . TemplateManager ) error {
2017-03-27 14:43:49 -04:00
tem . AddTemplate ( "_internal/shortcodes/row.html" , ` -row- {{ .Inner }} -rowStop- ` )
tem . AddTemplate ( "_internal/shortcodes/column.html" , ` -col- {{ .Inner }} -colStop- ` )
tem . AddTemplate ( "_internal/shortcodes/aside.html" , ` -aside- {{ .Inner }} -asideStop- ` )
2016-08-07 08:03:03 -04:00
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
CheckShortCodeMatch ( t , ` {{ < row > }} 1-s {{ % column % }} 2-**s** {{ < aside > }} 3-**s** {{ < / aside > }} 4-s {{ % / column % }} 5-s {{ < / row > }} 6-s ` ,
2016-08-07 08:03:03 -04:00
"-row-1-s-col-2-<strong>s</strong>-aside-3-<strong>s</strong>-asideStop-4-s-colStop-5-s-rowStop-6-s" , wt )
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
// turn around the markup flag
CheckShortCodeMatch ( t , ` {{ % row % }} 1-s {{ < column > }} 2-**s** {{ % aside % }} 3-**s** {{ % / aside % }} 4-s {{ < / column > }} 5-s {{ % / row % }} 6-s ` ,
2016-08-07 08:03:03 -04:00
"-row-1-s-col-2-<strong>s</strong>-aside-3-<strong>s</strong>-asideStop-4-s-colStop-5-s-rowStop-6-s" , wt )
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
}
2016-03-08 14:56:24 -05:00
func TestParentShortcode ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2019-12-10 13:56:44 -05:00
wt := func ( tem tpl . TemplateManager ) error {
2017-03-27 14:43:49 -04:00
tem . AddTemplate ( "_internal/shortcodes/r1.html" , ` 1: {{ .Get "pr1" }} {{ .Inner }} ` )
tem . AddTemplate ( "_internal/shortcodes/r2.html" , ` 2: {{ .Parent .Get "pr1" }} {{ .Get "pr2" }} {{ .Inner }} ` )
tem . AddTemplate ( "_internal/shortcodes/r3.html" , ` 3: {{ .Parent .Parent .Get "pr1" }} {{ .Parent .Get "pr2" }} {{ .Get "pr3" }} {{ .Inner }} ` )
2016-08-07 08:03:03 -04:00
return nil
}
2016-03-08 14:56:24 -05:00
CheckShortCodeMatch ( t , ` {{ < r1 pr1 = "p1" > }} 1: {{ < r2 pr2 = "p2" > }} 2: {{ < r3 pr3 = "p3" > }} {{ < / r3 > }} {{ < / r2 > }} {{ < / r1 > }} ` ,
2016-08-07 08:03:03 -04:00
"1: p1 1: 2: p1p2 2: 3: p1p2p3 " , wt )
2016-03-08 14:56:24 -05:00
}
2018-02-05 14:06:29 -05:00
func TestFigureOnlySrc ( t * testing . T ) {
t . Parallel ( )
2018-10-02 13:56:31 -04:00
CheckShortCodeMatch ( t , ` {{ < figure src = "/found/here" > }} ` , "<figure>\n <img src=\"/found/here\"/> \n</figure>" , nil )
2018-02-05 14:06:29 -05:00
}
2018-10-02 15:23:10 -04:00
func TestFigureCaptionAttrWithMarkdown ( t * testing . T ) {
t . Parallel ( )
CheckShortCodeMatch ( t , ` {{ < figure src = "/found/here" caption = "Something **bold** _italic_" > }} ` , "<figure>\n <img src=\"/found/here\"\n alt=\"Something bold italic\"/> <figcaption>\n <p>Something <strong>bold</strong> <em>italic</em></p>\n </figcaption>\n</figure>" , nil )
CheckShortCodeMatch ( t , ` {{ < figure src = "/found/here" attr = "Something **bold** _italic_" > }} ` , "<figure>\n <img src=\"/found/here\"/> <figcaption>\n <p>Something <strong>bold</strong> <em>italic</em></p>\n </figcaption>\n</figure>" , nil )
}
2014-10-08 10:54:50 -04:00
func TestFigureImgWidth ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2018-10-02 13:56:31 -04:00
CheckShortCodeMatch ( t , ` {{ % figure src = "/found/here" class = "bananas orange" alt = "apple" width = "100px" % }} ` , "<figure class=\"bananas orange\">\n <img src=\"/found/here\"\n alt=\"apple\" width=\"100px\"/> \n</figure>" , nil )
2014-10-08 10:54:50 -04:00
}
2017-10-27 11:37:54 -04:00
func TestFigureImgHeight ( t * testing . T ) {
t . Parallel ( )
2018-10-02 13:56:31 -04:00
CheckShortCodeMatch ( t , ` {{ % figure src = "/found/here" class = "bananas orange" alt = "apple" height = "100px" % }} ` , "<figure class=\"bananas orange\">\n <img src=\"/found/here\"\n alt=\"apple\" height=\"100px\"/> \n</figure>" , nil )
2017-10-27 11:37:54 -04:00
}
func TestFigureImgWidthAndHeight ( t * testing . T ) {
t . Parallel ( )
2018-10-02 13:56:31 -04:00
CheckShortCodeMatch ( t , ` {{ % figure src = "/found/here" class = "bananas orange" alt = "apple" width = "50" height = "100" % }} ` , "<figure class=\"bananas orange\">\n <img src=\"/found/here\"\n alt=\"apple\" width=\"50\" height=\"100\"/> \n</figure>" , nil )
2017-10-27 11:37:54 -04:00
}
2018-02-05 14:06:29 -05:00
func TestFigureLinkNoTarget ( t * testing . T ) {
t . Parallel ( )
2018-10-02 13:56:31 -04:00
CheckShortCodeMatch ( t , ` {{ < figure src = "/found/here" link = "/jump/here/on/clicking" > }} ` , "<figure><a href=\"/jump/here/on/clicking\">\n <img src=\"/found/here\"/> </a>\n</figure>" , nil )
2018-02-05 14:06:29 -05:00
}
func TestFigureLinkWithTarget ( t * testing . T ) {
t . Parallel ( )
2018-10-02 13:56:31 -04:00
CheckShortCodeMatch ( t , ` {{ < figure src = "/found/here" link = "/jump/here/on/clicking" target = "_self" > }} ` , "<figure><a href=\"/jump/here/on/clicking\" target=\"_self\">\n <img src=\"/found/here\"/> </a>\n</figure>" , nil )
2018-02-05 14:06:29 -05:00
}
func TestFigureLinkWithTargetAndRel ( t * testing . T ) {
t . Parallel ( )
2018-10-02 13:56:31 -04:00
CheckShortCodeMatch ( t , ` {{ < figure src = "/found/here" link = "/jump/here/on/clicking" target = "_blank" rel = "noopener" > }} ` , "<figure><a href=\"/jump/here/on/clicking\" target=\"_blank\" rel=\"noopener\">\n <img src=\"/found/here\"/> </a>\n</figure>" , nil )
2018-02-05 14:06:29 -05:00
}
2018-08-17 03:01:02 -04:00
// #1642
func TestShortcodeWrappedInPIssue ( t * testing . T ) {
t . Parallel ( )
2019-12-10 13:56:44 -05:00
wt := func ( tem tpl . TemplateManager ) error {
2018-08-17 03:01:02 -04:00
tem . AddTemplate ( "_internal/shortcodes/bug.html" , ` xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ` )
return nil
}
CheckShortCodeMatch ( t , `
{ { < bug > } }
{ { < bug > } }
` , "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" , wt )
}
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
/ * errCheck := func ( s string ) func ( name string , assert * require . Assertions , shortcode * shortcode , err error ) {
return func ( name string , assert * require . Assertions , shortcode * shortcode , err error ) {
2019-08-10 15:05:17 -04:00
c . Assert ( err , name , qt . Not ( qt . IsNil ) )
c . Assert ( err . Error ( ) , name , qt . Equals , s )
2018-10-18 04:21:23 -04:00
}
2019-01-02 06:33:26 -05:00
} * /
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:{}" ) } ,
{ "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->}" ) } ,
{ "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
counter := 0
placeholderFunc := func ( ) string {
counter ++
return fmt . Sprintf ( "HAHA%s-%dHBHB" , shortcodePlaceholderPrefix , counter )
}
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 )
2019-01-02 06:33:26 -05:00
handler := newShortcodeHandler ( nil , s , placeholderFunc )
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
2019-01-02 06:33:26 -05:00
short , err := handler . extractShortcode ( 0 , 0 , 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 )
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
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
}
2016-01-09 10:11:38 -05:00
func TestShortcodesInSite ( t * testing . T ) {
baseURL := "http://foo/bar"
2016-09-08 15:23:01 -04:00
2016-01-09 10:11:38 -05:00
tests := [ ] struct {
contentPath string
content string
outFile string
2019-01-02 06:33:26 -05:00
expected interface { }
2016-01-09 10:11:38 -05:00
} {
{ "sect/doc1.md" , ` a {{ < b > }} c ` ,
2017-01-10 04:55:03 -05:00
filepath . FromSlash ( "public/sect/doc1/index.html" ) , "<p>abc</p>\n" } ,
2016-01-09 10:11:38 -05:00
// Issue #1642: Multiple shortcodes wrapped in P
// Deliberately forced to pass even if they maybe shouldn't.
{ "sect/doc2.md" , ` a
{ { < b > } }
{ { < c > } }
{ { < d > } }
e ` ,
2017-01-10 04:55:03 -05:00
filepath . FromSlash ( "public/sect/doc2/index.html" ) ,
2016-01-09 10:11:38 -05:00
"<p>a</p>\n\n<p>b<br />\nc\nd</p>\n\n<p>e</p>\n" } ,
{ "sect/doc3.md" , ` a
{ { < b > } }
{ { < c > } }
{ { < d > } }
e ` ,
2017-01-10 04:55:03 -05:00
filepath . FromSlash ( "public/sect/doc3/index.html" ) ,
2016-01-09 10:11:38 -05:00
"<p>a</p>\n\n<p>b<br />\nc</p>\n\nd\n\n<p>e</p>\n" } ,
{ "sect/doc4.md" , ` a
{ { < b > } }
{ { < b > } }
{ { < b > } }
{ { < b > } }
{ { < b > } }
` ,
2017-01-10 04:55:03 -05:00
filepath . FromSlash ( "public/sect/doc4/index.html" ) ,
2016-01-09 10:11:38 -05:00
"<p>a\nb\nb\nb\nb\nb</p>\n" } ,
2016-06-13 13:10:53 -04:00
// #2192 #2209: Shortcodes in markdown headers
{ "sect/doc5.md" , ` # { { < b > } }
# # { { % c % } } ` ,
2019-01-02 06:33:26 -05:00
filepath . FromSlash ( "public/sect/doc5/index.html" ) , ` -hbhb">b</h1> ` } ,
2016-09-08 15:23:01 -04:00
// #2223 pygments
2017-09-29 04:10:13 -04:00
{ "sect/doc6.md" , "\n```bash\nb = {{< b >}} c = {{% c %}}\n```\n" ,
2017-01-10 04:55:03 -05:00
filepath . FromSlash ( "public/sect/doc6/index.html" ) ,
2017-09-29 04:10:13 -04:00
` <span class="nv">b</span> ` } ,
2016-07-03 18:33:08 -04:00
// #2249
{ "sect/doc7.ad" , ` _Shortcodes:_ *b: {{ < b > }} c: {{ % c % }} * ` ,
2017-01-10 04:55:03 -05:00
filepath . FromSlash ( "public/sect/doc7/index.html" ) ,
2016-07-03 18:33:08 -04:00
"<div class=\"paragraph\">\n<p><em>Shortcodes:</em> <strong>b: b c: c</strong></p>\n</div>\n" } ,
2016-07-04 04:49:20 -04:00
{ "sect/doc8.rst" , ` **Shortcodes:** *b: {{ < b > }} c: {{ % c % }} * ` ,
2017-01-10 04:55:03 -05:00
filepath . FromSlash ( "public/sect/doc8/index.html" ) ,
2016-07-04 04:49:20 -04:00
"<div class=\"document\">\n\n\n<p><strong>Shortcodes:</strong> <em>b: b c: c</em></p>\n</div>" } ,
2016-08-01 17:04:44 -04:00
{ "sect/doc9.mmark" , `
-- -
menu :
main :
parent : ' parent '
-- -
* * Shortcodes : * * * b : { { < b > } } c : { { % c % } } * ` ,
2017-01-10 04:55:03 -05:00
filepath . FromSlash ( "public/sect/doc9/index.html" ) ,
2016-07-19 18:40:40 -04:00
"<p><strong>Shortcodes:</strong> <em>b: b c: c</em></p>\n" } ,
2016-08-01 17:04:44 -04:00
// Issue #1229: Menus not available in shortcode.
{ "sect/doc10.md" , ` -- -
menu :
main :
identifier : ' parent '
tags :
- Menu
-- -
* * Menus : * * { { < menu > } } ` ,
2017-01-10 04:55:03 -05:00
filepath . FromSlash ( "public/sect/doc10/index.html" ) ,
2016-08-01 17:04:44 -04:00
"<p><strong>Menus:</strong> 1</p>\n" } ,
// Issue #2323: Taxonomies not available in shortcode.
{ "sect/doc11.md" , ` -- -
tags :
- Bugs
-- -
* * Tags : * * { { < tags > } } ` ,
2017-01-10 04:55:03 -05:00
filepath . FromSlash ( "public/sect/doc11/index.html" ) ,
2016-08-01 17:04:44 -04:00
"<p><strong>Tags:</strong> 2</p>\n" } ,
2019-04-24 08:05:37 -04:00
{ "sect/doc12.md" , ` -- -
title : "Foo"
-- -
{ { % html - indented - v1 % } } ` ,
"public/sect/doc12/index.html" ,
"<h1>Hugo!</h1>" } ,
2016-01-09 10:11:38 -05:00
}
2019-08-16 09:55:03 -04:00
temp := tests [ : 0 ]
for _ , test := range tests {
if strings . HasSuffix ( test . contentPath , ".ad" ) && ! asciidoc . Supports ( ) {
t . Log ( "Skip Asciidoc test case as no Asciidoc present." )
continue
} else if strings . HasSuffix ( test . contentPath , ".rst" ) && ! rst . Supports ( ) {
t . Log ( "Skip Rst test case as no rst2html present." )
continue
}
temp = append ( temp , test )
}
tests = temp
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
sources := make ( [ ] [ 2 ] string , len ( tests ) )
2016-01-09 10:11:38 -05:00
for i , test := range tests {
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
sources [ i ] = [ 2 ] string { filepath . FromSlash ( test . contentPath ) , test . content }
2016-01-09 10:11:38 -05:00
}
2019-12-10 13:56:44 -05:00
addTemplates := func ( templ tpl . TemplateManager ) error {
2019-01-02 06:33:26 -05:00
templ . AddTemplate ( "_default/single.html" , "{{.Content}} Word Count: {{ .WordCount }}" )
2016-03-09 08:05:31 -05:00
2017-03-27 14:43:49 -04:00
templ . AddTemplate ( "_internal/shortcodes/b.html" , ` b ` )
templ . AddTemplate ( "_internal/shortcodes/c.html" , ` c ` )
templ . AddTemplate ( "_internal/shortcodes/d.html" , ` d ` )
2019-04-24 08:05:37 -04:00
templ . AddTemplate ( "_internal/shortcodes/html-indented-v1.html" , "{{ $_hugo_config := `{ \"version\": 1 }` }}" + `
< h1 > Hugo ! < / h1 >
` )
2017-03-27 14:43:49 -04:00
templ . AddTemplate ( "_internal/shortcodes/menu.html" , ` {{ len ( index .Page .Menus "main" ) .Children }} ` )
templ . AddTemplate ( "_internal/shortcodes/tags.html" , ` {{ len .Page .Site .Taxonomies .tags }} ` )
2016-03-09 08:05:31 -05:00
2016-07-28 03:30:58 -04:00
return nil
2016-03-09 08:05:31 -05:00
2016-07-28 03:30:58 -04:00
}
2016-01-09 10:11:38 -05:00
2017-02-04 22:20:06 -05:00
cfg , fs := newTestCfg ( )
cfg . Set ( "defaultContentLanguage" , "en" )
cfg . Set ( "baseURL" , baseURL )
cfg . Set ( "uglyURLs" , false )
cfg . Set ( "verbose" , true )
cfg . Set ( "pygmentsUseClasses" , true )
cfg . Set ( "pygmentsCodefences" , true )
2019-11-06 14:10:47 -05:00
cfg . Set ( "markup" , map [ string ] interface { } {
"defaultMarkdownHandler" : "blackfriday" , // TODO(bep)
} )
2016-01-09 10:11:38 -05:00
2017-01-10 04:55:03 -05:00
writeSourcesToSource ( t , "content" , fs , sources ... )
2016-07-28 03:30:58 -04:00
2017-02-04 22:20:06 -05:00
s := buildSingleSite ( t , deps . DepsCfg { WithTemplate : addTemplates , Fs : fs , Cfg : cfg } , BuildCfg { } )
2016-07-04 04:49:20 -04:00
2019-01-02 06:33:26 -05:00
for i , test := range tests {
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 ( fmt . Sprintf ( "test=%d;contentPath=%s" , i , test . contentPath ) , 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-01-02 06:33:26 -05:00
2019-08-10 15:05:17 -04:00
th := newTestHelper ( s . Cfg , s . Fs , t )
2019-01-02 06:33:26 -05:00
expected := cast . ToStringSlice ( test . expected )
2019-11-06 14:10:47 -05:00
2019-04-24 08:05:37 -04:00
th . assertFileContent ( filepath . FromSlash ( test . outFile ) , expected ... )
2019-01-02 06:33:26 -05:00
} )
2016-01-09 10:11:38 -05:00
}
}
2017-05-06 14:15:28 -04:00
func TestShortcodeMultipleOutputFormats ( t * testing . T ) {
t . Parallel ( )
siteConfig := `
baseURL = "http://example.com/blog"
paginate = 1
disableKinds = [ "section" , "taxonomy" , "taxonomyTerm" , "RSS" , "sitemap" , "robotsTXT" , "404" ]
[ 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 ]
2019-01-02 06:33:26 -05:00
home := s . getPage ( page . 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
replacements map [ string ] string
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
}
2016-03-23 05:10:28 -04:00
var in = make ( [ ] input , b . N * len ( data ) )
2015-11-13 15:21:03 -05:00
var cnt = 0
for i := 0 ; i < b . N ; i ++ {
for _ , this := range data {
in [ cnt ] = input { [ ] byte ( this . input ) , this . replacements , this . expect }
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
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 ++
2019-01-02 06:33:26 -05:00
results , err := replaceShortcodeTokens ( currIn . in , currIn . replacements )
2015-05-19 16:00:48 -04:00
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
}
}
}
}
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
expect interface { }
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" } ,
{ "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" ,
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
2019-01-02 06:33:26 -05:00
results , err := replaceShortcodeTokens ( [ ] byte ( this . input ) , this . replacements )
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 ( )
2019-04-12 04:44:21 -04:00
b := newTestSitesBuilder ( t ) . Running ( ) . WithSimpleConfigFile ( )
b . WithTemplatesAdded (
"index.html" , `
{ { $ b := . Site . GetPage "b1" } }
2019-04-15 06:06:12 -04:00
b1 Content : { { $ b . Content } }
2019-04-12 04:44:21 -04:00
{ { $ p := $ b . Resources . GetMatch "p1*" } }
Content : { { $ p . Content } }
{ { $ article := . Site . GetPage "blog/article" } }
Article Content : { { $ article . Content } }
` ,
"shortcodes/c.html" , `
2019-04-12 04:26:10 -04:00
{ { range . Page . Parent . Resources } }
2019-04-12 04:44:21 -04:00
* Parent resource : { { . Name } } : { { . RelPermalink } }
2019-04-12 04:26:10 -04:00
{ { end } }
` )
2019-04-12 04:44:21 -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
2019-04-12 04:44:21 -04:00
`
b . WithContent ( "b1/index.md" , pageContent ,
2019-04-12 04:26:10 -04:00
"b1/logo.png" , "PNG logo" ,
2019-04-12 04:44:21 -04:00
"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
)
b . Build ( BuildCfg { } )
2019-04-15 06:06:12 -04:00
assert := func ( matchers ... string ) {
allMatchers := append ( matchers , "Parent resource: logo.png: /b1/logo.png" ,
2019-04-12 04:44:21 -04:00
"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" ,
allMatchers ... ,
)
2019-04-12 04:44:21 -04:00
}
assert ( )
2019-04-15 06:06:12 -04:00
b . EditFiles ( "content/b1/index.md" , pageContent + " Edit." )
2019-04-12 04:44:21 -04:00
b . Build ( BuildCfg { } )
2019-04-15 06:06:12 -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 ` )
builder . AssertFileContent ( "public/en/p1/index.html" , ` outer ordinal : 5 inner :
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 :
`
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-01-31 05:53:21 -05:00
b . WithTemplatesAdded ( "layouts/index.html" , `
CONTENT : { { . Content } }
` )
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]" ,
}
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
2019-11-14 03:57:37 -05:00
// https://github.com/gohugoio/hugo/issues/6504
func TestShortcodeEmoji ( t * testing . T ) {
t . Parallel ( )
v := viper . New ( )
v . Set ( "enableEmoji" , true )
builder := newTestSitesBuilder ( t ) . WithViper ( v )
builder . WithContent ( "page.md" , ` -- -
title : "Hugo Rocks!"
-- -
# doc
{ { < event > } } 10 : 30 - 11 : 00 My : smile : Event { { < / event > } }
` ) . WithTemplatesAdded (
"layouts/shortcodes/event.html" , ` <div> {{ "\u29BE" }} {{ .Inner }} </div> ` )
builder . Build ( BuildCfg { } )
builder . AssertFileContent ( "public/page/index.html" ,
"⦾ 10:30-11:00 My 😄 Event" ,
)
}
2019-09-29 08:51:51 -04:00
func TestShortcodeTypedParams ( t * testing . T ) {
t . Parallel ( )
c := qt . New ( t )
builder := newTestSitesBuilder ( t ) . WithSimpleConfigFile ( )
builder . WithContent ( "page.md" , ` -- -
title : "Hugo Rocks!"
-- -
# 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" > } }
` ) . WithTemplatesAdded (
"layouts/shortcodes/hello.html" ,
` { { range $ i , $ v := . Params } }
- { { printf "%v: %v (%T)" $ i $ v $ v } }
{ { end } }
{ { $ b1 := . Get "b1" } }
Get : { { printf "%v (%T)" $ b1 $ b1 | safeHTML } }
` ) . Build ( BuildCfg { } )
s := builder . H . Sites [ 0 ]
c . Assert ( len ( s . RegularPages ( ) ) , qt . Equals , 1 )
builder . AssertFileContent ( "public/page/index.html" ,
"types positional: - 0: true (bool) - 1: false (bool) - 2: 33 (int) - 3: 3.14 (float64)" ,
"types named: - b1: true (bool) - b2: false (bool) - f1: 3.14 (float64) - i1: 33 (int) Get: true (bool) " ,
"types string: - 0: true (string) - 1: trues (string) - 2: 33 (string) - 3: 3.14 (string) " ,
)
}
2019-11-06 03:20:59 -05:00
func TestShortcodeRef ( t * testing . T ) {
for _ , plainIDAnchors := range [ ] bool { false , true } {
plainIDAnchors := plainIDAnchors
t . Run ( fmt . Sprintf ( "plainIDAnchors=%t" , plainIDAnchors ) , func ( t * testing . T ) {
t . Parallel ( )
v := viper . New ( )
v . Set ( "baseURL" , "https://example.org" )
v . Set ( "blackfriday" , map [ string ] interface { } {
"plainIDAnchors" : plainIDAnchors ,
} )
2019-11-06 14:10:47 -05:00
v . Set ( "markup" , map [ string ] interface { } {
"defaultMarkdownHandler" : "blackfriday" , // TODO(bep)
} )
2019-11-06 03:20:59 -05:00
builder := newTestSitesBuilder ( t ) . WithViper ( v )
for i := 1 ; i <= 2 ; i ++ {
builder . WithContent ( fmt . Sprintf ( "page%d.md" , i ) , ` -- -
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
` )
}
builder . Build ( BuildCfg { } )
if plainIDAnchors {
builder . AssertFileContent ( "public/page2/index.html" ,
`
< 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 >
` ,
)
} else {
builder . AssertFileContent ( "public/page2/index.html" ,
`
< p > < a href = "https://example.org/page1/" > Page 1 < / a >
< a href = "/page1/#doc:45ca767ba77bc1445a0acab74f80812f" > Page 1 with anchor < / a >
< a href = "https://example.org/page2/" > Page 2 < / a >
< a href = "/page2/#doc:8e3cdf52fa21e33270c99433820e46bd" > Page 2 with anchor < / a > < / p >
< h2 id = "doc:8e3cdf52fa21e33270c99433820e46bd" > Doc < / h2 >
` ,
)
}
} )
}
}