Commit graph

60 commits

Author SHA1 Message Date
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
c2277fcbc1 docs: Add doc and a test for readDir
Fixes #2009
2016-03-31 23:16:42 +02:00
Bjørn Erik Pedersen
4f66f790b1 Add readFile template func
This also includes a refactor of the hugofs package and its usage.

The motivation for that is:

The Afero filesystems are brilliant. Hugo's way of adding a dozen of global variables for the different filesystems was a mistake. In readFile (and also in some other places in Hugo today) we need a way to restrict the access inside the working dir. We could use ioutil.ReadFile and implement the path checking, checking the base path and the dots ("..") etc. But it is obviously better to use an Afero BasePathFs combined witha ReadOnlyFs. We could create a use-once-filesystem and handle the initialization ourselves, but since this is also useful to others and the initialization depends on some other global state (which would mean to create a new file system on every invocation), we might as well do it properly and encapsulate the predefined set of filesystems. This change also leads the way, if needed, to encapsulate the file systems in a struct, making it possible to have several file system sets in action at once (parallel multilanguage site building? With Moore's law and all...)

Fixes #1551
2016-03-31 21:24:18 +02:00
Bjørn Erik Pedersen
e5e1bcc271 Add plainify template function
To strip away any HTML. May be useful for the .Title in head etc.

People may shoot themself in the foot with this, maybe ...

The replacement function is pretty fast.
2016-03-21 20:27:25 -04:00
Bjørn Erik Pedersen
02e50c0126 Remove duplicate doArithmetic 2016-03-18 23:18:40 +01:00
Cameron Moore
df44b1930a tpl: Send actual values to in from intersect
The `intersect` function uses `in` to avoid adding duplicates to the
resulting set.  We were passing `reflect.Value` items when we should
have been using `Value.Interface()` to send the actual data structure.
This fixes that.

See #1952
2016-03-17 20:09:43 +01:00
Cameron Moore
b0326a1c0f tpl: Treat booleans as set in default function
Booleans and `default` don't really make sense together, so we'll always treat
booleans as "set" and return the given value.
2016-03-16 20:05:48 +01:00
Bjørn Erik Pedersen
3a2a4c3b07 tpl: Make the safeHTML and cousins accept more than strings 2016-03-14 22:27:40 +01:00
Bjørn Erik Pedersen
989c7487eb Add emojify to the template func smoke tests 2016-03-12 16:53:39 +01:00
Cameron Moore
b8d3651242 tpl: Add replaceRE function
This commit addes a `replaceRE` template function.  Regexp patterns are compiled
once and cached.
2016-03-11 19:59:18 +01:00
Cameron Moore
2d0650dbd9 tpl: Add custom index function
This commit adds a custom index template function that deviates from the stdlib
simply by not returning an "index out of range" error if an array, slice or
string index is out of range.  Instead, we just return nil values.  This should
help make the new default function more useful for Hugo users.

Fixes #1949
2016-03-10 17:47:59 +01:00
Cameron Moore
09c8c17bf0 tpl: fix default function
This commit fixes a few things:

1. `given` is now a variadic parameter so that piping works properly
2. add separate template tests to make sure piping works
3. support time values
4. add more tests of the dfault function
2016-03-10 17:47:59 +01:00
Cameron Moore
ce9ee3cf49 tpl: Add default function 2016-03-10 17:47:59 +01:00
digitalcraftsman
94c3825e5b Add md5 and sha1 template funcs 2016-03-10 11:03:06 +01:00
Bjørn Erik Pedersen
435e996c4f Add jsonify template func 2016-03-06 15:01:14 +01:00
Bjørn Erik Pedersen
d774b142e8 Make where accept slice
Fixes #1926
2016-03-06 13:15:07 +01:00
Bjørn Erik Pedersen
f6591eca35 tpl: Add some dateFormat testcases with both time and date
See #235
2016-03-03 23:17:35 +01:00
Bjørn Erik Pedersen
e85b821d3e tpl: Fix broken chomp test 2016-03-03 21:24:21 +01:00
Bjørn Erik Pedersen
f223f17c76 tpl: Make chomp return template.HTML 2016-03-03 19:45:23 +01:00
Bjørn Erik Pedersen
daaf4eb330 tpl: Add integration tests for most of the template funcs examples in doc 2016-03-03 19:35:04 +01:00
Bjørn Erik Pedersen
c1e4309516 Clean up the trim template func test 2016-02-16 00:47:14 +01:00
Bjørn Erik Pedersen
2b0fad79b7 tpl: Add two more doArithmetic test cases 2016-02-07 23:02:46 +01:00
Bjørn Erik Pedersen
f90b45f7f3 tpl: Test with nil value in first, last, after and shuffle 2016-02-07 21:16:43 +01:00
Bjørn Erik Pedersen
3cc468a82f tpl: Add missing test variants for slicestr 2016-02-07 20:14:24 +01:00
Bjørn Erik Pedersen
b3c2d90ba2 tpl: Add missing substr test variants 2016-02-07 15:22:57 +01:00
Bjørn Erik Pedersen
4dcb63c2f6 tpl: Add highlight test 2016-02-07 15:15:27 +01:00
Bjørn Erik Pedersen
0a8583a451 tpl: Add missing base64 tests 2016-02-07 14:58:23 +01:00
Bjørn Erik Pedersen
0888ddd01f tpl: Add tests for word and rune counting 2016-02-07 14:51:06 +01:00
Bjørn Erik Pedersen
ec49dbb8f5 tpl: Consolidate and complete the Inflect tests 2016-02-07 14:37:04 +01:00
Bjørn Erik Pedersen
1e8b4d9cde Add some missing doArithmetic test cases 2016-02-07 14:20:25 +01:00
Bjørn Erik Pedersen
5995eaaa08 tpl: Unexport all template funcs
There is no good reason to export all the template funcs:

* They're not used outside the templates.
* If usable in other packages, they should be moved (to helpers?)
* They create too broad an interface;
  users of the tpl package don't see the forest for all the trees.
2016-02-07 11:45:45 +01:00
Bjørn Erik Pedersen
b3a70abe40 tpl: Complete coverage for Humanize 2016-02-07 01:45:37 +01:00
Cameron Moore
45df4596bb tpl: Add humanize func and cleanup lint
Add humanize (inflect.Humanize) to the template funcMap.  Documentation and
tests are included.

Various code cleanups of the template funcs:

- Break pluralize and singularize out into stand-alone funcs.
- Sort the list of funcMap entries.
- Add some minimal godoc comments to all public funcs.
- Fix some issues found by golint and grind.
2016-02-06 15:31:25 +01:00
Bjørn Erik Pedersen
318a984526 Apply gofmt -s 2016-02-06 13:09:52 +01:00
Antti Järvinen
9008ac0b55 Rename random to shuffle. Remove count parameteter to simplify its role. Add tests for randomising. 2016-01-04 11:47:37 -05:00
Antti Järvinen
302a6ac701 Add Random function to template functions
Adds Random function to pick N random items from sequence.
2016-01-04 11:38:57 -05:00
Anthony Fok
8509727fe8 Add copyright header to that source files that don’t have one.
See #1646
2015-12-10 15:19:38 -07:00
Tatsushi Demachi
f3c74c9db4 Add boolean value comparison to where tpl function
`where` template function's internal condition check function doesn't
check boolean values and always returns `false` silently.

This adds missing boolean value comparison to the function.
`where Values ".Param.key" true` like clause can be used.

Only "=", "==", "eq", "!=", "<>", "ne" operators are allowed to be used
with a boolean value. If an other operator is passed with it, the
condition check function returns `false` like before.
2015-12-08 05:39:11 +09:00
Cameron Moore
c5a4c07b89 Add SafeJS template function
This commit adds a SafeJS template function.  Tests and documentation are
included.

Fixes #1579
2015-11-20 21:12:01 +01:00
Bjørn Erik Pedersen
35abd179e2 Add time.Time support in ge, gt, le, lt
Fixes #1593
2015-11-20 11:53:25 +01:00
Bjørn Erik Pedersen
4c04d7101a Fix In func given an []interface{}
Fixes #1486
2015-10-13 17:05:36 +02:00
Bjørn Erik Pedersen
17fb752279 Correct number of args to Errorf 2015-10-12 21:34:15 +02:00
Bjørn Erik Pedersen
72f14a8202 Apply project wide go fmt 2015-10-12 20:47:06 +02:00
NotZippy
3a27cefec1 Add dictionary function to be passed into a template
Allows templates to dynamically build maps.

Example usage: Creating and passing a map to a subtemplate while in a range on the parent.
2015-10-09 18:29:16 +02:00
digitalcraftsman
79f8bb625d Add base64Decode and base64Encode template functions
Fixes #1416
2015-09-25 21:31:55 +02:00
Bjørn Erik Pedersen
dac9c0dae6 Use cast.ToIntE for int conversions in substr and slicestr
It is less restrictive, and it is what is used in other template funcs.
2015-08-15 15:47:16 +02:00
Benny Wu
5b51b3b9fb Slicestr fix for other int type param
Fixes #1347
2015-08-14 17:26:30 +02:00
Tatsushi Demachi
153332706a Make sort tpl func accept field/key chaining arg
'sort' template function used to accept only each element's struct field
name, method name and map key name as its second argument. This extends
it to accept a field/method/key chaining key string like
'Params.foo.bar' as the argument. It evaluates sub elements of each
array or map elements and sorts by them.

Typical use case would be sorting pages by user defined front matter
value. For example, sorting pages by 'Params.foo.bar' is possible by
writing the following template code

    {{ range sort .Data.Pages "Params.foo.bar" }}
        {{ .Content }}
    {{ end }}

It ignores all leading and trailing dots so "Params.foo.bar" can be
written in ".Params.foo.bar"

This also fixes the issue that 'sort' cannot evaluate a pointer value.

Fix #1330
2015-08-07 14:06:06 +02:00
Tatsushi Demachi
56534beaf6 Fix sort tpl func to return explicit type value
sort template function returns `[]interface{}` type slice value
regardless of its original element type.

This fixes it to keep the original element type. For example, if it
sorts `map[string]int` type value, it returns `[]int` slice value
instead of `[]interface{}` slice value.
2015-08-07 14:06:06 +02:00
Bjørn Erik Pedersen
c7aa881d90 Fix Unicode issue in Slicestr and Substr
Fixes #1333
2015-08-07 08:52:22 +02:00