Commit graph

119 commits

Author SHA1 Message Date
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
Cameron Moore
5cc8b58907 tpl: Accept limit as interface in findRE func
Fixes #3018
2017-02-10 20:02:28 +07:00
Bjørn Erik Pedersen
c71e1b106e all: Refactor to nonglobal file systems
Updates #2701
Fixes #2951
2017-02-04 11:37:25 +07:00
Cameron Moore
3ac1b9fe33 tpl: Fix deadlock in cached partials
Cached partials that contained cached partials would create a deadlock.

Fixes #2935
2017-01-18 10:30:22 +01:00
Bjørn Erik Pedersen
d6000a208c all: Refactor to nonglobal template handling
Updates #2701
2017-01-10 01:36:59 +01:00
Mathias Biilmann
2989c38245 tpl: Add truncate template function
This commit adds a truncate template function for safely truncating text without
breaking words. The truncate function is HTML aware, so if the input text is a
template.HTML it will be truncated without leaving broken or unclosed HTML tags.

    {{ "this is a very long text" | truncate 10 " ..." }}
    {{ "With [Markdown](/markdown) inside." | markdownify | truncate 10 }}
2017-01-06 10:42:32 +01:00
Nishanth Shanmugham
cabc6b3186 tpl: Add now function
Add a now template function that returns the current time as time.Time.
Also, update related docs.
2017-01-01 02:50:46 +01:00
Tristan Rice
d055862a41 tpl: Fix crash when using imageConfig
defaultImageConfigCache is now initialized statically instead of relying on it
being initialized in the reset function.

Fixes #2806
2016-12-25 13:43:50 +01:00
Cameron Moore
34c87421b8 tpl: Update getenv to accept interface{} params
Updates #2822
2016-12-23 09:51:03 +01:00
Cameron Moore
29caaddce6 tpl: Update title to accept interface{} params
Updates #2822
2016-12-23 09:51:03 +01:00
Cameron Moore
661d64c46a tpl: Update upper to accept interface{} params
Updates #2822
2016-12-23 09:51:03 +01:00
Cameron Moore
4e5b4ac504 tpl: Update lower to accept interface{} params
Updates #2822
2016-12-23 09:51:03 +01:00
Cameron Moore
6b8422bb35 tpl: Update hasPrefix to accept interface{} params
Updates #2822
2016-12-23 09:51:03 +01:00
digitalcraftsman
0f6c73cf2e tpl: Add sha256 template function
Fixes #2742
2016-12-04 15:03:52 +01:00
Bjørn Erik Pedersen
139585f84a tpl: Simplify 2016-11-23 18:53:50 +01:00
bogem
7665dd239e tpl: Drop an else block in checkCondition
Fix golint warning: tpl/template_funcs.go:853:10: if block ends with a
return statement, so drop this else and outdent its block

See #2014
2016-11-23 09:13:00 +01:00
bogem
dec1706ae0 commands, hugolib, parser, tpl: Use errors.New instead of fmt.Errorf 2016-11-22 23:43:55 +01:00
Tristan Rice
a49f838cd0 tpl: Add imageConfig function
Add imageConfig function which calls image.DecodeConfig and returns the height, width and color mode of the image. (#2677)

This allows for more advanced image shortcodes and templates such as those required by AMP.

layouts/shortcodes/amp-img.html
```
{{ $src := .Get "src" }}
{{ $config := imageConfig (printf "/static/%s" $src) }}

<amp-img src="{{$src}}"
           height="{{$config.Height}}"
           width="{{$config.Width}}"
           layout="responsive">
</amp-img>
```
2016-11-16 13:00:45 +01:00
Albert Nigmatzianov
950034db5c source, tpl: Fix staticcheck complaints
tpl/template_funcs.go:1019:3: the surrounding loop is unconditionally terminated
source/lazy_file_reader.go:66:5: err != nil is always true for all possible
values ([nil:error] != [nil:error])
2016-11-15 21:22:43 +01:00
Bjørn Erik Pedersen
07eb3a50c7 tpl: Golint fixes 2016-11-05 17:28:14 +01:00
Albert Nigmatzianov
f21e2f25c9 all: Unify case of config variable names
All config variables starts with low-case and uses camelCase.

If there is abbreviation at the beginning of the name, the whole
abbreviation will be written in low-case.
If there is abbreviation at the end of the name, the
whole abbreviation will be written in upper-case.
For example, rssURI.
2016-10-24 20:56:00 +02:00
Bjørn Erik Pedersen
a10b2cd372 Avoid reading from Viper for path and URL funcs
The gain, given the "real sites benchmark" below, is obvious:

```
benchmark           old ns/op       new ns/op       delta
BenchmarkHugo-4     14497594101     13084156335     -9.75%

benchmark           old allocs     new allocs     delta
BenchmarkHugo-4     57404335       48282002       -15.89%

benchmark           old bytes       new bytes      delta
BenchmarkHugo-4     9933505624      9721984424     -2.13%
```

Fixes #2495
2016-10-24 13:45:30 +02:00
Cameron Moore
474eb454df tpl: Add partialCached template function
Supports an optional variant string parameter so that a given partial
will be cached based upon the name+variant.

Fixes #1368
Closes #2552
2016-10-11 23:56:06 +02:00
Bjørn Erik Pedersen
a04abf0dda tpl: Make the *langURL funcs tace interface{}
Fixes #2447
2016-09-13 17:07:52 +02:00
Cameron Moore
3908489ccd tpl: Extend where to iterate over maps
Fixes #2028
2016-09-12 11:03:53 +02:00
Ben Campbell
3392133128 Remove redundant field in sort helper struct
The pair struct used by sortSeq() had two key fields, where only one
is really needed.
2016-09-11 10:42:50 +02:00
Bjørn Erik Pedersen
34fe5cd34f tpl: Fix echoParam
Fixes #2406
2016-09-07 22:03:09 +03:00
Bjørn Erik Pedersen
54141f71dd Improve language handling in URLs
The current "rendering language" is needed outside of Site. This commit moves the Language type to the helpers package, and then used to get correct correct language configuration in the markdownify template func.
This commit also adds two new template funcs: relLangURL and absLangURL.

See #2309
2016-09-06 18:32:18 +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
Alexandre Bourget
ec33732fbe Add multilingual support in Hugo
Implements:
* support to render:
  * content/post/whatever.en.md to /en/2015/12/22/whatever/index.html
  * content/post/whatever.fr.md to /fr/2015/12/22/whatever/index.html
* gets enabled when `Multilingual:` is specified in config.
* support having language switchers in templates, that know
  where the translated page is (with .Page.Translations)
  (when you're on /en/about/, you can have a "Francais" link pointing to
   /fr/a-propos/)
  * all translations are in the `.Page.Translations` map, including the current one.
* easily tweak themes to support Multilingual mode
* renders in a single swift, no need for two config files.

Adds a couple of variables useful for multilingual sites

Adds documentation (content/multilingual.md)

Added language prefixing for all URL generation/permalinking see in the
code base.

Implements i18n. Leverages the great github.com/nicksnyder/go-i18n lib.. thanks Nick.
* Adds "i18n" and "T" template functions..
2016-09-06 18:32:15 +03:00
Cameron Moore
5498a1bd56 tpl: Return all errors from casting
Most non-boolean template functions should return errors.

Fixes #2354
2016-08-16 21:42:38 -05:00
Stephan Kulla
88dcbc6efa tpl: Make absURL and relURL accept anything 2016-08-13 00:31:50 +02:00
Wade Fitzpatrick
70544f9e62 tpl: Add time template func
Fixes #2328
2016-08-04 01:55:24 +02:00
Cathal Garvey
20555b1630 Add htmlEscape and htmlUnescape template functions
These functions allow trivial escaping and unescaping of HTML entities,
and make it far easier to compose other functions for the creation of
parameterised URLs.
2016-07-19 00:14:05 +02:00
Cathal Garvey
55a4f6e48e Do not double-escape in querify
Test case modified and expanded for querify to reflect original bug and changes.

Fixes #2279
2016-07-14 15:53:33 +02:00
Michael Orr
0a812beb12 tpl: Modify tpl.humanize to ordinalize integer input
Add logic to tpl.humanize such that it understands input of int literals
or strings which represent an integer. When tpl.humanize sees this type
of input, it will use inflect.Ordinalize as opposed to the standard
inflect.Humanize.

Fixes #1886
2016-07-13 22:09:59 +02:00
Bjørn Erik Pedersen
adf405496e Fix humanize when string is empty
Fixes #2272
2016-07-10 15:10:22 +02:00
Bjørn Erik Pedersen
39fe42cf6b Simplify querify 2016-07-06 20:57:37 +02:00
Jimmy Sawczuk
fbf48824ae tpl: Add a querify function to generate query strings inside templates
The query function will take a set of parameters specified like a dict and return a url.Values object which can be .Encode'd into a query string.

Example:

<a href="http://www.google.com?{{ (querify "q" "test" "page" 3).Encode | safeHTML }}">Search</a>

Returns:

<a href="http://www.google.com?page=3&q=test">Search</a>

Closes #2257
2016-07-05 19:52:45 +02:00
marco
c21e2b3b4d tpl: Enable safeHTMLAttr
See #2234 and #347
2016-06-27 09:18:14 -06:00
Bjørn Erik Pedersen
273a68400f Add non-string support in markdownify 2016-05-06 10:11:17 +02:00
Christopher Mancini
09038865c2 tpl: Add intersect operator to where function
Returns true if a given field value that is a slice / array of strings, integers or floats contains elements in common with the matching value. It follows the same rules as the intersect function.

Closes #1945
2016-04-28 00:58:47 +02:00
Bjørn Erik Pedersen
d48b986c45 Revert "tpl: Extend where to iterate over maps"
Committed by accident.

This reverts commit 0141a02160.
2016-04-13 19:13:33 +02:00
Cameron Moore
0141a02160 tpl: Extend where to iterate over maps
Refactor and extend where to iterate over maps.
2016-04-13 11:43:06 +02:00
Bjørn Erik Pedersen
f2ff77f3ac Make Hugo build without Mercurial
By creating a fork of the Bitbucket inflect repo. It hasn't changed since 2013,
so it must be considered pretty stable.

Fixes #2062
2016-04-08 21:11:44 +02:00
digitalcraftsman
5bfe16ef8d tpl: Add findRE template func 2016-04-05 22:26:03 +02:00
Cameron Moore
fe929114d3 tpl: Fix panic in sort of nil sequence
Properly handle a nil sequence in the sortSeq function.  Test included.
Discovered while using `range sort .Site.Data.source.Undefined "foo"`.
2016-04-02 23:06:48 +02:00
Cameron Moore
d15fda5000 tpl: Fix panic in pairList.Less
While sorting on data sources with missing fields, a panic can occur in
pairList.Less if `Interface()` is called on a invalid `reflect.Value`.
This commit detects an invalid Value and replacing it with a zero value
for the comparison.
2016-04-02 23:01:36 +02:00
Bjørn Erik Pedersen
cb89ae63e9 tpl: Make readDir use the WorkingDir fs
Fixes #2010
2016-03-31 23:06:51 +02:00