You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lupa.lua 72KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  1. -- Copyright 2015-2019 Mitchell mitchell.att.foicica.com. See LICENSE.
  2. -- Sponsored by the Library of the University of Antwerp.
  3. -- Contributions from Ana Balan.
  4. -- Lupa templating engine.
  5. --[[ This comment is for LuaDoc.
  6. ---
  7. -- Lupa is a Jinja2 template engine implementation written in Lua and supports
  8. -- Lua syntax within tags and variables.
  9. module('lupa')]]
  10. local M = {}
  11. local lpeg = require('lpeg')
  12. lpeg.locale(lpeg)
  13. local space, newline = lpeg.space, lpeg.P('\r')^-1 * '\n'
  14. local P, S, V = lpeg.P, lpeg.S, lpeg.V
  15. local C, Cc, Cg, Cp, Ct = lpeg.C, lpeg.Cc, lpeg.Cg, lpeg.Cp, lpeg.Ct
  16. ---
  17. -- Lupa's expression filters.
  18. -- @class table
  19. -- @name filters
  20. M.filters = {}
  21. ---
  22. -- Lupa's value tests.
  23. -- @class table
  24. -- @name tests
  25. M.tests = {}
  26. ---
  27. -- Lupa's template loaders.
  28. -- @class table
  29. -- @name loaders
  30. M.loaders = {}
  31. -- Lua version compatibility.
  32. if _VERSION == 'Lua 5.1' then
  33. function load(ld, source, mode, env)
  34. local f, err = loadstring(ld)
  35. if f and env then return setfenv(f, env) end
  36. return f, err
  37. end
  38. table.unpack = unpack
  39. end
  40. local newline_sequence, keep_trailing_newline, autoescape = '\n', false, false
  41. local loader
  42. -- Creates and returns a token pattern with token name *name* and pattern
  43. -- *patt*.
  44. -- The returned pattern captures three values: the token's position and name,
  45. -- and either a string value or table of capture values.
  46. -- Tokens are used to construct an Abstract Syntax Tree (AST) for a template.
  47. -- @param name The name of the token.
  48. -- @param patt The pattern to match. It must contain only one capture: either a
  49. -- string or table of captures.
  50. -- @see evaluate
  51. local function token(name, patt) return Cp() * Cc(name) * patt end
  52. -- Returns an LPeg pattern that immediately raises an error with message
  53. -- *errmsg* for invalid syntax when parsing a template.
  54. -- @param errmsg The error message to raise an error with.
  55. local function lpeg_error(errmsg)
  56. return P(function(input, index)
  57. input = input:sub(1, index)
  58. local _, line_num = input:gsub('\n', '')
  59. local col_num = #input:match('[^\n]*$')
  60. error(string.format('Parse Error in file "%s" on line %d, column %d: %s',
  61. M._FILENAME, line_num + 1, col_num, errmsg), 0)
  62. end)
  63. end
  64. ---
  65. -- Configures the basic delimiters and options for templates.
  66. -- This function then regenerates the grammar for parsing templates.
  67. -- Note: this function cannot be used iteratively to configure Lupa options.
  68. -- Any options not provided are reset to their default values.
  69. -- @param ts The tag start delimiter. The default value is '{%'.
  70. -- @param te The tag end delimiter. The default value is '%}'.
  71. -- @param vs The variable start delimiter. The default value is '{{'.
  72. -- @param ve The variable end delimiter. The default value is '}}'.
  73. -- @param cs The comment start delimiter. The default value is '{#'.
  74. -- @param ce The comment end delimiter. The default value is '#}'.
  75. -- @param options Optional set of options for templates:
  76. --
  77. -- * `trim_blocks`: Trim the first newline after blocks.
  78. -- * `lstrip_blocks`: Strip line-leading whitespace in front of tags.
  79. -- * `newline_sequence`: The end-of-line character to use.
  80. -- * `keep_trailing_newline`: Whether or not to keep a newline at the end of
  81. -- a template.
  82. -- * `autoescape`: Whether or not to autoescape HTML entities. May be a
  83. -- function that accepts the template's filename as an argument and returns
  84. -- a boolean.
  85. -- * `loader`: Function that receives a template name to load and returns the
  86. -- path to that template.
  87. -- @name configure
  88. function M.configure(ts, te, vs, ve, cs, ce, options)
  89. if type(ts) == 'table' then options, ts = ts, nil end
  90. if not ts then ts = '{%' end
  91. if not te then te = '%}' end
  92. if not vs then vs = '{{' end
  93. if not ve then ve = '}}' end
  94. if not cs then cs = '{#' end
  95. if not ce then ce = '#}' end
  96. -- Tokens for whitespace control.
  97. local lstrip = token('lstrip', C('-')) + '+' -- '+' is handled by grammar
  98. local rstrip = token('rstrip', -(P(te) + ve + ce) * C('-'))
  99. -- Configure delimiters, including whitespace control.
  100. local tag_start = P(ts) * lstrip^-1 * space^0
  101. local tag_end = space^0 * rstrip^-1 * P(te)
  102. local variable_start = P(vs) * lstrip^-1 * space^0
  103. local variable_end = space^0 * rstrip^-1 * P(ve)
  104. local comment_start = P(cs) * lstrip^-1 * space^0
  105. local comment_end = space^0 * rstrip^-1 * P(ce)
  106. if options and options.trim_blocks then
  107. -- Consider whitespace, including a newline, immediately following a tag as
  108. -- part of that tag so it is not captured as plain text. Basically, strip
  109. -- the trailing newline from tags.
  110. tag_end = tag_end * S(' \t')^0 * newline^-1
  111. comment_end = comment_end * S(' \t')^0 * newline^-1
  112. end
  113. -- Error messages.
  114. local variable_end_error = lpeg_error('"'..ve..'" expected')
  115. local comment_end_error = lpeg_error('"'..ce..'" expected')
  116. local tag_end_error = lpeg_error('"'..te..'" expected')
  117. local endraw_error = lpeg_error('additional tag or "'..ts..' endraw '..te..
  118. '" expected')
  119. local expr_error = lpeg_error('expression expected')
  120. local endblock_error = lpeg_error('additional tag or "'..ts..' endblock '..
  121. te..'" expected')
  122. local endfor_error = lpeg_error('additional tag or "'..ts..' endfor '..te..
  123. '" expected')
  124. local endif_error = lpeg_error('additional tag or "'..ts..' endif '..te..
  125. '" expected')
  126. local endmacro_error = lpeg_error('additional tag or "'..ts..' endmacro '..
  127. te..'" expected')
  128. local endcall_error = lpeg_error('additional tag or "'..ts..' endcall '..te..
  129. '" expected')
  130. local endfilter_error = lpeg_error('additional tag or "'..ts..' endfilter '..
  131. te..'" expected')
  132. local tag_error = lpeg_error('unknown or unexpected tag')
  133. local main_error = lpeg_error('unexpected character; text or tag expected')
  134. -- Grammar.
  135. M.grammar = Ct(P{
  136. -- Utility patterns used by tokens.
  137. entity_start = tag_start + variable_start + comment_start,
  138. any_text = (1 - V('entity_start'))^1,
  139. -- Allow '{{' by default in expression text since it is valid in Lua.
  140. expr_text = (1 - tag_end - tag_start - comment_start)^1,
  141. -- When `options.lstrip_blocks` is enabled, ignore leading whitespace
  142. -- immediately followed by a tag (as long as '+' is not present) so that
  143. -- whitespace not captured as plain text. Basically, strip leading spaces
  144. -- from tags.
  145. line_text = (1 - newline - V('entity_start'))^1,
  146. lstrip_entity_start = -P(vs) * (P(ts) + cs) * -P('+'),
  147. lstrip_space = S(' \t')^1 * #V('lstrip_entity_start'),
  148. text_lines = V('line_text') * (newline * -(S(' \t')^0 * V('lstrip_entity_start')) * V('line_text'))^0 * newline^-1 + newline,
  149. -- Plain text.
  150. text = (not options or not options.lstrip_blocks) and
  151. token('text', C(V('any_text'))) or
  152. V('lstrip_space') + token('text', C(V('text_lines'))),
  153. -- Variables: {{ expr }}.
  154. lua_table = '{' * ((1 - S('{}')) + V('lua_table'))^0 * '}',
  155. variable = variable_start *
  156. token('variable', C((V('lua_table') + (1 - variable_end))^0)) *
  157. (variable_end + variable_end_error),
  158. -- Filters: handled in variable evaluation.
  159. -- Tests: handled in control structure expression evaluation.
  160. -- Comments: {# comment #}.
  161. comment = comment_start * (1 - comment_end)^0 * (comment_end + comment_end_error),
  162. -- Whitespace control: handled in tag/variable/comment start/end.
  163. -- Escaping: {% raw %} body {% endraw %}.
  164. raw_block = tag_start * 'raw' * (tag_end + tag_end_error) *
  165. token('text', C((1 - (tag_start * 'endraw' * tag_end))^0)) *
  166. (tag_start * 'endraw' * tag_end + endraw_error),
  167. -- Note: line statements are not supported since this grammer cannot parse
  168. -- Lua itself.
  169. -- Template inheritence.
  170. -- {% block ... %} body {% endblock %}
  171. block_block = tag_start * 'block' * space^1 * token('block', Ct((Cg(V('expr_text'), 'expression') + expr_error) * (tag_end + tag_end_error) *
  172. V('body')^-1)) *
  173. (tag_start * 'endblock' * tag_end + endblock_error),
  174. -- {% extends ... %}
  175. extends_tag = tag_start * 'extends' * space^1 * token('extends', C(V('expr_text')) + expr_error) * (tag_end + tag_end_error),
  176. -- Super blocks are handled in variables.
  177. -- Note: named block end tags are not supported since keeping track of that
  178. -- state information is difficult.
  179. -- Note: block nesting and scope is not applicable since blocks always have
  180. -- access to scoped variables in this implementation.
  181. -- Control Structures.
  182. -- {% for expr %} body {% else %} body {% endfor %}
  183. for_block = tag_start * 'for' * space^1 * token('for', Ct((Cg(V('expr_text'), 'expression') + expr_error) * (tag_end + tag_end_error) *
  184. V('body')^-1 *
  185. Cg(Ct(tag_start * 'else' * tag_end *
  186. V('body')^-1), 'else')^-1)) *
  187. (tag_start * 'endfor' * tag_end + endfor_error),
  188. -- {% if expr %} body {% elseif expr %} body {% else %} body {% endif %}
  189. if_block = tag_start * 'if' * space^1 * token('if', Ct((Cg(V('expr_text'), 'expression') + expr_error) * (tag_end + tag_end_error) *
  190. V('body')^-1 *
  191. Cg(Ct(Ct(tag_start * 'elseif' * space^1 * (Cg(V('expr_text'), 'expression') + expr_error) * (tag_end + tag_end_error) *
  192. V('body')^-1)^1), 'elseif')^-1 *
  193. Cg(Ct(tag_start * 'else' * tag_end *
  194. V('body')^-1), 'else')^-1)) *
  195. (tag_start * 'endif' * tag_end + endif_error),
  196. -- {% macro expr %} body {% endmacro %}
  197. macro_block = tag_start * 'macro' * space^1 * token('macro', Ct((Cg(V('expr_text'), 'expression') + expr_error) * (tag_end + tag_end_error) *
  198. V('body')^-1)) *
  199. (tag_start * 'endmacro' * tag_end + endmacro_error),
  200. -- {% call expr %} body {% endcall %}
  201. call_block = tag_start * 'call' * (space^1 + #P('(')) * token('call', Ct((Cg(V('expr_text'), 'expression') + expr_error) * (tag_end + tag_end_error) *
  202. V('body')^-1)) *
  203. (tag_start * 'endcall' * tag_end + endcall_error),
  204. -- {% filter expr %} body {% endfilter %}
  205. filter_block = tag_start * 'filter' * space^1 * token('filter', Ct((Cg(V('expr_text'), 'expression') + expr_error) * (tag_end + tag_end_error) *
  206. V('body')^-1)) *
  207. (tag_start * 'endfilter' * tag_end + endfilter_error),
  208. -- {% set ... %}
  209. set_tag = tag_start * 'set' * space^1 * token('set', C(V('expr_text')) + expr_error) * (tag_end + tag_end_error),
  210. -- {% include ... %}
  211. include_tag = tag_start * 'include' * space^1 * token('include', C(V('expr_text')) + expr_error) * (tag_end + tag_end_error),
  212. -- {% import ... %}
  213. import_tag = tag_start * 'import' * space^1 * token('import', C(V('expr_text')) + expr_error) * (tag_end + tag_end_error),
  214. -- Note: i18n is not supported since it is out of scope for this
  215. -- implementation.
  216. -- Expression statement: {% do ... %}.
  217. do_tag = tag_start * 'do' * space^1 * token('do', C(V('expr_text')) + expr_error) * (tag_end + tag_end_error),
  218. -- Note: loop controls are not supported since that would require jumping
  219. -- between "scopes" (e.g. from within an "if" block to outside that "if"
  220. -- block's parent "for" block when coming across a {% break %} tag).
  221. -- Note: with statement is not supported since it is out of scope for this
  222. -- implementation.
  223. -- Note: autoescape is not supported since it is out of scope for this
  224. -- implementation.
  225. -- Any valid blocks of text or tags.
  226. body = (V('text') + V('variable') + V('comment') + V('raw_block') +
  227. V('block_block') + V('extends_tag') + V('for_block') +
  228. V('if_block') + V('macro_block') + V('call_block') +
  229. V('filter_block') + V('set_tag') + V('include_tag') +
  230. V('import_tag') + V('do_tag'))^0,
  231. -- Main pattern.
  232. V('body') * (-1 + tag_start * tag_error + main_error),
  233. })
  234. -- Other options.
  235. if options and options.newline_sequence then
  236. assert(options.newline_sequence:find('^\r?\n$'),
  237. 'options.newline_sequence must be "\r\n" or "\n"')
  238. newline_sequence = options.newline_sequence
  239. else
  240. newline_sequence = '\n'
  241. end
  242. if options and options.keep_trailing_newline then
  243. keep_trailing_newline = options.keep_trailing_newline
  244. else
  245. keep_trailing_newline = false
  246. end
  247. if options and options.autoescape then
  248. autoescape = options.autoescape
  249. else
  250. autoescape = false
  251. end
  252. if options and options.loader then
  253. assert(type(options.loader) == 'function',
  254. 'options.loader must be a function that returns a filename')
  255. loader = options.loader
  256. else
  257. loader = M.loaders.filesystem()
  258. end
  259. end
  260. -- Wraps Lua's `assert()` in template environment *env* such that, when called
  261. -- in conjunction with another Lua function that produces an error message (e.g.
  262. -- `load()` and `pcall()`), that error message's context (source and line
  263. -- number) is replaced by the template's context.
  264. -- This results in Lua's error messages pointing to a template position rather
  265. -- than this library's source code.
  266. -- @param env The environment for the currently running template. It must have
  267. -- a `_SOURCE` field with the template's source text and a `_POSITION` field
  268. -- with the current position of expansion.
  269. -- @param ... Arguments to Lua's `assert()`.
  270. local function env_assert(env, ...)
  271. if not select(1, ...) then
  272. local input = env._LUPASOURCE:sub(1, env._LUPAPOSITION)
  273. local _, line_num = input:gsub('\n', '')
  274. local col_num = #input:match('[^\n]*$')
  275. local errmsg = select(2, ...)
  276. errmsg = errmsg:match(':%d+: (.*)$') or errmsg -- reformat if necessary
  277. error(string.format('Runtime Error in file "%s" on line %d, column %d: %s',
  278. env._LUPAFILENAME, line_num + 1, col_num, errmsg), 0)
  279. end
  280. return ...
  281. end
  282. -- Returns a generator that returns the position and filter in a list of
  283. -- filters, taking into account '|'s that may be within filter arguments.
  284. -- @usage for pos, filter in each_filter('foo|join("|")|bar') do ... end
  285. local function each_filter(s)
  286. local init = 1
  287. return function(s)
  288. local pos, filter, e = s:match('^%s*()([^|(]+%b()[^|]*)|?()', init)
  289. if not pos then pos, filter, e = s:match('()([^|]+)|?()', init) end
  290. init = e
  291. return pos, filter
  292. end, s
  293. end
  294. -- Evaluates template variable *expression* subject to template environment
  295. -- *env*, applying any filters given in *expression*.
  296. -- @param expression The string expression to evaluate.
  297. -- @param env The environment to evaluate the expression in.
  298. local function eval(expression, env)
  299. local expr, pos, filters = expression:match('^([^|]*)|?()(.-)$')
  300. -- Evaluate base expression.
  301. local f = env_assert(env, load('return '..expr, nil, nil, env))
  302. local result = select(2, env_assert(env, pcall(f)))
  303. -- Apply any filters.
  304. local results, multiple_results = nil, false
  305. local p = env._LUPAPOSITION + pos - 1 -- mark position at first filter
  306. for pos, filter in each_filter(filters) do
  307. env._LUPAPOSITION = p + pos - 1 -- update position for error messages
  308. local name, params = filter:match('^%s*([%w_]+)%(?(.-)%)?%s*$')
  309. f = M.filters[name]
  310. env_assert(env, f, 'unknown filter "'..name..'"')
  311. local args = env_assert(env, load('return {'..params..'}', nil, nil, env),
  312. 'invalid filter parameter(s) for "'..name..'"')()
  313. if not multiple_results then
  314. results = {select(2,
  315. env_assert(env, pcall(f, result, table.unpack(args))))}
  316. else
  317. for i = 1, #results do table.insert(args, i, results[i]) end
  318. results = {select(2, env_assert(env, pcall(f, table.unpack(args))))}
  319. end
  320. result, multiple_results = results[1], #results > 1
  321. end
  322. if multiple_results then return table.unpack(results) end
  323. return result
  324. end
  325. local iterate
  326. -- Iterates over *ast*, a collection of tokens from a portion of a template's
  327. -- Abstract Syntax Tree (AST), evaluating any expressions in template
  328. -- environment *env*, and returns a concatenation of the results.
  329. -- @param ast A template's AST or portion of its AST (e.g. portion inside a
  330. -- 'for' control structure).
  331. -- @param env Environment to evaluate any expressions in.
  332. local function evaluate(ast, env)
  333. local chunks = {}
  334. local extends -- text of a parent template
  335. local rstrip -- flag for stripping leading whitespace of next token
  336. for i = 1, #ast, 3 do
  337. local pos, token, block = ast[i], ast[i + 1], ast[i + 2]
  338. env._LUPAPOSITION = pos
  339. if token == 'text' then
  340. chunks[#chunks + 1] = block
  341. elseif token == 'variable' then
  342. local value = eval(block, env)
  343. if autoescape then
  344. local escape = autoescape
  345. if type(autoescape) == 'function' then
  346. escape = autoescape(env._LUPAFILENAME) -- TODO: test
  347. end
  348. if escape and type(value) == 'string' then
  349. value = M.filters.escape(value)
  350. end
  351. end
  352. chunks[#chunks + 1] = value ~= nil and tostring(value) or ''
  353. elseif token == 'extends' then
  354. env_assert(env, not extends,
  355. 'cannot have multiple "extends" in the same scope')
  356. local file = eval(block, env) -- covers strings and variables
  357. extends = file
  358. env._LUPAEXTENDED = true -- used by parent templates
  359. elseif token == 'block' then
  360. local name = block.expression:match('^[%w_]+$')
  361. env_assert(env, name, 'invalid block name')
  362. -- Store the block for potential use by the parent template if this
  363. -- template is a child template, or for use by `self`.
  364. if not env._LUPABLOCKS then env._LUPABLOCKS = {} end
  365. if not env._LUPABLOCKS[name] then env._LUPABLOCKS[name] = {} end
  366. table.insert(env._LUPABLOCKS[name], 1, block)
  367. -- Handle the block properly.
  368. if not extends then
  369. if not env._LUPAEXTENDED then
  370. -- Evaluate the block normally.
  371. chunks[#chunks + 1] = evaluate(block, env)
  372. else
  373. -- A child template is overriding this parent's named block. Evaluate
  374. -- the child's block and use it instead of the parent's.
  375. local blocks = env._LUPABLOCKS[name]
  376. local super_env = setmetatable({super = function()
  377. -- Loop through the chain of defined blocks, evaluating from top to
  378. -- bottom, and return the bottom block. In each sub-block, the
  379. -- 'super' variable needs to point to the next-highest block's
  380. -- evaluated result.
  381. local super = evaluate(block, env) -- start with parent block
  382. local sub_env = setmetatable({super = function() return super end},
  383. {__index = env})
  384. for i = 1, #blocks - 1 do super = evaluate(blocks[i], sub_env) end
  385. return super
  386. end}, {__index = env})
  387. chunks[#chunks + 1] = evaluate(blocks[#blocks], super_env)
  388. end
  389. end
  390. elseif token == 'for' then
  391. local expr = block.expression
  392. local p = env._LUPAPOSITION -- mark position at beginning of expression
  393. -- Extract variable list and generator.
  394. local patt = '^([%w_,%s]+)%s+in%s+()(.+)%s+if%s+(.+)$'
  395. local var_list, pos, generator, if_expr = expr:match(patt)
  396. if not var_list then
  397. var_list, pos, generator = expr:match('^([%w_,%s]+)%s+in%s+()(.+)$')
  398. end
  399. env_assert(env, var_list and generator, 'invalid for expression')
  400. -- Store variable names in a list for loop assignment.
  401. local variables = {}
  402. for variable, pos in var_list:gmatch('([^,%s]+)()') do
  403. env._LUPAPOSITION = p + pos - 1 -- update position for error messages
  404. env_assert(env, variable:find('^[%a_]') and variable ~= 'loop',
  405. 'invalid variable name')
  406. variables[#variables + 1] = variable
  407. end
  408. -- Evaluate the generator and perform the iteration.
  409. env._LUPAPOSITION = p + pos - 1 -- update position to generator
  410. if not generator:find('|') then
  411. generator = env_assert(env, load('return '..generator, nil, nil, env))
  412. else
  413. local generator_expr = generator
  414. generator = function() return eval(generator_expr, env) end
  415. end
  416. local new_env = setmetatable({}, {__index = env})
  417. chunks[#chunks + 1] = iterate(generator, variables, if_expr, block,
  418. new_env, 1, ast[i + 4] == 'lstrip')
  419. elseif token == 'if' then
  420. if eval(block.expression, env) then
  421. chunks[#chunks + 1] = evaluate(block, env)
  422. else
  423. local elseifs = block['elseif']
  424. if elseifs then
  425. for j = 1, #elseifs do
  426. if eval(elseifs[j].expression, env) then
  427. chunks[#chunks + 1] = evaluate(elseifs[j], env)
  428. break
  429. end
  430. end
  431. elseif block['else'] then
  432. chunks[#chunks + 1] = evaluate(block['else'], env)
  433. end
  434. end
  435. elseif token == 'macro' then
  436. -- Parse the macro's name and parameter list.
  437. local signature = block.expression
  438. local name, param_list = signature:match('^([%w_]+)(%b())')
  439. env_assert(env, name and param_list, 'invalid macro expression')
  440. param_list = param_list:sub(2, -2)
  441. local p = env._LUPAPOSITION + #name + 1 -- mark pos at beginning of args
  442. local params, defaults = {}, {}
  443. for param, pos, default in param_list:gmatch('([%w_]+)=?()([^,]*)') do
  444. params[#params + 1] = param
  445. if default ~= '' then
  446. env._LUPAPOSITION = p + pos - 1 -- update position for error messages
  447. local f = env_assert(env, load('return '..default))
  448. defaults[param] = select(2, env_assert(env, pcall(f)))
  449. end
  450. end
  451. -- Create the function associated with the macro such that when the
  452. -- function is called (from within {{ ... }}), the macro's body is
  453. -- evaluated subject to an environment where parameter names are variables
  454. -- whose values are the ones passed to the macro itself.
  455. env[name] = function(...)
  456. local new_env = setmetatable({}, {__index = function(_, k)
  457. if k == 'caller' and type(env[k]) ~= 'function' then return nil end
  458. return env[k]
  459. end})
  460. local args = {...}
  461. -- Assign the given parameter values.
  462. for i = 1, #args do
  463. if i > #params then break end
  464. new_env[params[i]] = args[i]
  465. end
  466. -- Clear all other unspecified parameter values or set them to their
  467. -- defined defaults.
  468. for i = #args + 1, #params do
  469. new_env[params[i]] = defaults[params[i]]
  470. end
  471. -- Store extra parameters in "varargs" variable.
  472. new_env.varargs = {}
  473. for i = #params + 1, #args do
  474. new_env.varargs[#new_env.varargs + 1] = args[i]
  475. end
  476. local chunk = evaluate(block, new_env)
  477. if ast[i + 4] == 'lstrip' then chunk = chunk:gsub('%s*$', '') end
  478. return chunk
  479. end
  480. elseif token == 'call' then
  481. -- Parse the call block's parameter list (if any) and determine the macro
  482. -- to call.
  483. local param_list = block.expression:match('^(%b())')
  484. local params = {}
  485. if param_list then
  486. for param in param_list:gmatch('[%w_]+') do
  487. params[#params + 1] = param
  488. end
  489. end
  490. local macro = block.expression:match('^%b()(.+)$') or block.expression
  491. -- Evaluate the given macro, subject to a "caller" function that returns
  492. -- the contents of this call block. Any arguments passed to the caller
  493. -- function are used as values of this parameters parsed earlier.
  494. local old_caller = M.env.caller -- save
  495. M.env.caller = function(...)
  496. local new_env = setmetatable({}, {__index = env})
  497. local args = {...}
  498. -- Assign the given parameter values (if any).
  499. for i = 1, #args do new_env[params[i]] = args[i] end
  500. local chunk = evaluate(block, new_env)
  501. if ast[i + 4] == 'lstrip' then chunk = chunk:gsub('%s*$', '') end
  502. return chunk
  503. end
  504. chunks[#chunks + 1] = eval(macro, env)
  505. M.env.caller = old_caller -- restore
  506. elseif token == 'filter' then
  507. local text = evaluate(block, env)
  508. local p = env._LUPAPOSITION -- mark position at beginning of expression
  509. for pos, filter in each_filter(block.expression) do
  510. env._LUPAPOSITION = p + pos - 1 -- update position for error messages
  511. local name, params = filter:match('^%s*([%w_]+)%(?(.-)%)?%s*$')
  512. local f = M.filters[name]
  513. env_assert(env, f, 'unknown filter "'..name..'"')
  514. local args = env_assert(env, load('return {'..params..'}'),
  515. 'invalid filter parameter(s) for "'..name..
  516. '"')()
  517. text = select(2, env_assert(env, pcall(f, text, table.unpack(args))))
  518. end
  519. chunks[#chunks + 1] = text
  520. elseif token == 'set' then
  521. local var, expr = block:match('^([%a_][%w_]*)%s*=%s*(.+)$')
  522. env_assert(env, var and expr, 'invalid variable name or expression')
  523. env[var] = eval(expr, env)
  524. elseif token == 'do' then
  525. env_assert(env, pcall(env_assert(env, load(block, nil, nil, env))))
  526. elseif token == 'include' then
  527. -- Parse the include block for flags.
  528. local without_context = block:find('without%s+context%s*')
  529. local ignore_missing = block:find('ignore%s+missing%s*')
  530. block = block:gsub('witho?u?t?%s+context%s*', '')
  531. :gsub('ignore%s+missing%s*', '')
  532. -- Evaluate the include expression in order to determine the file to
  533. -- include. If the result is a table, use the first file that exists.
  534. local file = eval(block, env) -- covers strings and variables
  535. if type(file) == 'table' then
  536. local files = file
  537. for i = 1, #files do
  538. file = loader(files[i], env)
  539. if file then break end
  540. end
  541. if type(file) == 'table' then file = nil end
  542. elseif type(file) == 'string' then
  543. file = loader(file, env)
  544. else
  545. error('"include" requires a string or table of files')
  546. end
  547. -- If the file exists, include it. Otherwise throw an error unless the
  548. -- "ignore missing" flag was given.
  549. env_assert(env, file or ignore_missing, 'no file(s) found to include')
  550. if file then
  551. chunks[#chunks + 1] = M.expand_file(file, not without_context and env or
  552. M.env)
  553. end
  554. elseif token == 'import' then
  555. local file, global = block:match('^%s*(.+)%s+as%s+([%a][%w_]*)%s*')
  556. local new_env = setmetatable({}, {
  557. __index = block:find('with%s+context%s*$') and env or M.env
  558. })
  559. M.expand_file(eval(file or block, env), new_env)
  560. -- Copy any defined macros and variables over into the proper namespace.
  561. if global then env[global] = {} end
  562. local namespace = global and env[global] or env
  563. for k, v in pairs(new_env) do if not env[k] then namespace[k] = v end end
  564. elseif token == 'lstrip' and chunks[#chunks] then
  565. chunks[#chunks] = chunks[#chunks]:gsub('%s*$', '')
  566. elseif token == 'rstrip' then
  567. rstrip = true -- can only strip after determining the next chunk
  568. end
  569. if rstrip and token ~= 'rstrip' then
  570. chunks[#chunks] = chunks[#chunks]:gsub('^%s*', '')
  571. rstrip = false
  572. end
  573. end
  574. return not extends and table.concat(chunks) or M.expand_file(extends, env)
  575. end
  576. local pairs_gen, ipairs_gen = pairs({}), ipairs({})
  577. -- Iterates over the generator *generator* subject to string "if" expression
  578. -- *if_expr*, assigns that generator's returned values to the variable names
  579. -- listed in *variables* within template environment *env*, evaluates any
  580. -- expressions in *block* (a portion of a template's AST), and returns a
  581. -- concatenation of the results.
  582. -- @param generator Either a function that returns a generator function, or a
  583. -- table to iterate over. In the latter case, `ipairs()` is used as the
  584. -- generator function.
  585. -- @param variables List of variable names to assign values returned by
  586. -- *generator* to.
  587. -- @param if_expr A conditional expression that when `false`, skips the current
  588. -- loop item.
  589. -- @param block The portion inside the 'for' structure of a template's AST to
  590. -- iterate with.
  591. -- @param env The environment iteration variables are defined in and where
  592. -- expressions are evaluated in.
  593. -- @param depth The current recursion depth. Recursion is performed by calling
  594. -- `loop(t)` with a table to iterate over.
  595. -- @param lstrip Whether or not the "endfor" block strips whitespace on the
  596. -- left. When `true`, all blocks produced by iteration are left-stripped.
  597. iterate = function(generator, variables, if_expr, block, env, depth, lstrip)
  598. local chunks = {}
  599. local orig_variables = {} -- used to store original loop variables' values
  600. for i = 1, #variables do orig_variables[variables[i]] = env[variables[i]] end
  601. local i, n = 1 -- used for loop variables
  602. local _, s, v -- state variables
  603. if type(generator) == 'function' then
  604. _, generator, s, v = env_assert(env, pcall(generator))
  605. -- In practice, a generator's state variable is normally unused and hidden.
  606. -- This is not the case for 'pairs()' and 'ipairs', though.
  607. if variables[1] ~= '_index' and generator ~= pairs_gen and
  608. generator ~= ipairs_gen then
  609. table.insert(variables, 1, '_index')
  610. end
  611. end
  612. if type(generator) == 'table' then
  613. n = #generator
  614. generator, s, v = ipairs(generator)
  615. -- "for x in y" translates to "for _, x in ipairs(y)"; hide _ state variable
  616. if variables[1] ~= '_index' then table.insert(variables, 1, '_index') end
  617. end
  618. if generator then
  619. local first_results -- for preventing infinite loop from invalid generator
  620. while true do
  621. local results = {generator(s, v)}
  622. if results[1] == nil then break end
  623. -- If the results from the generator look like results returned by a
  624. -- generator itself (function, state, initial variable), verify last two
  625. -- results are different. If they are the same, then the original
  626. -- generator is invalid and will loop infinitely.
  627. if first_results == nil then
  628. first_results = #results == 3 and type(results[1]) == 'function' and
  629. results
  630. elseif first_results then
  631. env_assert(env, results[3] ~= first_results[3] or
  632. results[2] ~= first_results[2],
  633. 'invalid generator (infinite loop)')
  634. end
  635. -- Assign context variables and evaluate the body of the loop.
  636. -- As long as the result (ignoring the _index variable) is not a single
  637. -- table and there is only one loop variable defined (again, ignoring
  638. -- _index variable), assignment occurs as normal in Lua. Otherwise,
  639. -- unpacking on the table is done (like assignment to ...).
  640. if not (type(results[2]) == 'table' and #results == 2 and
  641. #variables > 2) then
  642. for j = 1, #variables do env[variables[j]] = results[j] end
  643. else
  644. for j = 2, #variables do env[variables[j]] = results[2][j - 1] end
  645. end
  646. if not if_expr or eval(if_expr, env) then
  647. env.loop = setmetatable({
  648. index = i, index0 = i - 1,
  649. revindex = n and n - (i - 1), revindex0 = n and n - i,
  650. first = i == 1, last = i == n, length = n,
  651. cycle = function(...)
  652. return select((i - 1) % select('#', ...) + 1, ...)
  653. end,
  654. depth = depth, depth0 = depth - 1
  655. }, {__call = function(_, t)
  656. return iterate(t, variables, if_expr, block, env, depth + 1, lstrip)
  657. end})
  658. chunks[#chunks + 1] = evaluate(block, env)
  659. if lstrip then chunks[#chunks] = chunks[#chunks]:gsub('%s*$', '') end
  660. i = i + 1
  661. end
  662. -- Prepare for next iteration.
  663. v = results[1]
  664. end
  665. end
  666. if i == 1 and block['else'] then
  667. chunks[#chunks + 1] = evaluate(block['else'], env)
  668. end
  669. for i = 1, #variables do env[variables[i]] = orig_variables[variables[i]] end
  670. return table.concat(chunks)
  671. end
  672. -- Expands string template *template* from source *source*, subject to template
  673. -- environment *env*, and returns the result.
  674. -- @param template String template to expand.
  675. -- @param env Environment for the given template.
  676. -- @param source Filename or identifier the template comes from for error
  677. -- messages and debugging.
  678. local function expand(template, env, source)
  679. template = template:gsub('\r?\n', newline_sequence) -- normalize
  680. if not keep_trailing_newline then template = template:gsub('\r?\n$', '') end
  681. -- Set up environment.
  682. if not env then env = {} end
  683. if not getmetatable(env) then env = setmetatable(env, {__index = M.env}) end
  684. env.self = setmetatable({}, {__index = function(_, k)
  685. env_assert(env, env._LUPABLOCKS and env._LUPABLOCKS[k],
  686. 'undefined block "'..k..'"')
  687. return function() return evaluate(env._LUPABLOCKS[k][1], env) end
  688. end})
  689. -- Set context variables and expand the template.
  690. env._LUPASOURCE, env._LUPAFILENAME = template, source
  691. M._FILENAME = source -- for lpeg errors only
  692. local ast = assert(lpeg.match(M.grammar, template), "internal error")
  693. local result = evaluate(ast, env)
  694. return result
  695. end
  696. ---
  697. -- Expands the string template *template*, subject to template environment
  698. -- *env*, and returns the result.
  699. -- @param template String template to expand.
  700. -- @param env Optional environment for the given template.
  701. -- @name expand
  702. function M.expand(template, env) return expand(template, env, '<string>') end
  703. ---
  704. -- Expands the template within file *filename*, subject to template environment
  705. -- *env*, and returns the result.
  706. -- @param filename Filename containing the template to expand.
  707. -- @param env Optional environment for the template to expand.
  708. -- @name expand_file
  709. function M.expand_file(filename, env)
  710. filename = loader(filename, env) or filename
  711. local f = (not env or not env._LUPASOURCE) and assert(io.open(filename)) or
  712. env_assert(env, io.open(filename))
  713. local template = f:read('*a')
  714. f:close()
  715. return expand(template, env, filename)
  716. end
  717. ---
  718. -- Returns a loader for templates that uses the filesystem starting at directory
  719. -- *directory*.
  720. -- When looking up the template for a given filename, the loader considers the
  721. -- following: if no template is being expanded, the loader assumes the given
  722. -- filename is relative to *directory* and returns the full path; otherwise the
  723. -- loader assumes the given filename is relative to the current template's
  724. -- directory and returns the full path.
  725. -- The returned path may be passed to `io.open()`.
  726. -- @param directory Optional the template root directory. The default value is
  727. -- ".", which is the current working directory.
  728. -- @name loaders.filesystem
  729. -- @see configure
  730. function M.loaders.filesystem(directory)
  731. return function(filename, env)
  732. if not filename then return nil end
  733. local current_dir = env and env._LUPAFILENAME and
  734. env._LUPAFILENAME:match('^(.+)[/\\]')
  735. if not filename:find('^/') and not filename:find('^%a:[/\\]') then
  736. filename = (current_dir or directory or '.')..'/'..filename
  737. end
  738. local f = io.open(filename)
  739. if not f then return nil end
  740. f:close()
  741. return filename
  742. end
  743. end
  744. -- Globally defined functions.
  745. ---
  746. -- Returns a sequence of integers from *start* to *stop*, inclusive, in
  747. -- increments of *step*.
  748. -- The complete sequence is generated at once -- no generator is returned.
  749. -- @param start Optional number to start at. The default value is `1`.
  750. -- @param stop Number to stop at.
  751. -- @param step Optional increment between sequence elements. The default value
  752. -- is `1`.
  753. -- @name _G.range
  754. function range(start, stop, step)
  755. if not stop and not step then stop, start = start, 1 end
  756. if not step then step = 1 end
  757. local t = {}
  758. for i = start, stop, step do t[#t + 1] = i end
  759. return t
  760. end
  761. ---
  762. -- Returns an object that cycles through the given values by calls to its
  763. -- `next()` function.
  764. -- A `current` field contains the cycler's current value and a `reset()`
  765. -- function resets the cycler to its beginning.
  766. -- @param ... Values to cycle through.
  767. -- @usage c = cycler(1, 2, 3)
  768. -- @usage c:next(), c:next() --> 1, 2
  769. -- @usage c:reset() --> c.current == 1
  770. -- @name _G.cycler
  771. function cycler(...)
  772. local c = {...}
  773. c.n, c.i, c.current = #c, 1, c[1]
  774. function c:next()
  775. local current = self.current
  776. self.i = self.i + 1
  777. if self.i > self.n then self.i = 1 end
  778. self.current = self[self.i]
  779. return current
  780. end
  781. function c:reset() self.i, self.current = 1, self[1] end
  782. return c
  783. end
  784. -- Create the default sandbox environment for templates.
  785. local safe = {
  786. -- Lua globals.
  787. '_VERSION', 'ipairs', 'math', 'pairs', 'select', 'tonumber', 'tostring',
  788. 'type', 'bit32', 'os.date', 'os.time', 'string', 'table', 'utf8',
  789. -- Lupa globals.
  790. 'range', 'cycler'
  791. }
  792. local sandbox_env = setmetatable({}, {__index = M.tests})
  793. for i = 1, #safe do
  794. local v = safe[i]
  795. if not v:find('%.') then
  796. sandbox_env[v] = _G[v]
  797. else
  798. local mod, func = v:match('^([^.]+)%.(.+)$')
  799. if not sandbox_env[mod] then sandbox_env[mod] = {} end
  800. sandbox_env[mod][func] = _G[mod][func]
  801. end
  802. end
  803. sandbox_env._G = sandbox_env
  804. ---
  805. -- Resets Lupa's default delimiters, options, and environments to their
  806. -- original default values.
  807. -- @name reset
  808. function M.reset()
  809. M.configure('{%', '%}', '{{', '}}', '{#', '#}')
  810. M.env = setmetatable({}, {__index = sandbox_env})
  811. end
  812. M.reset()
  813. ---
  814. -- The default template environment.
  815. -- @class table
  816. -- @name env
  817. local env
  818. -- Lupa filters.
  819. ---
  820. -- Returns the absolute value of number *n*.
  821. -- @param n The number to compute the absolute value of.
  822. -- @name filters.abs
  823. M.filters.abs = math.abs
  824. -- Returns a table that, when indexed with an integer, indexes table *t* with
  825. -- that integer along with string *attribute*.
  826. -- This is used by filters that operate on particular attributes of table
  827. -- elements.
  828. -- @param t The table to index.
  829. -- @param attribute The additional attribute to index with.
  830. local function attr_accessor(t, attribute)
  831. return setmetatable({}, {__index = function(_, i)
  832. local value = t[i]
  833. attribute = tonumber(attribute) or attribute
  834. if type(attribute) == 'number' then return value[attribute] end
  835. for k in attribute:gmatch('[^.]+') do value = value[k] end
  836. return value
  837. end})
  838. end
  839. ---
  840. -- Returns a generator that produces all of the items in table *t* in batches
  841. -- of size *size*, filling any empty spaces with value *fill*.
  842. -- Combine this with the "list" filter to produce a list.
  843. -- @param t The table to split into batches.
  844. -- @param size The batch size.
  845. -- @param fill The value to use when filling in any empty space in the last
  846. -- batch.
  847. -- @usage expand('{% for i in {1, 2, 3}|batch(2, 0) %}{{ i|string }}
  848. -- {% endfor %}') --> {1, 2} {3, 0}
  849. -- @see filters.list
  850. -- @name filters.batch
  851. function M.filters.batch(t, size, fill)
  852. assert(t, 'input to filter "batch" was nil instead of a table')
  853. local n = #t
  854. return function(t, i)
  855. if i > n then return nil end
  856. local batch = {}
  857. for j = i, i + size - 1 do batch[j - i + 1] = t[j] end
  858. if i + size > n and fill then
  859. for j = n + 1, i + size - 1 do batch[#batch + 1] = fill end
  860. end
  861. return i + size, batch
  862. end, t, 1
  863. end
  864. ---
  865. -- Capitalizes string *s*.
  866. -- The first character will be uppercased, the others lowercased.
  867. -- @param s The string to capitalize.
  868. -- @usage expand('{{ "foo bar"|capitalize }}') --> Foo bar
  869. -- @name filters.capitalize
  870. function M.filters.capitalize(s)
  871. assert(s, 'input to filter "capitalize" was nil instead of a string')
  872. local first, rest = s:match('^(.)(.*)$')
  873. return first and first:upper()..rest:lower() or s
  874. end
  875. ---
  876. -- Centers string *s* within a string of length *width*.
  877. -- @param s The string to center.
  878. -- @param width The length of the centered string.
  879. -- @usage expand('{{ "foo"|center(9) }}') --> " foo "
  880. -- @name filters.center
  881. function M.filters.center(s, width)
  882. assert(s, 'input to filter "center" was nil instead of a string')
  883. local padding = (width or 80) - #s
  884. local left, right = math.ceil(padding / 2), math.floor(padding / 2)
  885. return ("%s%s%s"):format((' '):rep(left), s, (' '):rep(right))
  886. end
  887. ---
  888. -- Returns value *value* or value *default*, depending on whether or not *value*
  889. -- is "true" and whether or not boolean *false_defaults* is `true`.
  890. -- @param value The value return if "true" or if `false` and *false_defaults*
  891. -- is `true`.
  892. -- @param default The value to return if *value* is `nil` or `false` (the latter
  893. -- applies only if *false_defaults* is `true`).
  894. -- @param false_defaults Optional flag indicating whether or not to return
  895. -- *default* if *value* is `false`. The default value is `false`.
  896. -- @usage expand('{{ false|default("no") }}') --> false
  897. -- @usage expand('{{ false|default("no", true) }') --> no
  898. -- @name filters.default
  899. function M.filters.default(value, default, false_defaults)
  900. if value == nil or false_defaults and not value then return default end
  901. return value
  902. end
  903. ---
  904. -- Returns a table constructed from table *t* such that each element is a list
  905. -- that contains a single key-value pair and all elements are sorted according
  906. -- to string *by* (which is either "key" or "value") and boolean
  907. -- *case_sensitive*.
  908. -- @param value The table to sort.
  909. -- @param case_sensitive Optional flag indicating whether or not to consider
  910. -- case when sorting string values. The default value is `false`.
  911. -- @param by Optional string that specifies which of the key-value to sort by,
  912. -- either "key" or "value". The default value is `"key"`.
  913. -- @usage expand('{{ {b = 1, a = 2}|dictsort|string }}') --> {{"a", 2},
  914. -- {"b", 1}}
  915. -- @name filters.dictsort
  916. function M.filters.dictsort(t, case_sensitive, by)
  917. assert(t, 'input to filter "dictsort" was nil instead of a table')
  918. assert(not by or by == 'key' or by == 'value',
  919. 'filter "dictsort" can only sort tables by "key" or "value"')
  920. local i = (not by or by == 'key') and 1 or 2
  921. local items = {}
  922. for k, v in pairs(t) do items[#items + 1] = {k, v} end
  923. table.sort(items, function(a, b)
  924. a, b = a[i], b[i]
  925. if not case_sensitive then
  926. if type(a) == 'string' then a = a:lower() end
  927. if type(b) == 'string' then b = b:lower() end
  928. end
  929. return a < b
  930. end)
  931. return items
  932. end
  933. ---
  934. -- Returns an HTML-safe copy of string *s*.
  935. -- @param s String to ensure is HTML-safe.
  936. -- @usage expand([[{{ '<">&'|e}}]]) --> &lt;&#34;&gt;&amp;
  937. -- @name filters.escape
  938. function M.filters.escape(s)
  939. assert(s, 'input to filter "escape" was nil instead of a string')
  940. return s:gsub('[<>"\'&]', {
  941. ['<'] = '&lt;', ['>'] = '&gt;', ['"'] = '&#34;', ["'"] = '&#39;',
  942. ['&'] = '&amp;'
  943. })
  944. end
  945. ---
  946. -- Returns an HTML-safe copy of string *s*.
  947. -- @param s String to ensure is HTML-safe.
  948. -- @usage expand([[{{ '<">&'|escape}}]]) --> &lt;&#34;&gt;&amp;
  949. -- @name filters.e
  950. function M.filters.e(s)
  951. assert(s, 'input to filter "e" was nil instead of a string')
  952. return M.filters.escape(s)
  953. end
  954. ---
  955. -- Returns a human-readable, decimal (or binary, depending on boolean *binary*)
  956. -- file size for *bytes* number of bytes.
  957. -- @param bytes The number of bytes to return the size for.
  958. -- @param binary Flag indicating whether or not to report binary file size
  959. -- as opposed to decimal file size. The default value is `false`.
  960. -- @usage expand('{{ 1000|filesizeformat }}') --> 1.0 kB
  961. -- @name filters.filesizeformat
  962. function M.filters.filesizeformat(bytes, binary)
  963. assert(bytes, 'input to filter "filesizeformat" was nil instead of a number')
  964. local base = binary and 1024 or 1000
  965. local units = {
  966. binary and 'KiB' or 'kB', binary and 'MiB' or 'MB',
  967. binary and 'GiB' or 'GB', binary and 'TiB' or 'TB',
  968. binary and 'PiB' or 'PB', binary and 'EiB' or 'EB',
  969. binary and 'ZiB' or 'ZB', binary and 'YiB' or 'YB'
  970. }
  971. if bytes < base then
  972. return string.format('%d Byte%s', bytes, bytes > 1 and 's' or '')
  973. else
  974. local limit, unit
  975. for i = 1, #units do
  976. limit, unit = base^(i + 1), units[i]
  977. if bytes < limit then break end
  978. end
  979. return string.format('%.1f %s', (base * bytes / limit), unit)
  980. end
  981. end
  982. ---
  983. -- Returns the first element in table *t*.
  984. -- @param t The table to get the first element of.
  985. -- @usage expand('{{ range(10)|first }}') --> 1
  986. -- @name filters.first
  987. function M.filters.first(t)
  988. assert(t, 'input to filter "first" was nil instead of a table')
  989. return t[1]
  990. end
  991. ---
  992. -- Returns value *value* as a float.
  993. -- This filter only works in Lua 5.3, which has a distinction between floats and
  994. -- integers.
  995. -- @param value The value to interpret as a float.
  996. -- @usage expand('{{ 42|float }}') --> 42.0
  997. -- @name filters.float
  998. function M.filters.float(value)
  999. assert(value, 'input to filter "float" was nil instead of a number')
  1000. return (tonumber(value) or 0) * 1.0
  1001. end
  1002. ---
  1003. -- Returns an HTML-safe copy of value *value*, even if *value* was returned by
  1004. -- the "safe" filter.
  1005. -- @param value Value to ensure is HTML-safe.
  1006. -- @usage expand('{% set x = "<div />"|safe %}{{ x|forceescape }}') -->
  1007. -- &lt;div /&gt;
  1008. -- @name filters.forceescape
  1009. function M.filters.forceescape(value)
  1010. assert(value, 'input to filter "forceescape" was nil instead of a string')
  1011. return M.filters.escape(tostring(value))
  1012. end
  1013. ---
  1014. -- Returns the given arguments formatted according to string *s*.
  1015. -- See Lua's `string.format()` for more information.
  1016. -- @param s The string to format subsequent arguments according to.
  1017. -- @param ... Arguments to format.
  1018. -- @usage expand('{{ "%s,%s"|format("a", "b") }}') --> a,b
  1019. -- @name filters.format
  1020. function M.filters.format(s, ...)
  1021. assert(s, 'input to filter "format" was nil instead of a string')
  1022. return string.format(s, ...)
  1023. end
  1024. ---
  1025. -- Returns a generator that produces lists of items in table *t* grouped by
  1026. -- string attribute *attribute*.
  1027. -- @param t The table to group items from.
  1028. -- @param attribute The attribute of items in the table to group by. This may
  1029. -- be nested (e.g. "foo.bar" groups by t[i].foo.bar for all i).
  1030. -- @usage expand('{% for age, group in people|groupby("age") %}...{% endfor %}')
  1031. -- @name filters.groupby
  1032. function M.filters.groupby(t, attribute)
  1033. assert(t, 'input to filter "groupby" was nil instead of a table')
  1034. local n = #t
  1035. local seen = {} -- keep track of groupers in order to avoid duplicates
  1036. return function(t, i)
  1037. if i > n then return nil end
  1038. local ta = attr_accessor(t, attribute)
  1039. -- Determine the next grouper.
  1040. local grouper = ta[i]
  1041. while seen[grouper] do
  1042. i = i + 1
  1043. if i > n then return nil end
  1044. grouper = ta[i]
  1045. end
  1046. seen[grouper] = true
  1047. -- Create and return the group.
  1048. local group = {}
  1049. for j = i, #t do if ta[j] == grouper then group[#group + 1] = t[j] end end
  1050. return i + 1, grouper, group
  1051. end, t, 1
  1052. end
  1053. ---
  1054. -- Returns a copy of string *s* with all lines after the first indented by
  1055. -- *width* number of spaces.
  1056. -- If boolean *first_line* is `true`, indents the first line as well.
  1057. -- @param s The string to indent lines of.
  1058. -- @param width The number of spaces to indent lines with.
  1059. -- @param first_line Optional flag indicating whether or not to indent the
  1060. -- first line of text. The default value is `false`.
  1061. -- @usage expand('{{ "foo\nbar"|indent(2) }}') --> "foo\n bar"
  1062. -- @name filters.indent
  1063. function M.filters.indent(s, width, first_line)
  1064. assert(s, 'input to filter "indent" was nil instead of a string')
  1065. local indent = (' '):rep(width)
  1066. return (first_line and indent or '')..s:gsub('([\r\n]+)', '%1'..indent)
  1067. end
  1068. ---
  1069. -- Returns value *value* as an integer.
  1070. -- @param value The value to interpret as an integer.
  1071. -- @usage expand('{{ 32.32|int }}') --> 32
  1072. -- @name filters.int
  1073. function M.filters.int(value)
  1074. assert(value, 'input to filter "int" was nil instead of a number')
  1075. return math.floor(tonumber(value) or 0)
  1076. end
  1077. ---
  1078. -- Returns a string that contains all the elements in table *t* (or all the
  1079. -- attributes named *attribute* in *t*) separated by string *sep*.
  1080. -- @param t The table to join.
  1081. -- @param sep The string to separate table elements with.
  1082. -- @param attribute Optional attribute of elements to use for joining instead
  1083. -- of the elements themselves. This may be nested (e.g. "foo.bar" joins
  1084. -- `t[i].foo.bar` for all i).
  1085. -- @usage expand('{{ {1, 2, 3}|join("|") }}') --> 1|2|3
  1086. -- @name filters.join
  1087. function M.filters.join(t, sep, attribute)
  1088. assert(t, 'input to filter "join" was nil instead of a table')
  1089. if not attribute then
  1090. local strings = {}
  1091. for i = 1, #t do strings[#strings + 1] = tostring(t[i]) end
  1092. return table.concat(strings, sep)
  1093. end
  1094. local ta = attr_accessor(t, attribute)
  1095. local attributes = {}
  1096. for i = 1, #t do attributes[#attributes + 1] = ta[i] end
  1097. return table.concat(attributes, sep)
  1098. end
  1099. ---
  1100. -- Returns the last element in table *t*.
  1101. -- @param t The table to get the last element of.
  1102. -- @usage expand('{{ range(10)|last }}') --> 10
  1103. -- @name filters.last
  1104. function M.filters.last(t)
  1105. assert(t, 'input to filter "last" was nil instead of a table')
  1106. return t[#t]
  1107. end
  1108. ---
  1109. -- Returns the length of string or table *value*.
  1110. -- @param value The value to get the length of.
  1111. -- @usage expand('{{ "hello world"|length }}') --> 11
  1112. -- @name filters.length
  1113. function M.filters.length(value)
  1114. assert(value, 'input to filter "length" was nil instead of a table or string')
  1115. return #value
  1116. end
  1117. ---
  1118. -- Returns the list of items produced by generator *generator*, subject to
  1119. -- initial state *s* and initial iterator variable *i*.
  1120. -- This filter should only be used after a filter that returns a generator.
  1121. -- @param generator Generator function that produces an item.
  1122. -- @param s Initial state for the generator.
  1123. -- @param i Initial iterator variable for the generator.
  1124. -- @usage expand('{{ range(4)|batch(2)|list|string }}') --> {{1, 2}, {3, 4}}
  1125. -- @see filters.batch
  1126. -- @see filters.groupby
  1127. -- @see filters.slice
  1128. -- @name filters.list
  1129. function M.filters.list(generator, s, i)
  1130. assert(type(generator) == 'function',
  1131. 'input to filter "list" must be a generator')
  1132. local list = {}
  1133. for _, v in generator, s, i do list[#list + 1] = v end
  1134. return list
  1135. end
  1136. ---
  1137. -- Returns a copy of string *s* with all lowercase characters.
  1138. -- @param s The string to lowercase.
  1139. -- @usage expand('{{ "FOO"|lower }}') --> foo
  1140. -- @name filters.lower
  1141. function M.filters.lower(s)
  1142. assert(s, 'input to filter "lower" was nil instead of a string')
  1143. return string.lower(s)
  1144. end
  1145. ---
  1146. -- Maps each element of table *t* to a value produced by filter name *filter*
  1147. -- and returns the resultant table.
  1148. -- @param t The table of elements to map.
  1149. -- @param filter The name of the filter to pass table elements through.
  1150. -- @param ... Any arguments for the filter.
  1151. -- @usage expand('{{ {"1", "2", "3"}|map("int")|sum }}') --> 6
  1152. -- @name filters.map
  1153. function M.filters.map(t, filter, ...)
  1154. assert(t, 'input to filter "map" was nil instead of a table')
  1155. local f = M.filters[filter]
  1156. assert(f, 'unknown filter "'..filter..'"')
  1157. local map = {}
  1158. for i = 1, #t do map[i] = f(t[i], ...) end
  1159. return map
  1160. end
  1161. ---
  1162. -- Maps the value of each element's string *attribute* in table *t* to the
  1163. -- value produced by filter name *filter* and returns the resultant table.
  1164. -- @param t The table of elements with attributes to map.
  1165. -- @param attribute The attribute of elements in the table to filter. This may
  1166. -- be nested (e.g. "foo.bar" maps t[i].foo.bar for all i).
  1167. -- @param filter The name of the filter to pass table elements through.
  1168. -- @param ... Any arguments for the filter.
  1169. -- @usage expand('{{ users|mapattr("name")|join("|") }}')
  1170. -- @name filters.mapattr
  1171. function M.filters.mapattr(t, attribute, filter, ...)
  1172. assert(t, 'input to filter "mapattr" was nil instead of a table')
  1173. local ta = attr_accessor(t, attribute)
  1174. local f = M.filters[filter]
  1175. if filter then
  1176. assert(f, 'unknown filter "'..filter..'" given to filter "mapattr"')
  1177. end
  1178. local map = {}
  1179. for i = 1, #t do map[i] = filter and f(ta[i], ...) or ta[i] end
  1180. return map
  1181. end
  1182. ---
  1183. -- Returns a random element from table *t*.
  1184. -- @param t The table to get a random element from.
  1185. -- @usage expand('{{ range(100)|random }}')
  1186. -- @name filters.random
  1187. function M.filters.random(t)
  1188. assert(t, 'input to filter "random" was nil instead of a table')
  1189. math.randomseed(os.time())
  1190. return t[math.random(#t)]
  1191. end
  1192. ---
  1193. -- Returns a list of elements in table *t* that fail test name *test*.
  1194. -- @param t The table of elements to reject from.
  1195. -- @param test The name of the test to use on table elements.
  1196. -- @param ... Any arguments for the test.
  1197. -- @usage expand('{{ range(5)|reject(is_odd)|join("|") }}') --> 2|4
  1198. -- @name filters.reject
  1199. function M.filters.reject(t, test, ...)
  1200. assert(t, 'input to filter "reject" was nil instead of a table')
  1201. local f = test or function(value) return not not value end
  1202. local items = {}
  1203. for i = 1, #t do if not f(t[i], ...) then items[#items + 1] = t[i] end end
  1204. return items
  1205. end
  1206. ---
  1207. -- Returns a list of elements in table *t* whose string attribute *attribute*
  1208. -- fails test name *test*.
  1209. -- @param t The table of elements to reject from.
  1210. -- @param attribute The attribute of items in the table to reject from. This
  1211. -- may be nested (e.g. "foo.bar" tests t[i].foo.bar for all i).
  1212. -- @param test The name of the test to use on table elements.
  1213. -- @param ... Any arguments for the test.
  1214. -- @usage expand('{{ users|rejectattr("offline")|mapattr("name")|join(",") }}')
  1215. -- @name filters.rejectattr
  1216. function M.filters.rejectattr(t, attribute, test, ...)
  1217. assert(t, 'input to filter "rejectattr" was nil instead of a table')
  1218. local ta = attr_accessor(t, attribute)
  1219. local f = test or function(value) return not not value end
  1220. local items = {}
  1221. for i = 1, #t do if not f(ta[i], ...) then items[#items + 1] = t[i] end end
  1222. return items
  1223. end
  1224. ---
  1225. -- Returns a copy of string *s* with all (or up to *n*) occurrences of string
  1226. -- *old* replaced by string *new*.
  1227. -- Identical to Lua's `string.gsub()` and handles Lua patterns.
  1228. -- @param s The subject string.
  1229. -- @param pattern The string or Lua pattern to replace.
  1230. -- @param repl The replacement text (may contain Lua captures).
  1231. -- @param n Optional number indicating the maximum number of replacements to
  1232. -- make. The default value is `nil`, which is unlimited.
  1233. -- @usage expand('{% filter upper|replace("FOO", "foo") %}foobar
  1234. -- {% endfilter %}') --> fooBAR
  1235. -- @name filters.replace
  1236. function M.filters.replace(s, pattern, repl, n)
  1237. assert(s, 'input to filter "replace" was nil instead of a string')
  1238. return string.gsub(s, pattern, repl, n)
  1239. end
  1240. ---
  1241. -- Returns a copy of the given string or table *value* in reverse order.
  1242. -- @param value The value to reverse.
  1243. -- @usage expand('{{ {1, 2, 3}|reverse|string }}') --> {3, 2, 1}
  1244. -- @name filters.reverse
  1245. function M.filters.reverse(value)
  1246. assert(type(value) == 'table' or type(value) == 'string',
  1247. 'input to filter "reverse" was nil instead of a table or string')
  1248. if type(value) == 'string' then return value:reverse() end
  1249. local t = {}
  1250. for i = 1, #value do t[i] = value[#value - i + 1] end
  1251. return t
  1252. end
  1253. ---
  1254. -- Returns number *value* rounded to *precision* decimal places based on string
  1255. -- *method* (if given).
  1256. -- @param value The number to round.
  1257. -- @param precision Optional precision to round the number to. The default
  1258. -- value is `0`.
  1259. -- @param method Optional string rounding method, either `"ceil"` or
  1260. -- `"floor"`. The default value is `nil`, which uses the common rounding
  1261. -- method (if a number's fractional part is 0.5 or greater, rounds up;
  1262. -- otherwise rounds down).
  1263. -- @usage expand('{{ 2.1236|round(3, "floor") }}') --> 2.123
  1264. -- @name filters.round
  1265. function M.filters.round(value, precision, method)
  1266. assert(value, 'input to filter "round" was nil instead of a number')
  1267. assert(not method or method == 'ceil' or method == 'floor',
  1268. 'rounding method given to filter "round" must be "ceil" or "floor"')
  1269. precision = precision or 0
  1270. method = method or (select(2, math.modf(value)) >= 0.5 and 'ceil' or 'floor')
  1271. local s = string.format('%.'..(precision >= 0 and precision or 0)..'f',
  1272. math[method](value * 10^precision) / 10^precision)
  1273. return tonumber(s)
  1274. end
  1275. ---
  1276. -- Marks string *s* as HTML-safe, preventing Lupa from modifying it when
  1277. -- configured to autoescape HTML entities.
  1278. -- This filter must be used at the end of a filter chain unless it is
  1279. -- immediately proceeded by the "forceescape" filter.
  1280. -- @param s The string to mark as HTML-safe.
  1281. -- @usage lupa.configure{autoescape = true}
  1282. -- @usage expand('{{ "<div>foo</div>"|safe }}') --> <div>foo</div>
  1283. -- @name filters.safe
  1284. function M.filters.safe(s)
  1285. assert(s, 'input to filter "safe" was nil instead of a string')
  1286. return setmetatable({}, {__tostring = function() return s end})
  1287. end
  1288. ---
  1289. -- Returns a list of the elements in table *t* that pass test name *test*.
  1290. -- @param t The table of elements to select from.
  1291. -- @param test The name of the test to use on table elements.
  1292. -- @param ... Any arguments for the test.
  1293. -- @usage expand('{{ range(5)|select(is_odd)|join("|") }}') --> 1|3|5
  1294. -- @name filters.select
  1295. function M.filters.select(t, test, ...)
  1296. assert(t, 'input to filter "select" was nil instead of a table')
  1297. local f = test or function(value) return not not value end
  1298. local items = {}
  1299. for i = 1, #t do if f(t[i], ...) then items[#items + 1] = t[i] end end
  1300. return items
  1301. end
  1302. ---
  1303. -- Returns a list of elements in table *t* whose string attribute *attribute*
  1304. -- passes test name *test*.
  1305. -- @param t The table of elements to select from.
  1306. -- @param attribute The attribute of items in the table to select from. This
  1307. -- may be nested (e.g. "foo.bar" tests t[i].foo.bar for all i).
  1308. -- @param test The name of the test to use on table elements.
  1309. -- @param ... Any arguments for the test.
  1310. -- @usage expand('{{ users|selectattr("online")|mapattr("name")|join("|") }}')
  1311. -- @name filters.selectattr
  1312. function M.filters.selectattr(t, attribute, test, ...)
  1313. assert(t, 'input to filter "selectattr" was nil instead of a table')
  1314. local ta = attr_accessor(t, attribute)
  1315. local f = test or function(value) return not not value end
  1316. local items = {}
  1317. for i = 1, #t do if f(ta[i], ...) then items[#items + 1] = t[i] end end
  1318. return items
  1319. end
  1320. ---
  1321. -- Returns a generator that produces all of the items in table *t* in *slices*
  1322. -- number of iterations, filling any empty spaces with value *fill*.
  1323. -- Combine this with the "list" filter to produce a list.
  1324. -- @param t The table to slice.
  1325. -- @param slices The number of slices to produce.
  1326. -- @param fill The value to use when filling in any empty space in the last
  1327. -- slice.
  1328. -- @usage expand('{% for i in {1, 2, 3}|slice(2, 0) %}{{ i|string }}
  1329. -- {% endfor %}') --> {1, 2} {3, 0}
  1330. -- @see filters.list
  1331. -- @name filters.slice
  1332. function M.filters.slice(t, slices, fill)
  1333. assert(t, 'input to filter "slice" was nil instead of a table')
  1334. local size, slices_with_extra = math.floor(#t / slices), #t % slices
  1335. return function(t, i)
  1336. if i > slices then return nil end
  1337. local slice = {}
  1338. local s = (i - 1) * size + math.min(i, slices_with_extra + 1)
  1339. local e = i * size + math.min(i, slices_with_extra)
  1340. for j = s, e do slice[j - s + 1] = t[j] end
  1341. if slices_with_extra > 0 and i > slices_with_extra and fill then
  1342. slice[#slice + 1] = fill
  1343. end
  1344. return i + 1, slice
  1345. end, t, 1
  1346. end
  1347. ---
  1348. -- Returns a copy of table or string *value* in sorted order by value (or by
  1349. -- an attribute named *attribute*), depending on booleans *reverse* and
  1350. -- *case_sensitive*.
  1351. -- @param value The table or string to sort.
  1352. -- @param reverse Optional flag indicating whether or not to sort in reverse
  1353. -- (descending) order. The default value is `false`, which sorts in ascending
  1354. -- order.
  1355. -- @param case_sensitive Optional flag indicating whether or not to consider
  1356. -- case when sorting string values. The default value is `false`.
  1357. -- @param attribute Optional attribute of elements to sort by instead of the
  1358. -- elements themselves.
  1359. -- @usage expand('{{ {2, 3, 1}|sort|string }}') --> {1, 2, 3}
  1360. -- @name filters.sort
  1361. function M.filters.sort(value, reverse, case_sensitive, attribute)
  1362. assert(value, 'input to filter "sort" was nil instead of a table or string')
  1363. assert(not attribute or type(attribute) == 'string' or
  1364. type(attribute) == 'number',
  1365. 'attribute to filter "sort" must be a string or number')
  1366. local t = {}
  1367. local sort_string = type(value) == 'string'
  1368. if not sort_string then
  1369. for i = 1, #value do t[#t + 1] = value[i] end
  1370. else
  1371. for char in value:gmatch('.') do t[#t + 1] = char end -- chars in string
  1372. end
  1373. table.sort(t, function(a, b)
  1374. if attribute then
  1375. if type(attribute) == 'number' then
  1376. a, b = a[attribute], b[attribute]
  1377. else
  1378. for k in attribute:gmatch('[^.]+') do a, b = a[k], b[k] end
  1379. end
  1380. end
  1381. if not case_sensitive then
  1382. if type(a) == 'string' then a = a:lower() end
  1383. if type(b) == 'string' then b = b:lower() end
  1384. end
  1385. if not reverse then
  1386. return a < b
  1387. else
  1388. return a > b
  1389. end
  1390. end)
  1391. return not sort_string and t or table.concat(t)
  1392. end
  1393. ---
  1394. -- Returns the string representation of value *value*, handling lists properly.
  1395. -- @param value Value to return the string representation of.
  1396. -- @usage expand('{{ {1 * 1, 2 * 2, 3 * 3}|string }}') --> {1, 4, 9}
  1397. -- @name filters.string
  1398. function M.filters.string(value)
  1399. if type(value) ~= 'table' then return tostring(value) end
  1400. local t = {}
  1401. for i = 1, #value do
  1402. local item = value[i]
  1403. t[i] = type(item) == 'string' and '"'..item..'"' or M.filters.string(item)
  1404. end
  1405. return '{'..table.concat(t, ', ')..'}'
  1406. end
  1407. ---
  1408. -- Returns a copy of string *s* with any HTML tags stripped.
  1409. -- Also cleans up whitespace.
  1410. -- @param s String to strip HTML tags from.
  1411. -- @usage expand('{{ "<div>foo</div>"|striptags }}') --> foo
  1412. -- @name filters.striptags
  1413. function M.filters.striptags(s)
  1414. assert(s, 'input to filter "striptags" was nil instead of a string')
  1415. return s:gsub('%b<>', ''):gsub('%s+', ' '):match('^%s*(.-)%s*$')
  1416. end
  1417. ---
  1418. -- Returns the numeric sum of the elements in table *t* or the sum of all
  1419. -- attributes named *attribute* in *t*.
  1420. -- @param t The table to calculate the sum of.
  1421. -- @param attribute Optional attribute of elements to use for summing instead
  1422. -- of the elements themselves. This may be nested (e.g. "foo.bar" sums
  1423. -- `t[i].foo.bar` for all i).
  1424. -- @usage expand('{{ range(6)|sum }}') --> 21
  1425. -- @name filters.sum
  1426. function M.filters.sum(t, attribute)
  1427. assert(t, 'input to filter "sum" was nil instead of a table')
  1428. local ta = attribute and attr_accessor(t, attribute) or t
  1429. local sum = 0
  1430. for i = 1, #t do sum = sum + ta[i] end
  1431. return sum
  1432. end
  1433. ---
  1434. -- Returns a copy of all words in string *s* in titlecase.
  1435. -- @param s The string to titlecase.
  1436. -- @usage expand('{{ "foo bar"|title }}') --> Foo Bar
  1437. -- @name filters.title
  1438. function M.filters.title(s)
  1439. assert(s, 'input to filter "title" was nil instead of a string')
  1440. return s:gsub('[^-%s]+', M.filters.capitalize)
  1441. end
  1442. ---
  1443. -- Returns a copy of string *s* truncated to *length* number of characters.
  1444. -- Truncated strings end with '...' or string *delimiter*. If boolean
  1445. -- *partial_words* is `false`, truncation will only happen at word boundaries.
  1446. -- @param s The string to truncate.
  1447. -- @param length The length to truncate the string to.
  1448. -- @param partial_words Optional flag indicating whether or not to allow
  1449. -- truncation within word boundaries. The default value is `false`.
  1450. -- @param delimiter Optional delimiter text. The default value is '...'.
  1451. -- @usage expand('{{ "foo bar"|truncate(4) }}') --> "foo ..."
  1452. -- @name filters.truncate
  1453. function M.filters.truncate(s, length, partial_words, delimiter)
  1454. assert(s, 'input to filter "truncate" was nil instead of a string')
  1455. if #s <= length then return s end
  1456. local truncated = s:sub(1, length)
  1457. if s:find('[%w_]', length) and not partial_words then
  1458. truncated = truncated:match('^(.-)[%w_]*$') -- drop partial word
  1459. end
  1460. return truncated..(delimiter or '...')
  1461. end
  1462. ---
  1463. -- Returns a copy of string *s* with all uppercase characters.
  1464. -- @param s The string to uppercase.
  1465. -- @usage expand('{{ "foo"|upper }}') --> FOO
  1466. -- @name filters.upper
  1467. function M.filters.upper(s)
  1468. assert(s, 'input to filter "upper" was nil instead of a string')
  1469. return string.upper(s)
  1470. end
  1471. ---
  1472. -- Returns a string suitably encoded to be used in a URL from value *value*.
  1473. -- *value* may be a string, table of key-value query parameters, or table of
  1474. -- lists of key-value query parameters (for order).
  1475. -- @param value Value to URL-encode.
  1476. -- @usage expand('{{ {{'f', 1}, {'z', 2}}|urlencode }}') --> f=1&z=2
  1477. -- @name filters.urlencode
  1478. function M.filters.urlencode(value)
  1479. assert(value,
  1480. 'input to filter "urlencode" was nil instead of a string or table')
  1481. if type(value) ~= 'table' then
  1482. return tostring(value):gsub('[^%w.-]', function(c)
  1483. return string.format('%%%X', string.byte(c))
  1484. end)
  1485. end
  1486. local params = {}
  1487. if #value > 0 then
  1488. for i = 1, #value do
  1489. local k = M.filters.urlencode(value[i][1])
  1490. local v = M.filters.urlencode(value[i][2])
  1491. params[#params + 1] = k..'='..v
  1492. end
  1493. else
  1494. for k, v in pairs(value) do
  1495. params[#params + 1] = M.filters.urlencode(k)..'='..M.filters.urlencode(v)
  1496. end
  1497. end
  1498. return table.concat(params, '&')
  1499. end
  1500. ---
  1501. -- Replaces any URLs in string *s* with HTML links, limiting link text to
  1502. -- *length* characters.
  1503. -- @param s The string to replace URLs with HTML links in.
  1504. -- @param length Optional maximum number of characters to include in link text.
  1505. -- The default value is `nil`, which imposes no limit.
  1506. -- @param nofollow Optional flag indicating whether or not HTML links will get a
  1507. -- "nofollow" attribute.
  1508. -- @usage expand('{{ "example.com"|urlize }}') -->
  1509. -- <a href="http://example.com">example.com</a>
  1510. -- @name filters.urlize
  1511. function M.filters.urlize(s, length, nofollow)
  1512. assert(s, 'input to filter "urlize" was nil instead of a string')
  1513. -- Trims the given url.
  1514. local function trim_url(url)
  1515. return length and s:sub(1, length)..(#s > length and '...' or '') or url
  1516. end
  1517. local nofollow_attr = nofollow and ' rel="nofollow"' or ''
  1518. local lead, trail = C((S('(<') + '&lt;')^0), C((S('.,)>\n') + '&gt;')^0) * -1
  1519. local middle = C((1 - trail)^0)
  1520. local patt = lpeg.Cs(lead * middle * trail / function(lead, middle, trail)
  1521. local linked
  1522. if middle:find('^www%.') or (not middle:find('@') and
  1523. not middle:find('^https?://') and
  1524. #middle > 0 and middle:find('^%w') and (
  1525. middle:find('%.com$') or
  1526. middle:find('%.net$') or
  1527. middle:find('%.org$')
  1528. )) then
  1529. middle, linked = string.format('<a href="http://%s"%s>%s</a>', middle,
  1530. nofollow_attr, trim_url(middle)), true
  1531. end
  1532. if middle:find('^https?://') then
  1533. middle, linked = string.format('<a href="%s"%s>%s</a>', middle,
  1534. nofollow_attr, trim_url(middle)), true
  1535. end
  1536. if middle:find('@') and not middle:find('^www%.') and
  1537. not middle:find(':') and middle:find('^%S+@[%w._-]+%.[%w._-]+$') then
  1538. middle, linked = string.format('<a href="mailto:%s">%s</a>', middle,
  1539. middle), true
  1540. end
  1541. if linked then return lead..middle..trail end
  1542. end)
  1543. return M.filters.escape(s):gsub('%S+', function(word)
  1544. return lpeg.match(patt, word)
  1545. end)
  1546. end
  1547. ---
  1548. -- Returns the number of words in string *s*.
  1549. -- A word is a sequence of non-space characters.
  1550. -- @param s The string to count words in.
  1551. -- @usage expand('{{ "foo bar baz"|wordcount }}') --> 3
  1552. -- @name filters.wordcount
  1553. function M.filters.wordcount(s)
  1554. assert(s, 'input to filter "wordcount" was nil instead of a string')
  1555. return select(2, s:gsub('%S+', ''))
  1556. end
  1557. ---
  1558. -- Interprets table *t* as a list of XML attribute-value pairs, returning them
  1559. -- as a properly formatted, space-separated string.
  1560. -- @param t The table of XML attribute-value pairs.
  1561. -- @usage expand('<data {{ {foo = 42, bar = 23}|xmlattr }} />')
  1562. -- @name filters.xmlattr
  1563. function M.filters.xmlattr(t)
  1564. assert(t, 'input to filter "xmlattr" was nil instead of a table')
  1565. local attributes = {}
  1566. for k, v in pairs(t) do
  1567. attributes[#attributes + 1] = string.format('%s="%s"', k,
  1568. M.filters.escape(tostring(v)))
  1569. end
  1570. return table.concat(attributes, ' ')
  1571. end
  1572. -- Lupa tests.
  1573. ---
  1574. -- Returns whether or not number *n* is odd.
  1575. -- @param n The number to test.
  1576. -- @usage expand('{% for x in range(10) if is_odd(x) %}...{% endif %}')
  1577. -- @name tests.is_odd
  1578. function M.tests.is_odd(n) return n % 2 == 1 end
  1579. ---
  1580. -- Returns whether or not number *n* is even.
  1581. -- @param n The number to test.
  1582. -- @usage expand('{% for x in range(10) if is_even(x) %}...{% endif %}')
  1583. -- @name tests.is_even
  1584. function M.tests.is_even(n) return n % 2 == 0 end
  1585. ---
  1586. -- Returns whether or not number *n* is evenly divisible by number *num*.
  1587. -- @param n The dividend to test.
  1588. -- @param num The divisor to use.
  1589. -- @usage expand('{% if is_divisibleby(x, y) %}...{% endif %}')
  1590. -- @name tests.is_divisibleby
  1591. function M.tests.is_divisibleby(n, num) return n % num == 0 end
  1592. ---
  1593. -- Returns whether or not value *value* is non-nil, and thus defined.
  1594. -- @param value The value to test.
  1595. -- @usage expand('{% if is_defined(x) %}...{% endif %}')
  1596. -- @name tests.is_defined
  1597. function M.tests.is_defined(value) return value ~= nil end
  1598. ---
  1599. -- Returns whether or not value *value* is nil, and thus effectively undefined.
  1600. -- @param value The value to test.
  1601. -- @usage expand('{% if is_undefined(x) %}...{% endif %}')
  1602. -- @name tests.is_undefined
  1603. function M.tests.is_undefined(value) return value == nil end
  1604. ---
  1605. -- Returns whether or not value *value* is nil.
  1606. -- @param value The value to test.
  1607. -- @usage expand('{% if is_none(x) %}...{% endif %}')
  1608. -- @name tests.is_none
  1609. function M.tests.is_none(value) return value == nil end
  1610. ---
  1611. -- Returns whether or not value *value* is nil.
  1612. -- @param value The value to test.
  1613. -- @usage expand('{% if is_nil(x) %}...{% endif %}')
  1614. -- @name tests.is_nil
  1615. function M.tests.is_nil(value) return value == nil end
  1616. ---
  1617. -- Returns whether or not string *s* is in all lower-case characters.
  1618. -- @param s The string to test.
  1619. -- @usage expand('{% if is_lower(s) %}...{% endif %}')
  1620. -- @name tests.is_lower
  1621. function M.tests.is_lower(s) return s:lower() == s end
  1622. ---
  1623. -- Returns whether or not string *s* is in all upper-case characters.
  1624. -- @param s The string to test.
  1625. -- @usage expand('{% if is_upper(s) %}...{% endif %}')
  1626. -- @name tests.is_upper
  1627. function M.tests.is_upper(s) return s:upper() == s end
  1628. ---
  1629. -- Returns whether or not value *value* is a string.
  1630. -- @param value The value to test.
  1631. -- @usage expand('{% if is_string(x) %}...{% endif %}')
  1632. -- @name tests.is_string
  1633. function M.tests.is_string(value) return type(value) == 'string' end
  1634. ---
  1635. -- Returns whether or not value *value* is a table.
  1636. -- @param value The value to test.
  1637. -- @usage expand('{% if is_mapping(x) %}...{% endif %}')
  1638. -- @name tests.is_mapping
  1639. function M.tests.is_mapping(value) return type(value) == 'table' end
  1640. ---
  1641. -- Returns whether or not value *value* is a table.
  1642. -- @param value The value to test.
  1643. -- @usage expand('{% if is_table(x) %}...{% endif %}')
  1644. -- @name tests.is_table
  1645. function M.tests.is_table(value) return type(value) == 'table' end
  1646. ---
  1647. -- Returns whether or not value *value* is a number.
  1648. -- @param value The value to test.
  1649. -- @usage expand('{% if is_number(x) %}...{% endif %}')
  1650. -- @name tests.is_number
  1651. function M.tests.is_number(value) return type(value) == 'number' end
  1652. ---
  1653. -- Returns whether or not value *value* is a sequence, namely a table with
  1654. -- non-zero length.
  1655. -- @param value The value to test.
  1656. -- @usage expand('{% if is_sequence(x) %}...{% endif %}')
  1657. -- @name tests.is_sequence
  1658. function M.tests.is_sequence(value)
  1659. return type(value) == 'table' and #value > 0
  1660. end
  1661. ---
  1662. -- Returns whether or not value *value* is a sequence (a table with non-zero
  1663. -- length) or a generator.
  1664. -- At the moment, all functions are considered generators.
  1665. -- @param value The value to test.
  1666. -- @usage expand('{% if is_iterable(x) %}...{% endif %}')
  1667. -- @name tests.is_iterable
  1668. function M.tests.is_iterable(value)
  1669. return M.tests.is_sequence(value) or type(value) == 'function'
  1670. end
  1671. ---
  1672. -- Returns whether or not value *value* is a function.
  1673. -- @param value The value to test.
  1674. -- @usage expand('{% if is_callable(x) %}...{% endif %}')
  1675. -- @name tests.is_callable
  1676. function M.tests.is_callable(value) return type(value) == 'function' end
  1677. ---
  1678. -- Returns whether or not value *value* is the same as value *other*.
  1679. -- @param value The value to test.
  1680. -- @param other The value to compare with.
  1681. -- @usage expand('{% if is_sameas(x, y) %}...{% endif %}')
  1682. -- @name tests.is_sameas
  1683. function M.tests.is_sameas(value, other) return value == other end
  1684. ---
  1685. -- Returns whether or not value *value* is HTML-safe.
  1686. -- @param value The value to test.
  1687. -- @usage expand('{% if is_escaped(x) %}...{% endif %}')
  1688. -- @name tests.is_escaped
  1689. function M.tests.is_escaped(value)
  1690. return getmetatable(value) and getmetatable(value).__tostring ~= nil
  1691. end
  1692. return M