Commit graph

93 commits

Author SHA1 Message Date
Bjørn Erik Pedersen 3decf4a327
hugolib: Add zero-based Ordinal to shortcode
The count starts at 0 relative to the shortcode's parent: Either the page or the surrounding shortcode.

Access it in a shortcode like this:

```bash
Ordinal is {{ .Ordinal }}
```

Note that this is a shared ordinal for all shortcodes in the relevant context, so, as an example, you have this in a content page:

```markdown
This is a shortcode:

{{< hello >}}

This is another shortcode:

{{< hugo >}}

The `.Ordinal` you get in the two shortcodes above is 0 and 1.
```

See #3359
2018-04-23 08:09:56 +02:00
Bjørn Erik Pedersen 85535084de
hugolib: Process and render shortcodes in their order of appearance
Fixes #3359
2018-04-22 17:40:51 +02:00
Bjørn Erik Pedersen 4d26ab33dc
Make .Content (almost) always available in shortcodes
This resolves some surprising behaviour when reading other pages' content from shortcodes. Before this commit, that behaviour was undefined. Note that this has never been an issue from regular templates.

It will still not be possible to get **the current shortcode's  page's rendered content**. That would have impressed Einstein.

The new and well defined rules are:

* `.Page.Content` from a shortcode will be empty. The related `.Page.Truncated` `.Page.Summary`, `.Page.WordCount`, `.Page.ReadingTime`, `.Page.Plain` and `.Page.PlainWords` will also have empty values.
* For _other pages_ (retrieved via `.Page.Site.GetPage`, `.Site.Pages` etc.) the `.Content` is there to use as you please as long as you don't have infinite content recursion in your shortcode/content setup. See below.
* `.Page.TableOfContents` is good to go (but does not support shortcodes in headlines; this is unchanged)

If you get into a situation of infinite recursion, the `.Content` will be empty. Run `hugo -v` for more information.

Fixes #4632
Fixes #4653
Fixes #4655
2018-04-21 22:02:56 +02:00
cmal 236f0c840b .Get doesn't crash on missing positional param
fixes #4619
2018-04-17 15:39:33 +02:00
Bjørn Erik Pedersen aa6b1b9be7 output: Support templates per site/language
This applies to both regular templates and shortcodes. So, if the site language is French and the output format is AMP, this is the (start) of the lookup order for the home page:

1. index.fr.amp.html
2. index.amp.html
3. index.fr.html
4. index.html
5. ...

Fixes #3360
2017-07-04 09:12:44 +02:00
Bjørn Erik Pedersen d8717cd4c7 all: Update import paths to gohugoio/hugo 2017-06-13 18:42:45 +02:00
Bjørn Erik Pedersen af72db806f hugolib: Handle shortcode per output format
This commit allows shortcode per output format, a typical use case would be the special AMP media tags.

Note that this will only re-render the "overridden" shortcodes and only  in pages where these are used, so performance in the normal case should not suffer.

Closes #3220
2017-05-13 22:44:15 +03:00
Bjørn Erik Pedersen 2bcbf10400 hugolib: Prepare render per output format
See #3220
2017-05-13 22:44:15 +03:00
Bjørn Erik Pedersen 58d9cbd31b hugolib: Improve shortcode error message 2017-05-09 18:43:48 +02:00
Bjørn Erik Pedersen 8a49c0b3b8 tpl/collections: Make it a package that stands on its own
See #3042
2017-05-01 15:13:41 +02:00
Bjørn Erik Pedersen 8b5b558bb5 tpl: Rework to handle both text and HTML templates
Before this commit, Hugo used `html/template` for all Go templates.

While this is a fine choice for HTML and maybe also RSS feeds, it is painful for plain text formats such as CSV, JSON etc.

This commit fixes that by using the `IsPlainText` attribute on the output format to decide what to use.

A couple of notes:

* The above requires a nonambiguous template name to type mapping. I.e. `/layouts/_default/list.json` will only work if there is only one JSON output format, `/layouts/_default/list.mytype.json` will always work.
* Ambiguous types will fall back to HTML.
* Partials inherits the text vs HTML identificator of the container template. This also means that plain text templates can only include plain text partials.
* Shortcode templates are, by definition, currently HTML templates only.

Fixes #3221
2017-04-02 23:13:10 +02:00
Bjørn Erik Pedersen 7eb71ee064 Revert "tpl: Rework to handle both text and HTML templates"
Will have to take another stab at this ...

This reverts commit 5c5efa03d2.

Closes #3260
2017-04-02 14:20:34 +02:00
Bjørn Erik Pedersen 5c5efa03d2 tpl: Rework to handle both text and HTML templates
Before this commit, Hugo used `html/template` for all Go templates.

While this is a fine choice for HTML and maybe also RSS feeds, it is painful for plain text formats such as CSV, JSON etc.

This commit fixes that by using the `IsPlainText` attribute on the output format to decide what to use.

A couple of notes:

* The above requires a nonambiguous template name to type mapping. I.e. `/layouts/_default/list.json` will only work if there is only one JSON output format, `/layouts/_default/list.mytype.json` will always work.
* Ambiguous types will fall back to HTML.
* Partials inherits the text vs HTML identificator of the container template. This also means that plain text templates can only include plain text partials.
* Shortcode templates are, by definition, currently HTML templates only.

Fixes #3221
2017-04-02 11:37:30 +02:00
Bjørn Erik Pedersen 2f2ea42c09 hugolib: Fix reloading corner cases for shortcodes
This commit fixes two different, but related issues:

1) Live-reload when a new shortcode was defined in the content file before the shortcode itself was created.
2) Live-reload when a newly defined shortcode changed its "inner content" status.

This commit also improves the shortcode related error messages to include the full path to the content file in question.

Fixes #3156
2017-03-11 20:21:06 +01:00
Bjørn Erik Pedersen 6278393540 hugolib: Include full filepath on shortcode errors
Fixes #3079
2017-02-21 22:54:19 +01:00
Bjørn Erik Pedersen c507e2717d tpl: Refactor package
Now:

* The template API lives in /tpl
* The rest lives in /tpl/tplimpl

This is bound te be more improved in the future.

Updates #2701
2017-02-17 17:15:26 +01:00
Bjørn Erik Pedersen 93ca7c9e95 all: Refactor to nonglobal Viper, i18n etc.
This is a final rewrite that removes all the global state in Hugo, which also enables
the use if `t.Parallel` in tests.

Updates #2701
Fixes #3016
2017-02-17 17:15:26 +01:00
Bjørn Erik Pedersen c71e1b106e all: Refactor to nonglobal file systems
Updates #2701
Fixes #2951
2017-02-04 11:37:25 +07:00
Bjørn Erik Pedersen d6000a208c all: Refactor to nonglobal template handling
Updates #2701
2017-01-10 01:36:59 +01:00
Bjørn Erik Pedersen 45e3ed517a all: Refactor to non-global logger
Note that this looks like overkill for just the logger, and that is correct,
but this will make sense once we start with the template handling etc.

Updates #2701
2017-01-07 17:06:35 +01:00
magikstm 01ee525f47 hugolib: Correct usage of "shortcode" in error messages 2017-01-05 15:40:28 +01:00
C. Hoeppler 766c82a6bb Add context to asciidoc/-tor error logging
Add DocumentName (path to the file being rendered) to RenderingContext
and use that information to include the path in the error print.

See #2399
Closes #2567
2016-10-13 13:48:43 +02:00
Bjørn Erik Pedersen 76bf2dcdd2 Fix shortcode vs pygments
This is the nth attempt to fix an issue by changing the placeholder token pattern, but
now we actually have tests for all the historic trouble cases.

Fixes #2223
2016-09-08 21:23:01 +02:00
Bjørn Erik Pedersen b3563b40a4 Fix multilingual reload when shortcode changes
This commit also refines the partial rebuild logic, to make sure we do not do more work than needed.

Updates #2309
2016-09-06 18:32:21 +03:00
Bjørn Erik Pedersen 2079a23dd8 Make it possible to configure Blackfroday per language
See #2309
2016-09-06 18:32:17 +03:00
Bjørn Erik Pedersen ed0985404d Render the shortcodes as late as possible
This is needed to make shortcode users happy with the new multilanguage support,
but it will also solve many other related posts about "stuff not available in the shortcode".

We will have to revisit this re the handler chain at some point, but that will be easier
now as the integration test story has improved so much.

As part of this commit, the site-building tests in page_test.go is refreshed, they now
tests for all the rendering engines (when available), and all of them now uses the
same code-path as used in production.

Fixes #1229
Fixes #2323
Fixes ##1076
2016-09-06 18:32:16 +03:00
Bjørn Erik Pedersen 593a546fc6 Check for nil Params in shortcode's Get
Fixes #2294
2016-07-21 17:22:31 +02:00
Bjørn Erik Pedersen 34f40044d7 Fix shortcode in markdown headers
This issue was introduced as a fix to shortcode not working in RST.

One could argue that Blackfriday and friends should handle `#` in titles, but that will be a discussion
for another day.

The new placeholder pattern should be RST safe and work with titles.

And now with a test so this doesn't break again.

Fixes #2192
Fixes #2209
Closes #2210
2016-06-15 09:12:07 +02:00
Bjørn Erik Pedersen 39c9ae3108 Revert "Use Node.ID for anchor ID"
This reverts commit cd558958a0.
2016-04-12 18:11:24 +02:00
Bjørn Erik Pedersen cd558958a0 Use Node.ID for anchor ID
Fixes #2057
2016-04-11 13:17:25 +02:00
Bjørn Erik Pedersen 8fe48a6576 Fix potential data race in test 2016-04-09 14:30:22 +02:00
ypnos 9f6b5ad3b4 Save auto-detected markup type in Page.Markup
If Page.Markup was not set by the user, it will now be set after
guessing from the file extension. This means, Page.Markup will be set in
any case. It can be used by a theme to differentiate between markup
types.

Fixes #1950
2016-04-09 00:48:53 +02:00
Bjørn Erik Pedersen c2c73c2bd2 hugolib: Some more GoLint fixes 2016-03-24 14:11:04 +01:00
Bjørn Erik Pedersen 048e07422d Add Scratch to shortcode
Fixes #2000
2016-03-21 21:11:20 -04:00
Bjørn Erik Pedersen de8fc8761a Re-render shortcode on template or data file change
Fixes  #1971
2016-03-21 21:09:25 -04:00
Bjørn Erik Pedersen 70739c972e Remove unnecessary type conversions 2016-03-14 20:35:50 +01:00
Bjørn Erik Pedersen 0fb31e4a82 Create an alias from shortcode.Page.Site to shortcode.Site
Fixes #1976
2016-03-14 14:10:15 +01:00
Bjørn Erik Pedersen 9896cd0030 Add reference to parent shortcode
Fixes #1936
2016-03-09 11:06:40 +01:00
Bjørn Erik Pedersen 7f682d7802 Fix shortcode handling in RST
Fixes #1904
2016-03-01 11:04:25 +01:00
Bjørn Erik Pedersen 455df10752 Optimize replaceShortcodeTokens
We can of course skip reading the entire byte slice again and again.

This was a slip in the original implementation; functionally the same,
but is slightly faster, esp. for larger data sets with many shortcodes:

```
benchmark                             old ns/op     new ns/op     delta
BenchmarkReplaceShortcodeTokens-4     15505         14753         -4.85%

benchmark                             old allocs     new allocs     delta
BenchmarkReplaceShortcodeTokens-4     1              1              +0.00%

benchmark                             old bytes     new bytes     delta
BenchmarkReplaceShortcodeTokens-4     3072          3072          +0.00%
```
2016-02-25 23:50:43 +01:00
Bjørn Erik Pedersen e445c35d6a Fix copyright headers in source files
Still need to add some missing headers and an AUTHORS file.

See #1646
2015-12-07 19:57:01 +01:00
Steve Francia f045d7a611 Change the license to Apache 2.0 2015-11-23 22:16:36 -05:00
Cameron Moore fde47c5eb9 Add shortcode IsNamedParams property
It would be helpful to know whether a shortcode was called with positional or
named parameters.  This commit adds a boolean `IsNamedParams` property to the
`ShortcodeWithPage` struct.
2015-11-23 17:17:18 +01:00
Bjørn Erik Pedersen 6fa7f2ac3e Avoid multiple locks for shortcode template
Fixes #1600
2015-11-21 23:29:58 +01:00
Bjørn Erik Pedersen c5f40e8b8b Improve RW-locking of template in shortcode handling
See #1599
2015-11-21 21:57:26 +01:00
Bjørn Erik Pedersen 58c446f562 Fix recently introduced new data race in the shortcode handling
Fixes #1599
2015-11-21 20:03:18 +01:00
Cameron Moore 05c29ad593 Return error if shortcode template.Tree is nil
If a shortcode template fails to compile, the template will be non-nil, but
template.Tree will be nil which caused a panic.
2015-11-20 23:34:30 +01:00
Bjørn Erik Pedersen b08d9f26ae Make replaceShortcodeTokens rewrite the input slice
Currently a `[]byte` copy is returned. In most cases this is the safe thing to do, but we should just modify/grow the slice as needed.

This is faster and consumes less memory:

```
benchmark                             old ns/op     new ns/op     delta
BenchmarkReplaceShortcodeTokens-4     7350          4419          -39.88%

benchmark                             old allocs     new allocs     delta
BenchmarkReplaceShortcodeTokens-4     5              1              -80.00%

benchmark                             old bytes     new bytes     delta
BenchmarkReplaceShortcodeTokens-4     4816          1152          -76.08%
```

This commit is aso a small spring cleaning of duplicated code in the different `PageConvert` methods.

Fixes #1516
2015-11-17 18:24:17 +01:00
Bjørn Erik Pedersen 0f1fb8c7d8 Avoid panic in shortcode param handling
Fixes #1337
2015-08-07 20:08:23 +02:00
Bjørn Erik Pedersen 35bb72c83e Do not panic on index out of range in shortcode.Get
Fixes #1335
2015-08-07 19:21:26 +02:00