Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:huakim:matrix
lua-luajinja
_service:obs_scm:luajinja.obscpio
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File _service:obs_scm:luajinja.obscpio of Package lua-luajinja
07070100000000000081A400000000000000000000000166D5601B0000006A000000000000000000000000000000000000001100000000luajinja/.hgtags8e10c31c3b1980bebed089bf0825e59329822627 lupa_0.1_alpha 5c8613bf064a93dc1617403ea9de4a42f322bb68 lupa_1.0 07070100000001000081A400000000000000000000000166D5601B00000432000000000000000000000000000000000000001100000000luajinja/LICENSEThe MIT License Copyright (c) 2015-2020 Mitchell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 07070100000002000081A400000000000000000000000166D5601B000002C2000000000000000000000000000000000000001200000000luajinja/Makefile# Copyright 2015-2020 Mitchell. See LICENSE. # Run test suite. .PHONY: tests tests: ; cd tests && lua5.1 suite.lua # Documentation. docs: docs/index.md docs/api.md $(wildcard docs/*.md) | \ docs/_layouts/default.html for file in $(basename $^); do \ cat $| | docs/fill_layout.lua $$file.md > $$file.html; \ done docs/index.md: README.md sed 's/^\# [[:alpha:]]\+/## Introduction/;' $< > $@ sed -i 's|https://[[:alpha:]]\+\.github\.io/[[:alpha:]]\+/||;' $@ sed -i '1 i {% raw %}' $@ && echo "{% endraw %}" >> $@ docs/api.md: lupa.lua luadoc --doclet docs/markdowndoc $^ > $@ sed -i '1 i {% raw %}' $@ && echo "{% endraw %}" >> $@ cleandocs: ; rm -f docs/*.html docs/index.md docs/api.html 07070100000003000081A400000000000000000000000166D5601B00001E59000000000000000000000000000000000000001300000000luajinja/README.md# luajinja luajinja is a [Jinja2](http://jinja.pocoo.org) template engine implementation written in Lua and supports Lua syntax within tags and variables. luajinja was forked from https://github.com/orbitalquark/lupa and was sponsored by the [Library of the University of Antwerp](http://www.uantwerpen.be/). [Jinja2]: http://jinja.pocoo.org [Library of the University of Antwerp]: http://www.uantwerpen.be/ ## Requirements Luajinja has the following requirements: * [Lua](https://lua.org/) 5.1, 5.2, 5.3 or 5.4. * [LuaJIT](https://luajit.org/) 2.1 * The [LPeg][] library. [Lua]: https://www.lua.org [LPeg]: http://www.inf.puc-rio.br/~roberto/lpeg/ ## Download Luajinja releases can be found [here][]. [here]: https://github.com/huakim/luajinja/releases ## Installation Unzip luajinja and place the "lupa.lua" file in your Lua installation's `package.path`. This location depends on your version of Lua. Typical locations are listed below. * Lua 5.1: */usr/share/lua/5.1/* or */usr/local/share/lua/5.1/* * Lua 5.2: */usr/share/lua/5.2/* or */usr/local/share/lua/5.2/* * Lua 5.3: */usr/share/lua/5.3/* or */usr/local/share/lua/5.3/* * Lua 5.4: */usr/share/lua/5.4/* or */usr/local/share/lua/5.4/* * LuaJit 2.1: */usr/share/lua/5.1/* or */usr/local/share/lua/5.1/* You can also place the *luajinja.lua* file wherever you'd like and add it to Lua's `package.path` manually in your program. For example, if Lupa was placed in a */home/user/lua/* directory, it can be used as follows: package.path = package.path .. ';/home/user/lua/?.lua' ## Usage Luajinja is simply a Lua library, which can process templates by calling `luajinja.expand()`, as described below: luajinja = require('luajinja') luajinja.expand("hello {{ s }}!", {s = "world"}) --> "hello world!" luajinja.expand("{% for i in {1, 2, 3} %}{{ i }}{% endfor %}") --> 123 By default, Luajinja loads templates relative to the current working directory. This can be changed by reconfiguring Lupa: luajinja.expand_file('name') --> expands template "./name" luajinja.configure{loader = luajinja.loaders.filesystem('path/to/templates')} luajinja.expand_file('name') --> expands template "path/to/templates/name" See Lupa's [API documentation][] for more information. [API documentation]: https://orbitalquark.github.io/lupa/api.html ## Syntax Please refer to Jinja2's extensive [template documentation][]. Any incompatibilities are listed in the sections below. [template documentation]: http://jinja.pocoo.org/docs/dev/templates/ ## Comparison with Jinja2 While Lua and Python (Jinja2's implementation language) share some similarities, the languages themselves are fundamentally different. Nevertheless, a significant effort was made to support a vast majority of Jinja2's Python-style syntax. As a result, Lupa passes Jinja2's test suite with only a handful of modifications. The comprehensive list of differences between Lupa and Jinja2 is described in the following sections. ### Fundamental Differences * Expressions use Lua's syntax instead of Python's, so many of Python's syntactic constructs are not valid. However, the following constructs *are valid*, despite being invalid in pure Lua: + Iterating over table literals or table variables directly in a "for" loop: {% for i in {1, 2, 3} %}...{% endfor %} + Conditional loops via an "if" expression suffix: {% for x in range(10) if is_odd(x) %}...{% endfor %} + Table unpacking for list elements when iterating through a list of lists: {% for a, b, c in {{1, 2, 3}, {4, 5, 6}} %}...{% endfor %} + Default values for macro arguments: {% macro m(a, b, c='c', d='d') %}...{% endmacro %} * Strings do not have unicode escapes nor is unicode interpreted in any way. ### Syntactic Differences * Line statements are not supported due to parsing complexity. * In `{% for ... %}` loops, the `loop.length`, `loop.revindex`, `loop.revindex0`, and `loop.last` variables only apply to sequences, where Lua's `'#'` operator applies. * The `{% continue %}` and `{% break %}` loop controls are not supported due to complexity. * Loops may be used recursively by default, so the `recursive` loop modifier is not supported. * The `is` operator is not supported by Lua, so tests of the form `{{ x is y }}` should be written `{{ is_y(x) }}` (e.g. `{{ is_number(42) }}`). * Filters cannot occur after tokens within an expression (e.g. `{{ "foo"|upper .. "bar"|upper }}`), but can only occur at the end of an expression (e.g. `{{ "foo".."bar"|upper }}`). * Blocks always have access to scoped variables, so the `scoped` block modifier is not supported. * Named block end tags are not supported since the parser cannot easily keep track of that state information. * Any `{% block ... %}` tags within a "false" block (e.g. `{% if a %}` where `a` evaluates to `false`) are never read and stored due to the parser implementation. * Inline "if" expressions (e.g. `{% extends b if a else c %}`) are not supported. Instead, use a Lua conditional expression (e.g. `{% extends a and b or c %}`). * Any `{% extends ... %}` tags within a sub-scope are not effective outside that scope (e.g. `{% if a %}{% extends a %}{% else %}{% extends b %}{% endif %}`). Instead, use a Lua conditional expression (e.g. `{% extends a or b %}`). * Macros are simply Lua functions and have no metadata attributes. * Macros do not have access to a `kwargs` variable since Lua does not support keyword arguments. * `{% from x import y %}` tags are not supported. Instead, you must use either `{% import x %}`, which imports all globals in `x` into the current environment, or use `{% import x as z %}`, which imports all globals in `x` into the variable `z`. * `{% set ... %}` does not support multiple assignment. Use `{% do ...%}` instead. The catch is that `{% do ... %}` does not support filters. * The `{% trans %}` and `{% endtrans %}` tags, `{% with %}` and `{% endwith %}` tags, and `{% autoescape %}` and `{% endautoescape %}` tags are not supported since they are outside the scope of this implementation. ### Filter Differences * Only the `batch`, `groupby`, and `slice` filters return generators which produce one item at a time when looping. All other filters that produce iterable results generate all items at once. * The `float` filter only works in Lua 5.3 since that version of Lua has a distinction between floats and integers. * The `safe` filter must appear at the end of a filter chain since its output cannot be passed to any other filter. ### Function Differences * The global `range(n)` function returns a sequence from 1 to `n`, inclusive, since lists start at 1 in Lua. * No `lipsum()`, `dict()`, or `joiner()` functions for the sake of simplicity. ### API Differences * Luajinja has a much simpler API consisting of just four functions and three fields: + `luajinja.expand()`: Expands a string template subject to an environment. + `luajinja.expand_file()`: Expands a file template subject to an environment. + `luajinja.configure()` Configures delimiters and template options. + `luajinja.reset()`: Resets delimiters and options to their defaults. + `luajinja.env`: The default environment for templates. + `luajinja.filters`: The set of available filters (`escape`, `join`, etc.). + `luajinja.tests`: The set of available tests (`is_odd`, `is_defined`, etc.). * There is no bytecode caching. * Later will be added extension mechanism to luajinja. By now, modify `lupa.env`, `lupa.filters`, and `lupa.tests` directly. However, the parser cannot be extended. * Sandboxing is not supported, although `lupa.env` is safe by default (`io`, `os.execute`, `os.remove`, etc. are not available). 07070100000004000041ED00000000000000000000000366D5601B00000000000000000000000000000000000000000000000E00000000luajinja/docs07070100000005000041ED00000000000000000000000266D5601B00000000000000000000000000000000000000000000001700000000luajinja/docs/_layouts07070100000006000081A400000000000000000000000166D5601B00000322000000000000000000000000000000000000002400000000luajinja/docs/_layouts/default.html<!doctype html> <html lang="en"> <head> <title>{{ page.title }}</title> <link rel="stylesheet" href="style.css" type="text/css" /> <meta charset="utf-8" /> </head> <body> <div id="content"> <div id="header"> <h1>Lupa</h1> <ul> <li><a href="index.html">Home</a> |</li> <li><a href="https://github.com/orbitalquark/lupa/releases">Download</a> |</li> <li><a href="api.html">API</a> |</li> <li><a href="https://github.com/orbitalquark/lupa">Source</a></li> </ul> </div> <div id="main"> {{ content }} </div> <div id="footer"> <p style="text-align:center;"> © 2015-2020 <a href="https://github.com/orbitalquark">Mitchell</a></p> </div> </div> </body> </html> 07070100000007000081A400000000000000000000000166D5601B000065F6000000000000000000000000000000000000001500000000luajinja/docs/api.md{% raw %} ## Lupa API Documentation <a id="lupa"></a> ## The `lupa` Module --- Lupa is a Jinja2 template engine implementation written in Lua and supports Lua syntax within tags and variables. ### Functions defined by `lupa` <a id="_G.cycler"></a> #### `_G.cycler`(*...*) Returns an object that cycles through the given values by calls to its `next()` function. A `current` field contains the cycler's current value and a `reset()` function resets the cycler to its beginning. Parameters: * *`...`*: Values to cycle through. Usage: * `c = cycler(1, 2, 3)` * `c:next(), c:next() --> 1, 2` * `c:reset() --> c.current == 1` <a id="_G.range"></a> #### `_G.range`(*start, stop, step*) Returns a sequence of integers from *start* to *stop*, inclusive, in increments of *step*. The complete sequence is generated at once -- no generator is returned. Parameters: * *`start`*: Optional number to start at. The default value is `1`. * *`stop`*: Number to stop at. * *`step`*: Optional increment between sequence elements. The default value is `1`. <a id="lupa.configure"></a> #### `lupa.configure`(*ts, te, vs, ve, cs, ce, options*) Configures the basic delimiters and options for templates. This function then regenerates the grammar for parsing templates. Note: this function cannot be used iteratively to configure Lupa options. Any options not provided are reset to their default values. Parameters: * *`ts`*: The tag start delimiter. The default value is '{%'. * *`te`*: The tag end delimiter. The default value is '%}'. * *`vs`*: The variable start delimiter. The default value is '{{'. * *`ve`*: The variable end delimiter. The default value is '}}'. * *`cs`*: The comment start delimiter. The default value is '{#'. * *`ce`*: The comment end delimiter. The default value is '#}'. * *`options`*: Optional set of options for templates: * `trim_blocks`: Trim the first newline after blocks. * `lstrip_blocks`: Strip line-leading whitespace in front of tags. * `newline_sequence`: The end-of-line character to use. * `keep_trailing_newline`: Whether or not to keep a newline at the end of a template. * `autoescape`: Whether or not to autoescape HTML entities. May be a function that accepts the template's filename as an argument and returns a boolean. * `loader`: Function that receives a template name to load and returns the path to that template. <a id="lupa.expand"></a> #### `lupa.expand`(*template, env*) Expands the string template *template*, subject to template environment *env*, and returns the result. Parameters: * *`template`*: String template to expand. * *`env`*: Optional environment for the given template. <a id="lupa.expand_file"></a> #### `lupa.expand_file`(*filename, env*) Expands the template within file *filename*, subject to template environment *env*, and returns the result. Parameters: * *`filename`*: Filename containing the template to expand. * *`env`*: Optional environment for the template to expand. <a id="filters.batch"></a> #### `filters.batch`(*t, size, fill*) Returns a generator that produces all of the items in table *t* in batches of size *size*, filling any empty spaces with value *fill*. Combine this with the "list" filter to produce a list. Parameters: * *`t`*: The table to split into batches. * *`size`*: The batch size. * *`fill`*: The value to use when filling in any empty space in the last batch. Usage: * `expand('{% for i in {1, 2, 3}|batch(2, 0) %}{{ i|string }} {% endfor %}') --> {1, 2} {3, 0}` See also: * [`filters.list`](#filters.list) <a id="filters.capitalize"></a> #### `filters.capitalize`(*s*) Capitalizes string *s*. The first character will be uppercased, the others lowercased. Parameters: * *`s`*: The string to capitalize. Usage: * `expand('{{ "foo bar"|capitalize }}') --> Foo bar` <a id="filters.center"></a> #### `filters.center`(*s, width*) Centers string *s* within a string of length *width*. Parameters: * *`s`*: The string to center. * *`width`*: The length of the centered string. Usage: * `expand('{{ "foo"|center(9) }}') --> " foo "` <a id="filters.default"></a> #### `filters.default`(*value, default, false\_defaults*) Returns value *value* or value *default*, depending on whether or not *value* is "true" and whether or not boolean *false_defaults* is `true`. Parameters: * *`value`*: The value return if "true" or if `false` and *false_defaults* is `true`. * *`default`*: The value to return if *value* is `nil` or `false` (the latter applies only if *false_defaults* is `true`). * *`false_defaults`*: Optional flag indicating whether or not to return *default* if *value* is `false`. The default value is `false`. Usage: * `expand('{{ false|default("no") }}') --> false` * `expand('{{ false|default("no", true) }') --> no` <a id="filters.dictsort"></a> #### `filters.dictsort`(*t, case\_sensitive, by, value*) Returns a table constructed from table *t* such that each element is a list that contains a single key-value pair and all elements are sorted according to string *by* (which is either "key" or "value") and boolean *case_sensitive*. Parameters: * *`t`*: * *`case_sensitive`*: Optional flag indicating whether or not to consider case when sorting string values. The default value is `false`. * *`by`*: Optional string that specifies which of the key-value to sort by, either "key" or "value". The default value is `"key"`. * *`value`*: The table to sort. Usage: * `expand('{{ {b = 1, a = 2}|dictsort|string }}') --> {{"a", 2}, {"b", 1}}` <a id="filters.e"></a> #### `filters.e`(*s*) Returns an HTML-safe copy of string *s*. Parameters: * *`s`*: String to ensure is HTML-safe. Usage: * `expand([[{{ '<">&'|escape}}]]) --> <">&` <a id="filters.escape"></a> #### `filters.escape`(*s*) Returns an HTML-safe copy of string *s*. Parameters: * *`s`*: String to ensure is HTML-safe. Usage: * `expand([[{{ '<">&'|e}}]]) --> <">&` <a id="filters.filesizeformat"></a> #### `filters.filesizeformat`(*bytes, binary*) Returns a human-readable, decimal (or binary, depending on boolean *binary*) file size for *bytes* number of bytes. Parameters: * *`bytes`*: The number of bytes to return the size for. * *`binary`*: Flag indicating whether or not to report binary file size as opposed to decimal file size. The default value is `false`. Usage: * `expand('{{ 1000|filesizeformat }}') --> 1.0 kB` <a id="filters.first"></a> #### `filters.first`(*t*) Returns the first element in table *t*. Parameters: * *`t`*: The table to get the first element of. Usage: * `expand('{{ range(10)|first }}') --> 1` <a id="filters.float"></a> #### `filters.float`(*value*) Returns value *value* as a float. This filter only works in Lua 5.3, which has a distinction between floats and integers. Parameters: * *`value`*: The value to interpret as a float. Usage: * `expand('{{ 42|float }}') --> 42.0` <a id="filters.forceescape"></a> #### `filters.forceescape`(*value*) Returns an HTML-safe copy of value *value*, even if *value* was returned by the "safe" filter. Parameters: * *`value`*: Value to ensure is HTML-safe. Usage: * `expand('{% set x = "<div />"|safe %}{{ x|forceescape }}') --> <div />` <a id="filters.format"></a> #### `filters.format`(*s, ...*) Returns the given arguments formatted according to string *s*. See Lua's `string.format()` for more information. Parameters: * *`s`*: The string to format subsequent arguments according to. * *`...`*: Arguments to format. Usage: * `expand('{{ "%s,%s"|format("a", "b") }}') --> a,b` <a id="filters.groupby"></a> #### `filters.groupby`(*t, attribute*) Returns a generator that produces lists of items in table *t* grouped by string attribute *attribute*. Parameters: * *`t`*: The table to group items from. * *`attribute`*: The attribute of items in the table to group by. This may be nested (e.g. "foo.bar" groups by t[i].foo.bar for all i). Usage: * `expand('{% for age, group in people|groupby("age") %}...{% endfor %}')` <a id="filters.indent"></a> #### `filters.indent`(*s, width, first\_line*) Returns a copy of string *s* with all lines after the first indented by *width* number of spaces. If boolean *first_line* is `true`, indents the first line as well. Parameters: * *`s`*: The string to indent lines of. * *`width`*: The number of spaces to indent lines with. * *`first_line`*: Optional flag indicating whether or not to indent the first line of text. The default value is `false`. Usage: * `expand('{{ "foo\nbar"|indent(2) }}') --> "foo\n bar"` <a id="filters.int"></a> #### `filters.int`(*value*) Returns value *value* as an integer. Parameters: * *`value`*: The value to interpret as an integer. Usage: * `expand('{{ 32.32|int }}') --> 32` <a id="filters.join"></a> #### `filters.join`(*t, sep, attribute*) Returns a string that contains all the elements in table *t* (or all the attributes named *attribute* in *t*) separated by string *sep*. Parameters: * *`t`*: The table to join. * *`sep`*: The string to separate table elements with. * *`attribute`*: Optional attribute of elements to use for joining instead of the elements themselves. This may be nested (e.g. "foo.bar" joins `t[i].foo.bar` for all i). Usage: * `expand('{{ {1, 2, 3}|join("|") }}') --> 1|2|3` <a id="filters.last"></a> #### `filters.last`(*t*) Returns the last element in table *t*. Parameters: * *`t`*: The table to get the last element of. Usage: * `expand('{{ range(10)|last }}') --> 10` <a id="filters.length"></a> #### `filters.length`(*value*) Returns the length of string or table *value*. Parameters: * *`value`*: The value to get the length of. Usage: * `expand('{{ "hello world"|length }}') --> 11` <a id="filters.list"></a> #### `filters.list`(*generator, s, i*) Returns the list of items produced by generator *generator*, subject to initial state *s* and initial iterator variable *i*. This filter should only be used after a filter that returns a generator. Parameters: * *`generator`*: Generator function that produces an item. * *`s`*: Initial state for the generator. * *`i`*: Initial iterator variable for the generator. Usage: * `expand('{{ range(4)|batch(2)|list|string }}') --> {{1, 2}, {3, 4}}` See also: * [`filters.batch`](#filters.batch) * [`filters.groupby`](#filters.groupby) * [`filters.slice`](#filters.slice) <a id="filters.lower"></a> #### `filters.lower`(*s*) Returns a copy of string *s* with all lowercase characters. Parameters: * *`s`*: The string to lowercase. Usage: * `expand('{{ "FOO"|lower }}') --> foo` <a id="filters.map"></a> #### `filters.map`(*t, filter, ...*) Maps each element of table *t* to a value produced by filter name *filter* and returns the resultant table. Parameters: * *`t`*: The table of elements to map. * *`filter`*: The name of the filter to pass table elements through. * *`...`*: Any arguments for the filter. Usage: * `expand('{{ {"1", "2", "3"}|map("int")|sum }}') --> 6` <a id="filters.mapattr"></a> #### `filters.mapattr`(*t, attribute, filter, ...*) Maps the value of each element's string *attribute* in table *t* to the value produced by filter name *filter* and returns the resultant table. Parameters: * *`t`*: The table of elements with attributes to map. * *`attribute`*: The attribute of elements in the table to filter. This may be nested (e.g. "foo.bar" maps t[i].foo.bar for all i). * *`filter`*: The name of the filter to pass table elements through. * *`...`*: Any arguments for the filter. Usage: * `expand('{{ users|mapattr("name")|join("|") }}')` <a id="filters.random"></a> #### `filters.random`(*t*) Returns a random element from table *t*. Parameters: * *`t`*: The table to get a random element from. Usage: * `expand('{{ range(100)|random }}')` <a id="filters.reject"></a> #### `filters.reject`(*t, test, ...*) Returns a list of elements in table *t* that fail test name *test*. Parameters: * *`t`*: The table of elements to reject from. * *`test`*: The name of the test to use on table elements. * *`...`*: Any arguments for the test. Usage: * `expand('{{ range(5)|reject(is_odd)|join("|") }}') --> 2|4` <a id="filters.rejectattr"></a> #### `filters.rejectattr`(*t, attribute, test, ...*) Returns a list of elements in table *t* whose string attribute *attribute* fails test name *test*. Parameters: * *`t`*: The table of elements to reject from. * *`attribute`*: The attribute of items in the table to reject from. This may be nested (e.g. "foo.bar" tests t[i].foo.bar for all i). * *`test`*: The name of the test to use on table elements. * *`...`*: Any arguments for the test. Usage: * `expand('{{ users|rejectattr("offline")|mapattr("name")|join(",") }}')` <a id="filters.replace"></a> #### `filters.replace`(*s, pattern, repl, n*) Returns a copy of string *s* with all (or up to *n*) occurrences of string *old* replaced by string *new*. Identical to Lua's `string.gsub()` and handles Lua patterns. Parameters: * *`s`*: The subject string. * *`pattern`*: The string or Lua pattern to replace. * *`repl`*: The replacement text (may contain Lua captures). * *`n`*: Optional number indicating the maximum number of replacements to make. The default value is `nil`, which is unlimited. Usage: * `expand('{% filter upper|replace("FOO", "foo") %}foobar {% endfilter %}') --> fooBAR` <a id="filters.reverse"></a> #### `filters.reverse`(*value*) Returns a copy of the given string or table *value* in reverse order. Parameters: * *`value`*: The value to reverse. Usage: * `expand('{{ {1, 2, 3}|reverse|string }}') --> {3, 2, 1}` <a id="filters.round"></a> #### `filters.round`(*value, precision, method*) Returns number *value* rounded to *precision* decimal places based on string *method* (if given). Parameters: * *`value`*: The number to round. * *`precision`*: Optional precision to round the number to. The default value is `0`. * *`method`*: Optional string rounding method, either `"ceil"` or `"floor"`. The default value is `nil`, which uses the common rounding method (if a number's fractional part is 0.5 or greater, rounds up; otherwise rounds down). Usage: * `expand('{{ 2.1236|round(3, "floor") }}') --> 2.123` <a id="filters.safe"></a> #### `filters.safe`(*s*) Marks string *s* as HTML-safe, preventing Lupa from modifying it when configured to autoescape HTML entities. This filter must be used at the end of a filter chain unless it is immediately proceeded by the "forceescape" filter. Parameters: * *`s`*: The string to mark as HTML-safe. Usage: * `lupa.configure{autoescape = true}` * `expand('{{ "<div>foo</div>"|safe }}') --> <div>foo</div>` <a id="filters.select"></a> #### `filters.select`(*t, test, ...*) Returns a list of the elements in table *t* that pass test name *test*. Parameters: * *`t`*: The table of elements to select from. * *`test`*: The name of the test to use on table elements. * *`...`*: Any arguments for the test. Usage: * `expand('{{ range(5)|select(is_odd)|join("|") }}') --> 1|3|5` <a id="filters.selectattr"></a> #### `filters.selectattr`(*t, attribute, test, ...*) Returns a list of elements in table *t* whose string attribute *attribute* passes test name *test*. Parameters: * *`t`*: The table of elements to select from. * *`attribute`*: The attribute of items in the table to select from. This may be nested (e.g. "foo.bar" tests t[i].foo.bar for all i). * *`test`*: The name of the test to use on table elements. * *`...`*: Any arguments for the test. Usage: * `expand('{{ users|selectattr("online")|mapattr("name")|join("|") }}')` <a id="filters.slice"></a> #### `filters.slice`(*t, slices, fill*) Returns a generator that produces all of the items in table *t* in *slices* number of iterations, filling any empty spaces with value *fill*. Combine this with the "list" filter to produce a list. Parameters: * *`t`*: The table to slice. * *`slices`*: The number of slices to produce. * *`fill`*: The value to use when filling in any empty space in the last slice. Usage: * `expand('{% for i in {1, 2, 3}|slice(2, 0) %}{{ i|string }} {% endfor %}') --> {1, 2} {3, 0}` See also: * [`filters.list`](#filters.list) <a id="filters.sort"></a> #### `filters.sort`(*value, reverse, case\_sensitive, attribute*) Returns a copy of table or string *value* in sorted order by value (or by an attribute named *attribute*), depending on booleans *reverse* and *case_sensitive*. Parameters: * *`value`*: The table or string to sort. * *`reverse`*: Optional flag indicating whether or not to sort in reverse (descending) order. The default value is `false`, which sorts in ascending order. * *`case_sensitive`*: Optional flag indicating whether or not to consider case when sorting string values. The default value is `false`. * *`attribute`*: Optional attribute of elements to sort by instead of the elements themselves. Usage: * `expand('{{ {2, 3, 1}|sort|string }}') --> {1, 2, 3}` <a id="filters.string"></a> #### `filters.string`(*value*) Returns the string representation of value *value*, handling lists properly. Parameters: * *`value`*: Value to return the string representation of. Usage: * `expand('{{ {1 * 1, 2 * 2, 3 * 3}|string }}') --> {1, 4, 9}` <a id="filters.striptags"></a> #### `filters.striptags`(*s*) Returns a copy of string *s* with any HTML tags stripped. Also cleans up whitespace. Parameters: * *`s`*: String to strip HTML tags from. Usage: * `expand('{{ "<div>foo</div>"|striptags }}') --> foo` <a id="filters.sum"></a> #### `filters.sum`(*t, attribute*) Returns the numeric sum of the elements in table *t* or the sum of all attributes named *attribute* in *t*. Parameters: * *`t`*: The table to calculate the sum of. * *`attribute`*: Optional attribute of elements to use for summing instead of the elements themselves. This may be nested (e.g. "foo.bar" sums `t[i].foo.bar` for all i). Usage: * `expand('{{ range(6)|sum }}') --> 21` <a id="filters.title"></a> #### `filters.title`(*s*) Returns a copy of all words in string *s* in titlecase. Parameters: * *`s`*: The string to titlecase. Usage: * `expand('{{ "foo bar"|title }}') --> Foo Bar` <a id="filters.truncate"></a> #### `filters.truncate`(*s, length, partial\_words, delimiter*) Returns a copy of string *s* truncated to *length* number of characters. Truncated strings end with '...' or string *delimiter*. If boolean *partial_words* is `false`, truncation will only happen at word boundaries. Parameters: * *`s`*: The string to truncate. * *`length`*: The length to truncate the string to. * *`partial_words`*: Optional flag indicating whether or not to allow truncation within word boundaries. The default value is `false`. * *`delimiter`*: Optional delimiter text. The default value is '...'. Usage: * `expand('{{ "foo bar"|truncate(4) }}') --> "foo ..."` <a id="filters.upper"></a> #### `filters.upper`(*s*) Returns a copy of string *s* with all uppercase characters. Parameters: * *`s`*: The string to uppercase. Usage: * `expand('{{ "foo"|upper }}') --> FOO` <a id="filters.urlencode"></a> #### `filters.urlencode`(*value*) Returns a string suitably encoded to be used in a URL from value *value*. *value* may be a string, table of key-value query parameters, or table of lists of key-value query parameters (for order). Parameters: * *`value`*: Value to URL-encode. Usage: * `expand('{{ {{'f', 1}, {'z', 2}}|urlencode }}') --> f=1&z=2` <a id="filters.urlize"></a> #### `filters.urlize`(*s, length, nofollow*) Replaces any URLs in string *s* with HTML links, limiting link text to *length* characters. Parameters: * *`s`*: The string to replace URLs with HTML links in. * *`length`*: Optional maximum number of characters to include in link text. The default value is `nil`, which imposes no limit. * *`nofollow`*: Optional flag indicating whether or not HTML links will get a "nofollow" attribute. Usage: * `expand('{{ "example.com"|urlize }}') --> <a href="http://example.com">example.com</a>` <a id="filters.wordcount"></a> #### `filters.wordcount`(*s*) Returns the number of words in string *s*. A word is a sequence of non-space characters. Parameters: * *`s`*: The string to count words in. Usage: * `expand('{{ "foo bar baz"|wordcount }}') --> 3` <a id="filters.xmlattr"></a> #### `filters.xmlattr`(*t*) Interprets table *t* as a list of XML attribute-value pairs, returning them as a properly formatted, space-separated string. Parameters: * *`t`*: The table of XML attribute-value pairs. Usage: * `expand('<data {{ {foo = 42, bar = 23}|xmlattr }} />')` <a id="loaders.filesystem"></a> #### `loaders.filesystem`(*directory*) Returns a loader for templates that uses the filesystem starting at directory *directory*. When looking up the template for a given filename, the loader considers the following: if no template is being expanded, the loader assumes the given filename is relative to *directory* and returns the full path; otherwise the loader assumes the given filename is relative to the current template's directory and returns the full path. The returned path may be passed to `io.open()`. Parameters: * *`directory`*: Optional the template root directory. The default value is ".", which is the current working directory. See also: * [`lupa.configure`](#lupa.configure) <a id="lupa.reset"></a> #### `lupa.reset`() Resets Lupa's default delimiters, options, and environments to their original default values. <a id="tests.is_callable"></a> #### `tests.is_callable`(*value*) Returns whether or not value *value* is a function. Parameters: * *`value`*: The value to test. Usage: * `expand('{% if is_callable(x) %}...{% endif %}')` <a id="tests.is_defined"></a> #### `tests.is_defined`(*value*) Returns whether or not value *value* is non-nil, and thus defined. Parameters: * *`value`*: The value to test. Usage: * `expand('{% if is_defined(x) %}...{% endif %}')` <a id="tests.is_divisibleby"></a> #### `tests.is_divisibleby`(*n, num*) Returns whether or not number *n* is evenly divisible by number *num*. Parameters: * *`n`*: The dividend to test. * *`num`*: The divisor to use. Usage: * `expand('{% if is_divisibleby(x, y) %}...{% endif %}')` <a id="tests.is_escaped"></a> #### `tests.is_escaped`(*value*) Returns whether or not value *value* is HTML-safe. Parameters: * *`value`*: The value to test. Usage: * `expand('{% if is_escaped(x) %}...{% endif %}')` <a id="tests.is_even"></a> #### `tests.is_even`(*n*) Returns whether or not number *n* is even. Parameters: * *`n`*: The number to test. Usage: * `expand('{% for x in range(10) if is_even(x) %}...{% endif %}')` <a id="tests.is_iterable"></a> #### `tests.is_iterable`(*value*) Returns whether or not value *value* is a sequence (a table with non-zero length) or a generator. At the moment, all functions are considered generators. Parameters: * *`value`*: The value to test. Usage: * `expand('{% if is_iterable(x) %}...{% endif %}')` <a id="tests.is_lower"></a> #### `tests.is_lower`(*s*) Returns whether or not string *s* is in all lower-case characters. Parameters: * *`s`*: The string to test. Usage: * `expand('{% if is_lower(s) %}...{% endif %}')` <a id="tests.is_mapping"></a> #### `tests.is_mapping`(*value*) Returns whether or not value *value* is a table. Parameters: * *`value`*: The value to test. Usage: * `expand('{% if is_mapping(x) %}...{% endif %}')` <a id="tests.is_nil"></a> #### `tests.is_nil`(*value*) Returns whether or not value *value* is nil. Parameters: * *`value`*: The value to test. Usage: * `expand('{% if is_nil(x) %}...{% endif %}')` <a id="tests.is_none"></a> #### `tests.is_none`(*value*) Returns whether or not value *value* is nil. Parameters: * *`value`*: The value to test. Usage: * `expand('{% if is_none(x) %}...{% endif %}')` <a id="tests.is_number"></a> #### `tests.is_number`(*value*) Returns whether or not value *value* is a number. Parameters: * *`value`*: The value to test. Usage: * `expand('{% if is_number(x) %}...{% endif %}')` <a id="tests.is_odd"></a> #### `tests.is_odd`(*n*) Returns whether or not number *n* is odd. Parameters: * *`n`*: The number to test. Usage: * `expand('{% for x in range(10) if is_odd(x) %}...{% endif %}')` <a id="tests.is_sameas"></a> #### `tests.is_sameas`(*value, other*) Returns whether or not value *value* is the same as value *other*. Parameters: * *`value`*: The value to test. * *`other`*: The value to compare with. Usage: * `expand('{% if is_sameas(x, y) %}...{% endif %}')` <a id="tests.is_sequence"></a> #### `tests.is_sequence`(*value*) Returns whether or not value *value* is a sequence, namely a table with non-zero length. Parameters: * *`value`*: The value to test. Usage: * `expand('{% if is_sequence(x) %}...{% endif %}')` <a id="tests.is_string"></a> #### `tests.is_string`(*value*) Returns whether or not value *value* is a string. Parameters: * *`value`*: The value to test. Usage: * `expand('{% if is_string(x) %}...{% endif %}')` <a id="tests.is_table"></a> #### `tests.is_table`(*value*) Returns whether or not value *value* is a table. Parameters: * *`value`*: The value to test. Usage: * `expand('{% if is_table(x) %}...{% endif %}')` <a id="tests.is_undefined"></a> #### `tests.is_undefined`(*value*) Returns whether or not value *value* is nil, and thus effectively undefined. Parameters: * *`value`*: The value to test. Usage: * `expand('{% if is_undefined(x) %}...{% endif %}')` <a id="tests.is_upper"></a> #### `tests.is_upper`(*s*) Returns whether or not string *s* is in all upper-case characters. Parameters: * *`s`*: The string to test. Usage: * `expand('{% if is_upper(s) %}...{% endif %}')` ### Tables defined by `lupa` <a id="lupa.env"></a> #### `lupa.env` The default template environment. <a id="lupa.filters"></a> #### `lupa.filters` Lupa's expression filters. <a id="lupa.loaders"></a> #### `lupa.loaders` Lupa's template loaders. <a id="lupa.tests"></a> #### `lupa.tests` Lupa's value tests. --- {% endraw %} 07070100000008000081A400000000000000000000000166D5601B0000013D000000000000000000000000000000000000001B00000000luajinja/docs/changelog.md# Changelog ## 1.0 (11 Nov 2015) Download: * [Lupa 1.0][] Bugfixes: * Fixed some warnings issued by Luacheck, a Lua linter. Changes: * None. [Lupa 1.0]: download/lupa_1.0.zip ## 0.1_alpha (18 Mar 2015) Download: * [Lupa 0.1 alpha][] Initial release. [Lupa 0.1 alpha]: download/lupa_0.1_alpha.zip 07070100000009000081ED00000000000000000000000166D5601B0000033F000000000000000000000000000000000000001E00000000luajinja/docs/fill_layout.lua#!/usr/bin/lua -- Filters the given file through markdown, inserts it into the template -- specified by stdin by replacing simple {{ variable }} tags, and outputs the -- result to stdout. -- Filter the file through markdown using TOC generation in order to get header -- anchors, but ignore the actual TOC. local p = io.popen('markdown -f toc -T ' .. arg[1]) local html = p:read('*a'):match('^.-\n</ul>\n(.+)$') html = html:gsub('<h(%d) id="([^"]+)"', function(n, id) id = id:gsub('%p+', '-'):gsub('%-$', ''):lower():gsub('^l%-', '') return string.format('<h%d id="%s"', n, id) end) p:close() -- Fill in HTML layout (stdin) with markdown output and print the result. local tags = { ['page.title'] = html:match('<h%d.->([^<]+)'), content = html:gsub('%%', '%%%%') } io.write(io.stdin:read('*a'):gsub('{{ (%S+) }}', tags)) 0707010000000A000081A400000000000000000000000166D5601B00001CE4000000000000000000000000000000000000001700000000luajinja/docs/index.md{% raw %} ## Introduction Lupa is a [Jinja2][] template engine implementation written in Lua and supports Lua syntax within tags and variables. Lupa was sponsored by the [Library of the University of Antwerp][]. [Jinja2]: http://jinja.pocoo.org [Library of the University of Antwerp]: http://www.uantwerpen.be/ ## Requirements Lupa has the following requirements: * [Lua][] 5.1, 5.2, or 5.3. * The [LPeg][] library. [Lua]: https://www.lua.org [LPeg]: http://www.inf.puc-rio.br/~roberto/lpeg/ ## Download Lupa releases can be found [here][]. [here]: https://github.com/orbitalquark/lupa/releases ## Installation Unzip Lupa and place the "lupa.lua" file in your Lua installation's `package.path`. This location depends on your version of Lua. Typical locations are listed below. * Lua 5.1: */usr/local/share/lua/5.1/* or */usr/local/share/lua/5.1/* * Lua 5.2: */usr/local/share/lua/5.2/* or */usr/local/share/lua/5.2/* * Lua 5.3: */usr/local/share/lua/5.3/* or */usr/local/share/lua/5.3/* You can also place the *lupa.lua* file wherever you'd like and add it to Lua's `package.path` manually in your program. For example, if Lupa was placed in a */home/user/lua/* directory, it can be used as follows: package.path = package.path .. ';/home/user/lua/?.lua' ## Usage Lupa is simply a Lua library. Its `lupa.expand()` and `lupa.expand_file()` functions may called to process templates. For example: lupa = require('lupa') lupa.expand("hello {{ s }}!", {s = "world"}) --> "hello world!" lupa.expand("{% for i in {1, 2, 3} %}{{ i }}{% endfor %}") --> 123 By default, Lupa loads templates relative to the current working directory. This can be changed by reconfiguring Lupa: lupa.expand_file('name') --> expands template "./name" lupa.configure{loader = lupa.loaders.filesystem('path/to/templates')} lupa.expand_file('name') --> expands template "path/to/templates/name" See Lupa's [API documentation][] for more information. [API documentation]: api.html ## Syntax Please refer to Jinja2's extensive [template documentation][]. Any incompatibilities are listed in the sections below. [template documentation]: http://jinja.pocoo.org/docs/dev/templates/ ## Comparison with Jinja2 While Lua and Python (Jinja2's implementation language) share some similarities, the languages themselves are fundamentally different. Nevertheless, a significant effort was made to support a vast majority of Jinja2's Python-style syntax. As a result, Lupa passes Jinja2's test suite with only a handful of modifications. The comprehensive list of differences between Lupa and Jinja2 is described in the following sections. ### Fundamental Differences * Expressions use Lua's syntax instead of Python's, so many of Python's syntactic constructs are not valid. However, the following constructs *are valid*, despite being invalid in pure Lua: + Iterating over table literals or table variables directly in a "for" loop: {% for i in {1, 2, 3} %}...{% endfor %} + Conditional loops via an "if" expression suffix: {% for x in range(10) if is_odd(x) %}...{% endfor %} + Table unpacking for list elements when iterating through a list of lists: {% for a, b, c in {{1, 2, 3}, {4, 5, 6}} %}...{% endfor %} + Default values for macro arguments: {% macro m(a, b, c='c', d='d') %}...{% endmacro %} * Strings do not have unicode escapes nor is unicode interpreted in any way. ### Syntactic Differences * Line statements are not supported due to parsing complexity. * In `{% for ... %}` loops, the `loop.length`, `loop.revindex`, `loop.revindex0`, and `loop.last` variables only apply to sequences, where Lua's `'#'` operator applies. * The `{% continue %}` and `{% break %}` loop controls are not supported due to complexity. * Loops may be used recursively by default, so the `recursive` loop modifier is not supported. * The `is` operator is not supported by Lua, so tests of the form `{{ x is y }}` should be written `{{ is_y(x) }}` (e.g. `{{ is_number(42) }}`). * Filters cannot occur after tokens within an expression (e.g. `{{ "foo"|upper .. "bar"|upper }}`), but can only occur at the end of an expression (e.g. `{{ "foo".."bar"|upper }}`). * Blocks always have access to scoped variables, so the `scoped` block modifier is not supported. * Named block end tags are not supported since the parser cannot easily keep track of that state information. * Any `{% block ... %}` tags within a "false" block (e.g. `{% if a %}` where `a` evaluates to `false`) are never read and stored due to the parser implementation. * Inline "if" expressions (e.g. `{% extends b if a else c %}`) are not supported. Instead, use a Lua conditional expression (e.g. `{% extends a and b or c %}`). * Any `{% extends ... %}` tags within a sub-scope are not effective outside that scope (e.g. `{% if a %}{% extends a %}{% else %}{% extends b %}{% endif %}`). Instead, use a Lua conditional expression (e.g. `{% extends a or b %}`). * Macros are simply Lua functions and have no metadata attributes. * Macros do not have access to a `kwargs` variable since Lua does not support keyword arguments. * `{% from x import y %}` tags are not supported. Instead, you must use either `{% import x %}`, which imports all globals in `x` into the current environment, or use `{% import x as z %}`, which imports all globals in `x` into the variable `z`. * `{% set ... %}` does not support multiple assignment. Use `{% do ...%}` instead. The catch is that `{% do ... %}` does not support filters. * The `{% trans %}` and `{% endtrans %}` tags, `{% with %}` and `{% endwith %}` tags, and `{% autoescape %}` and `{% endautoescape %}` tags are not supported since they are outside the scope of this implementation. ### Filter Differences * Only the `batch`, `groupby`, and `slice` filters return generators which produce one item at a time when looping. All other filters that produce iterable results generate all items at once. * The `float` filter only works in Lua 5.3 since that version of Lua has a distinction between floats and integers. * The `safe` filter must appear at the end of a filter chain since its output cannot be passed to any other filter. ### Function Differences * The global `range(n)` function returns a sequence from 1 to `n`, inclusive, since lists start at 1 in Lua. * No `lipsum()`, `dict()`, or `joiner()` functions for the sake of simplicity. ### API Differences * Lupa has a much simpler API consisting of just four functions and three fields: + `lupa.expand()`: Expands a string template subject to an environment. + `lupa.expand_file()`: Expands a file template subject to an environment. + `lupa.configure()` Configures delimiters and template options. + `lupa.reset()`: Resets delimiters and options to their defaults. + `lupa.env`: The default environment for templates. + `lupa.filters`: The set of available filters (`escape`, `join`, etc.). + `lupa.tests`: The set of available tests (`is_odd`, `is_defined`, etc.). * There is no bytecode caching. * Lupa has no extension mechanism. Instead, modify `lupa.env`, `lupa.filters`, and `lupa.tests` directly. However, the parser cannot be extended. * Sandboxing is not supported, although `lupa.env` is safe by default (`io`, `os.execute`, `os.remove`, etc. are not available). {% endraw %} 0707010000000B000081A400000000000000000000000166D5601B000016CD000000000000000000000000000000000000001E00000000luajinja/docs/markdowndoc.lua-- Copyright 2015-2020 Mitchell. See LICENSE. -- Markdown doclet for Luadoc. -- @usage luadoc -d [output_path] -doclet path/to/markdowndoc [file(s)] local M = {} local MODULE = '<a id="%s"></a>\n## The `%s` Module\n' local FIELD = '<a id="%s"></a>\n#### `%s` %s\n\n' local FUNCTION = '<a id="%s"></a>\n#### `%s`(*%s*)\n\n' local FUNCTION_NO_PARAMS = '<a id="%s"></a>\n#### `%s`()\n\n' local DESCRIPTION = '%s\n\n' local LIST_TITLE = '%s:\n\n' local PARAM = '* *`%s`*: %s\n' local USAGE = '* `%s`\n' local RETURN = '* %s\n' local SEE = '* [`%s`](#%s)\n' local TABLE = '<a id="%s"></a>\n#### `%s`\n\n' local TFIELD = '* `%s`: %s\n' local titles = { [PARAM] = 'Parameters', [USAGE] = 'Usage', [RETURN] = 'Return', [SEE] = 'See also', [TFIELD] = 'Fields' } -- Writes a LuaDoc description to the given file. -- @param f The markdown file being written to. -- @param description The description. -- @param name The name of the module the description belongs to. Used for -- headers in module descriptions. local function write_description(f, description, name) -- Substitute custom [`code`]() link convention with [`code`](#code) links. local self_link = '(%[`([^`(]+)%(?%)?`%])%(%)' description = description:gsub(self_link, function(link, id) return string.format("%s(#%s)", link, id:gsub(':', '.')) end) f:write(string.format(DESCRIPTION, description)) end -- Writes a LuaDoc list to the given file. -- @param f The markdown file being written to. -- @param fmt The format of a list item. -- @param list The LuaDoc list. -- @param name The name of the module the list belongs to. Used for @see. local function write_list(f, fmt, list, name) if not list or #list == 0 then return end if type(list) == 'string' then list = {list} end f:write(string.format(LIST_TITLE, titles[fmt])) for _, value in ipairs(list) do if fmt == SEE and name ~= '_G' then if not value:find('%.') then -- Prepend module name to identifier if necessary. value = name .. '.' .. value else -- TODO: cannot link to fields, functions, or tables in `_G`? value = value:gsub('^_G%.', '') end end f:write(string.format(fmt, value, value)) end f:write('\n') end -- Writes a LuaDoc hashmap to the given file. -- @param f The markdown file being written to. -- @param fmt The format of a hashmap item. -- @param list The LuaDoc hashmap. local function write_hashmap(f, fmt, hashmap) if not hashmap or #hashmap == 0 then return end f:write(string.format(LIST_TITLE, titles[fmt])) for _, name in ipairs(hashmap) do f:write(string.format(fmt, name, hashmap[name] or '')) end f:write('\n') end -- Called by LuaDoc to process a doc object. -- @param doc The LuaDoc doc object. function M.start(doc) local modules, files = doc.modules, doc.files local f = io.stdout f:write('## Lupa API Documentation\n\n') -- Create a map of doc objects to file names so their Markdown doc comments -- can be extracted. local filedocs = {} for _, name in ipairs(files) do filedocs[files[name].doc] = name end -- Loop over modules, writing the Markdown document to stdout. for _, name in ipairs(modules) do local module = modules[name] -- Write the header and description. f:write(string.format(MODULE, name, name)) f:write('---\n\n') write_description(f, module.description, name) -- Write fields. if module.doc[1].class == 'module' then local fields = module.doc[1].field if fields and #fields > 0 then table.sort(fields) f:write('### Fields defined by `', name, '`\n\n') for _, field in ipairs(fields) do local type, description = fields[field]:match('^(%b())%s*(.+)$') if not field:find('%.') and name ~= '_G' then field = name .. '.' .. field -- absolute name else field = field:gsub('^_G%.', '') -- strip _G required for Luadoc end f:write(string.format(FIELD, field, field, type or '')) write_description(f, description or fields[field]) end f:write('\n') end end -- Write functions. local funcs = module.functions if #funcs > 0 then f:write('### Functions defined by `', name, '`\n\n') for _, fname in ipairs(funcs) do local func = funcs[fname] local params = table.concat(func.param, ', '):gsub('_', '\\_') if not func.name:find('[%.:]') and name ~= '_G' then func.name = name .. '.' .. func.name -- absolute name end if params ~= '' then f:write(string.format(FUNCTION, func.name, func.name, params)) else f:write(string.format(FUNCTION_NO_PARAMS, func.name, func.name)) end write_description(f, func.description) write_hashmap(f, PARAM, func.param) write_list(f, USAGE, func.usage) write_list(f, RETURN, func.ret) write_list(f, SEE, func.see, name) end f:write('\n') end -- Write tables. local tables = module.tables if #tables > 0 then f:write('### Tables defined by `', name, '`\n\n') for _, tname in ipairs(tables) do local tbl = tables[tname] if not tbl.name:find('%.') and (name ~= '_G' or tbl.name == 'buffer' or tbl.name == 'view') then tbl.name = name .. '.' .. tbl.name -- absolute name elseif tbl.name ~= '_G.keys' and tbl.name ~= '_G.snippets' then tbl.name = tbl.name:gsub('^_G%.', '') -- strip _G required for Luadoc end f:write(string.format(TABLE, tbl.name, tbl.name)) write_description(f, tbl.description) write_hashmap(f, TFIELD, tbl.field) write_list(f, USAGE, tbl.usage) write_list(f, SEE, tbl.see, name) end end f:write('---\n') end end return M 0707010000000C000081A400000000000000000000000166D5601B000006B6000000000000000000000000000000000000001800000000luajinja/docs/style.css/* Copyright 2012-2020 Mitchell. See LICENSE. */ * { border: 0 solid #a6a6a6; margin: 0; padding: 0; } a { color: #1a66b3; text-decoration: none; } a:hover { text-decoration: underline; } a:visited { color: #661a66; } body { background-color: #f2f2f2; color: #333333; } code { font-size: 1.2em; } del { color: #994d4d; } h1 { margin: 0 0 1em 0; } h2, h3, h4, h5, h6 { margin: 1em 0 1em 0; } h1 { font-size: 1.3em; } h2 { font-size: 1.1em; } h3 { font-size: 1em; } h4 { font-size: 0.9em; } h5 { font-size: 0.8em; } hr { border: 1px solid #d9d9d9; border-width: 1px 0 0 0; margin: 1em 0 1em 0; } input, textarea { border-width: 1px; font-size: 1em; } ins { color: #4d994d; } li > code, p > code, em > code, td > code { color: #808080; } pre { color: #808080; margin: 0 2.5em 0 2.5em; white-space: pre-wrap; } table, th, td { border-width: 1px; border-collapse: collapse; margin-left: 1em; padding: 0.25em; } #content { border-width: 0 1px 0 1px; font-size: 1.2em; margin: 0 auto 0 auto; max-width: 1000px; } #header h1 { background-color: #d9d9d9; border-width: 0 0 1px 0; margin: 0; padding: 0.25em; } #header img { vertical-align: text-bottom; } #header ul, #header p { border-width: 0 0 1px 0; margin-bottom: 1.5em; padding: 0.25em; } #header ul { list-style: none; } #header li { color: #808080; display: inline; } #header li form { display: inline; } #main { margin-left: 1em; } #main dl, #main p { margin: 1em; } #main dd, #main ol, #main ul { margin-left: 2.5em; } #main ol p, #main ul p { margin-left: 0; } #footer { background-color: #d9d9d9; border-width: 1px 0 1px 0; clear: both; padding: 0.25em; margin-top: 1.5em; } 0707010000000D000081A400000000000000000000000166D5601B0000025B000000000000000000000000000000000000002100000000luajinja/luajinja-scm-1.rockspecpackage = 'luajinja' version = 'scm-1' local url='github.com/huakim/luajinja' source = { url = "git://" .. url .. ".git", } description = { detailed = [[ Lupa is a Jinja2 template engine implementation written in Lua and supports Lua syntax within tags and variables. Lupa was sponsored by the Library of the University of Antwerp. ]], homepage = 'https://' .. url, license = "LGPL", summary = "Jinja2 template engine implementation written in Lua.", } build = { modules = { ["luajinja"] = "luajinja.lua", }, type = "builtin", } dependencies = { "lua >= 5.1", "lpeg", } 0707010000000E000081A400000000000000000000000166D5601B00012063000000000000000000000000000000000000001600000000luajinja/luajinja.lua-- Copyright 2015-2020 Mitchell. See LICENSE. -- Sponsored by the Library of the University of Antwerp. -- Contributions from Ana Balan. -- Lupa templating engine. --[[ This comment is for LuaDoc. --- -- Lupa is a Jinja2 template engine implementation written in Lua and supports -- Lua syntax within tags and variables. module('lupa')]] local M = {} local lpeg = require('lpeg') lpeg.locale(lpeg) local space, newline = lpeg.space, lpeg.P('\r')^-1 * '\n' local P, S, V = lpeg.P, lpeg.S, lpeg.V local C, Cc, Cg, Cp, Ct = lpeg.C, lpeg.Cc, lpeg.Cg, lpeg.Cp, lpeg.Ct --- -- Lupa's expression filters. -- @class table -- @name filters M.filters = {} --- -- Lupa's value tests. -- @class table -- @name tests M.tests = {} --- -- Lupa's template loaders. -- @class table -- @name loaders M.loaders = {} -- Lua version compatibility. if _VERSION == 'Lua 5.1' then function load(ld, source, mode, env) local f, err = loadstring(ld) if f and env then return setfenv(f, env) end return f, err end table.unpack = unpack end local newline_sequence, keep_trailing_newline, autoescape = '\n', false, false local loader -- Creates and returns a token pattern with token name *name* and pattern -- *patt*. -- The returned pattern captures three values: the token's position and name, -- and either a string value or table of capture values. -- Tokens are used to construct an Abstract Syntax Tree (AST) for a template. -- @param name The name of the token. -- @param patt The pattern to match. It must contain only one capture: either a -- string or table of captures. -- @see evaluate local function token(name, patt) return Cp() * Cc(name) * patt end -- Returns an LPeg pattern that immediately raises an error with message -- *errmsg* for invalid syntax when parsing a template. -- @param errmsg The error message to raise an error with. local function lpeg_error(errmsg) return P(function(input, index) input = input:sub(1, index) local _, line_num = input:gsub('\n', '') local col_num = #input:match('[^\n]*$') error(string.format('Parse Error in file "%s" on line %d, column %d: %s', M._FILENAME, line_num + 1, col_num, errmsg), 0) end) end --- -- Configures the basic delimiters and options for templates. -- This function then regenerates the grammar for parsing templates. -- Note: this function cannot be used iteratively to configure Lupa options. -- Any options not provided are reset to their default values. -- @param ts The tag start delimiter. The default value is '{%'. -- @param te The tag end delimiter. The default value is '%}'. -- @param vs The variable start delimiter. The default value is '{{'. -- @param ve The variable end delimiter. The default value is '}}'. -- @param cs The comment start delimiter. The default value is '{#'. -- @param ce The comment end delimiter. The default value is '#}'. -- @param options Optional set of options for templates: -- -- * `trim_blocks`: Trim the first newline after blocks. -- * `lstrip_blocks`: Strip line-leading whitespace in front of tags. -- * `newline_sequence`: The end-of-line character to use. -- * `keep_trailing_newline`: Whether or not to keep a newline at the end of -- a template. -- * `autoescape`: Whether or not to autoescape HTML entities. May be a -- function that accepts the template's filename as an argument and returns -- a boolean. -- * `loader`: Function that receives a template name to load and returns the -- path to that template. -- @name configure function M.configure(ts, te, vs, ve, cs, ce, options) if type(ts) == 'table' then options, ts = ts, nil end if not ts then ts = '{%' end if not te then te = '%}' end if not vs then vs = '{{' end if not ve then ve = '}}' end if not cs then cs = '{#' end if not ce then ce = '#}' end -- Tokens for whitespace control. local lstrip = token('lstrip', C('-')) + '+' -- '+' is handled by grammar local rstrip = token('rstrip', -(P(te) + ve + ce) * C('-')) -- Configure delimiters, including whitespace control. local tag_start = P(ts) * lstrip^-1 * space^0 local tag_end = space^0 * rstrip^-1 * P(te) local variable_start = P(vs) * lstrip^-1 * space^0 local variable_end = space^0 * rstrip^-1 * P(ve) local comment_start = P(cs) * lstrip^-1 * space^0 local comment_end = space^0 * rstrip^-1 * P(ce) if options and options.trim_blocks then -- Consider whitespace, including a newline, immediately following a tag as -- part of that tag so it is not captured as plain text. Basically, strip -- the trailing newline from tags. tag_end = tag_end * S(' \t')^0 * newline^-1 comment_end = comment_end * S(' \t')^0 * newline^-1 end -- Error messages. local variable_end_error = lpeg_error('"'..ve..'" expected') local comment_end_error = lpeg_error('"'..ce..'" expected') local tag_end_error = lpeg_error('"'..te..'" expected') local endraw_error = lpeg_error('additional tag or "'..ts..' endraw '..te.. '" expected') local expr_error = lpeg_error('expression expected') local endblock_error = lpeg_error('additional tag or "'..ts..' endblock '.. te..'" expected') local endfor_error = lpeg_error('additional tag or "'..ts..' endfor '..te.. '" expected') local endif_error = lpeg_error('additional tag or "'..ts..' endif '..te.. '" expected') local endmacro_error = lpeg_error('additional tag or "'..ts..' endmacro '.. te..'" expected') local endcall_error = lpeg_error('additional tag or "'..ts..' endcall '..te.. '" expected') local endfilter_error = lpeg_error('additional tag or "'..ts..' endfilter '.. te..'" expected') local tag_error = lpeg_error('unknown or unexpected tag') local main_error = lpeg_error('unexpected character; text or tag expected') -- Grammar. M.grammar = Ct(P{ -- Utility patterns used by tokens. entity_start = tag_start + variable_start + comment_start, any_text = (1 - V('entity_start'))^1, -- Allow '{{' by default in expression text since it is valid in Lua. expr_text = (1 - tag_end - tag_start - comment_start)^1, -- When `options.lstrip_blocks` is enabled, ignore leading whitespace -- immediately followed by a tag (as long as '+' is not present) so that -- whitespace not captured as plain text. Basically, strip leading spaces -- from tags. line_text = (1 - newline - V('entity_start'))^1, lstrip_entity_start = -P(vs) * (P(ts) + cs) * -P('+'), lstrip_space = S(' \t')^1 * #V('lstrip_entity_start'), text_lines = V('line_text') * (newline * -(S(' \t')^0 * V('lstrip_entity_start')) * V('line_text'))^0 * newline^-1 + newline, -- Plain text. text = (not options or not options.lstrip_blocks) and token('text', C(V('any_text'))) or V('lstrip_space') + token('text', C(V('text_lines'))), -- Variables: {{ expr }}. lua_table = '{' * ((1 - S('{}')) + V('lua_table'))^0 * '}', variable = variable_start * token('variable', C((V('lua_table') + (1 - variable_end))^0)) * (variable_end + variable_end_error), -- Filters: handled in variable evaluation. -- Tests: handled in control structure expression evaluation. -- Comments: {# comment #}. comment = comment_start * (1 - comment_end)^0 * (comment_end + comment_end_error), -- Whitespace control: handled in tag/variable/comment start/end. -- Escaping: {% raw %} body {% endraw %}. raw_block = tag_start * 'raw' * (tag_end + tag_end_error) * token('text', C((1 - (tag_start * 'endraw' * tag_end))^0)) * (tag_start * 'endraw' * tag_end + endraw_error), -- Note: line statements are not supported since this grammer cannot parse -- Lua itself. -- Template inheritence. -- {% block ... %} body {% endblock %} block_block = tag_start * 'block' * space^1 * token('block', Ct((Cg(V('expr_text'), 'expression') + expr_error) * (tag_end + tag_end_error) * V('body')^-1)) * (tag_start * 'endblock' * tag_end + endblock_error), -- {% extends ... %} extends_tag = tag_start * 'extends' * space^1 * token('extends', C(V('expr_text')) + expr_error) * (tag_end + tag_end_error), -- Super blocks are handled in variables. -- Note: named block end tags are not supported since keeping track of that -- state information is difficult. -- Note: block nesting and scope is not applicable since blocks always have -- access to scoped variables in this implementation. -- Control Structures. -- {% for expr %} body {% else %} body {% endfor %} for_block = tag_start * 'for' * space^1 * token('for', Ct((Cg(V('expr_text'), 'expression') + expr_error) * (tag_end + tag_end_error) * V('body')^-1 * Cg(Ct(tag_start * 'else' * tag_end * V('body')^-1), 'else')^-1)) * (tag_start * 'endfor' * tag_end + endfor_error), -- {% if expr %} body {% elseif expr %} body {% else %} body {% endif %} if_block = tag_start * 'if' * space^1 * token('if', Ct((Cg(V('expr_text'), 'expression') + expr_error) * (tag_end + tag_end_error) * V('body')^-1 * Cg(Ct(Ct(tag_start * 'elseif' * space^1 * (Cg(V('expr_text'), 'expression') + expr_error) * (tag_end + tag_end_error) * V('body')^-1)^1), 'elseif')^-1 * Cg(Ct(tag_start * 'else' * tag_end * V('body')^-1), 'else')^-1)) * (tag_start * 'endif' * tag_end + endif_error), -- {% macro expr %} body {% endmacro %} macro_block = tag_start * 'macro' * space^1 * token('macro', Ct((Cg(V('expr_text'), 'expression') + expr_error) * (tag_end + tag_end_error) * V('body')^-1)) * (tag_start * 'endmacro' * tag_end + endmacro_error), -- {% call expr %} body {% endcall %} call_block = tag_start * 'call' * (space^1 + #P('(')) * token('call', Ct((Cg(V('expr_text'), 'expression') + expr_error) * (tag_end + tag_end_error) * V('body')^-1)) * (tag_start * 'endcall' * tag_end + endcall_error), -- {% filter expr %} body {% endfilter %} filter_block = tag_start * 'filter' * space^1 * token('filter', Ct((Cg(V('expr_text'), 'expression') + expr_error) * (tag_end + tag_end_error) * V('body')^-1)) * (tag_start * 'endfilter' * tag_end + endfilter_error), -- {% set ... %} set_tag = tag_start * 'set' * space^1 * token('set', C(V('expr_text')) + expr_error) * (tag_end + tag_end_error), -- {% include ... %} include_tag = tag_start * 'include' * space^1 * token('include', C(V('expr_text')) + expr_error) * (tag_end + tag_end_error), -- {% import ... %} import_tag = tag_start * 'import' * space^1 * token('import', C(V('expr_text')) + expr_error) * (tag_end + tag_end_error), -- Note: i18n is not supported since it is out of scope for this -- implementation. -- Expression statement: {% do ... %}. do_tag = tag_start * 'do' * space^1 * token('do', C(V('expr_text')) + expr_error) * (tag_end + tag_end_error), -- Note: loop controls are not supported since that would require jumping -- between "scopes" (e.g. from within an "if" block to outside that "if" -- block's parent "for" block when coming across a {% break %} tag). -- Note: with statement is not supported since it is out of scope for this -- implementation. -- Note: autoescape is not supported since it is out of scope for this -- implementation. -- Any valid blocks of text or tags. body = (V('text') + V('variable') + V('comment') + V('raw_block') + V('block_block') + V('extends_tag') + V('for_block') + V('if_block') + V('macro_block') + V('call_block') + V('filter_block') + V('set_tag') + V('include_tag') + V('import_tag') + V('do_tag'))^0, -- Main pattern. V('body') * (-1 + tag_start * tag_error + main_error), }) -- Other options. if options and options.newline_sequence then assert(options.newline_sequence:find('^\r?\n$'), 'options.newline_sequence must be "\r\n" or "\n"') newline_sequence = options.newline_sequence else newline_sequence = '\n' end if options and options.keep_trailing_newline then keep_trailing_newline = options.keep_trailing_newline else keep_trailing_newline = false end if options and options.autoescape then autoescape = options.autoescape else autoescape = false end if options and options.loader then assert(type(options.loader) == 'function', 'options.loader must be a function that returns a filename') loader = options.loader else loader = M.loaders.filesystem() end end -- Wraps Lua's `assert()` in template environment *env* such that, when called -- in conjunction with another Lua function that produces an error message (e.g. -- `load()` and `pcall()`), that error message's context (source and line -- number) is replaced by the template's context. -- This results in Lua's error messages pointing to a template position rather -- than this library's source code. -- @param env The environment for the currently running template. It must have -- a `_SOURCE` field with the template's source text and a `_POSITION` field -- with the current position of expansion. -- @param ... Arguments to Lua's `assert()`. local function env_assert(env, ...) if not select(1, ...) then local input = env._LUPASOURCE:sub(1, env._LUPAPOSITION) local _, line_num = input:gsub('\n', '') local col_num = #input:match('[^\n]*$') local errmsg = select(2, ...) errmsg = errmsg:match(':%d+: (.*)$') or errmsg -- reformat if necessary error(string.format('Runtime Error in file "%s" on line %d, column %d: %s', env._LUPAFILENAME, line_num + 1, col_num, errmsg), 0) end return ... end -- Returns a generator that returns the position and filter in a list of -- filters, taking into account '|'s that may be within filter arguments. -- @usage for pos, filter in each_filter('foo|join("|")|bar') do ... end local function each_filter(s) local init = 1 return function(s) local pos, filter, e = s:match('^%s*()([^|(]+%b()[^|]*)|?()', init) if not pos then pos, filter, e = s:match('()([^|]+)|?()', init) end init = e return pos, filter end, s end -- Evaluates template variable *expression* subject to template environment -- *env*, applying any filters given in *expression*. -- @param expression The string expression to evaluate. -- @param env The environment to evaluate the expression in. local function eval(expression, env) local expr, pos, filters = expression:match('^([^|]*)|?()(.-)$') -- Evaluate base expression. local f = env_assert(env, load('return '..expr, nil, nil, env)) local result = select(2, env_assert(env, pcall(f))) -- Apply any filters. local results, multiple_results = nil, false local p = env._LUPAPOSITION + pos - 1 -- mark position at first filter for pos, filter in each_filter(filters) do env._LUPAPOSITION = p + pos - 1 -- update position for error messages local name, params = filter:match('^%s*([%w_]+)%(?(.-)%)?%s*$') f = M.filters[name] env_assert(env, f, 'unknown filter "'..name..'"') local args = env_assert(env, load('return {'..params..'}', nil, nil, env), 'invalid filter parameter(s) for "'..name..'"')() if not multiple_results then results = {select(2, env_assert(env, pcall(f, result, table.unpack(args))))} else for i = 1, #results do table.insert(args, i, results[i]) end results = {select(2, env_assert(env, pcall(f, table.unpack(args))))} end result, multiple_results = results[1], #results > 1 end if multiple_results then return table.unpack(results) end return result end local iterate -- Iterates over *ast*, a collection of tokens from a portion of a template's -- Abstract Syntax Tree (AST), evaluating any expressions in template -- environment *env*, and returns a concatenation of the results. -- @param ast A template's AST or portion of its AST (e.g. portion inside a -- 'for' control structure). -- @param env Environment to evaluate any expressions in. local function evaluate(ast, env) local chunks = {} local extends -- text of a parent template local rstrip -- flag for stripping leading whitespace of next token for i = 1, #ast, 3 do local pos, token, block = ast[i], ast[i + 1], ast[i + 2] env._LUPAPOSITION = pos if token == 'text' then chunks[#chunks + 1] = block elseif token == 'variable' then local value = eval(block, env) if autoescape then local escape = autoescape if type(autoescape) == 'function' then escape = autoescape(env._LUPAFILENAME) -- TODO: test end if escape and type(value) == 'string' then value = M.filters.escape(value) end end chunks[#chunks + 1] = value ~= nil and tostring(value) or '' elseif token == 'extends' then env_assert(env, not extends, 'cannot have multiple "extends" in the same scope') local file = eval(block, env) -- covers strings and variables extends = file env._LUPAEXTENDED = true -- used by parent templates elseif token == 'block' then local name = block.expression:match('^[%w_]+$') env_assert(env, name, 'invalid block name') -- Store the block for potential use by the parent template if this -- template is a child template, or for use by `self`. if not env._LUPABLOCKS then env._LUPABLOCKS = {} end if not env._LUPABLOCKS[name] then env._LUPABLOCKS[name] = {} end table.insert(env._LUPABLOCKS[name], 1, block) -- Handle the block properly. if not extends then if not env._LUPAEXTENDED then -- Evaluate the block normally. chunks[#chunks + 1] = evaluate(block, env) else -- A child template is overriding this parent's named block. Evaluate -- the child's block and use it instead of the parent's. local blocks = env._LUPABLOCKS[name] local super_env = setmetatable({super = function() -- Loop through the chain of defined blocks, evaluating from top to -- bottom, and return the bottom block. In each sub-block, the -- 'super' variable needs to point to the next-highest block's -- evaluated result. local super = evaluate(block, env) -- start with parent block local sub_env = setmetatable({super = function() return super end}, {__index = env}) for i = 1, #blocks - 1 do super = evaluate(blocks[i], sub_env) end return super end}, {__index = env}) chunks[#chunks + 1] = evaluate(blocks[#blocks], super_env) end end elseif token == 'for' then local expr = block.expression local p = env._LUPAPOSITION -- mark position at beginning of expression -- Extract variable list and generator. local patt = '^([%w_,%s]+)%s+in%s+()(.+)%s+if%s+(.+)$' local var_list, pos, generator, if_expr = expr:match(patt) if not var_list then var_list, pos, generator = expr:match('^([%w_,%s]+)%s+in%s+()(.+)$') end env_assert(env, var_list and generator, 'invalid for expression') -- Store variable names in a list for loop assignment. local variables = {} for variable, pos in var_list:gmatch('([^,%s]+)()') do env._LUPAPOSITION = p + pos - 1 -- update position for error messages env_assert(env, variable:find('^[%a_]') and variable ~= 'loop', 'invalid variable name') variables[#variables + 1] = variable end -- Evaluate the generator and perform the iteration. env._LUPAPOSITION = p + pos - 1 -- update position to generator if not generator:find('|') then generator = env_assert(env, load('return '..generator, nil, nil, env)) else local generator_expr = generator generator = function() return eval(generator_expr, env) end end local new_env = setmetatable({}, {__index = env}) chunks[#chunks + 1] = iterate(generator, variables, if_expr, block, new_env, 1, ast[i + 4] == 'lstrip') elseif token == 'if' then if eval(block.expression, env) then chunks[#chunks + 1] = evaluate(block, env) else local evaluate_else = true local elseifs = block['elseif'] if elseifs then for j = 1, #elseifs do if eval(elseifs[j].expression, env) then chunks[#chunks + 1] = evaluate(elseifs[j], env) evaluate_else = false break end end end if evaluate_else and block['else'] then chunks[#chunks + 1] = evaluate(block['else'], env) end end elseif token == 'macro' then -- Parse the macro's name and parameter list. local signature = block.expression local name, param_list = signature:match('^([%w_]+)(%b())') env_assert(env, name and param_list, 'invalid macro expression') param_list = param_list:sub(2, -2) local p = env._LUPAPOSITION + #name + 1 -- mark pos at beginning of args local params, defaults = {}, {} for param, pos, default in param_list:gmatch('([%w_]+)=?()([^,]*)') do params[#params + 1] = param if default ~= '' then env._LUPAPOSITION = p + pos - 1 -- update position for error messages local f = env_assert(env, load('return '..default)) defaults[param] = select(2, env_assert(env, pcall(f))) end end -- Create the function associated with the macro such that when the -- function is called (from within {{ ... }}), the macro's body is -- evaluated subject to an environment where parameter names are variables -- whose values are the ones passed to the macro itself. env[name] = function(...) local new_env = setmetatable({}, {__index = function(_, k) if k == 'caller' and type(env[k]) ~= 'function' then return nil end return env[k] end}) local args = {...} -- Assign the given parameter values. for i = 1, #args do if i > #params then break end new_env[params[i]] = args[i] end -- Clear all other unspecified parameter values or set them to their -- defined defaults. for i = #args + 1, #params do new_env[params[i]] = defaults[params[i]] end -- Store extra parameters in "varargs" variable. new_env.varargs = {} for i = #params + 1, #args do new_env.varargs[#new_env.varargs + 1] = args[i] end local chunk = evaluate(block, new_env) if ast[i + 4] == 'lstrip' then chunk = chunk:gsub('%s*$', '') end return chunk end elseif token == 'call' then -- Parse the call block's parameter list (if any) and determine the macro -- to call. local param_list = block.expression:match('^(%b())') local params = {} if param_list then for param in param_list:gmatch('[%w_]+') do params[#params + 1] = param end end local macro = block.expression:match('^%b()(.+)$') or block.expression -- Evaluate the given macro, subject to a "caller" function that returns -- the contents of this call block. Any arguments passed to the caller -- function are used as values of this parameters parsed earlier. local old_caller = M.env.caller -- save M.env.caller = function(...) local new_env = setmetatable({}, {__index = env}) local args = {...} -- Assign the given parameter values (if any). for i = 1, #args do new_env[params[i]] = args[i] end local chunk = evaluate(block, new_env) if ast[i + 4] == 'lstrip' then chunk = chunk:gsub('%s*$', '') end return chunk end chunks[#chunks + 1] = eval(macro, env) M.env.caller = old_caller -- restore elseif token == 'filter' then local text = evaluate(block, env) local p = env._LUPAPOSITION -- mark position at beginning of expression for pos, filter in each_filter(block.expression) do env._LUPAPOSITION = p + pos - 1 -- update position for error messages local name, params = filter:match('^%s*([%w_]+)%(?(.-)%)?%s*$') local f = M.filters[name] env_assert(env, f, 'unknown filter "'..name..'"') local args = env_assert(env, load('return {'..params..'}'), 'invalid filter parameter(s) for "'..name.. '"')() text = select(2, env_assert(env, pcall(f, text, table.unpack(args)))) end chunks[#chunks + 1] = text elseif token == 'set' then local var, expr = block:match('^([%a_][%w_]*)%s*=%s*(.+)$') env_assert(env, var and expr, 'invalid variable name or expression') env[var] = eval(expr, env) elseif token == 'do' then env_assert(env, pcall(env_assert(env, load(block, nil, nil, env)))) elseif token == 'include' then -- Parse the include block for flags. local without_context = block:find('without%s+context%s*') local ignore_missing = block:find('ignore%s+missing%s*') block = block:gsub('witho?u?t?%s+context%s*', '') :gsub('ignore%s+missing%s*', '') -- Evaluate the include expression in order to determine the file to -- include. If the result is a table, use the first file that exists. local file = eval(block, env) -- covers strings and variables if type(file) == 'table' then local files = file for i = 1, #files do file = loader(files[i], env) if file then break end end if type(file) == 'table' then file = nil end elseif type(file) == 'string' then file = loader(file, env) else error('"include" requires a string or table of files') end -- If the file exists, include it. Otherwise throw an error unless the -- "ignore missing" flag was given. env_assert(env, file or ignore_missing, 'no file(s) found to include') if file then chunks[#chunks + 1] = M.expand_file(file, not without_context and env or M.env) end elseif token == 'import' then local file, global = block:match('^%s*(.+)%s+as%s+([%a][%w_]*)%s*') local new_env = setmetatable({}, { __index = block:find('with%s+context%s*$') and env or M.env }) M.expand_file(eval(file or block, env), new_env) -- Copy any defined macros and variables over into the proper namespace. if global then env[global] = {} end local namespace = global and env[global] or env for k, v in pairs(new_env) do if not env[k] then namespace[k] = v end end elseif token == 'lstrip' and chunks[#chunks] then chunks[#chunks] = chunks[#chunks]:gsub('%s*$', '') elseif token == 'rstrip' then rstrip = true -- can only strip after determining the next chunk end if rstrip and token ~= 'rstrip' then chunks[#chunks] = chunks[#chunks]:gsub('^%s*', '') rstrip = false end end return not extends and table.concat(chunks) or M.expand_file(extends, env) end local pairs_gen, ipairs_gen = pairs({}), ipairs({}) -- Iterates over the generator *generator* subject to string "if" expression -- *if_expr*, assigns that generator's returned values to the variable names -- listed in *variables* within template environment *env*, evaluates any -- expressions in *block* (a portion of a template's AST), and returns a -- concatenation of the results. -- @param generator Either a function that returns a generator function, or a -- table to iterate over. In the latter case, `ipairs()` is used as the -- generator function. -- @param variables List of variable names to assign values returned by -- *generator* to. -- @param if_expr A conditional expression that when `false`, skips the current -- loop item. -- @param block The portion inside the 'for' structure of a template's AST to -- iterate with. -- @param env The environment iteration variables are defined in and where -- expressions are evaluated in. -- @param depth The current recursion depth. Recursion is performed by calling -- `loop(t)` with a table to iterate over. -- @param lstrip Whether or not the "endfor" block strips whitespace on the -- left. When `true`, all blocks produced by iteration are left-stripped. iterate = function(generator, variables, if_expr, block, env, depth, lstrip) local chunks = {} local orig_variables = {} -- used to store original loop variables' values for i = 1, #variables do orig_variables[variables[i]] = env[variables[i]] end local i, n = 1 -- used for loop variables local _, s, v -- state variables if type(generator) == 'function' then _, generator, s, v = env_assert(env, pcall(generator)) -- In practice, a generator's state variable is normally unused and hidden. -- This is not the case for 'pairs()' and 'ipairs', though. if variables[1] ~= '_index' and generator ~= pairs_gen and generator ~= ipairs_gen then table.insert(variables, 1, '_index') end end if type(generator) == 'table' then n = #generator generator, s, v = ipairs(generator) -- "for x in y" translates to "for _, x in ipairs(y)"; hide _ state variable if variables[1] ~= '_index' then table.insert(variables, 1, '_index') end end if generator then local first_results -- for preventing infinite loop from invalid generator while true do local results = {generator(s, v)} if results[1] == nil then break end -- If the results from the generator look like results returned by a -- generator itself (function, state, initial variable), verify last two -- results are different. If they are the same, then the original -- generator is invalid and will loop infinitely. if first_results == nil then first_results = #results == 3 and type(results[1]) == 'function' and results elseif first_results then env_assert(env, results[3] ~= first_results[3] or results[2] ~= first_results[2], 'invalid generator (infinite loop)') end -- Assign context variables and evaluate the body of the loop. -- As long as the result (ignoring the _index variable) is not a single -- table and there is only one loop variable defined (again, ignoring -- _index variable), assignment occurs as normal in Lua. Otherwise, -- unpacking on the table is done (like assignment to ...). if not (type(results[2]) == 'table' and #results == 2 and #variables > 2) then for j = 1, #variables do env[variables[j]] = results[j] end else for j = 2, #variables do env[variables[j]] = results[2][j - 1] end end if not if_expr or eval(if_expr, env) then env.loop = setmetatable({ index = i, index0 = i - 1, revindex = n and n - (i - 1), revindex0 = n and n - i, first = i == 1, last = i == n, length = n, cycle = function(...) return select((i - 1) % select('#', ...) + 1, ...) end, depth = depth, depth0 = depth - 1 }, {__call = function(_, t) return iterate(t, variables, if_expr, block, env, depth + 1, lstrip) end}) chunks[#chunks + 1] = evaluate(block, env) if lstrip then chunks[#chunks] = chunks[#chunks]:gsub('%s*$', '') end i = i + 1 end -- Prepare for next iteration. v = results[1] end end if i == 1 and block['else'] then chunks[#chunks + 1] = evaluate(block['else'], env) end for i = 1, #variables do env[variables[i]] = orig_variables[variables[i]] end return table.concat(chunks) end -- Expands string template *template* from source *source*, subject to template -- environment *env*, and returns the result. -- @param template String template to expand. -- @param env Environment for the given template. -- @param source Filename or identifier the template comes from for error -- messages and debugging. local function expand(template, env, source) template = template:gsub('\r?\n', newline_sequence) -- normalize if not keep_trailing_newline then template = template:gsub('\r?\n$', '') end -- Set up environment. if not env then env = {} end if not getmetatable(env) then env = setmetatable(env, {__index = M.env}) end env.self = setmetatable({}, {__index = function(_, k) env_assert(env, env._LUPABLOCKS and env._LUPABLOCKS[k], 'undefined block "'..k..'"') return function() return evaluate(env._LUPABLOCKS[k][1], env) end end}) -- Set context variables and expand the template. env._LUPASOURCE, env._LUPAFILENAME = template, source M._FILENAME = source -- for lpeg errors only local ast = assert(lpeg.match(M.grammar, template), "internal error") local result = evaluate(ast, env) return result end --- -- Expands the string template *template*, subject to template environment -- *env*, and returns the result. -- @param template String template to expand. -- @param env Optional environment for the given template. -- @name expand function M.expand(template, env) return expand(template, env, '<string>') end --- -- Expands the template within file *filename*, subject to template environment -- *env*, and returns the result. -- @param filename Filename containing the template to expand. -- @param env Optional environment for the template to expand. -- @name expand_file function M.expand_file(filename, env) filename = loader(filename, env) or filename local f = (not env or not env._LUPASOURCE) and assert(io.open(filename)) or env_assert(env, io.open(filename)) local template = f:read('*a') f:close() return expand(template, env, filename) end --- -- Returns a loader for templates that uses the filesystem starting at directory -- *directory*. -- When looking up the template for a given filename, the loader considers the -- following: if no template is being expanded, the loader assumes the given -- filename is relative to *directory* and returns the full path; otherwise the -- loader assumes the given filename is relative to the current template's -- directory and returns the full path. -- The returned path may be passed to `io.open()`. -- @param directory Optional the template root directory. The default value is -- ".", which is the current working directory. -- @name loaders.filesystem -- @see configure function M.loaders.filesystem(directory) return function(filename, env) if not filename then return nil end local current_dir = env and env._LUPAFILENAME and env._LUPAFILENAME:match('^(.+)[/\\]') if not filename:find('^/') and not filename:find('^%a:[/\\]') then filename = (current_dir or directory or '.')..'/'..filename end local f = io.open(filename) if not f then return nil end f:close() return filename end end -- Globally defined functions. --- -- Returns a sequence of integers from *start* to *stop*, inclusive, in -- increments of *step*. -- The complete sequence is generated at once -- no generator is returned. -- @param start Optional number to start at. The default value is `1`. -- @param stop Number to stop at. -- @param step Optional increment between sequence elements. The default value -- is `1`. -- @name _G.range function range(start, stop, step) if not stop and not step then stop, start = start, 1 end if not step then step = 1 end local t = {} for i = start, stop, step do t[#t + 1] = i end return t end --- -- Returns an object that cycles through the given values by calls to its -- `next()` function. -- A `current` field contains the cycler's current value and a `reset()` -- function resets the cycler to its beginning. -- @param ... Values to cycle through. -- @usage c = cycler(1, 2, 3) -- @usage c:next(), c:next() --> 1, 2 -- @usage c:reset() --> c.current == 1 -- @name _G.cycler function cycler(...) local c = {...} c.n, c.i, c.current = #c, 1, c[1] function c:next() local current = self.current self.i = self.i + 1 if self.i > self.n then self.i = 1 end self.current = self[self.i] return current end function c:reset() self.i, self.current = 1, self[1] end return c end -- Create the default sandbox environment for templates. local safe = { -- Lua globals. '_VERSION', 'ipairs', 'math', 'pairs', 'select', 'tonumber', 'tostring', 'type', 'bit32', 'os.date', 'os.time', 'string', 'table', 'utf8', -- Lupa globals. 'range', 'cycler' } local sandbox_env = setmetatable({}, {__index = M.tests}) for i = 1, #safe do local v = safe[i] if not v:find('%.') then sandbox_env[v] = _G[v] else local mod, func = v:match('^([^.]+)%.(.+)$') if not sandbox_env[mod] then sandbox_env[mod] = {} end sandbox_env[mod][func] = _G[mod][func] end end sandbox_env._G = sandbox_env --- -- Resets Lupa's default delimiters, options, and environments to their -- original default values. -- @name reset function M.reset() M.configure('{%', '%}', '{{', '}}', '{#', '#}') M.env = setmetatable({}, {__index = sandbox_env}) end M.reset() --- -- The default template environment. -- @class table -- @name env local env -- Lupa filters. --- -- Returns the absolute value of number *n*. -- @param n The number to compute the absolute value of. -- @name filters.abs M.filters.abs = math.abs -- Returns a table that, when indexed with an integer, indexes table *t* with -- that integer along with string *attribute*. -- This is used by filters that operate on particular attributes of table -- elements. -- @param t The table to index. -- @param attribute The additional attribute to index with. local function attr_accessor(t, attribute) return setmetatable({}, {__index = function(_, i) local value = t[i] attribute = tonumber(attribute) or attribute if type(attribute) == 'number' then return value[attribute] end for k in attribute:gmatch('[^.]+') do value = value[k] end return value end}) end --- -- Returns a generator that produces all of the items in table *t* in batches -- of size *size*, filling any empty spaces with value *fill*. -- Combine this with the "list" filter to produce a list. -- @param t The table to split into batches. -- @param size The batch size. -- @param fill The value to use when filling in any empty space in the last -- batch. -- @usage expand('{% for i in {1, 2, 3}|batch(2, 0) %}{{ i|string }} -- {% endfor %}') --> {1, 2} {3, 0} -- @see filters.list -- @name filters.batch function M.filters.batch(t, size, fill) assert(t, 'input to filter "batch" was nil instead of a table') local n = #t return function(t, i) if i > n then return nil end local batch = {} for j = i, i + size - 1 do batch[j - i + 1] = t[j] end if i + size > n and fill then for j = n + 1, i + size - 1 do batch[#batch + 1] = fill end end return i + size, batch end, t, 1 end --- -- Capitalizes string *s*. -- The first character will be uppercased, the others lowercased. -- @param s The string to capitalize. -- @usage expand('{{ "foo bar"|capitalize }}') --> Foo bar -- @name filters.capitalize function M.filters.capitalize(s) assert(s, 'input to filter "capitalize" was nil instead of a string') local first, rest = s:match('^(.)(.*)$') return first and first:upper()..rest:lower() or s end --- -- Centers string *s* within a string of length *width*. -- @param s The string to center. -- @param width The length of the centered string. -- @usage expand('{{ "foo"|center(9) }}') --> " foo " -- @name filters.center function M.filters.center(s, width) assert(s, 'input to filter "center" was nil instead of a string') local padding = (width or 80) - #s local left, right = math.ceil(padding / 2), math.floor(padding / 2) return ("%s%s%s"):format((' '):rep(left), s, (' '):rep(right)) end --- -- Returns value *value* or value *default*, depending on whether or not *value* -- is "true" and whether or not boolean *false_defaults* is `true`. -- @param value The value return if "true" or if `false` and *false_defaults* -- is `true`. -- @param default The value to return if *value* is `nil` or `false` (the latter -- applies only if *false_defaults* is `true`). -- @param false_defaults Optional flag indicating whether or not to return -- *default* if *value* is `false`. The default value is `false`. -- @usage expand('{{ false|default("no") }}') --> false -- @usage expand('{{ false|default("no", true) }') --> no -- @name filters.default function M.filters.default(value, default, false_defaults) if value == nil or false_defaults and not value then return default end return value end --- -- Returns a table constructed from table *t* such that each element is a list -- that contains a single key-value pair and all elements are sorted according -- to string *by* (which is either "key" or "value") and boolean -- *case_sensitive*. -- @param value The table to sort. -- @param case_sensitive Optional flag indicating whether or not to consider -- case when sorting string values. The default value is `false`. -- @param by Optional string that specifies which of the key-value to sort by, -- either "key" or "value". The default value is `"key"`. -- @usage expand('{{ {b = 1, a = 2}|dictsort|string }}') --> {{"a", 2}, -- {"b", 1}} -- @name filters.dictsort function M.filters.dictsort(t, case_sensitive, by) assert(t, 'input to filter "dictsort" was nil instead of a table') assert(not by or by == 'key' or by == 'value', 'filter "dictsort" can only sort tables by "key" or "value"') local i = (not by or by == 'key') and 1 or 2 local items = {} for k, v in pairs(t) do items[#items + 1] = {k, v} end table.sort(items, function(a, b) a, b = a[i], b[i] if not case_sensitive then if type(a) == 'string' then a = a:lower() end if type(b) == 'string' then b = b:lower() end end return a < b end) return items end --- -- Returns an HTML-safe copy of string *s*. -- @param s String to ensure is HTML-safe. -- @usage expand([[{{ '<">&'|e}}]]) --> <">& -- @name filters.escape function M.filters.escape(s) assert(s, 'input to filter "escape" was nil instead of a string') return s:gsub('[<>"\'&]', { ['<'] = '<', ['>'] = '>', ['"'] = '"', ["'"] = ''', ['&'] = '&' }) end --- -- Returns an HTML-safe copy of string *s*. -- @param s String to ensure is HTML-safe. -- @usage expand([[{{ '<">&'|escape}}]]) --> <">& -- @name filters.e function M.filters.e(s) assert(s, 'input to filter "e" was nil instead of a string') return M.filters.escape(s) end --- -- Returns a human-readable, decimal (or binary, depending on boolean *binary*) -- file size for *bytes* number of bytes. -- @param bytes The number of bytes to return the size for. -- @param binary Flag indicating whether or not to report binary file size -- as opposed to decimal file size. The default value is `false`. -- @usage expand('{{ 1000|filesizeformat }}') --> 1.0 kB -- @name filters.filesizeformat function M.filters.filesizeformat(bytes, binary) assert(bytes, 'input to filter "filesizeformat" was nil instead of a number') local base = binary and 1024 or 1000 local units = { binary and 'KiB' or 'kB', binary and 'MiB' or 'MB', binary and 'GiB' or 'GB', binary and 'TiB' or 'TB', binary and 'PiB' or 'PB', binary and 'EiB' or 'EB', binary and 'ZiB' or 'ZB', binary and 'YiB' or 'YB' } if bytes < base then return string.format('%d Byte%s', bytes, bytes > 1 and 's' or '') else local limit, unit for i = 1, #units do limit, unit = base^(i + 1), units[i] if bytes < limit then break end end return string.format('%.1f %s', (base * bytes / limit), unit) end end --- -- Returns the first element in table *t*. -- @param t The table to get the first element of. -- @usage expand('{{ range(10)|first }}') --> 1 -- @name filters.first function M.filters.first(t) assert(t, 'input to filter "first" was nil instead of a table') return t[1] end --- -- Returns value *value* as a float. -- This filter only works in Lua 5.3, which has a distinction between floats and -- integers. -- @param value The value to interpret as a float. -- @usage expand('{{ 42|float }}') --> 42.0 -- @name filters.float function M.filters.float(value) assert(value, 'input to filter "float" was nil instead of a number') return (tonumber(value) or 0) * 1.0 end --- -- Returns an HTML-safe copy of value *value*, even if *value* was returned by -- the "safe" filter. -- @param value Value to ensure is HTML-safe. -- @usage expand('{% set x = "<div />"|safe %}{{ x|forceescape }}') --> -- <div /> -- @name filters.forceescape function M.filters.forceescape(value) assert(value, 'input to filter "forceescape" was nil instead of a string') return M.filters.escape(tostring(value)) end --- -- Returns the given arguments formatted according to string *s*. -- See Lua's `string.format()` for more information. -- @param s The string to format subsequent arguments according to. -- @param ... Arguments to format. -- @usage expand('{{ "%s,%s"|format("a", "b") }}') --> a,b -- @name filters.format function M.filters.format(s, ...) assert(s, 'input to filter "format" was nil instead of a string') return string.format(s, ...) end --- -- Returns a generator that produces lists of items in table *t* grouped by -- string attribute *attribute*. -- @param t The table to group items from. -- @param attribute The attribute of items in the table to group by. This may -- be nested (e.g. "foo.bar" groups by t[i].foo.bar for all i). -- @usage expand('{% for age, group in people|groupby("age") %}...{% endfor %}') -- @name filters.groupby function M.filters.groupby(t, attribute) assert(t, 'input to filter "groupby" was nil instead of a table') local n = #t local seen = {} -- keep track of groupers in order to avoid duplicates return function(t, i) if i > n then return nil end local ta = attr_accessor(t, attribute) -- Determine the next grouper. local grouper = ta[i] while seen[grouper] do i = i + 1 if i > n then return nil end grouper = ta[i] end seen[grouper] = true -- Create and return the group. local group = {} for j = i, #t do if ta[j] == grouper then group[#group + 1] = t[j] end end return i + 1, grouper, group end, t, 1 end --- -- Returns a copy of string *s* with all lines after the first indented by -- *width* number of spaces. -- If boolean *first_line* is `true`, indents the first line as well. -- @param s The string to indent lines of. -- @param width The number of spaces to indent lines with. -- @param first_line Optional flag indicating whether or not to indent the -- first line of text. The default value is `false`. -- @usage expand('{{ "foo\nbar"|indent(2) }}') --> "foo\n bar" -- @name filters.indent function M.filters.indent(s, width, first_line) assert(s, 'input to filter "indent" was nil instead of a string') local indent = (' '):rep(width) return (first_line and indent or '')..s:gsub('([\r\n]+)', '%1'..indent) end --- -- Returns value *value* as an integer. -- @param value The value to interpret as an integer. -- @usage expand('{{ 32.32|int }}') --> 32 -- @name filters.int function M.filters.int(value) assert(value, 'input to filter "int" was nil instead of a number') return math.floor(tonumber(value) or 0) end --- -- Returns a string that contains all the elements in table *t* (or all the -- attributes named *attribute* in *t*) separated by string *sep*. -- @param t The table to join. -- @param sep The string to separate table elements with. -- @param attribute Optional attribute of elements to use for joining instead -- of the elements themselves. This may be nested (e.g. "foo.bar" joins -- `t[i].foo.bar` for all i). -- @usage expand('{{ {1, 2, 3}|join("|") }}') --> 1|2|3 -- @name filters.join function M.filters.join(t, sep, attribute) assert(t, 'input to filter "join" was nil instead of a table') if not attribute then local strings = {} for i = 1, #t do strings[#strings + 1] = tostring(t[i]) end return table.concat(strings, sep) end local ta = attr_accessor(t, attribute) local attributes = {} for i = 1, #t do attributes[#attributes + 1] = ta[i] end return table.concat(attributes, sep) end --- -- Returns the last element in table *t*. -- @param t The table to get the last element of. -- @usage expand('{{ range(10)|last }}') --> 10 -- @name filters.last function M.filters.last(t) assert(t, 'input to filter "last" was nil instead of a table') return t[#t] end --- -- Returns the length of string or table *value*. -- @param value The value to get the length of. -- @usage expand('{{ "hello world"|length }}') --> 11 -- @name filters.length function M.filters.length(value) assert(value, 'input to filter "length" was nil instead of a table or string') return #value end --- -- Returns the list of items produced by generator *generator*, subject to -- initial state *s* and initial iterator variable *i*. -- This filter should only be used after a filter that returns a generator. -- @param generator Generator function that produces an item. -- @param s Initial state for the generator. -- @param i Initial iterator variable for the generator. -- @usage expand('{{ range(4)|batch(2)|list|string }}') --> {{1, 2}, {3, 4}} -- @see filters.batch -- @see filters.groupby -- @see filters.slice -- @name filters.list function M.filters.list(generator, s, i) assert(type(generator) == 'function', 'input to filter "list" must be a generator') local list = {} for _, v in generator, s, i do list[#list + 1] = v end return list end --- -- Returns a copy of string *s* with all lowercase characters. -- @param s The string to lowercase. -- @usage expand('{{ "FOO"|lower }}') --> foo -- @name filters.lower function M.filters.lower(s) assert(s, 'input to filter "lower" was nil instead of a string') return string.lower(s) end --- -- Maps each element of table *t* to a value produced by filter name *filter* -- and returns the resultant table. -- @param t The table of elements to map. -- @param filter The name of the filter to pass table elements through. -- @param ... Any arguments for the filter. -- @usage expand('{{ {"1", "2", "3"}|map("int")|sum }}') --> 6 -- @name filters.map function M.filters.map(t, filter, ...) assert(t, 'input to filter "map" was nil instead of a table') local f = M.filters[filter] assert(f, 'unknown filter "'..filter..'"') local map = {} for i = 1, #t do map[i] = f(t[i], ...) end return map end --- -- Maps the value of each element's string *attribute* in table *t* to the -- value produced by filter name *filter* and returns the resultant table. -- @param t The table of elements with attributes to map. -- @param attribute The attribute of elements in the table to filter. This may -- be nested (e.g. "foo.bar" maps t[i].foo.bar for all i). -- @param filter The name of the filter to pass table elements through. -- @param ... Any arguments for the filter. -- @usage expand('{{ users|mapattr("name")|join("|") }}') -- @name filters.mapattr function M.filters.mapattr(t, attribute, filter, ...) assert(t, 'input to filter "mapattr" was nil instead of a table') local ta = attr_accessor(t, attribute) local f = M.filters[filter] if filter then assert(f, 'unknown filter "'..filter..'" given to filter "mapattr"') end local map = {} for i = 1, #t do map[i] = filter and f(ta[i], ...) or ta[i] end return map end --- -- Returns a random element from table *t*. -- @param t The table to get a random element from. -- @usage expand('{{ range(100)|random }}') -- @name filters.random function M.filters.random(t) assert(t, 'input to filter "random" was nil instead of a table') math.randomseed(os.time()) return t[math.random(#t)] end --- -- Returns a list of elements in table *t* that fail test name *test*. -- @param t The table of elements to reject from. -- @param test The name of the test to use on table elements. -- @param ... Any arguments for the test. -- @usage expand('{{ range(5)|reject(is_odd)|join("|") }}') --> 2|4 -- @name filters.reject function M.filters.reject(t, test, ...) assert(t, 'input to filter "reject" was nil instead of a table') local f = test or function(value) return not not value end local items = {} for i = 1, #t do if not f(t[i], ...) then items[#items + 1] = t[i] end end return items end --- -- Returns a list of elements in table *t* whose string attribute *attribute* -- fails test name *test*. -- @param t The table of elements to reject from. -- @param attribute The attribute of items in the table to reject from. This -- may be nested (e.g. "foo.bar" tests t[i].foo.bar for all i). -- @param test The name of the test to use on table elements. -- @param ... Any arguments for the test. -- @usage expand('{{ users|rejectattr("offline")|mapattr("name")|join(",") }}') -- @name filters.rejectattr function M.filters.rejectattr(t, attribute, test, ...) assert(t, 'input to filter "rejectattr" was nil instead of a table') local ta = attr_accessor(t, attribute) local f = test or function(value) return not not value end local items = {} for i = 1, #t do if not f(ta[i], ...) then items[#items + 1] = t[i] end end return items end --- -- Returns a copy of string *s* with all (or up to *n*) occurrences of string -- *old* replaced by string *new*. -- Identical to Lua's `string.gsub()` and handles Lua patterns. -- @param s The subject string. -- @param pattern The string or Lua pattern to replace. -- @param repl The replacement text (may contain Lua captures). -- @param n Optional number indicating the maximum number of replacements to -- make. The default value is `nil`, which is unlimited. -- @usage expand('{% filter upper|replace("FOO", "foo") %}foobar -- {% endfilter %}') --> fooBAR -- @name filters.replace function M.filters.replace(s, pattern, repl, n) assert(s, 'input to filter "replace" was nil instead of a string') return string.gsub(s, pattern, repl, n) end --- -- Returns a copy of the given string or table *value* in reverse order. -- @param value The value to reverse. -- @usage expand('{{ {1, 2, 3}|reverse|string }}') --> {3, 2, 1} -- @name filters.reverse function M.filters.reverse(value) assert(type(value) == 'table' or type(value) == 'string', 'input to filter "reverse" was nil instead of a table or string') if type(value) == 'string' then return value:reverse() end local t = {} for i = 1, #value do t[i] = value[#value - i + 1] end return t end --- -- Returns number *value* rounded to *precision* decimal places based on string -- *method* (if given). -- @param value The number to round. -- @param precision Optional precision to round the number to. The default -- value is `0`. -- @param method Optional string rounding method, either `"ceil"` or -- `"floor"`. The default value is `nil`, which uses the common rounding -- method (if a number's fractional part is 0.5 or greater, rounds up; -- otherwise rounds down). -- @usage expand('{{ 2.1236|round(3, "floor") }}') --> 2.123 -- @name filters.round function M.filters.round(value, precision, method) assert(value, 'input to filter "round" was nil instead of a number') assert(not method or method == 'ceil' or method == 'floor', 'rounding method given to filter "round" must be "ceil" or "floor"') precision = precision or 0 method = method or (select(2, math.modf(value)) >= 0.5 and 'ceil' or 'floor') local s = string.format('%.'..(precision >= 0 and precision or 0)..'f', math[method](value * 10^precision) / 10^precision) return tonumber(s) end --- -- Marks string *s* as HTML-safe, preventing Lupa from modifying it when -- configured to autoescape HTML entities. -- This filter must be used at the end of a filter chain unless it is -- immediately proceeded by the "forceescape" filter. -- @param s The string to mark as HTML-safe. -- @usage lupa.configure{autoescape = true} -- @usage expand('{{ "<div>foo</div>"|safe }}') --> <div>foo</div> -- @name filters.safe function M.filters.safe(s) assert(s, 'input to filter "safe" was nil instead of a string') return setmetatable({}, {__tostring = function() return s end}) end --- -- Returns a list of the elements in table *t* that pass test name *test*. -- @param t The table of elements to select from. -- @param test The name of the test to use on table elements. -- @param ... Any arguments for the test. -- @usage expand('{{ range(5)|select(is_odd)|join("|") }}') --> 1|3|5 -- @name filters.select function M.filters.select(t, test, ...) assert(t, 'input to filter "select" was nil instead of a table') local f = test or function(value) return not not value end local items = {} for i = 1, #t do if f(t[i], ...) then items[#items + 1] = t[i] end end return items end --- -- Returns a list of elements in table *t* whose string attribute *attribute* -- passes test name *test*. -- @param t The table of elements to select from. -- @param attribute The attribute of items in the table to select from. This -- may be nested (e.g. "foo.bar" tests t[i].foo.bar for all i). -- @param test The name of the test to use on table elements. -- @param ... Any arguments for the test. -- @usage expand('{{ users|selectattr("online")|mapattr("name")|join("|") }}') -- @name filters.selectattr function M.filters.selectattr(t, attribute, test, ...) assert(t, 'input to filter "selectattr" was nil instead of a table') local ta = attr_accessor(t, attribute) local f = test or function(value) return not not value end local items = {} for i = 1, #t do if f(ta[i], ...) then items[#items + 1] = t[i] end end return items end --- -- Returns a generator that produces all of the items in table *t* in *slices* -- number of iterations, filling any empty spaces with value *fill*. -- Combine this with the "list" filter to produce a list. -- @param t The table to slice. -- @param slices The number of slices to produce. -- @param fill The value to use when filling in any empty space in the last -- slice. -- @usage expand('{% for i in {1, 2, 3}|slice(2, 0) %}{{ i|string }} -- {% endfor %}') --> {1, 2} {3, 0} -- @see filters.list -- @name filters.slice function M.filters.slice(t, slices, fill) assert(t, 'input to filter "slice" was nil instead of a table') local size, slices_with_extra = math.floor(#t / slices), #t % slices return function(t, i) if i > slices then return nil end local slice = {} local s = (i - 1) * size + math.min(i, slices_with_extra + 1) local e = i * size + math.min(i, slices_with_extra) for j = s, e do slice[j - s + 1] = t[j] end if slices_with_extra > 0 and i > slices_with_extra and fill then slice[#slice + 1] = fill end return i + 1, slice end, t, 1 end --- -- Returns a copy of table or string *value* in sorted order by value (or by -- an attribute named *attribute*), depending on booleans *reverse* and -- *case_sensitive*. -- @param value The table or string to sort. -- @param reverse Optional flag indicating whether or not to sort in reverse -- (descending) order. The default value is `false`, which sorts in ascending -- order. -- @param case_sensitive Optional flag indicating whether or not to consider -- case when sorting string values. The default value is `false`. -- @param attribute Optional attribute of elements to sort by instead of the -- elements themselves. -- @usage expand('{{ {2, 3, 1}|sort|string }}') --> {1, 2, 3} -- @name filters.sort function M.filters.sort(value, reverse, case_sensitive, attribute) assert(value, 'input to filter "sort" was nil instead of a table or string') assert(not attribute or type(attribute) == 'string' or type(attribute) == 'number', 'attribute to filter "sort" must be a string or number') local t = {} local sort_string = type(value) == 'string' if not sort_string then for i = 1, #value do t[#t + 1] = value[i] end else for char in value:gmatch('.') do t[#t + 1] = char end -- chars in string end table.sort(t, function(a, b) if attribute then if type(attribute) == 'number' then a, b = a[attribute], b[attribute] else for k in attribute:gmatch('[^.]+') do a, b = a[k], b[k] end end end if not case_sensitive then if type(a) == 'string' then a = a:lower() end if type(b) == 'string' then b = b:lower() end end if not reverse then return a < b else return a > b end end) return not sort_string and t or table.concat(t) end --- -- Returns the string representation of value *value*, handling lists properly. -- @param value Value to return the string representation of. -- @usage expand('{{ {1 * 1, 2 * 2, 3 * 3}|string }}') --> {1, 4, 9} -- @name filters.string function M.filters.string(value) if type(value) ~= 'table' then return tostring(value) end local t = {} for i = 1, #value do local item = value[i] t[i] = type(item) == 'string' and '"'..item..'"' or M.filters.string(item) end return '{'..table.concat(t, ', ')..'}' end --- -- Returns a copy of string *s* with any HTML tags stripped. -- Also cleans up whitespace. -- @param s String to strip HTML tags from. -- @usage expand('{{ "<div>foo</div>"|striptags }}') --> foo -- @name filters.striptags function M.filters.striptags(s) assert(s, 'input to filter "striptags" was nil instead of a string') return s:gsub('%b<>', ''):gsub('%s+', ' '):match('^%s*(.-)%s*$') end --- -- Returns the numeric sum of the elements in table *t* or the sum of all -- attributes named *attribute* in *t*. -- @param t The table to calculate the sum of. -- @param attribute Optional attribute of elements to use for summing instead -- of the elements themselves. This may be nested (e.g. "foo.bar" sums -- `t[i].foo.bar` for all i). -- @usage expand('{{ range(6)|sum }}') --> 21 -- @name filters.sum function M.filters.sum(t, attribute) assert(t, 'input to filter "sum" was nil instead of a table') local ta = attribute and attr_accessor(t, attribute) or t local sum = 0 for i = 1, #t do sum = sum + ta[i] end return sum end --- -- Returns a copy of all words in string *s* in titlecase. -- @param s The string to titlecase. -- @usage expand('{{ "foo bar"|title }}') --> Foo Bar -- @name filters.title function M.filters.title(s) assert(s, 'input to filter "title" was nil instead of a string') return s:gsub('[^-%s]+', M.filters.capitalize) end --- -- Returns a copy of string *s* truncated to *length* number of characters. -- Truncated strings end with '...' or string *delimiter*. If boolean -- *partial_words* is `false`, truncation will only happen at word boundaries. -- @param s The string to truncate. -- @param length The length to truncate the string to. -- @param partial_words Optional flag indicating whether or not to allow -- truncation within word boundaries. The default value is `false`. -- @param delimiter Optional delimiter text. The default value is '...'. -- @usage expand('{{ "foo bar"|truncate(4) }}') --> "foo ..." -- @name filters.truncate function M.filters.truncate(s, length, partial_words, delimiter) assert(s, 'input to filter "truncate" was nil instead of a string') if #s <= length then return s end local truncated = s:sub(1, length) if s:find('[%w_]', length) and not partial_words then truncated = truncated:match('^(.-)[%w_]*$') -- drop partial word end return truncated..(delimiter or '...') end --- -- Returns a copy of string *s* with all uppercase characters. -- @param s The string to uppercase. -- @usage expand('{{ "foo"|upper }}') --> FOO -- @name filters.upper function M.filters.upper(s) assert(s, 'input to filter "upper" was nil instead of a string') return string.upper(s) end --- -- Returns a string suitably encoded to be used in a URL from value *value*. -- *value* may be a string, table of key-value query parameters, or table of -- lists of key-value query parameters (for order). -- @param value Value to URL-encode. -- @usage expand('{{ {{'f', 1}, {'z', 2}}|urlencode }}') --> f=1&z=2 -- @name filters.urlencode function M.filters.urlencode(value) assert(value, 'input to filter "urlencode" was nil instead of a string or table') if type(value) ~= 'table' then return tostring(value):gsub('[^%w.-]', function(c) return string.format('%%%X', string.byte(c)) end) end local params = {} if #value > 0 then for i = 1, #value do local k = M.filters.urlencode(value[i][1]) local v = M.filters.urlencode(value[i][2]) params[#params + 1] = k..'='..v end else for k, v in pairs(value) do params[#params + 1] = M.filters.urlencode(k)..'='..M.filters.urlencode(v) end end return table.concat(params, '&') end --- -- Replaces any URLs in string *s* with HTML links, limiting link text to -- *length* characters. -- @param s The string to replace URLs with HTML links in. -- @param length Optional maximum number of characters to include in link text. -- The default value is `nil`, which imposes no limit. -- @param nofollow Optional flag indicating whether or not HTML links will get a -- "nofollow" attribute. -- @usage expand('{{ "example.com"|urlize }}') --> -- <a href="http://example.com">example.com</a> -- @name filters.urlize function M.filters.urlize(s, length, nofollow) assert(s, 'input to filter "urlize" was nil instead of a string') -- Trims the given url. local function trim_url(url) return length and s:sub(1, length)..(#s > length and '...' or '') or url end local nofollow_attr = nofollow and ' rel="nofollow"' or '' local lead, trail = C((S('(<') + '<')^0), C((S('.,)>\n') + '>')^0) * -1 local middle = C((1 - trail)^0) local patt = lpeg.Cs(lead * middle * trail / function(lead, middle, trail) local linked if middle:find('^www%.') or (not middle:find('@') and not middle:find('^https?://') and #middle > 0 and middle:find('^%w') and ( middle:find('%.com$') or middle:find('%.net$') or middle:find('%.org$') )) then middle, linked = string.format('<a href="http://%s"%s>%s</a>', middle, nofollow_attr, trim_url(middle)), true end if middle:find('^https?://') then middle, linked = string.format('<a href="%s"%s>%s</a>', middle, nofollow_attr, trim_url(middle)), true end if middle:find('@') and not middle:find('^www%.') and not middle:find(':') and middle:find('^%S+@[%w._-]+%.[%w._-]+$') then middle, linked = string.format('<a href="mailto:%s">%s</a>', middle, middle), true end if linked then return lead..middle..trail end end) return M.filters.escape(s):gsub('%S+', function(word) return lpeg.match(patt, word) end) end --- -- Returns the number of words in string *s*. -- A word is a sequence of non-space characters. -- @param s The string to count words in. -- @usage expand('{{ "foo bar baz"|wordcount }}') --> 3 -- @name filters.wordcount function M.filters.wordcount(s) assert(s, 'input to filter "wordcount" was nil instead of a string') return select(2, s:gsub('%S+', '')) end --- -- Interprets table *t* as a list of XML attribute-value pairs, returning them -- as a properly formatted, space-separated string. -- @param t The table of XML attribute-value pairs. -- @usage expand('<data {{ {foo = 42, bar = 23}|xmlattr }} />') -- @name filters.xmlattr function M.filters.xmlattr(t) assert(t, 'input to filter "xmlattr" was nil instead of a table') local attributes = {} for k, v in pairs(t) do attributes[#attributes + 1] = string.format('%s="%s"', k, M.filters.escape(tostring(v))) end return table.concat(attributes, ' ') end -- Lupa tests. --- -- Returns whether or not number *n* is odd. -- @param n The number to test. -- @usage expand('{% for x in range(10) if is_odd(x) %}...{% endif %}') -- @name tests.is_odd function M.tests.is_odd(n) return n % 2 == 1 end --- -- Returns whether or not number *n* is even. -- @param n The number to test. -- @usage expand('{% for x in range(10) if is_even(x) %}...{% endif %}') -- @name tests.is_even function M.tests.is_even(n) return n % 2 == 0 end --- -- Returns whether or not number *n* is evenly divisible by number *num*. -- @param n The dividend to test. -- @param num The divisor to use. -- @usage expand('{% if is_divisibleby(x, y) %}...{% endif %}') -- @name tests.is_divisibleby function M.tests.is_divisibleby(n, num) return n % num == 0 end --- -- Returns whether or not value *value* is non-nil, and thus defined. -- @param value The value to test. -- @usage expand('{% if is_defined(x) %}...{% endif %}') -- @name tests.is_defined function M.tests.is_defined(value) return value ~= nil end --- -- Returns whether or not value *value* is nil, and thus effectively undefined. -- @param value The value to test. -- @usage expand('{% if is_undefined(x) %}...{% endif %}') -- @name tests.is_undefined function M.tests.is_undefined(value) return value == nil end --- -- Returns whether or not value *value* is nil. -- @param value The value to test. -- @usage expand('{% if is_none(x) %}...{% endif %}') -- @name tests.is_none function M.tests.is_none(value) return value == nil end --- -- Returns whether or not value *value* is nil. -- @param value The value to test. -- @usage expand('{% if is_nil(x) %}...{% endif %}') -- @name tests.is_nil function M.tests.is_nil(value) return value == nil end --- -- Returns whether or not string *s* is in all lower-case characters. -- @param s The string to test. -- @usage expand('{% if is_lower(s) %}...{% endif %}') -- @name tests.is_lower function M.tests.is_lower(s) return s:lower() == s end --- -- Returns whether or not string *s* is in all upper-case characters. -- @param s The string to test. -- @usage expand('{% if is_upper(s) %}...{% endif %}') -- @name tests.is_upper function M.tests.is_upper(s) return s:upper() == s end --- -- Returns whether or not value *value* is a string. -- @param value The value to test. -- @usage expand('{% if is_string(x) %}...{% endif %}') -- @name tests.is_string function M.tests.is_string(value) return type(value) == 'string' end --- -- Returns whether or not value *value* is a table. -- @param value The value to test. -- @usage expand('{% if is_mapping(x) %}...{% endif %}') -- @name tests.is_mapping function M.tests.is_mapping(value) return type(value) == 'table' end --- -- Returns whether or not value *value* is a table. -- @param value The value to test. -- @usage expand('{% if is_table(x) %}...{% endif %}') -- @name tests.is_table function M.tests.is_table(value) return type(value) == 'table' end --- -- Returns whether or not value *value* is a number. -- @param value The value to test. -- @usage expand('{% if is_number(x) %}...{% endif %}') -- @name tests.is_number function M.tests.is_number(value) return type(value) == 'number' end --- -- Returns whether or not value *value* is a sequence, namely a table with -- non-zero length. -- @param value The value to test. -- @usage expand('{% if is_sequence(x) %}...{% endif %}') -- @name tests.is_sequence function M.tests.is_sequence(value) return type(value) == 'table' and #value > 0 end --- -- Returns whether or not value *value* is a sequence (a table with non-zero -- length) or a generator. -- At the moment, all functions are considered generators. -- @param value The value to test. -- @usage expand('{% if is_iterable(x) %}...{% endif %}') -- @name tests.is_iterable function M.tests.is_iterable(value) return M.tests.is_sequence(value) or type(value) == 'function' end --- -- Returns whether or not value *value* is a function. -- @param value The value to test. -- @usage expand('{% if is_callable(x) %}...{% endif %}') -- @name tests.is_callable function M.tests.is_callable(value) return type(value) == 'function' end --- -- Returns whether or not value *value* is the same as value *other*. -- @param value The value to test. -- @param other The value to compare with. -- @usage expand('{% if is_sameas(x, y) %}...{% endif %}') -- @name tests.is_sameas function M.tests.is_sameas(value, other) return value == other end --- -- Returns whether or not value *value* is HTML-safe. -- @param value The value to test. -- @usage expand('{% if is_escaped(x) %}...{% endif %}') -- @name tests.is_escaped function M.tests.is_escaped(value) return getmetatable(value) and getmetatable(value).__tostring ~= nil end return M 0707010000000F000041ED00000000000000000000000366D5601B00000000000000000000000000000000000000000000000F00000000luajinja/tests07070100000010000041ED00000000000000000000000666D5601B00000000000000000000000000000000000000000000001400000000luajinja/tests/data07070100000011000041ED00000000000000000000000266D5601B00000000000000000000000000000000000000000000001A00000000luajinja/tests/data/debug07070100000012000081A400000000000000000000000166D5601B0000001A000000000000000000000000000000000000002600000000luajinja/tests/data/debug/broken.htmlBefore {{ fail() }} After 07070100000013000081A400000000000000000000000166D5601B0000002F000000000000000000000000000000000000002B00000000luajinja/tests/data/debug/syntaxerror.htmlFoo {% for item in broken %} ... {% endif %} 07070100000014000041ED00000000000000000000000266D5601B00000000000000000000000000000000000000000000001C00000000luajinja/tests/data/imports07070100000015000081A400000000000000000000000166D5601B000000FE000000000000000000000000000000000000002400000000luajinja/tests/data/imports/exports {% macro toplevel() %}...{% endmacro %} {% macro __private() %}...{% endmacro %} {% set variable = 42 %} {% for item in {1} %} {% macro notthere() %}{% endmacro %} {% endfor %} 07070100000016000081A400000000000000000000000166D5601B00000015000000000000000000000000000000000000002300000000luajinja/tests/data/imports/header[{{ foo }}|{{ 23 }}] 07070100000017000081A400000000000000000000000166D5601B0000000B000000000000000000000000000000000000002100000000luajinja/tests/data/imports/item{{ item }} 07070100000018000081A400000000000000000000000166D5601B0000003C000000000000000000000000000000000000002100000000luajinja/tests/data/imports/main{% for item in {1, 2, 3} %}{% include 'item' %}{% endfor %} 07070100000019000081A400000000000000000000000166D5601B00000036000000000000000000000000000000000000002300000000luajinja/tests/data/imports/module{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %} 0707010000001A000081A400000000000000000000000166D5601B0000000A000000000000000000000000000000000000002600000000luajinja/tests/data/imports/o_printer({{ o }}) 0707010000001B000041ED00000000000000000000000966D5601B00000000000000000000000000000000000000000000002000000000luajinja/tests/data/inheritence0707010000001C000081A400000000000000000000000166D5601B000000B1000000000000000000000000000000000000002800000000luajinja/tests/data/inheritence/doublee{% extends "layout" %} {% extends "layout" %} {% block block1 %} {% if false %} {% block block2 %} this should workd {% endblock %} {% endif %} {% endblock %} 0707010000001D000041ED00000000000000000000000266D5601B00000000000000000000000000000000000000000000002800000000luajinja/tests/data/inheritence/dynamic0707010000001E000081A400000000000000000000000166D5601B00000035000000000000000000000000000000000000002E00000000luajinja/tests/data/inheritence/dynamic/child{% extends master %}{% block x %}CHILD{% endblock %} 0707010000001F000081A400000000000000000000000166D5601B00000023000000000000000000000000000000000000003000000000luajinja/tests/data/inheritence/dynamic/master1MASTER1{% block x %}{% endblock %} 07070100000020000081A400000000000000000000000166D5601B00000023000000000000000000000000000000000000003000000000luajinja/tests/data/inheritence/dynamic/master2MASTER2{% block x %}{% endblock %} 07070100000021000081A400000000000000000000000166D5601B000000C9000000000000000000000000000000000000002700000000luajinja/tests/data/inheritence/layout|{% block block1 %}block 1 from layout{% endblock %} |{% block block2 %}block 2 from layout{% endblock %} |{% block block3 %} {% block block4 %}nested block 4 from layout{% endblock %} {% endblock %}| 07070100000022000081A400000000000000000000000166D5601B0000004B000000000000000000000000000000000000002700000000luajinja/tests/data/inheritence/level1{% extends "layout" %} {% block block1 %}block 1 from level1{% endblock %} 07070100000023000081A400000000000000000000000166D5601B00000072000000000000000000000000000000000000002700000000luajinja/tests/data/inheritence/level2{% extends "level1" %} {% block block2 %}{% block block5 %}nested block 5 from level2{% endblock %}{% endblock %} 07070100000024000081A400000000000000000000000166D5601B0000007F000000000000000000000000000000000000002700000000luajinja/tests/data/inheritence/level3{% extends "level2" %} {% block block5 %}block 5 from level3{% endblock %} {% block block4 %}block 4 from level3{% endblock %} 07070100000025000081A400000000000000000000000166D5601B0000004B000000000000000000000000000000000000002700000000luajinja/tests/data/inheritence/level4{% extends "level3" %} {% block block3 %}block 3 from level4{% endblock %} 07070100000026000041ED00000000000000000000000266D5601B00000000000000000000000000000000000000000000002E00000000luajinja/tests/data/inheritence/macro_scoping07070100000027000081A400000000000000000000000166D5601B0000015C000000000000000000000000000000000000003B00000000luajinja/tests/data/inheritence/macro_scoping/details.html {% extends 'standard.html' %} {% macro my_macro() %} my_macro {% endmacro %} {% block content %} {% block outer_box %} outer_box {% block inner_box %} inner_box {% endblock %} {% endblock %} {% endblock %} 07070100000028000081A400000000000000000000000166D5601B00000031000000000000000000000000000000000000003C00000000luajinja/tests/data/inheritence/macro_scoping/standard.html {% block content %} {% endblock %} 07070100000029000081A400000000000000000000000166D5601B000000C0000000000000000000000000000000000000003800000000luajinja/tests/data/inheritence/macro_scoping/test.html {% extends 'details.html' %} {% macro my_macro() %} my_macro {% endmacro %} {% block inner_box %} {{ my_macro() }} {% endblock %} 0707010000002A000041ED00000000000000000000000266D5601B00000000000000000000000000000000000000000000002600000000luajinja/tests/data/inheritence/multi0707010000002B000081A400000000000000000000000166D5601B00000042000000000000000000000000000000000000002C00000000luajinja/tests/data/inheritence/multi/child{% extends master or 'master1' %}{% block x %}CHILD{% endblock %} 0707010000002C000081A400000000000000000000000166D5601B00000023000000000000000000000000000000000000002E00000000luajinja/tests/data/inheritence/multi/master1MASTER1{% block x %}{% endblock %} 0707010000002D000081A400000000000000000000000166D5601B00000023000000000000000000000000000000000000002E00000000luajinja/tests/data/inheritence/multi/master2MASTER2{% block x %}{% endblock %} 0707010000002E000041ED00000000000000000000000266D5601B00000000000000000000000000000000000000000000002900000000luajinja/tests/data/inheritence/preserve0707010000002F000081A400000000000000000000000166D5601B00000044000000000000000000000000000000000000002B00000000luajinja/tests/data/inheritence/preserve/a{% if false %}{% block x %}A{% endblock %}{% endif %}{{ self.x() }} 07070100000030000081A400000000000000000000000166D5601B0000003B000000000000000000000000000000000000002B00000000luajinja/tests/data/inheritence/preserve/b{% extends "a" %}{% block x %}B{{ super() }}{% endblock %} 07070100000031000041ED00000000000000000000000266D5601B00000000000000000000000000000000000000000000002700000000luajinja/tests/data/inheritence/scoped07070100000032000081A400000000000000000000000166D5601B0000003F000000000000000000000000000000000000003400000000luajinja/tests/data/inheritence/scoped/helpers.html {% macro foo(x) %}{{ the_foo + x }}{% endmacro %} 07070100000033000081A400000000000000000000000166D5601B00000144000000000000000000000000000000000000003200000000luajinja/tests/data/inheritence/scoped/index.html {%- extends 'layout.html' %} {% include 'helpers.html' %} {% block useless %} {% for x in {1, 2, 3} %} {% block testing %} {{ foo(x) }} {% endblock %} {% endfor %} {% endblock %} 07070100000034000081A400000000000000000000000166D5601B0000002F000000000000000000000000000000000000003300000000luajinja/tests/data/inheritence/scoped/layout.html {% block useless %}{% endblock %} 07070100000035000081A400000000000000000000000166D5601B00000042000000000000000000000000000000000000003300000000luajinja/tests/data/inheritence/scoped/master.html{% for item in seq %}[{% block item %}{% endblock %}]{% endfor %} 07070100000036000041ED00000000000000000000000266D5601B00000000000000000000000000000000000000000000002600000000luajinja/tests/data/inheritence/super07070100000037000081A400000000000000000000000166D5601B00000056000000000000000000000000000000000000002800000000luajinja/tests/data/inheritence/super/a{% block intro %}INTRO{% endblock %}|BEFORE|{% block data %}INNER{% endblock %}|AFTER 07070100000038000081A400000000000000000000000166D5601B0000003F000000000000000000000000000000000000002800000000luajinja/tests/data/inheritence/super/b{% extends "a" %}{% block data %}({{ super() }}){% endblock %} 07070100000039000081A400000000000000000000000166D5601B0000006F000000000000000000000000000000000000002800000000luajinja/tests/data/inheritence/super/c{% extends "b" %}{% block intro %}--{{ super() }}--{% endblock %}{% block data %}[{{ super() }}]{% endblock %} 0707010000003A000041ED00000000000000000000000266D5601B00000000000000000000000000000000000000000000002D00000000luajinja/tests/data/inheritence/super_scoped0707010000003B000081A400000000000000000000000166D5601B0000004C000000000000000000000000000000000000003900000000luajinja/tests/data/inheritence/super_scoped/master.html{% for item in seq %}[{% block item %}{{ item }}{% endblock %}]{% endfor %} 0707010000003C000081A400000000000000000000000166D5601B0000009A000000000000000000000000000000000000002800000000luajinja/tests/data/inheritence/working{% extends "layout" %} {% block block1 %} {% if false %} {% block block2 %} this should workd {% endblock %} {% endif %} {% endblock %} 0707010000003D000041ED00000000000000000000000266D5601B00000000000000000000000000000000000000000000001A00000000luajinja/tests/data/other0707010000003E000081A400000000000000000000000166D5601B00000024000000000000000000000000000000000000002600000000luajinja/tests/data/other/parent.html(({% block title %}{% endblock %})) 0707010000003F000081A400000000000000000000000166D5601B0000002F000000000000000000000000000000000000001F00000000luajinja/tests/data/test_macro{% macro test(foo) %}[{{ foo }}]{% endmacro %} 07070100000040000081A400000000000000000000000166D5601B0000F1E6000000000000000000000000000000000000001900000000luajinja/tests/suite.lua-- Copyright 2015-2020 Mitchell. See LICENSE. -- Contributions from Ana Balan. -- Contains Lupa's copy of Jinja2's test suite. -- Any descrepancies are noted and/or described. -- Note: Lupa's range(n) behaves differently than Jinja2's in that it produces -- sequences from 1 to n. All tests that utilize range() reflect this. -- Also, Lua tables are 1-indexed, not 0-indexed, so the tests reflect that. local lupa = dofile('../lupa.lua') local expand, expand_file = lupa.expand, lupa.expand_file -- Asserts that value *value* is equal to value *expected*. -- @param value Resultant value. -- @param expected Expected value. local function assert_equal(value, expected) assert(expected ~= nil, 'expected argument not given to assert_equal') assert(value == expected, 'assertion failed! "'..expected..'" expected, got "'..value..'"') end -- Asserts that function *f* raises an error that contains string or pattern -- *message*. -- @param message String or pattern that matches the error raised by *f*. -- @param f The function to call. -- @param ... Any arguments to *f*. local function assert_raises(message, f, ...) local ok, errmsg = pcall(f, ...) assert(not ok, 'no error raised') assert(errmsg:find(message), 'raised error was "'..errmsg..'" and did not contain "'..message..'"') end -- Returns string *s* with any leading or trailing whitespace removed. -- @param s The string to trim. function string.trim(s) return s:gsub('^%s*(.-)%s*$', '%1') end local test_suite = { api = { -- Note: Nearly all Jinja2 API tests are not applicable since Lupa's API is -- completely different. api_tests = { test_cycler = function() local c = cycler(1, 2, 3) for _, item in ipairs{1, 2, 3, 1, 2, 3} do assert_equal(c.current, item) assert_equal(c:next(), item) end c:next() assert_equal(c.current, 2) c:reset() assert_equal(c.current, 1) end, } }, core_tags = { for_tests = { test_simple = function() local tmpl = '{% for item in seq %}{{ item }}{% endfor %}' local env = {seq = range(10)} assert_equal(expand(tmpl, env), '12345678910') end, test_else = function() local tmpl = '{% for item in seq %}XXX{% else %}...{% endfor %}' assert_equal(expand(tmpl), '...') end, test_empty_blocks = function() local tmpl = '<{% for item in seq %}{% else %}{% endfor %}>' assert_equal(expand(tmpl), '<>') end, test_context_vars = function() local tmpl = [[{% for item in seq -%} {{ loop.index }}|{{ loop.index0 }}|{{ loop.revindex }}|{{ loop.revindex0}}|{{ loop.first }}||{{ loop.last }}|{{ loop.length}}###{% endfor %}]] local env = {seq = {0, 1}} local one, two = expand(tmpl, env):match('^(.+)###(.+)###$') local one_values, two_values = {}, {} for v in one:gmatch('[^|]+') do one_values[#one_values + 1] = v end for v in two:gmatch('[^|]+') do two_values[#two_values + 1] = v end assert_equal(tonumber(one_values[1]), 1) assert_equal(tonumber(two_values[1]), 2) assert_equal(tonumber(one_values[2]), 0) assert_equal(tonumber(two_values[2]), 1) assert_equal(tonumber(one_values[3]), 2) assert_equal(tonumber(two_values[3]), 1) assert_equal(tonumber(one_values[4]), 1) assert_equal(tonumber(two_values[4]), 0) assert_equal(one_values[5], 'true') assert_equal(two_values[5], 'false') assert_equal(one_values[6], 'false') assert_equal(two_values[6], 'true') assert_equal(one_values[7], '2') assert_equal(two_values[7], '2') end, test_cycling = function() local tmpl = [[{% for item in seq %}{{ loop.cycle('<1>', '<2>') }}{% endfor %}{% for item in seq %}{{ loop.cycle(table.unpack(through)) }}{% endfor %}]] local env = {seq = range(4), through = {'<1>', '<2>'}} assert_equal(expand(tmpl, env), string.rep('<1><2>', 4)) end, test_scope = function() local tmpl = '{% for item in seq %}{% endfor %}{{ item }}' local env = {seq = range(10)} assert_equal(expand(tmpl, env), '') end, test_varlen = function() local t = range(5) local function iter() return function(_, i) if i > #t then return nil end return i + 1, t[i] end, t, 1 end local tmpl = '{% for item in iter() %}{{ item }}{% endfor %}' local env = {iter = iter} assert_equal(expand(tmpl, env), '12345') tmpl = '{% for item in iter %}{{ item }}{% endfor %}' assert_raises('invalid generator', expand, tmpl, env) end, test_noniter = function() local tmpl = '{% for item in seq() %}...{% endfor %}' assert_raises('attempt to call.+nil value', expand, tmpl) end, test_recursive = function() -- Note: no need for 'recursive' keyword, unlike Jinja2. local tmpl = [[{% for item in seq -%} [{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}] {%- endfor %}]] local env = {seq = {{a = 1, b = {{a = 1}, {a = 2}}}, {a = 2, b = {{a = 1}, {a = 2}}}, {a = 3, b = {{a = 'a'}}}}} assert_equal(expand(tmpl, env), '[1<[1][2]>][2<[1][2]>][3<[a]>]') end, test_recursive_depth0 = function() local tmpl = [[{% for item in seq -%} [{{ loop.depth0 }}:{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}] {%- endfor %}]] local env = {seq = {{a = 1, b = {{a = 1}, {a = 2}}}, {a = 2, b = {{a = 1}, {a = 2}}}, {a = 3, b = {{a = 'a'}}}}} assert_equal(expand(tmpl, env), '[0:1<[1:1][1:2]>][0:2<[1:1][1:2]>][0:3<[1:a]>]') end, test_recursive_depth = function() local tmpl = [[{% for item in seq -%} [{{ loop.depth }}:{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}] {%- endfor %}]] local env = {seq = {{a = 1, b = {{a = 1}, {a = 2}}}, {a = 2, b = {{a = 1}, {a = 2}}}, {a = 3, b = {{a = 'a'}}}}} assert_equal(expand(tmpl, env), '[1:1<[2:1][2:2]>][1:2<[2:1][2:2]>][1:3<[2:a]>]') end, test_looploop = function() local tmpl = [[{% for row in table %} {%- set rowloop = loop -%} {% for cell in row:gmatch('.') -%} [{{ rowloop.index }}|{{ loop.index }}] {%- endfor %} {%- endfor %}]] local env = {table = {'ab', 'cd'}} assert_equal(expand(tmpl, env), '[1|1][1|2][2|1][2|2]') end, test_loop_last = function() local tmpl = '{% for i in items %}{{ i }}'.. '{% if not loop.last %}'.. ',{% endif %}{% endfor %}' local env = {items={1, 2, 3}} assert_equal(expand(tmpl, env), '1,2,3') end, test_loop_errors = function() local tmpl = [[{% for item in {1} if loop.index == 0 %}...{% endfor %}]] assert_raises('attempt to index.+nil value', expand, tmpl) tmpl = [[{% for item in {} %}...{% else %}{{ loop }}{% endfor %}]] assert_equal(expand(tmpl), '') end, test_loop_filter = function() local tmpl = '{% for item in range(10) if '.. 'is_even(item) %}[{{ item }}]{% endfor %}' assert_equal(expand(tmpl), '[2][4][6][8][10]') tmpl = [[ {%- for item in range(10) if is_even(item) %}[{{ loop.index }}:{{ item }}]{% endfor %}]] assert_equal(expand(tmpl), '[1:2][2:4][3:6][4:8][5:10]') end, test_loop_unassignable = function() local tmpl = '{% for loop in seq %}...{% endfor %}' local env = {0} assert_raises('invalid variable name', expand, tmpl, env) end, test_scoped_special_var = function() local tmpl = '{% for s in seq %}[{{ loop.first }}{% for c in s:gmatch(".") %}'.. '|{{ loop.first }}{% endfor %}]{% endfor %}' local env = {seq = {'ab', 'cd'}} assert_equal(expand(tmpl, env), '[true|true|false][false|true|false]') end, test_scoped_loop_var = function() local tmpl = '{% for x in seq %}{{ loop.first }}'.. '{% for y in seq %}{% endfor %}{% endfor %}' local env = {seq = {'a', 'b'}} assert_equal(expand(tmpl, env), 'truefalse') tmpl = '{% for x in seq %}{% for y in seq %}'.. '{{ loop.first }}{% endfor %}{% endfor %}' assert_equal(expand(tmpl, env), 'truefalsetruefalse') end, test_recursive_empty_loop_iter = function() local tmpl = [[ {%- for item in foo -%}{%- endfor -%} ]] local env = {foo = {}} assert_equal(expand(tmpl, env), '') end, test_call_in_loop = function() local tmpl = [[ {%- macro do_something() -%} [{{ caller() }}] {%- endmacro %} {%- for i in {1,2,3} %} {%- call do_something() -%} {{ i }} {%- endcall %} {%- endfor -%} ]] assert_equal(expand(tmpl), '[1][2][3]') end, test_scoping = function() local tmpl = [[ {%- for item in foo %}...{{ item }}...{% endfor %} {%- macro item(a) %}...{{ a }}...{% endmacro %} {{- item(2) -}} ]] local env = {foo = {1}} assert_equal(expand(tmpl, env), '...1......2...') end, test_unpacking = function() local tmpl = '{% for a, b, c in {{1, 2, 3}} %}'.. '{{ a }}|{{ b }}|{{ c }}{% endfor %}' assert_equal(expand(tmpl), '1|2|3') end }, if_tests = { test_simple = function() local tmpl = '{% if true %}...{% endif %}' assert_equal(expand(tmpl), '...') end, test_elif = function() local tmpl = '{% if false %}XXX{% elseif true'.. '%}...{% else %}XXX{% endif %}' assert_equal(expand(tmpl), '...') end, test_else = function() local tmpl = '{% if false %}XXX{% else %}...{% endif %}' assert_equal(expand(tmpl), '...') end, test_empty = function() local tmpl = '[{% if true %}{% else %}{% endif %}]' assert_equal(expand(tmpl), '[]') end, test_complete = function() local tmpl = '{% if a %}A{% elseif b %}B{% elseif c == d %}'.. 'C{% else %}D{% endif %}' local env = {a = false, b = false, c = 42, d = 42.0} assert_equal(expand(tmpl, env), 'C') end, test_no_scope = function() local tmpl = '{% if a %}{% set foo = 1 %}{% endif %}{{ foo }}' local env = {a = true} assert_equal(expand(tmpl, env), '1') tmpl = '{% if true %}{% set foo = 1 %}{% endif %}{{ foo }}' assert_equal(expand(tmpl), '1') end, -- Note: this test does not exist in Jinja2's suite, but is needed for -- completeness. test_elif_else = function() local tmpl = '{% if false %}XXX{% elseif false %}XXX{% else'.. '%}...{% endif %}' assert_equal(expand(tmpl), '...') end }, macro_tests = { setup = function() lupa.configure{trim_blocks = true} end, teardown = lupa.reset, test_simple = function() local tmpl = [[ {%macro say_hello(name) %}Hello {{ name }}!{% endmacro %} {{ say_hello('Peter') }}]] assert_equal(expand(tmpl), 'Hello Peter!') end, test_scoping = function() local tmpl = [[ {% macro level1(data1) %} {% macro level2(data2) %}{{ data1 }}|{{ data2 }}{% endmacro %} {{ level2('bar') }}{% endmacro %} {{ level1('foo') }}]] assert_equal(expand(tmpl), 'foo|bar') end, test_arguments = function() local tmpl = [[ {% macro m(a, b, c='c', d='d') %}{{ a }}|{{ b }}|{{ c }}|{{ d }}{% endmacro %} {{ m() }}|{{ m('a') }}|{{ m('a', 'b') }}|{{ m(1, 2, 3) }}]] assert_equal(expand(tmpl), '||c|d|a||c|d|a|b|c|d|1|2|3|d') end, test_varargs = function() local tmpl = [[ {% macro test() %}{{ varargs|join('|') }}{% endmacro %} {{ test(1, 2, 3) }}]] assert_equal(expand(tmpl), '1|2|3') end, test_simple_call = function() local tmpl = [=[ {% macro test() %}[[{{ caller() }}{% endmacro %} {% call test() %}data{% endcall %}]]]=] assert_equal(expand(tmpl), '[[data]]') end, test_complex_call = function() local tmpl = [=[ {% macro test() %}[[{{ caller('data') }}]]{% endmacro %} {% call(data) test() %}{{ data }}{% endcall %}]=] assert_equal(expand(tmpl), '[[data]]') end, test_caller_undefined = function() local tmpl = [[ {% set caller = 42 %} {% macro test() %}{{ not caller }}{% endmacro %} {{ test() }}]] assert_equal(expand(tmpl), 'true') end, test_include = function() local tmpl = '{% include "data/test_macro" %}{{ test("foo") }}' assert_equal(expand(tmpl), '[foo]') end, -- Note: test_macro_api is not applicable since this implementation stores -- macros as Lua functions with no metadata. test_callself = function() local tmpl = '{% macro foo(x) %}{{ x }}{% if x > 1 %}|'.. '{{ foo(x - 1) }}{% endif %}{% endmacro %}'.. '{{ foo(5) }}' assert_equal(expand(tmpl), '5|4|3|2|1') end } }, debug = { debug_tests = { test_runtime_error = function() local tmpl = 'data/debug/broken.html' local env = {fail = function() next() end} assert_raises('^Runtime Error.+broken%.html.+'.. 'on line %d, column %d: bad argument', expand_file, tmpl, env) end, test_syntax_error = function() local tmpl = 'data/debug/syntaxerror.html' assert_raises('^Parse Error.+syntaxerror%.html.+'.. 'on line %d, column %d:.+endfor.+expected', expand_file, tmpl) end, test_regular_error = function() local tmpl = '{{ test() }}' local env = {test = function() error('wtf') end} assert_raises('^Runtime Error.+<string>.+wtf', expand, tmpl, env) end, } }, filters = { filter_tests = { test_filter_calling = function() local result = lupa.filters.sum{1, 2, 3} assert_equal(result, 6) end, test_capitalize = function() local tmpl = '{{ "foo bar"|capitalize }}' assert_equal(expand(tmpl), 'Foo bar') end, test_center = function() local tmpl = '{{ "foo"|center(9) }}' assert_equal(expand(tmpl), ' foo ') end, test_default = function() local tmpl = '{{ missing|default("no") }}|{{ false|default("no") }}|'.. '{{ false|default("no", true) }}|{{ given|default("no") }}' local env = {given = 'yes'} assert_equal(expand(tmpl, env), 'no|false|no|yes') end, test_dictsort = function() local tmpl = '{{ foo|dictsort|string }}|'.. '{{ foo|dictsort(true)|string }}|'.. '{{ foo|dictsort(false, "value")|string }}' local env = {foo={aa = 0, b = 1, c = 2, AB = 3}} assert_equal(expand(tmpl, env), '{{"aa", 0}, {"AB", 3}, {"b", 1}, {"c", 2}}|'.. '{{"AB", 3}, {"aa", 0}, {"b", 1}, {"c", 2}}|'.. '{{"aa", 0}, {"b", 1}, {"c", 2}, {"AB", 3}}') end, test_batch = function() local tmpl = '{{ foo|batch(3)|list|string }}|'.. '{{ foo|batch(3, "X")|list|string }}' local env = {foo = range(10)} assert_equal(expand(tmpl, env), '{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10}}|'.. '{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, "X", "X"}}') end, test_slice = function() local tmpl = '{{ foo|slice(3)|list|string }}|'.. '{{ foo|slice(3, "X")|list|string }}' local env = {foo = range(10)} assert_equal(expand(tmpl, env), '{{1, 2, 3, 4}, {5, 6, 7}, {8, 9, 10}}|'.. '{{1, 2, 3, 4}, {5, 6, 7, "X"}, {8, 9, 10, "X"}}') end, test_escape = function() local tmpl = [[{{ '<">&'|escape}}]] assert_equal(expand(tmpl), '<">&') end, test_striptags = function() local tmpl = '{{ foo|striptags }}' local env = {foo = ' <p>just a small \n <a href="#">'.. 'example</a> link</p>\n<p>to a webpage</p> '.. '<!-- <p>and some commented stuff</p> -->'} assert_equal(expand(tmpl, env), 'just a small example link to a webpage') end, test_filesizeformat = function() local tmpl = '{{ 100|filesizeformat }}|'.. '{{ 1000|filesizeformat }}|'.. '{{ 1000000|filesizeformat }}|'.. '{{ 1000000000|filesizeformat }}|'.. '{{ 1000000000000|filesizeformat }}|'.. '{{ 100|filesizeformat(true) }}|'.. '{{ 1000|filesizeformat(true) }}|'.. '{{ 1000000|filesizeformat(true) }}|'.. '{{ 1000000000|filesizeformat(true) }}|'.. '{{ 1000000000000|filesizeformat(true) }}' assert_equal(expand(tmpl), '100 Bytes|1.0 kB|1.0 MB|1.0 GB|1.0 TB|100 Bytes|'.. '1000 Bytes|976.6 KiB|953.7 MiB|931.3 GiB') tmpl = '{{ 300|filesizeformat }}|'.. '{{ 3000|filesizeformat }}|'.. '{{ 3000000|filesizeformat }}|'.. '{{ 3000000000|filesizeformat }}|'.. '{{ 3000000000000|filesizeformat }}|'.. '{{ 300|filesizeformat(true) }}|'.. '{{ 3000|filesizeformat(true) }}|'.. '{{ 3000000|filesizeformat(true) }}' assert_equal(expand(tmpl), '300 Bytes|3.0 kB|3.0 MB|3.0 GB|3.0 TB|300 Bytes|'.. '2.9 KiB|2.9 MiB') end, test_first = function() local tmpl = '{{ foo|first }}' local env = {foo = range(10)} assert_equal(expand(tmpl, env), '1') end, test_float = function() local tmpl = '{{ "42"|float }}|'.. '{{ "ajsghasjgd"|float }}|'.. '{{ "32.32"|float }}' if _VERSION >= 'Lua 5.3' then assert_equal(expand(tmpl), '42.0|0.0|32.32') end end, test_format = function() local tmpl = '{{ "%s,%s"|format("a", "b") }}' assert_equal(expand(tmpl), 'a,b') end, test_indent = function() local tmpl = '{{ foo|indent(2) }}|{{ foo|indent(2, true) }}' local env = {foo = 'foo bar foo bar\nfoo bar foo bar'} assert_equal(expand(tmpl, env), 'foo bar foo bar\n foo bar foo bar| '.. 'foo bar foo bar\n foo bar foo bar') end, test_int = function() local tmpl = '{{ "42"|int }}|{{ "ajsghasjgd"|int }}|{{ "32.32"|int }}' assert_equal(expand(tmpl), '42|0|32') end, test_join = function() local tmpl = '{{ {1, 2, 3}|join("|") }}' assert_equal(expand(tmpl), '1|2|3') -- Note: '|' cannot occur within an expression, only at the end, so this -- test input is slightly different. lupa.configure{autoescape = true} tmpl = '{{ {"<foo>", "<span>foo</span>"}|join }}' assert_equal(expand(tmpl), '<foo><span>foo</span>') lupa.reset() end, test_join_attribute = function() local tmpl = '{{ users|join(", ", "username") }}' local env = {users = {{username = 'foo'}, {username = 'bar'}}} assert_equal(expand(tmpl, env), 'foo, bar') end, test_last = function() local tmpl = '{{ foo|last }}' local env = {foo = range(10)} assert_equal(expand(tmpl, env), '10') end, test_length = function() local tmpl = '{{ "hello world"|length }}' assert_equal(expand(tmpl), '11') end, test_lower = function() local tmpl = '{{ "FOO"|lower }}' assert_equal(expand(tmpl), 'foo') end, -- Note: pprint filter is not applicable since Lua does not have a data -- pretty-printer. test_random = function() local tmpl = '{{ seq|random }}' local env = {seq = range(100)} for i = 1, 10 do local j = tonumber(expand(tmpl, env)) assert(j >= 1 and j <= 100) end end, test_reverse = function() local tmpl = '{{ "foobar"|reverse }}|'.. '{{ {1, 2, 3}|reverse|string }}' assert_equal(expand(tmpl), 'raboof|{3, 2, 1}') end, test_string = function() local tmpl = '{{ obj|string }}' local env = {obj = {1, 2, 3, 4, 5}} assert_equal(expand(tmpl, env), '{1, 2, 3, 4, 5}') end, test_title = function() local tmpl = '{{ "foo bar"|title }}' assert_equal(expand(tmpl), 'Foo Bar') tmpl = [[{{ "foo\'s bar"|title }}]] assert_equal(expand(tmpl), "Foo's Bar") tmpl = '{{ "foo bar"|title }}' assert_equal(expand(tmpl), 'Foo Bar') tmpl = '{{ "f bar f"|title }}' assert_equal(expand(tmpl), 'F Bar F') tmpl = '{{ "foo-bar"|title }}' assert_equal(expand(tmpl), 'Foo-Bar') tmpl = '{{ "foo\tbar"|title }}' assert_equal(expand(tmpl), 'Foo\tBar') tmpl = '{{ "FOO\tBAR"|title }}' assert_equal(expand(tmpl), 'Foo\tBar') end, test_truncate = function() local tmpl = '{{ data|truncate(15, true, ">>>") }}|'.. '{{ data|truncate(15, false, ">>>") }}|'.. '{{ smalldata|truncate(15) }}' local env = { data = string.rep('foobar baz bar', 1000), smalldata = 'foobar baz bar' } assert_equal(expand(tmpl, env), 'foobar baz barf>>>|foobar baz >>>|foobar baz bar') end, test_upper = function() local tmpl = '{{ "foo"|upper }}' assert_equal(expand(tmpl), 'FOO') end, test_urlize = function() local tmpl = '{{ "foo http://www.example.com/ bar"|urlize }}' assert_equal(expand(tmpl), 'foo <a href="http://www.example.com/">'.. 'http://www.example.com/</a> bar') end, test_wordcount = function() local tmpl = '{{ "foo bar baz"|wordcount }}' assert_equal(expand(tmpl), '3') end, test_block = function() local tmpl = '{% filter lower|escape %}<HEHE>{% endfilter %}' assert_equal(expand(tmpl), '<hehe>') end, test_chaining = function() local tmpl = '{{ {"<foo>", "<bar>"}|first|upper|escape}}' assert_equal(expand(tmpl), '<FOO>') end, test_sum = function() local tmpl = '{{ {1, 2, 3, 4, 5, 6}|sum }}' assert_equal(expand(tmpl), '21') end, test_sum_attributes = function() local tmpl = '{{ values|sum("value") }}' local env = {values = {{value = 23}, {value = 1}, {value = 18}}} assert_equal(expand(tmpl, env), '42') end, test_sum_attributes_nested = function() local tmpl = '{{ values|sum("real.value") }}' local env = {values = {{real = {value = 23}}, {real = {value = 1}}, {real = {value = 18}}}} assert_equal(expand(tmpl, env), '42') end, test_sum_attributes = function() local tmpl = [[{{ values|sum('2') }}]] local env = {values = {{'foo', 23}, {'bar', 1}, {'baz', 18}}} assert_equal(expand(tmpl, env), '42') tmpl = [[{{ values|sum(2) }}]] assert_equal(expand(tmpl, env), '42') end, test_abs = function() local tmpl = '{{ -1|abs }}|{{ 1|abs }}' assert_equal(expand(tmpl), '1|1') end, test_round_positive = function() local tmpl = '{{ 2.7|round }}|{{ 2.1|round }}|'.. '{{ 2.1234|round(3, "floor") }}|'.. '{{ 2.1|round(0, "ceil") }}' -- Note: Lua's results drop the fractional part if it is 0. assert_equal(expand(tmpl), '3|2|2.123|3') end, test_round_negative = function() local tmpl = '{{ 21.3|round(-1)}}|'.. '{{ 21.3|round(-1, "ceil")}}|'.. '{{ 21.3|round(-1, "floor")}}' assert_equal(expand(tmpl), '20|30|20') end, test_xmlattr = function() local tmpl = '{{ {foo = 42, bar = 23, ["blub:blub"] = "<?>"}|xmlattr }}' local s = expand(tmpl) assert(select(2, s:gsub(' ', '')) == 2) assert(s:find('foo="42"')) assert(s:find('bar="23"')) assert(s:find('blub:blub="<?>"', 1, true)) end, test_sort = function() local tmpl = '{{ {2, 3, 1}|sort|string }}|{{ {2, 3, 1}|sort(true)|string }}' assert_equal(expand(tmpl), '{1, 2, 3}|{3, 2, 1}') tmpl = '{{ {"c", "A", "b", "D"}|sort|join }}' assert_equal(expand(tmpl), 'AbcD') tmpl = '{{ {"foo", "Bar", "blah"}|sort|string }}' assert_equal(expand(tmpl), '{"Bar", "blah", "foo"}') tmpl = '{{ items|sort(nil, nil, "value")|join("", "value") }}' local env = { items = {{value = 3}, {value = 2}, {value = 4}, {value = 1}} } assert_equal(expand(tmpl, env), '1234') end, test_groupby = function() local tmpl = [[ {%- for grouper, list in {{foo = 1, bar = 2}, {foo = 2, bar = 3}, {foo = 1, bar = 1}, {foo = 3, bar = 4}}|groupby("foo") -%} {{ grouper }}{% for x in list %}: {{ x.foo }}, {{ x.bar }}{% endfor %}| {%- endfor %}]] assert_equal(expand(tmpl), '1: 1, 2: 1, 1|2: 2, 3|3: 3, 4|') end, test_grouby_index = function() local tmpl = [[ {%- for grouper, list in {{"a", 1}, {"a", 2}, {"b", 1}}|groupby(1) -%} {{ grouper }}{% for x in list %}:{{ x[2] }}{% endfor %}| {%- endfor %}]] assert_equal(expand(tmpl), 'a:1:2|b:1|') end, test_groupby_multidot = function() local tmpl = [[ {%- for year, list in articles|groupby("date.year") -%} {{ year }}{% for x in list %}[{{ x.title }}]{% endfor %}| {%- endfor %}]] local env = { articles = { {title = 'aha', date = {day = 1, month = 1, year = 1970}}, {title = 'interesting', date = {day = 2, month = 1, year = 1970}}, {title = 'really?', date = {day = 3, month = 1, year = 1970}}, {title = 'totally not', date = {day = 1, month = 1, year = 1971}}, } } assert_equal(expand(tmpl, env), '1970[aha][interesting][really?]|1971[totally not]|') end, test_filtertag = function() local tmpl = '{% filter upper|replace("FOO", "foo") %}'.. 'foobar{% endfilter %}' assert_equal(expand(tmpl), 'fooBAR') end, test_replace = function() local tmpl = '{{ string|replace("o", 42) }}' local env = {string = '<foo>'} assert_equal(expand(tmpl, env), '<f4242>') lupa.configure{autoescape = true} tmpl = '{{ string|replace("o", 42) }}' env = {string = '<foo>'} assert_equal(expand(tmpl, env), '<f4242>') tmpl = '{{ string|replace("<", 42) }}' env = {string = '<foo>'} assert_equal(expand(tmpl, env), '42foo>') tmpl = '{{ string|replace("o", ">x<") }}' env = {string = 'foo'} assert_equal(expand(tmpl, env), 'f>x<>x<') lupa.reset() end, test_forceescape = function() -- Note: This implementation does not support markup, so this test input -- is slightly different. local tmpl = '{% set x = "<div />"|safe %}{{ x|forceescape }}' assert_equal(expand(tmpl), '<div />') end, test_safe = function() lupa.configure{autoescape = true} local tmpl = '{{ "<div>foo</div>"|safe }}' assert_equal(expand(tmpl), '<div>foo</div>') tmpl = '{{ "<div>foo</div>" }}' assert_equal(expand(tmpl), '<div>foo</div>') lupa.reset() end, test_urlencode = function() lupa.configure{autoescape = true} local tmpl = '{{ "Hello, world!"|urlencode }}' assert_equal(expand(tmpl), 'Hello%2C%20world%21') -- Note: Lua does not support unicode escape sequences in strings so -- some unicode tests are left out. tmpl = '{{ o|urlencode }}' local env = {o = {{'f', 1}}} assert_equal(expand(tmpl, env), 'f=1') env = {o = {{'f', 1}, {'z', 2}}} assert_equal(expand(tmpl, env), 'f=1&z=2') env = {o = {[0] = 1}} assert_equal(expand(tmpl, env), '0=1') lupa.reset() end, test_simple_map = function() local tmpl = '{{ {"1", "2", "3"}|map("int")|sum }}' assert_equal(expand(tmpl), '6') end, test_attribute_map = function() local tmpl = '{{ users|mapattr("name")|join("|") }}' local env = { users = {{name = 'john'}, {name = 'jane'}, {name = 'mike'}} } assert_equal(expand(tmpl, env), 'john|jane|mike') end, test_empty_map = function() local tmpl = '{{ {}|map("upper")|string }}' assert_equal(expand(tmpl), '{}') end, test_simple_select = function() local tmpl = '{{ {1, 2, 3, 4, 5}|select(is_odd)|join("|") }}' assert_equal(expand(tmpl), '1|3|5') end, test_bool_select = function() local tmpl = '{{ {false, 0, 1, 2, 3, 4, 5}|select|join("|") }}' assert_equal(expand(tmpl), '0|1|2|3|4|5') end, test_simple_reject = function() local tmpl = '{{ {1, 2, 3, 4, 5}|reject(is_odd)|join("|") }}' assert_equal(expand(tmpl), '2|4') end, test_bool_reject = function() local tmpl = '{{ {false, 0, 1, 2, 3, 4, 5}|reject|join("|") }}' assert_equal(expand(tmpl), 'false') end, test_simple_select_attr = function() local tmpl = '{{ users|selectattr("is_active")|'.. 'mapattr("name")|join("|") }}' local env = {users = {{name = 'john', is_active = true}, {name = 'jane', is_active = true}, {name = 'mike', is_active = false}}} assert_equal(expand(tmpl, env), 'john|jane') end, test_simple_reject_attr = function() local tmpl = '{{ users|rejectattr("is_active")|'.. 'mapattr("name")|join(",") }}' local env = {users = {{name = 'john', is_active = true}, {name = 'jane', is_active = true}, {name = 'mike', is_active = false}}} assert_equal(expand(tmpl, env), 'mike') end, test_func_select_attr = function() local tmpl = '{{ users|selectattr("id", is_odd)|'.. 'mapattr("name")|join("|") }}' local env = {users = {{id = 1, name = 'john'}, {id = 2, name = 'jane'}, {id = 3, name = 'mike'}}} assert_equal(expand(tmpl, env), 'john|mike') end, test_func_reject_attr = function() local tmpl = '{{ users|rejectattr("id", is_odd)|'.. 'mapattr("name")|join(",") }}' local env = {users = {{id = 1, name = 'john'}, {id = 2, name = 'jane'}, {id = 3, name = 'mike'}}} assert_equal(expand(tmpl, env), 'jane') end, } }, imports = { import_tests = { setup = function() lupa.configure{loader = lupa.loaders.filesystem('data/imports')} end, teardown = lupa.reset, test_context_imports = function() lupa.env.bar = 23 local tmpl = '{% import "module" as m %}{{ m.test() }}' local env = {foo = 42} assert_equal(expand(tmpl, env), '[|23]') tmpl = '{% import "module" as m without context %}{{ m.test() }}' assert_equal(expand(tmpl, env), '[|23]') tmpl = '{% import "module" as m with context %}{{ m.test() }}' assert_equal(expand(tmpl, env), '[42|23]') -- Note: "from x import y" is not supported by this implementation. lupa.env.bar = nil end, -- Note: test_trailing_comma is not applicable since this implementation -- does not support "from x import y". test_exports = function() local tmpl = '{% import "exports" %}' local env = {} expand(tmpl, env) assert_equal(env.toplevel(), '...') assert(not env.__missing) assert_equal(env.variable, 42) assert(not env.nothere) end, }, include_tests = { setup = function() lupa.configure{loader = lupa.loaders.filesystem('data/imports')} end, teardown = lupa.reset, test_context_include = function() local tmpl = '{% include "header" %}' local env = {foo = 42} assert_equal(expand(tmpl, env), '[42|23]') tmpl = '{% include "header" with context %}' assert_equal(expand(tmpl, env), '[42|23]') tmpl = '{% include "header" without context %}' assert_equal(expand(tmpl, env), '[|23]') end, test_choice_includes = function() local tmpl = '{% include {"missing", "header"} %}' local env = {foo = 42} assert_equal(expand(tmpl, env), '[42|23]') tmpl = '{% include {"missing", "missing2"} ignore missing %}' assert_equal(expand(tmpl, env), '') tmpl = '{% include {"missing", "missing2"} %}' assert_raises('no file.-found', expand, tmpl, env) tmpl = '{% include x %}' env.x = {'missing', 'header'} assert_equal(expand(tmpl, env), '[42|23]') env.x = 'header' assert_equal(expand(tmpl, env), '[42|23]') tmpl = '{% include {x} %}' assert_equal(expand(tmpl, env), '[42|23]') end, test_include_ignoring_missing = function() local tmpl = '{% include "missing" %}' assert_raises('no file.-found', expand, tmpl) tmpl = '{% include "missing" ignore missing %}' assert_equal(expand(tmpl), '') tmpl = '{% include "missing" ignore missing with context %}' assert_equal(expand(tmpl), '') tmpl = '{% include "missing" ignore missing without context %}' assert_equal(expand(tmpl), '') end, test_context_include_with_override = function() local tmpl = 'main' assert_equal(expand_file(tmpl), '123') end, test_unoptimized_scopes = function() local tmpl = [[ {% macro outer(o) %} {% macro inner() %} {% include "o_printer" %} {% endmacro %} {{ inner() }} {% endmacro %} {{ outer("FOO") }} ]] assert_equal(expand(tmpl):trim(), '(FOO)') end, } }, inheritence = { inheritence_tests = { setup = function() lupa.configure{ trim_blocks = true, loader = lupa.loaders.filesystem('data/inheritence') } end, teardown = lupa.reset, test_layout = function() local tmpl = 'layout' assert_equal(expand_file(tmpl), '|block 1 from layout|block 2 from '.. 'layout|nested block 4 from layout|') end, test_level1 = function() local tmpl = 'level1' assert_equal(expand_file(tmpl), '|block 1 from level1|block 2 from '.. 'layout|nested block 4 from layout|') end, test_level2 = function() local tmpl = 'level2' assert_equal(expand_file(tmpl), '|block 1 from level1|nested block 5 from '.. 'level2|nested block 4 from layout|') end, test_level3 = function() local tmpl = 'level3' assert_equal(expand_file(tmpl), '|block 1 from level1|block 5 from level3|'.. 'block 4 from level3|') end, test_level4 = function() local tmpl = 'level4' assert_equal(expand_file(tmpl), '|block 1 from level1|block 5 from '.. 'level3|block 3 from level4|') end, test_super = function() local tmpl = 'super/c' assert_equal(expand_file(tmpl), '--INTRO--|BEFORE|[(INNER)]|AFTER') end, -- Note: test_working is not applicable since it is incomplete. test_reuse_blocks = function() local tmpl = '{% block foo %}42{% endblock %}|'.. '{{ self.foo() }}|{{ self.foo() }}' assert_equal(expand(tmpl), '42|42|42') end, -- Note: test_preserve_blocks is not applicable since false blocks are -- never loaded. test_dynamic_inheritence = function() for i = 1, 2 do local tmpl = 'dynamic/child' local env = {master = 'master'..i} assert_equal(expand_file(tmpl, env), 'MASTER'..i..'CHILD') end end, test_multi_inheritence = function() -- Note: cannot have -- {% if master %}{% extends master %}{% else %} -- {% extends 'master1' %}{% endif %}{% block x %}CHILD{% endblock %} -- since the extends within 'if' is local to that block. -- Must use {% extends master or 'master1' %} instead. local tmpl = 'multi/child' local env = {master = 'master2'} assert_equal(expand_file(tmpl, env), 'MASTER2CHILD') local env = {master = 'master1'} assert_equal(expand_file(tmpl, env), 'MASTER1CHILD') assert_equal(expand_file(tmpl), 'MASTER1CHILD') end, test_scoped_block = function() local tmpl = '{% extends "scoped/master.html" %}{% block item %}'.. '{{ item }}{% endblock %}' local env = {seq = range(5)} assert_equal(expand(tmpl, env), '[1][2][3][4][5]') end, test_super_in_scoped_block = function() local tmpl = '{% extends "super_scoped/master.html" %}{% block item %}'.. '{{ super() }}|{{ item * 2 }}{% endblock %}' local env = {seq = range(5)} assert_equal(expand(tmpl, env), '[1|2][2|4][3|6][4|8][5|10]') end, test_scoped_block_after_inheritence = function() local tmpl = 'scoped/index.html' local env = {the_foo = 42} assert_equal(expand_file(tmpl, env):trim():gsub('%s+', '|'), '43|44|45') end, test_fixed_macro_scoping = function() local tmpl = 'macro_scoping/test.html' assert_equal(expand_file(tmpl, env):trim():gsub('%s+', '|'), 'outer_box|my_macro') end, test_double_extends = function() local tmpl = 'doublee' assert_raises('multiple.+extends', expand_file, tmpl) end, } }, lexer = { -- Note: token_stream_tests are not applicable since this implementation -- does not have a similar tokenizer. lexer_tests = { test_raw1 = function() local tmpl = '{% raw %}foo{% endraw %}|'.. '{%raw%}{{ bar }}|{% baz %}{% endraw %}' assert_equal(expand(tmpl), 'foo|{{ bar }}|{% baz %}') end, test_raw2 = function() local tmpl = '1 {%- raw -%} 2 {%- endraw -%} 3' assert_equal(expand(tmpl), '123') end, test_raw3 = function() local tmpl = '{% raw %}{{ FOO }} and {% BAR %}{% endraw %}' assert_equal(expand(tmpl), '{{ FOO }} and {% BAR %}') end, test_balancing = function() lupa.configure('{%', '%}', '${', '}') local tmpl = [[{% for item in seq %}${{item..' foo'}|string|upper}{% endfor %}]] local env = {seq = range(3)} assert_equal(expand(tmpl, env), '{"1 FOO"}{"2 FOO"}{"3 FOO"}') lupa.reset() end, test_comments = function() lupa.configure('<!--', '-->', '{', '}') local tmpl = [[ <ul> <!--- for item in seq --> <li>{item}</li> <!--- endfor --> </ul>]] local env = {seq = range(3)} assert_equal(expand(tmpl, env), '<ul>\n <li>1</li>\n '.. '<li>2</li>\n <li>3</li>\n</ul>') lupa.reset() end, -- Note: test_string_escapes is not applicable since Lua does not handle -- unicode well enough (even with Lua 5.3). -- Note: test_bytefallback is not applicable since Lua does not have a -- data pretty-printer. -- Note: test_operators is not applicable since this implementation -- does not have a similar tokenizer. test_normalizing = function() local tmpl = '1\n2\r\n3\n4\n' for _, seq in ipairs{'\r\n', '\n'} do lupa.configure{newline_sequence = seq} assert_equal(expand(tmpl):gsub(seq, 'X'), '1X2X3X4') end lupa.reset() end, test_trailing_newline = function() for _, keep in ipairs{true, false} do lupa.configure{keep_trailing_newline = keep} local tmpl = '' assert_equal(expand(tmpl), '') tmpl = 'no\nnewline' assert_equal(expand(tmpl), tmpl) tmpl = 'with\nnewline\n' assert_equal(expand(tmpl), keep and tmpl or 'with\nnewline') tmpl = 'with\nseveral\n\n\n' assert_equal(expand(tmpl), keep and tmpl or 'with\nseveral\n\n') end lupa.reset() end, }, parser_tests = { test_php_syntax = function() lupa.configure('<?', '?>', '<?=', '?>', '<!--', '-->') local tmpl = [[ <!-- I'm a comment, I'm not interesting --> <? for item in seq -?> <?= item ?> <?- endfor ?>]] local env = {seq = range(5)} assert_equal(expand(tmpl, env), '\n12345') lupa.reset() end, test_erb_syntax = function() lupa.configure('<%', '%>', '<%=', '%>', '<%#', '%>') local tmpl = [[ <%# I'm a comment, I'm not interesting %> <% for item in seq -%> <%= item %> <%- endfor %>]] local env = {seq = range(5)} assert_equal(expand(tmpl, env), '\n12345') lupa.reset() end, test_comment_syntax = function() lupa.configure('<!--', '-->', '${', '}', '<!--#', '-->') local tmpl = [[ <!--# I'm a comment, I'm not interesting --> <!-- for item in seq ---> ${item} <!--- endfor -->]] local env = {seq = range(5)} assert_equal(expand(tmpl, env), '\n12345') lupa.reset() end, test_balancing = function() local tmpl = [[{{{1, 2, 3}|length}}]] assert_equal(expand(tmpl), '3') end, test_start_comment = function() local tmpl = [[{# foo comment and bar comment #} {% macro blub() %}foo{% endmacro %} {{ blub() }}]] assert_equal(expand(tmpl):trim(), 'foo') end, -- Note: test_line_syntax is not applicable since line statements are not -- supported. -- Note: test_line_syntax_priority is not applicable since line statements -- are not supported. test_error_messages = function() local tmpl = '{% for item in seq %}...{% endif %}' assert_raises('endfor.+expected', expand, tmpl) tmpl = '{% if foo %}{% for item in seq %}...{% endfor %}{% endfor %}' assert_raises('endif.+expected', expand, tmpl) tmpl = '{% if foo %}' assert_raises('endif.+expected', expand, tmpl) tmpl = '{% for item in seq %}' assert_raises('endfor.+expected', expand, tmpl) tmpl = '{% block foo-bar-baz %}{% endblock %}' assert_raises('invalid block name', expand, tmpl) tmpl = '{% unknown_tag %}' assert_raises('unknown or unexpected tag', expand, tmpl) end, }, -- Note: syntax_tests are not applicable since this implementation uses -- Lua's parser. lstrip_blocks_tests = { setup = function() lupa.configure{lstrip_blocks = true} end, teardown = lupa.reset, test_lstrip = function() local tmpl = ' {% if true %}\n {% endif %}' assert_equal(expand(tmpl), '\n') end, test_lstrip_trim = function() lupa.configure{lstrip_blocks = true, trim_blocks = true} local tmpl = ' {% if true %}\n {% endif %}' assert_equal(expand(tmpl), '') lupa.configure{lstrip_blocks = true} end, test_no_lstrip = function() local tmpl = ' {%+ if true %}\n {%+ endif %}' assert_equal(expand(tmpl), ' \n ') end, test_lstrip_endline = function() local tmpl = ' hello{% if true %}\n goodbye{% endif %}' assert_equal(expand(tmpl), ' hello\n goodbye') end, test_lstrip_inline = function() local tmpl = ' {% if true %}hello {% endif %}' assert_equal(expand(tmpl), 'hello ') end, test_lstrip_nested = function() local tmpl = ' {% if true %}a {% if true %}b {% endif %}c {% endif %}' assert_equal(expand(tmpl), 'a b c ') end, test_lstrip_left_chars = function() local tmpl = [[ abc {% if true %} hello{% endif %}]] assert_equal(expand(tmpl), ' abc \n hello') end, -- Note: test_lstrip_embedded_strings is not applicable since this -- implementation's grammar cannot parse Lua itself in order to handle -- embedded tags. test_lstrip_preserve_leading_newlines = function() local tmpl = '\n\n\n{% set hello = 1 %}' assert_equal(expand(tmpl), '\n\n\n') end, test_lstrip_comment = function() local tmpl = [[ {# if true #} hello {#endif#}]] assert_equal(expand(tmpl), '\nhello\n') end }, lstrip_blocks_angle_bracket_tests = { setup = function() lupa.configure('<%', '%>', '${', '}', '<%#', '%>', {lstrip_blocks = true, trim_blocks = true}) end, teardown = lupa.reset, test_lstrip_angle_bracket_simple = function() local tmpl = ' <% if true %>hello <% endif %>' assert_equal(expand(tmpl), 'hello ') end, test_lstrip_angle_bracket_comment = function() local tmpl = ' <%# if true %>hello <%# endif %>' assert_equal(expand(tmpl), 'hello ') end, test_lstrip_angle_bracket = function() -- Note: this implementation does not support line statements, so this -- test input is slightly different. local tmpl = [[ <%# regular comment %> <% for item in seq %> ${item} <% endfor %>]] local env = {seq = range(5)} assert_equal(expand(tmpl, env), '1\n2\n3\n4\n5\n') end, test_lstrip_angle_bracket_compact = function() -- Note: this implementation does not support line statements, so this -- test input is slightly different. local tmpl = [[ <%#regular comment%> <%for item in seq%> ${item} <%endfor%>]] local env = {seq = range(5)} assert_equal(expand(tmpl, env), '1\n2\n3\n4\n5\n') end, }, lstrip_blocks_php_syntax_tests = { setup = function() lupa.configure('<?', '?>', '<?=', '?>', '<!--', '-->', {lstrip_blocks = true, trim_blocks = true}) end, teardown = lupa.reset, test_php_syntax_with_manual = function() local tmpl = [[ <!-- I'm a comment, I'm not interesting --> <? for item in seq -?> <?= item ?> <?- endfor ?>]] local env = {seq = range(5)} assert_equal(expand(tmpl, env), '12345') end, test_php_syntax = function() local tmpl = [[ <!-- I'm a comment, I'm not interesting --> <? for item in seq ?> <?= item ?> <? endfor ?>]] local env = {seq = range(5)} assert_equal(expand(tmpl, env), ' 1\n 2\n 3\n 4\n 5\n') end, test_php_syntax_compact = function() local tmpl = [[ <!-- I'm a comment, I'm not interesting --> <?for item in seq?> <?=item?> <?endfor?>]] local env = {seq = range(5)} assert_equal(expand(tmpl, env), ' 1\n 2\n 3\n 4\n 5\n') end, }, lstrip_blocks_erb_syntax_tests = { setup = function() lupa.configure('<%', '%>', '<%=', '%>', '<%#', '%>', {lstrip_blocks = true, trim_blocks = true}) end, teardown = lupa.reset, test_erb_syntax = function() local tmpl = [[ <%# I'm a comment, I'm not interesting %> <% for item in seq %> <%= item %> <% endfor %> ]] local env = {seq = range(5)} assert_equal(expand(tmpl, env), ' 1\n 2\n 3\n 4\n 5\n') end, test_erb_syntax_with_manual = function() local tmpl = [[ <%# I'm a comment, I'm not interesting %> <% for item in seq -%> <%= item %> <%- endfor %>]] local env = {seq = range(5)} assert_equal(expand(tmpl, env), '12345') end, test_erb_syntax_no_lstrip = function() local tmpl = [[ <%# I'm a comment, I'm not interesting %> <%+ for item in seq -%> <%= item %> <%- endfor %>]] local env = {seq = range(5)} assert_equal(expand(tmpl, env), ' 12345') end, }, lstrip_blocks_comment_tests = { test_comment_syntax = function() lupa.configure('<!--', '-->', '${', '}', '<!--#', '-->', {lstrip_blocks = true, trim_blocks = true}) local tmpl = [[ <!--# I'm a comment, I'm not interesting --> <!-- for item in seq ---> ${item} <!--- endfor -->]] local env = {seq = range(5)} assert_equal(expand(tmpl, env), '12345') lupa.reset() end, } }, regression = { corner_case_tests = { test_assigned_scoping = function() local tmpl = [[ {%- for item in {1, 2, 3, 4} -%} [{{ item }}] {%- endfor %} {{- item -}} ]] local env = {item = 42} assert_equal(expand(tmpl, env), '[1][2][3][4]42') tmpl = [[ {%- for item in {1, 2, 3, 4} -%} [{{ item }}] {%- endfor %} {%- set item = 42 %} {{- item -}} ]] assert_equal(expand(tmpl), '[1][2][3][4]42') tmpl = [[ {%- set item = 42 %} {%- for item in {1, 2, 3, 4} -%} [{{ item }}] {%- endfor %} {{- item -}} ]] assert_equal(expand(tmpl), '[1][2][3][4]42') end, test_closure_scoping = function() local tmpl = [[ {%- set wrapper = "<FOO>" %} {%- for item in {1, 2, 3, 4} %} {%- macro wrapper() %}[{{ item }}]{% endmacro %} {{- wrapper() }} {%- endfor %} {{- wrapper -}} ]] assert_equal(expand(tmpl), '[1][2][3][4]<FOO>') end, }, other_tests = { test_keyword_folding = function() lupa.filters.testing = function(v, s) return v..s end local tmpl = [[{{ 'test'|testing('stuff') }}]] assert_equal(expand(tmpl), 'teststuff') lupa.filters.testing = nil end, test_extends_output = function() -- Note: "extends" cannot be within an "if" so use conditional expr. local tmpl = '{% extends expr and "data/other/parent.html" %}'.. '[[{% block title %}title{% endblock %}]]'.. '{% for item in {1, 2, 3} %}({{ item }}){% endfor %}' local env = {expr = false} assert_equal(expand(tmpl, env):gsub('\n', ''), '[[title]](1)(2)(3)') env = {expr = true} assert_equal(expand(tmpl, env):gsub('\n', ''), '((title))') end, test_urlize_filter_escaping = function() local tmpl = '{{ "http://www.example.org/<foo"|urlize }}' assert_equal(expand(tmpl), '<a href="http://www.example.org/<foo">http://www.example.org/<foo</a>') end, test_loop_call_loop = function() local tmpl = [[ {% macro test() %} {{ caller() }} {% endmacro %} {% for num1 in range(5) %} {% call test() %} {% for num2 in range(10) %} {{ loop.index }} {% endfor %} {% endcall %} {% endfor %} ]] assert_equal(expand(tmpl):trim():gsub('%s+', ''), string.rep('12345678910', 5)) end, test_weird_inline_comment = function() local tmpl = '{% for item in seq {# missing #}%}...{% endfor %}' assert_raises('%%}.+expected', expand, tmpl) end, test_old_macro_loop_scoping = function() local tmpl = '{% for i in {1, 2} %}{{ i }}{% endfor %}'.. '{% macro i() %}3{% endmacro %}{{ i() }}' assert_equal(expand(tmpl), '123') end, test_partial_conditional_assignments = function() local tmpl = '{% if b %}{% set a = 42 %}{% endif %}{{ a }}' local env = {a = 23} assert_equal(expand(tmpl, env), '23') env = {b = true} assert_equal(expand(tmpl, env), '42') end, test_stacked_locals_scoping = function() -- Note: this implementation does not support line statements, so this -- test input is slightly different. local tmpl = [[ {% for j in {1, 2} -%} {% set x = 1 -%} {% for i in {1, 2} -%} {{ x -}} {% if i % 2 == 0 -%} {% set x = x + 1 -%} {% endif -%} {% endfor -%} {% endfor -%} {% if a -%} {{ 'A' -}} {% elseif b -%} {{ 'B' -}} {% elseif c == d -%} {{ 'C' -}} {% else -%} {{ 'D' -}} {% endif -%} ]] local env = {a = nil, b = false, c = 42, d = 42.0} assert_equal(expand(tmpl, env), '1111C') end, test_stacked_locals_scoping_twoframe = function() local tmpl = [[ {% set x = 1 %} {% for item in foo %} {% if item == 1 %} {% set x = 2 %} {% endif %} {% endfor %} {{ x }} ]] local env = {foo = {1}} assert_equal(expand(tmpl, env):gsub('%s+', ''), '1') end, test_call_with_args = function() local tmpl = [[{% macro dump_users(users) -%} <ul> {%- for user in users -%} <li><p>{{ user.username|e }}</p>{{ caller(user) }}</li> {%- endfor -%} </ul> {%- endmacro -%} {% call(user) dump_users(list_of_user) -%} <dl> <dl>Realname</dl> <dd>{{ user.realname|e }}</dd> <dl>Description</dl> <dd>{{ user.description }}</dd> </dl> {% endcall %}]] local env = { list_of_user = { {username = 'apo', realname = 'something else', description = 'test'} } } local lines = {} for line in expand(tmpl, env):gmatch('[^\n]+') do lines[#lines + 1] = line:trim() end assert_equal(lines[1], '<ul><li><p>apo</p><dl>') assert_equal(lines[2], '<dl>Realname</dl>') assert_equal(lines[3], '<dd>something else</dd>') assert_equal(lines[4], '<dl>Description</dl>') assert_equal(lines[5], '<dd>test</dd>') assert_equal(lines[6], '</dl>') assert_equal(lines[7], '</li></ul>') end, test_empty_if_condition_fails = function() local tmpl = '{% if %}....{% endif %}' assert_raises('expression expected', expand, tmpl) tmpl = '{% if foo %}...{% elif %}...{% endif %}' assert_raises('additional tag or.+endif.+expected', expand, tmpl) tmpl = '{% for x in %}..{% endfor %}' assert_raises('invalid for expression', expand, tmpl) end, -- Note: test_recursive_loop is not applicable since it is incomplete. test_else_loop = function() local tmpl = [[ {% for x in y %} {{ loop.index0 }} {% else %} {% for i in range(3) %}{{ i }}{% endfor %} {% endfor %} ]] local env = {y = {}} assert_equal(expand(tmpl, env):trim(), '123') end, -- Note: test_correct_prefix_loader is not applicable since this -- implementation does not use a prefix loader. -- TODO: this isn't exactly practical... test_contextfunction_callable_classes = function() local tmpl = '{{ callableclass() }}' local env = {hello = 'TEST'} env.callableclass = setmetatable({env = env}, {__call = function(t) return t.env.hello end}) assert_equal(expand(tmpl, env), 'TEST') end, } }, -- Note: security tests are not applicable since Lua has no security -- mechanisms. tests = { tests_tests = { test_defined = function() local tmpl = '{{ is_defined(nil) }}|{{ is_defined(true) }}' assert_equal(expand(tmpl), 'false|true') end, test_even = function() local tmpl = '{{ is_even(1) }}|{{ is_even(2) }}' assert_equal(expand(tmpl), 'false|true') end, test_odd = function() local tmpl = '{{ is_odd(1) }}|{{ is_odd(2) }}' assert_equal(expand(tmpl), 'true|false') end, test_lower = function() local tmpl = '{{ is_lower("foo") }}|{{ is_lower("FOO") }}' assert_equal(expand(tmpl), 'true|false') end, test_typechecks = function() local tmpl = [[ {{ is_undefined(42) }} {{ is_defined(42) }} {{ is_nil(42) }} {{ is_nil(nil) }} {{ is_number(42) }} {{ is_string(42) }} {{ is_string("foo") }} {{ is_sequence("foo") }} {{ is_sequence({1}) }} {{ is_callable(range) }} {{ is_callable(42) }} {{ is_iterable(range(5)) }} {{ is_mapping({}) }} {{ is_mapping(mydict) }} {{ is_mapping("foo") }} ]] local env = {mydict = {}} local results = {} for result in expand(tmpl, env):gmatch('%S+') do results[#results + 1] = result end local expected = { 'false', 'true', 'false', 'true', 'true', 'false', 'true', 'false', 'true', 'true', 'false', 'true', 'true', 'true', 'false' } for i = 1, #results do assert_equal(results[i], expected[i]) end end, test_sequence = function() local tmpl = '{{ is_sequence({1, 2, 3}) }}|'.. '{{ is_sequence("foo") }}|'.. '{{ is_sequence(42) }}' assert_equal(expand(tmpl), 'true|false|false') end, test_upper = function() local tmpl = '{{ is_upper("FOO") }}|{{ is_upper("foo") }}' assert_equal(expand(tmpl), 'true|false') end, test_sameas = function() local tmpl = '{{ is_sameas(foo, false) }}|'.. '{{ is_sameas(nil, false) }}' local env = {foo = false} assert_equal(expand(tmpl, env), 'true|false') end, test_nil_is_nil = function() local tmpl = '{{ is_sameas(foo, nil) }}' local env = {foo = nil} assert_equal(expand(tmpl, env), 'true') end, test_escaped = function() lupa.configure{autoescape = true} -- Note: This implementation does not support markup, so this test input -- is slightly different. local tmpl = '{% set y = "foo"|safe %}{{ is_escaped(x) }}|{{ is_escaped(y) }}' local env = {x = 'foo'} assert_equal(expand(tmpl, env), 'false|true') lupa.reset() end, } } } local num_tests, failures = 0, {} print('Starting test suite.') for test_group, types in pairs(test_suite) do for test_type, tests in pairs(types) do print("Running all of "..test_type.."'s tests.") if tests.setup then tests.setup() end for test_name, test in pairs(tests) do if test_name ~= 'setup' and test_name ~= 'teardown' then local pass, message = pcall(test) if pass then io.output():write('.') else io.output():write('E') failures[#failures + 1] = { test_type, test_name, message:match('^[^:]*:?%d*:?%s*(.+)$') } end io.output():flush() num_tests = num_tests + 1 end end if tests.teardown then tests.teardown() end print('') -- newline end end print('\nSummary:') if #failures == 0 then print('All '..num_tests..' tests passed!') else local line = string.rep('-', 72) print(#failures..' of '..num_tests..' tests failed!') print(line) for i = 1, #failures do print("Failure in "..failures[i][1].."'s "..failures[i][2]..':') print(failures[i][3]) print(line) end end 07070100000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000B00000000TRAILER!!!395 blocks
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor