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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  1. -- Copyright 2015-2020 Mitchell. 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 evaluate_else = true
  424. local elseifs = block['elseif']
  425. if elseifs then
  426. for j = 1, #elseifs do
  427. if eval(elseifs[j].expression, env) then
  428. chunks[#chunks + 1] = evaluate(elseifs[j], env)
  429. evaluate_else = false
  430. break
  431. end
  432. end
  433. end
  434. if evaluate_else and block['else'] then
  435. chunks[#chunks + 1] = evaluate(block['else'], env)
  436. end
  437. end
  438. elseif token == 'macro' then
  439. -- Parse the macro's name and parameter list.
  440. local signature = block.expression
  441. local name, param_list = signature:match('^([%w_]+)(%b())')
  442. env_assert(env, name and param_list, 'invalid macro expression')
  443. param_list = param_list:sub(2, -2)
  444. local p = env._LUPAPOSITION + #name + 1 -- mark pos at beginning of args
  445. local params, defaults = {}, {}
  446. for param, pos, default in param_list:gmatch('([%w_]+)=?()([^,]*)') do
  447. params[#params + 1] = param
  448. if default ~= '' then
  449. env._LUPAPOSITION = p + pos - 1 -- update position for error messages
  450. local f = env_assert(env, load('return '..default))
  451. defaults[param] = select(2, env_assert(env, pcall(f)))
  452. end
  453. end
  454. -- Create the function associated with the macro such that when the
  455. -- function is called (from within {{ ... }}), the macro's body is
  456. -- evaluated subject to an environment where parameter names are variables
  457. -- whose values are the ones passed to the macro itself.
  458. env[name] = function(...)
  459. local new_env = setmetatable({}, {__index = function(_, k)
  460. if k == 'caller' and type(env[k]) ~= 'function' then return nil end
  461. return env[k]
  462. end})
  463. local args = {...}
  464. -- Assign the given parameter values.
  465. for i = 1, #args do
  466. if i > #params then break end
  467. new_env[params[i]] = args[i]
  468. end
  469. -- Clear all other unspecified parameter values or set them to their
  470. -- defined defaults.
  471. for i = #args + 1, #params do
  472. new_env[params[i]] = defaults[params[i]]
  473. end
  474. -- Store extra parameters in "varargs" variable.
  475. new_env.varargs = {}
  476. for i = #params + 1, #args do
  477. new_env.varargs[#new_env.varargs + 1] = args[i]
  478. end
  479. local chunk = evaluate(block, new_env)
  480. if ast[i + 4] == 'lstrip' then chunk = chunk:gsub('%s*$', '') end
  481. return chunk
  482. end
  483. elseif token == 'call' then
  484. -- Parse the call block's parameter list (if any) and determine the macro
  485. -- to call.
  486. local param_list = block.expression:match('^(%b())')
  487. local params = {}
  488. if param_list then
  489. for param in param_list:gmatch('[%w_]+') do
  490. params[#params + 1] = param
  491. end
  492. end
  493. local macro = block.expression:match('^%b()(.+)$') or block.expression
  494. -- Evaluate the given macro, subject to a "caller" function that returns
  495. -- the contents of this call block. Any arguments passed to the caller
  496. -- function are used as values of this parameters parsed earlier.
  497. local old_caller = M.env.caller -- save
  498. M.env.caller = function(...)
  499. local new_env = setmetatable({}, {__index = env})
  500. local args = {...}
  501. -- Assign the given parameter values (if any).
  502. for i = 1, #args do new_env[params[i]] = args[i] end
  503. local chunk = evaluate(block, new_env)
  504. if ast[i + 4] == 'lstrip' then chunk = chunk:gsub('%s*$', '') end
  505. return chunk
  506. end
  507. chunks[#chunks + 1] = eval(macro, env)
  508. M.env.caller = old_caller -- restore
  509. elseif token == 'filter' then
  510. local text = evaluate(block, env)
  511. local p = env._LUPAPOSITION -- mark position at beginning of expression
  512. for pos, filter in each_filter(block.expression) do
  513. env._LUPAPOSITION = p + pos - 1 -- update position for error messages
  514. local name, params = filter:match('^%s*([%w_]+)%(?(.-)%)?%s*$')
  515. local f = M.filters[name]
  516. env_assert(env, f, 'unknown filter "'..name..'"')
  517. local args = env_assert(env, load('return {'..params..'}'),
  518. 'invalid filter parameter(s) for "'..name..
  519. '"')()
  520. text = select(2, env_assert(env, pcall(f, text, table.unpack(args))))
  521. end
  522. chunks[#chunks + 1] = text
  523. elseif token == 'set' then
  524. local var, expr = block:match('^([%a_][%w_]*)%s*=%s*(.+)$')
  525. env_assert(env, var and expr, 'invalid variable name or expression')
  526. env[var] = eval(expr, env)
  527. elseif token == 'do' then
  528. env_assert(env, pcall(env_assert(env, load(block, nil, nil, env))))
  529. elseif token == 'include' then
  530. -- Parse the include block for flags.
  531. local without_context = block:find('without%s+context%s*')
  532. local ignore_missing = block:find('ignore%s+missing%s*')
  533. block = block:gsub('witho?u?t?%s+context%s*', '')
  534. :gsub('ignore%s+missing%s*', '')
  535. -- Evaluate the include expression in order to determine the file to
  536. -- include. If the result is a table, use the first file that exists.
  537. local file = eval(block, env) -- covers strings and variables
  538. if type(file) == 'table' then
  539. local files = file
  540. for i = 1, #files do
  541. file = loader(files[i], env)
  542. if file then break end
  543. end
  544. if type(file) == 'table' then file = nil end
  545. elseif type(file) == 'string' then
  546. file = loader(file, env)
  547. else
  548. error('"include" requires a string or table of files')
  549. end
  550. -- If the file exists, include it. Otherwise throw an error unless the
  551. -- "ignore missing" flag was given.
  552. env_assert(env, file or ignore_missing, 'no file(s) found to include')
  553. if file then
  554. chunks[#chunks + 1] = M.expand_file(file, not without_context and env or
  555. M.env)
  556. end
  557. elseif token == 'import' then
  558. local file, global = block:match('^%s*(.+)%s+as%s+([%a][%w_]*)%s*')
  559. local new_env = setmetatable({}, {
  560. __index = block:find('with%s+context%s*$') and env or M.env
  561. })
  562. M.expand_file(eval(file or block, env), new_env)
  563. -- Copy any defined macros and variables over into the proper namespace.
  564. if global then env[global] = {} end
  565. local namespace = global and env[global] or env
  566. for k, v in pairs(new_env) do if not env[k] then namespace[k] = v end end
  567. elseif token == 'lstrip' and chunks[#chunks] then
  568. chunks[#chunks] = chunks[#chunks]:gsub('%s*$', '')
  569. elseif token == 'rstrip' then
  570. rstrip = true -- can only strip after determining the next chunk
  571. end
  572. if rstrip and token ~= 'rstrip' then
  573. chunks[#chunks] = chunks[#chunks]:gsub('^%s*', '')
  574. rstrip = false
  575. end
  576. end
  577. return not extends and table.concat(chunks) or M.expand_file(extends, env)
  578. end
  579. local pairs_gen, ipairs_gen = pairs({}), ipairs({})
  580. -- Iterates over the generator *generator* subject to string "if" expression
  581. -- *if_expr*, assigns that generator's returned values to the variable names
  582. -- listed in *variables* within template environment *env*, evaluates any
  583. -- expressions in *block* (a portion of a template's AST), and returns a
  584. -- concatenation of the results.
  585. -- @param generator Either a function that returns a generator function, or a
  586. -- table to iterate over. In the latter case, `ipairs()` is used as the
  587. -- generator function.
  588. -- @param variables List of variable names to assign values returned by
  589. -- *generator* to.
  590. -- @param if_expr A conditional expression that when `false`, skips the current
  591. -- loop item.
  592. -- @param block The portion inside the 'for' structure of a template's AST to
  593. -- iterate with.
  594. -- @param env The environment iteration variables are defined in and where
  595. -- expressions are evaluated in.
  596. -- @param depth The current recursion depth. Recursion is performed by calling
  597. -- `loop(t)` with a table to iterate over.
  598. -- @param lstrip Whether or not the "endfor" block strips whitespace on the
  599. -- left. When `true`, all blocks produced by iteration are left-stripped.
  600. iterate = function(generator, variables, if_expr, block, env, depth, lstrip)
  601. local chunks = {}
  602. local orig_variables = {} -- used to store original loop variables' values
  603. for i = 1, #variables do orig_variables[variables[i]] = env[variables[i]] end
  604. local i, n = 1 -- used for loop variables
  605. local _, s, v -- state variables
  606. if type(generator) == 'function' then
  607. _, generator, s, v = env_assert(env, pcall(generator))
  608. -- In practice, a generator's state variable is normally unused and hidden.
  609. -- This is not the case for 'pairs()' and 'ipairs', though.
  610. if variables[1] ~= '_index' and generator ~= pairs_gen and
  611. generator ~= ipairs_gen then
  612. table.insert(variables, 1, '_index')
  613. end
  614. end
  615. if type(generator) == 'table' then
  616. n = #generator
  617. generator, s, v = ipairs(generator)
  618. -- "for x in y" translates to "for _, x in ipairs(y)"; hide _ state variable
  619. if variables[1] ~= '_index' then table.insert(variables, 1, '_index') end
  620. end
  621. if generator then
  622. local first_results -- for preventing infinite loop from invalid generator
  623. while true do
  624. local results = {generator(s, v)}
  625. if results[1] == nil then break end
  626. -- If the results from the generator look like results returned by a
  627. -- generator itself (function, state, initial variable), verify last two
  628. -- results are different. If they are the same, then the original
  629. -- generator is invalid and will loop infinitely.
  630. if first_results == nil then
  631. first_results = #results == 3 and type(results[1]) == 'function' and
  632. results
  633. elseif first_results then
  634. env_assert(env, results[3] ~= first_results[3] or
  635. results[2] ~= first_results[2],
  636. 'invalid generator (infinite loop)')
  637. end
  638. -- Assign context variables and evaluate the body of the loop.
  639. -- As long as the result (ignoring the _index variable) is not a single
  640. -- table and there is only one loop variable defined (again, ignoring
  641. -- _index variable), assignment occurs as normal in Lua. Otherwise,
  642. -- unpacking on the table is done (like assignment to ...).
  643. if not (type(results[2]) == 'table' and #results == 2 and
  644. #variables > 2) then
  645. for j = 1, #variables do env[variables[j]] = results[j] end
  646. else
  647. for j = 2, #variables do env[variables[j]] = results[2][j - 1] end
  648. end
  649. if not if_expr or eval(if_expr, env) then
  650. env.loop = setmetatable({
  651. index = i, index0 = i - 1,
  652. revindex = n and n - (i - 1), revindex0 = n and n - i,
  653. first = i == 1, last = i == n, length = n,
  654. cycle = function(...)
  655. return select((i - 1) % select('#', ...) + 1, ...)
  656. end,
  657. depth = depth, depth0 = depth - 1
  658. }, {__call = function(_, t)
  659. return iterate(t, variables, if_expr, block, env, depth + 1, lstrip)
  660. end})
  661. chunks[#chunks + 1] = evaluate(block, env)
  662. if lstrip then chunks[#chunks] = chunks[#chunks]:gsub('%s*$', '') end
  663. i = i + 1
  664. end
  665. -- Prepare for next iteration.
  666. v = results[1]
  667. end
  668. end
  669. if i == 1 and block['else'] then
  670. chunks[#chunks + 1] = evaluate(block['else'], env)
  671. end
  672. for i = 1, #variables do env[variables[i]] = orig_variables[variables[i]] end
  673. return table.concat(chunks)
  674. end
  675. -- Expands string template *template* from source *source*, subject to template
  676. -- environment *env*, and returns the result.
  677. -- @param template String template to expand.
  678. -- @param env Environment for the given template.
  679. -- @param source Filename or identifier the template comes from for error
  680. -- messages and debugging.
  681. local function expand(template, env, source)
  682. template = template:gsub('\r?\n', newline_sequence) -- normalize
  683. if not keep_trailing_newline then template = template:gsub('\r?\n$', '') end
  684. -- Set up environment.
  685. if not env then env = {} end
  686. if not getmetatable(env) then env = setmetatable(env, {__index = M.env}) end
  687. env.self = setmetatable({}, {__index = function(_, k)
  688. env_assert(env, env._LUPABLOCKS and env._LUPABLOCKS[k],
  689. 'undefined block "'..k..'"')
  690. return function() return evaluate(env._LUPABLOCKS[k][1], env) end
  691. end})
  692. -- Set context variables and expand the template.
  693. env._LUPASOURCE, env._LUPAFILENAME = template, source
  694. M._FILENAME = source -- for lpeg errors only
  695. local ast = assert(lpeg.match(M.grammar, template), "internal error")
  696. local result = evaluate(ast, env)
  697. return result
  698. end
  699. ---
  700. -- Expands the string template *template*, subject to template environment
  701. -- *env*, and returns the result.
  702. -- @param template String template to expand.
  703. -- @param env Optional environment for the given template.
  704. -- @name expand
  705. function M.expand(template, env) return expand(template, env, '<string>') end
  706. ---
  707. -- Expands the template within file *filename*, subject to template environment
  708. -- *env*, and returns the result.
  709. -- @param filename Filename containing the template to expand.
  710. -- @param env Optional environment for the template to expand.
  711. -- @name expand_file
  712. function M.expand_file(filename, env)
  713. filename = loader(filename, env) or filename
  714. local f = (not env or not env._LUPASOURCE) and assert(io.open(filename)) or
  715. env_assert(env, io.open(filename))
  716. local template = f:read('*a')
  717. f:close()
  718. return expand(template, env, filename)
  719. end
  720. ---
  721. -- Returns a loader for templates that uses the filesystem starting at directory
  722. -- *directory*.
  723. -- When looking up the template for a given filename, the loader considers the
  724. -- following: if no template is being expanded, the loader assumes the given
  725. -- filename is relative to *directory* and returns the full path; otherwise the
  726. -- loader assumes the given filename is relative to the current template's
  727. -- directory and returns the full path.
  728. -- The returned path may be passed to `io.open()`.
  729. -- @param directory Optional the template root directory. The default value is
  730. -- ".", which is the current working directory.
  731. -- @name loaders.filesystem
  732. -- @see configure
  733. function M.loaders.filesystem(directory)
  734. return function(filename, env)
  735. if not filename then return nil end
  736. local current_dir = env and env._LUPAFILENAME and
  737. env._LUPAFILENAME:match('^(.+)[/\\]')
  738. if not filename:find('^/') and not filename:find('^%a:[/\\]') then
  739. filename = (current_dir or directory or '.')..'/'..filename
  740. end
  741. local f = io.open(filename)
  742. if not f then return nil end
  743. f:close()
  744. return filename
  745. end
  746. end
  747. -- Globally defined functions.
  748. ---
  749. -- Returns a sequence of integers from *start* to *stop*, inclusive, in
  750. -- increments of *step*.
  751. -- The complete sequence is generated at once -- no generator is returned.
  752. -- @param start Optional number to start at. The default value is `1`.
  753. -- @param stop Number to stop at.
  754. -- @param step Optional increment between sequence elements. The default value
  755. -- is `1`.
  756. -- @name _G.range
  757. function range(start, stop, step)
  758. if not stop and not step then stop, start = start, 1 end
  759. if not step then step = 1 end
  760. local t = {}
  761. for i = start, stop, step do t[#t + 1] = i end
  762. return t
  763. end
  764. ---
  765. -- Returns an object that cycles through the given values by calls to its
  766. -- `next()` function.
  767. -- A `current` field contains the cycler's current value and a `reset()`
  768. -- function resets the cycler to its beginning.
  769. -- @param ... Values to cycle through.
  770. -- @usage c = cycler(1, 2, 3)
  771. -- @usage c:next(), c:next() --> 1, 2
  772. -- @usage c:reset() --> c.current == 1
  773. -- @name _G.cycler
  774. function cycler(...)
  775. local c = {...}
  776. c.n, c.i, c.current = #c, 1, c[1]
  777. function c:next()
  778. local current = self.current
  779. self.i = self.i + 1
  780. if self.i > self.n then self.i = 1 end
  781. self.current = self[self.i]
  782. return current
  783. end
  784. function c:reset() self.i, self.current = 1, self[1] end
  785. return c
  786. end
  787. -- Create the default sandbox environment for templates.
  788. local safe = {
  789. -- Lua globals.
  790. '_VERSION', 'ipairs', 'math', 'pairs', 'select', 'tonumber', 'tostring',
  791. 'type', 'bit32', 'os.date', 'os.time', 'string', 'table', 'utf8',
  792. -- Lupa globals.
  793. 'range', 'cycler'
  794. }
  795. local sandbox_env = setmetatable({}, {__index = M.tests})
  796. for i = 1, #safe do
  797. local v = safe[i]
  798. if not v:find('%.') then
  799. sandbox_env[v] = _G[v]
  800. else
  801. local mod, func = v:match('^([^.]+)%.(.+)$')
  802. if not sandbox_env[mod] then sandbox_env[mod] = {} end
  803. sandbox_env[mod][func] = _G[mod][func]
  804. end
  805. end
  806. sandbox_env._G = sandbox_env
  807. ---
  808. -- Resets Lupa's default delimiters, options, and environments to their
  809. -- original default values.
  810. -- @name reset
  811. function M.reset()
  812. M.configure('{%', '%}', '{{', '}}', '{#', '#}')
  813. M.env = setmetatable({}, {__index = sandbox_env})
  814. end
  815. M.reset()
  816. ---
  817. -- The default template environment.
  818. -- @class table
  819. -- @name env
  820. local env
  821. -- Lupa filters.
  822. ---
  823. -- Returns the absolute value of number *n*.
  824. -- @param n The number to compute the absolute value of.
  825. -- @name filters.abs
  826. M.filters.abs = math.abs
  827. -- Returns a table that, when indexed with an integer, indexes table *t* with
  828. -- that integer along with string *attribute*.
  829. -- This is used by filters that operate on particular attributes of table
  830. -- elements.
  831. -- @param t The table to index.
  832. -- @param attribute The additional attribute to index with.
  833. local function attr_accessor(t, attribute)
  834. return setmetatable({}, {__index = function(_, i)
  835. local value = t[i]
  836. attribute = tonumber(attribute) or attribute
  837. if type(attribute) == 'number' then return value[attribute] end
  838. for k in attribute:gmatch('[^.]+') do value = value[k] end
  839. return value
  840. end})
  841. end
  842. ---
  843. -- Returns a generator that produces all of the items in table *t* in batches
  844. -- of size *size*, filling any empty spaces with value *fill*.
  845. -- Combine this with the "list" filter to produce a list.
  846. -- @param t The table to split into batches.
  847. -- @param size The batch size.
  848. -- @param fill The value to use when filling in any empty space in the last
  849. -- batch.
  850. -- @usage expand('{% for i in {1, 2, 3}|batch(2, 0) %}{{ i|string }}
  851. -- {% endfor %}') --> {1, 2} {3, 0}
  852. -- @see filters.list
  853. -- @name filters.batch
  854. function M.filters.batch(t, size, fill)
  855. assert(t, 'input to filter "batch" was nil instead of a table')
  856. local n = #t
  857. return function(t, i)
  858. if i > n then return nil end
  859. local batch = {}
  860. for j = i, i + size - 1 do batch[j - i + 1] = t[j] end
  861. if i + size > n and fill then
  862. for j = n + 1, i + size - 1 do batch[#batch + 1] = fill end
  863. end
  864. return i + size, batch
  865. end, t, 1
  866. end
  867. ---
  868. -- Capitalizes string *s*.
  869. -- The first character will be uppercased, the others lowercased.
  870. -- @param s The string to capitalize.
  871. -- @usage expand('{{ "foo bar"|capitalize }}') --> Foo bar
  872. -- @name filters.capitalize
  873. function M.filters.capitalize(s)
  874. assert(s, 'input to filter "capitalize" was nil instead of a string')
  875. local first, rest = s:match('^(.)(.*)$')
  876. return first and first:upper()..rest:lower() or s
  877. end
  878. ---
  879. -- Centers string *s* within a string of length *width*.
  880. -- @param s The string to center.
  881. -- @param width The length of the centered string.
  882. -- @usage expand('{{ "foo"|center(9) }}') --> " foo "
  883. -- @name filters.center
  884. function M.filters.center(s, width)
  885. assert(s, 'input to filter "center" was nil instead of a string')
  886. local padding = (width or 80) - #s
  887. local left, right = math.ceil(padding / 2), math.floor(padding / 2)
  888. return ("%s%s%s"):format((' '):rep(left), s, (' '):rep(right))
  889. end
  890. ---
  891. -- Returns value *value* or value *default*, depending on whether or not *value*
  892. -- is "true" and whether or not boolean *false_defaults* is `true`.
  893. -- @param value The value return if "true" or if `false` and *false_defaults*
  894. -- is `true`.
  895. -- @param default The value to return if *value* is `nil` or `false` (the latter
  896. -- applies only if *false_defaults* is `true`).
  897. -- @param false_defaults Optional flag indicating whether or not to return
  898. -- *default* if *value* is `false`. The default value is `false`.
  899. -- @usage expand('{{ false|default("no") }}') --> false
  900. -- @usage expand('{{ false|default("no", true) }') --> no
  901. -- @name filters.default
  902. function M.filters.default(value, default, false_defaults)
  903. if value == nil or false_defaults and not value then return default end
  904. return value
  905. end
  906. ---
  907. -- Returns a table constructed from table *t* such that each element is a list
  908. -- that contains a single key-value pair and all elements are sorted according
  909. -- to string *by* (which is either "key" or "value") and boolean
  910. -- *case_sensitive*.
  911. -- @param value The table to sort.
  912. -- @param case_sensitive Optional flag indicating whether or not to consider
  913. -- case when sorting string values. The default value is `false`.
  914. -- @param by Optional string that specifies which of the key-value to sort by,
  915. -- either "key" or "value". The default value is `"key"`.
  916. -- @usage expand('{{ {b = 1, a = 2}|dictsort|string }}') --> {{"a", 2},
  917. -- {"b", 1}}
  918. -- @name filters.dictsort
  919. function M.filters.dictsort(t, case_sensitive, by)
  920. assert(t, 'input to filter "dictsort" was nil instead of a table')
  921. assert(not by or by == 'key' or by == 'value',
  922. 'filter "dictsort" can only sort tables by "key" or "value"')
  923. local i = (not by or by == 'key') and 1 or 2
  924. local items = {}
  925. for k, v in pairs(t) do items[#items + 1] = {k, v} end
  926. table.sort(items, function(a, b)
  927. a, b = a[i], b[i]
  928. if not case_sensitive then
  929. if type(a) == 'string' then a = a:lower() end
  930. if type(b) == 'string' then b = b:lower() end
  931. end
  932. return a < b
  933. end)
  934. return items
  935. end
  936. ---
  937. -- Returns an HTML-safe copy of string *s*.
  938. -- @param s String to ensure is HTML-safe.
  939. -- @usage expand([[{{ '<">&'|e}}]]) --> &lt;&#34;&gt;&amp;
  940. -- @name filters.escape
  941. function M.filters.escape(s)
  942. assert(s, 'input to filter "escape" was nil instead of a string')
  943. return s:gsub('[<>"\'&]', {
  944. ['<'] = '&lt;', ['>'] = '&gt;', ['"'] = '&#34;', ["'"] = '&#39;',
  945. ['&'] = '&amp;'
  946. })
  947. end
  948. ---
  949. -- Returns an HTML-safe copy of string *s*.
  950. -- @param s String to ensure is HTML-safe.
  951. -- @usage expand([[{{ '<">&'|escape}}]]) --> &lt;&#34;&gt;&amp;
  952. -- @name filters.e
  953. function M.filters.e(s)
  954. assert(s, 'input to filter "e" was nil instead of a string')
  955. return M.filters.escape(s)
  956. end
  957. ---
  958. -- Returns a human-readable, decimal (or binary, depending on boolean *binary*)
  959. -- file size for *bytes* number of bytes.
  960. -- @param bytes The number of bytes to return the size for.
  961. -- @param binary Flag indicating whether or not to report binary file size
  962. -- as opposed to decimal file size. The default value is `false`.
  963. -- @usage expand('{{ 1000|filesizeformat }}') --> 1.0 kB
  964. -- @name filters.filesizeformat
  965. function M.filters.filesizeformat(bytes, binary)
  966. assert(bytes, 'input to filter "filesizeformat" was nil instead of a number')
  967. local base = binary and 1024 or 1000
  968. local units = {
  969. binary and 'KiB' or 'kB', binary and 'MiB' or 'MB',
  970. binary and 'GiB' or 'GB', binary and 'TiB' or 'TB',
  971. binary and 'PiB' or 'PB', binary and 'EiB' or 'EB',
  972. binary and 'ZiB' or 'ZB', binary and 'YiB' or 'YB'
  973. }
  974. if bytes < base then
  975. return string.format('%d Byte%s', bytes, bytes > 1 and 's' or '')
  976. else
  977. local limit, unit
  978. for i = 1, #units do
  979. limit, unit = base^(i + 1), units[i]
  980. if bytes < limit then break end
  981. end
  982. return string.format('%.1f %s', (base * bytes / limit), unit)
  983. end
  984. end
  985. ---
  986. -- Returns the first element in table *t*.
  987. -- @param t The table to get the first element of.
  988. -- @usage expand('{{ range(10)|first }}') --> 1
  989. -- @name filters.first
  990. function M.filters.first(t)
  991. assert(t, 'input to filter "first" was nil instead of a table')
  992. return t[1]
  993. end
  994. ---
  995. -- Returns value *value* as a float.
  996. -- This filter only works in Lua 5.3, which has a distinction between floats and
  997. -- integers.
  998. -- @param value The value to interpret as a float.
  999. -- @usage expand('{{ 42|float }}') --> 42.0
  1000. -- @name filters.float
  1001. function M.filters.float(value)
  1002. assert(value, 'input to filter "float" was nil instead of a number')
  1003. return (tonumber(value) or 0) * 1.0
  1004. end
  1005. ---
  1006. -- Returns an HTML-safe copy of value *value*, even if *value* was returned by
  1007. -- the "safe" filter.
  1008. -- @param value Value to ensure is HTML-safe.
  1009. -- @usage expand('{% set x = "<div />"|safe %}{{ x|forceescape }}') -->
  1010. -- &lt;div /&gt;
  1011. -- @name filters.forceescape
  1012. function M.filters.forceescape(value)
  1013. assert(value, 'input to filter "forceescape" was nil instead of a string')
  1014. return M.filters.escape(tostring(value))
  1015. end
  1016. ---
  1017. -- Returns the given arguments formatted according to string *s*.
  1018. -- See Lua's `string.format()` for more information.
  1019. -- @param s The string to format subsequent arguments according to.
  1020. -- @param ... Arguments to format.
  1021. -- @usage expand('{{ "%s,%s"|format("a", "b") }}') --> a,b
  1022. -- @name filters.format
  1023. function M.filters.format(s, ...)
  1024. assert(s, 'input to filter "format" was nil instead of a string')
  1025. return string.format(s, ...)
  1026. end
  1027. ---
  1028. -- Returns a generator that produces lists of items in table *t* grouped by
  1029. -- string attribute *attribute*.
  1030. -- @param t The table to group items from.
  1031. -- @param attribute The attribute of items in the table to group by. This may
  1032. -- be nested (e.g. "foo.bar" groups by t[i].foo.bar for all i).
  1033. -- @usage expand('{% for age, group in people|groupby("age") %}...{% endfor %}')
  1034. -- @name filters.groupby
  1035. function M.filters.groupby(t, attribute)
  1036. assert(t, 'input to filter "groupby" was nil instead of a table')
  1037. local n = #t
  1038. local seen = {} -- keep track of groupers in order to avoid duplicates
  1039. return function(t, i)
  1040. if i > n then return nil end
  1041. local ta = attr_accessor(t, attribute)
  1042. -- Determine the next grouper.
  1043. local grouper = ta[i]
  1044. while seen[grouper] do
  1045. i = i + 1
  1046. if i > n then return nil end
  1047. grouper = ta[i]
  1048. end
  1049. seen[grouper] = true
  1050. -- Create and return the group.
  1051. local group = {}
  1052. for j = i, #t do if ta[j] == grouper then group[#group + 1] = t[j] end end
  1053. return i + 1, grouper, group
  1054. end, t, 1
  1055. end
  1056. ---
  1057. -- Returns a copy of string *s* with all lines after the first indented by
  1058. -- *width* number of spaces.
  1059. -- If boolean *first_line* is `true`, indents the first line as well.
  1060. -- @param s The string to indent lines of.
  1061. -- @param width The number of spaces to indent lines with.
  1062. -- @param first_line Optional flag indicating whether or not to indent the
  1063. -- first line of text. The default value is `false`.
  1064. -- @usage expand('{{ "foo\nbar"|indent(2) }}') --> "foo\n bar"
  1065. -- @name filters.indent
  1066. function M.filters.indent(s, width, first_line)
  1067. assert(s, 'input to filter "indent" was nil instead of a string')
  1068. local indent = (' '):rep(width)
  1069. return (first_line and indent or '')..s:gsub('([\r\n]+)', '%1'..indent)
  1070. end
  1071. ---
  1072. -- Returns value *value* as an integer.
  1073. -- @param value The value to interpret as an integer.
  1074. -- @usage expand('{{ 32.32|int }}') --> 32
  1075. -- @name filters.int
  1076. function M.filters.int(value)
  1077. assert(value, 'input to filter "int" was nil instead of a number')
  1078. return math.floor(tonumber(value) or 0)
  1079. end
  1080. ---
  1081. -- Returns a string that contains all the elements in table *t* (or all the
  1082. -- attributes named *attribute* in *t*) separated by string *sep*.
  1083. -- @param t The table to join.
  1084. -- @param sep The string to separate table elements with.
  1085. -- @param attribute Optional attribute of elements to use for joining instead
  1086. -- of the elements themselves. This may be nested (e.g. "foo.bar" joins
  1087. -- `t[i].foo.bar` for all i).
  1088. -- @usage expand('{{ {1, 2, 3}|join("|") }}') --> 1|2|3
  1089. -- @name filters.join
  1090. function M.filters.join(t, sep, attribute)
  1091. assert(t, 'input to filter "join" was nil instead of a table')
  1092. if not attribute then
  1093. local strings = {}
  1094. for i = 1, #t do strings[#strings + 1] = tostring(t[i]) end
  1095. return table.concat(strings, sep)
  1096. end
  1097. local ta = attr_accessor(t, attribute)
  1098. local attributes = {}
  1099. for i = 1, #t do attributes[#attributes + 1] = ta[i] end
  1100. return table.concat(attributes, sep)
  1101. end
  1102. ---
  1103. -- Returns the last element in table *t*.
  1104. -- @param t The table to get the last element of.
  1105. -- @usage expand('{{ range(10)|last }}') --> 10
  1106. -- @name filters.last
  1107. function M.filters.last(t)
  1108. assert(t, 'input to filter "last" was nil instead of a table')
  1109. return t[#t]
  1110. end
  1111. ---
  1112. -- Returns the length of string or table *value*.
  1113. -- @param value The value to get the length of.
  1114. -- @usage expand('{{ "hello world"|length }}') --> 11
  1115. -- @name filters.length
  1116. function M.filters.length(value)
  1117. assert(value, 'input to filter "length" was nil instead of a table or string')
  1118. return #value
  1119. end
  1120. ---
  1121. -- Returns the list of items produced by generator *generator*, subject to
  1122. -- initial state *s* and initial iterator variable *i*.
  1123. -- This filter should only be used after a filter that returns a generator.
  1124. -- @param generator Generator function that produces an item.
  1125. -- @param s Initial state for the generator.
  1126. -- @param i Initial iterator variable for the generator.
  1127. -- @usage expand('{{ range(4)|batch(2)|list|string }}') --> {{1, 2}, {3, 4}}
  1128. -- @see filters.batch
  1129. -- @see filters.groupby
  1130. -- @see filters.slice
  1131. -- @name filters.list
  1132. function M.filters.list(generator, s, i)
  1133. assert(type(generator) == 'function',
  1134. 'input to filter "list" must be a generator')
  1135. local list = {}
  1136. for _, v in generator, s, i do list[#list + 1] = v end
  1137. return list
  1138. end
  1139. ---
  1140. -- Returns a copy of string *s* with all lowercase characters.
  1141. -- @param s The string to lowercase.
  1142. -- @usage expand('{{ "FOO"|lower }}') --> foo
  1143. -- @name filters.lower
  1144. function M.filters.lower(s)
  1145. assert(s, 'input to filter "lower" was nil instead of a string')
  1146. return string.lower(s)
  1147. end
  1148. ---
  1149. -- Maps each element of table *t* to a value produced by filter name *filter*
  1150. -- and returns the resultant table.
  1151. -- @param t The table of elements to map.
  1152. -- @param filter The name of the filter to pass table elements through.
  1153. -- @param ... Any arguments for the filter.
  1154. -- @usage expand('{{ {"1", "2", "3"}|map("int")|sum }}') --> 6
  1155. -- @name filters.map
  1156. function M.filters.map(t, filter, ...)
  1157. assert(t, 'input to filter "map" was nil instead of a table')
  1158. local f = M.filters[filter]
  1159. assert(f, 'unknown filter "'..filter..'"')
  1160. local map = {}
  1161. for i = 1, #t do map[i] = f(t[i], ...) end
  1162. return map
  1163. end
  1164. ---
  1165. -- Maps the value of each element's string *attribute* in table *t* to the
  1166. -- value produced by filter name *filter* and returns the resultant table.
  1167. -- @param t The table of elements with attributes to map.
  1168. -- @param attribute The attribute of elements in the table to filter. This may
  1169. -- be nested (e.g. "foo.bar" maps t[i].foo.bar for all i).
  1170. -- @param filter The name of the filter to pass table elements through.
  1171. -- @param ... Any arguments for the filter.
  1172. -- @usage expand('{{ users|mapattr("name")|join("|") }}')
  1173. -- @name filters.mapattr
  1174. function M.filters.mapattr(t, attribute, filter, ...)
  1175. assert(t, 'input to filter "mapattr" was nil instead of a table')
  1176. local ta = attr_accessor(t, attribute)
  1177. local f = M.filters[filter]
  1178. if filter then
  1179. assert(f, 'unknown filter "'..filter..'" given to filter "mapattr"')
  1180. end
  1181. local map = {}
  1182. for i = 1, #t do map[i] = filter and f(ta[i], ...) or ta[i] end
  1183. return map
  1184. end
  1185. ---
  1186. -- Returns a random element from table *t*.
  1187. -- @param t The table to get a random element from.
  1188. -- @usage expand('{{ range(100)|random }}')
  1189. -- @name filters.random
  1190. function M.filters.random(t)
  1191. assert(t, 'input to filter "random" was nil instead of a table')
  1192. math.randomseed(os.time())
  1193. return t[math.random(#t)]
  1194. end
  1195. ---
  1196. -- Returns a list of elements in table *t* that fail test name *test*.
  1197. -- @param t The table of elements to reject from.
  1198. -- @param test The name of the test to use on table elements.
  1199. -- @param ... Any arguments for the test.
  1200. -- @usage expand('{{ range(5)|reject(is_odd)|join("|") }}') --> 2|4
  1201. -- @name filters.reject
  1202. function M.filters.reject(t, test, ...)
  1203. assert(t, 'input to filter "reject" was nil instead of a table')
  1204. local f = test or function(value) return not not value end
  1205. local items = {}
  1206. for i = 1, #t do if not f(t[i], ...) then items[#items + 1] = t[i] end end
  1207. return items
  1208. end
  1209. ---
  1210. -- Returns a list of elements in table *t* whose string attribute *attribute*
  1211. -- fails test name *test*.
  1212. -- @param t The table of elements to reject from.
  1213. -- @param attribute The attribute of items in the table to reject from. This
  1214. -- may be nested (e.g. "foo.bar" tests t[i].foo.bar for all i).
  1215. -- @param test The name of the test to use on table elements.
  1216. -- @param ... Any arguments for the test.
  1217. -- @usage expand('{{ users|rejectattr("offline")|mapattr("name")|join(",") }}')
  1218. -- @name filters.rejectattr
  1219. function M.filters.rejectattr(t, attribute, test, ...)
  1220. assert(t, 'input to filter "rejectattr" was nil instead of a table')
  1221. local ta = attr_accessor(t, attribute)
  1222. local f = test or function(value) return not not value end
  1223. local items = {}
  1224. for i = 1, #t do if not f(ta[i], ...) then items[#items + 1] = t[i] end end
  1225. return items
  1226. end
  1227. ---
  1228. -- Returns a copy of string *s* with all (or up to *n*) occurrences of string
  1229. -- *old* replaced by string *new*.
  1230. -- Identical to Lua's `string.gsub()` and handles Lua patterns.
  1231. -- @param s The subject string.
  1232. -- @param pattern The string or Lua pattern to replace.
  1233. -- @param repl The replacement text (may contain Lua captures).
  1234. -- @param n Optional number indicating the maximum number of replacements to
  1235. -- make. The default value is `nil`, which is unlimited.
  1236. -- @usage expand('{% filter upper|replace("FOO", "foo") %}foobar
  1237. -- {% endfilter %}') --> fooBAR
  1238. -- @name filters.replace
  1239. function M.filters.replace(s, pattern, repl, n)
  1240. assert(s, 'input to filter "replace" was nil instead of a string')
  1241. return string.gsub(s, pattern, repl, n)
  1242. end
  1243. ---
  1244. -- Returns a copy of the given string or table *value* in reverse order.
  1245. -- @param value The value to reverse.
  1246. -- @usage expand('{{ {1, 2, 3}|reverse|string }}') --> {3, 2, 1}
  1247. -- @name filters.reverse
  1248. function M.filters.reverse(value)
  1249. assert(type(value) == 'table' or type(value) == 'string',
  1250. 'input to filter "reverse" was nil instead of a table or string')
  1251. if type(value) == 'string' then return value:reverse() end
  1252. local t = {}
  1253. for i = 1, #value do t[i] = value[#value - i + 1] end
  1254. return t
  1255. end
  1256. ---
  1257. -- Returns number *value* rounded to *precision* decimal places based on string
  1258. -- *method* (if given).
  1259. -- @param value The number to round.
  1260. -- @param precision Optional precision to round the number to. The default
  1261. -- value is `0`.
  1262. -- @param method Optional string rounding method, either `"ceil"` or
  1263. -- `"floor"`. The default value is `nil`, which uses the common rounding
  1264. -- method (if a number's fractional part is 0.5 or greater, rounds up;
  1265. -- otherwise rounds down).
  1266. -- @usage expand('{{ 2.1236|round(3, "floor") }}') --> 2.123
  1267. -- @name filters.round
  1268. function M.filters.round(value, precision, method)
  1269. assert(value, 'input to filter "round" was nil instead of a number')
  1270. assert(not method or method == 'ceil' or method == 'floor',
  1271. 'rounding method given to filter "round" must be "ceil" or "floor"')
  1272. precision = precision or 0
  1273. method = method or (select(2, math.modf(value)) >= 0.5 and 'ceil' or 'floor')
  1274. local s = string.format('%.'..(precision >= 0 and precision or 0)..'f',
  1275. math[method](value * 10^precision) / 10^precision)
  1276. return tonumber(s)
  1277. end
  1278. ---
  1279. -- Marks string *s* as HTML-safe, preventing Lupa from modifying it when
  1280. -- configured to autoescape HTML entities.
  1281. -- This filter must be used at the end of a filter chain unless it is
  1282. -- immediately proceeded by the "forceescape" filter.
  1283. -- @param s The string to mark as HTML-safe.
  1284. -- @usage lupa.configure{autoescape = true}
  1285. -- @usage expand('{{ "<div>foo</div>"|safe }}') --> <div>foo</div>
  1286. -- @name filters.safe
  1287. function M.filters.safe(s)
  1288. assert(s, 'input to filter "safe" was nil instead of a string')
  1289. return setmetatable({}, {__tostring = function() return s end})
  1290. end
  1291. ---
  1292. -- Returns a list of the elements in table *t* that pass test name *test*.
  1293. -- @param t The table of elements to select from.
  1294. -- @param test The name of the test to use on table elements.
  1295. -- @param ... Any arguments for the test.
  1296. -- @usage expand('{{ range(5)|select(is_odd)|join("|") }}') --> 1|3|5
  1297. -- @name filters.select
  1298. function M.filters.select(t, test, ...)
  1299. assert(t, 'input to filter "select" was nil instead of a table')
  1300. local f = test or function(value) return not not value end
  1301. local items = {}
  1302. for i = 1, #t do if f(t[i], ...) then items[#items + 1] = t[i] end end
  1303. return items
  1304. end
  1305. ---
  1306. -- Returns a list of elements in table *t* whose string attribute *attribute*
  1307. -- passes test name *test*.
  1308. -- @param t The table of elements to select from.
  1309. -- @param attribute The attribute of items in the table to select from. This
  1310. -- may be nested (e.g. "foo.bar" tests t[i].foo.bar for all i).
  1311. -- @param test The name of the test to use on table elements.
  1312. -- @param ... Any arguments for the test.
  1313. -- @usage expand('{{ users|selectattr("online")|mapattr("name")|join("|") }}')
  1314. -- @name filters.selectattr
  1315. function M.filters.selectattr(t, attribute, test, ...)
  1316. assert(t, 'input to filter "selectattr" was nil instead of a table')
  1317. local ta = attr_accessor(t, attribute)
  1318. local f = test or function(value) return not not value end
  1319. local items = {}
  1320. for i = 1, #t do if f(ta[i], ...) then items[#items + 1] = t[i] end end
  1321. return items
  1322. end
  1323. ---
  1324. -- Returns a generator that produces all of the items in table *t* in *slices*
  1325. -- number of iterations, filling any empty spaces with value *fill*.
  1326. -- Combine this with the "list" filter to produce a list.
  1327. -- @param t The table to slice.
  1328. -- @param slices The number of slices to produce.
  1329. -- @param fill The value to use when filling in any empty space in the last
  1330. -- slice.
  1331. -- @usage expand('{% for i in {1, 2, 3}|slice(2, 0) %}{{ i|string }}
  1332. -- {% endfor %}') --> {1, 2} {3, 0}
  1333. -- @see filters.list
  1334. -- @name filters.slice
  1335. function M.filters.slice(t, slices, fill)
  1336. assert(t, 'input to filter "slice" was nil instead of a table')
  1337. local size, slices_with_extra = math.floor(#t / slices), #t % slices
  1338. return function(t, i)
  1339. if i > slices then return nil end
  1340. local slice = {}
  1341. local s = (i - 1) * size + math.min(i, slices_with_extra + 1)
  1342. local e = i * size + math.min(i, slices_with_extra)
  1343. for j = s, e do slice[j - s + 1] = t[j] end
  1344. if slices_with_extra > 0 and i > slices_with_extra and fill then
  1345. slice[#slice + 1] = fill
  1346. end
  1347. return i + 1, slice
  1348. end, t, 1
  1349. end
  1350. ---
  1351. -- Returns a copy of table or string *value* in sorted order by value (or by
  1352. -- an attribute named *attribute*), depending on booleans *reverse* and
  1353. -- *case_sensitive*.
  1354. -- @param value The table or string to sort.
  1355. -- @param reverse Optional flag indicating whether or not to sort in reverse
  1356. -- (descending) order. The default value is `false`, which sorts in ascending
  1357. -- order.
  1358. -- @param case_sensitive Optional flag indicating whether or not to consider
  1359. -- case when sorting string values. The default value is `false`.
  1360. -- @param attribute Optional attribute of elements to sort by instead of the
  1361. -- elements themselves.
  1362. -- @usage expand('{{ {2, 3, 1}|sort|string }}') --> {1, 2, 3}
  1363. -- @name filters.sort
  1364. function M.filters.sort(value, reverse, case_sensitive, attribute)
  1365. assert(value, 'input to filter "sort" was nil instead of a table or string')
  1366. assert(not attribute or type(attribute) == 'string' or
  1367. type(attribute) == 'number',
  1368. 'attribute to filter "sort" must be a string or number')
  1369. local t = {}
  1370. local sort_string = type(value) == 'string'
  1371. if not sort_string then
  1372. for i = 1, #value do t[#t + 1] = value[i] end
  1373. else
  1374. for char in value:gmatch('.') do t[#t + 1] = char end -- chars in string
  1375. end
  1376. table.sort(t, function(a, b)
  1377. if attribute then
  1378. if type(attribute) == 'number' then
  1379. a, b = a[attribute], b[attribute]
  1380. else
  1381. for k in attribute:gmatch('[^.]+') do a, b = a[k], b[k] end
  1382. end
  1383. end
  1384. if not case_sensitive then
  1385. if type(a) == 'string' then a = a:lower() end
  1386. if type(b) == 'string' then b = b:lower() end
  1387. end
  1388. if not reverse then
  1389. return a < b
  1390. else
  1391. return a > b
  1392. end
  1393. end)
  1394. return not sort_string and t or table.concat(t)
  1395. end
  1396. ---
  1397. -- Returns the string representation of value *value*, handling lists properly.
  1398. -- @param value Value to return the string representation of.
  1399. -- @usage expand('{{ {1 * 1, 2 * 2, 3 * 3}|string }}') --> {1, 4, 9}
  1400. -- @name filters.string
  1401. function M.filters.string(value)
  1402. if type(value) ~= 'table' then return tostring(value) end
  1403. local t = {}
  1404. for i = 1, #value do
  1405. local item = value[i]
  1406. t[i] = type(item) == 'string' and '"'..item..'"' or M.filters.string(item)
  1407. end
  1408. return '{'..table.concat(t, ', ')..'}'
  1409. end
  1410. ---
  1411. -- Returns a copy of string *s* with any HTML tags stripped.
  1412. -- Also cleans up whitespace.
  1413. -- @param s String to strip HTML tags from.
  1414. -- @usage expand('{{ "<div>foo</div>"|striptags }}') --> foo
  1415. -- @name filters.striptags
  1416. function M.filters.striptags(s)
  1417. assert(s, 'input to filter "striptags" was nil instead of a string')
  1418. return s:gsub('%b<>', ''):gsub('%s+', ' '):match('^%s*(.-)%s*$')
  1419. end
  1420. ---
  1421. -- Returns the numeric sum of the elements in table *t* or the sum of all
  1422. -- attributes named *attribute* in *t*.
  1423. -- @param t The table to calculate the sum of.
  1424. -- @param attribute Optional attribute of elements to use for summing instead
  1425. -- of the elements themselves. This may be nested (e.g. "foo.bar" sums
  1426. -- `t[i].foo.bar` for all i).
  1427. -- @usage expand('{{ range(6)|sum }}') --> 21
  1428. -- @name filters.sum
  1429. function M.filters.sum(t, attribute)
  1430. assert(t, 'input to filter "sum" was nil instead of a table')
  1431. local ta = attribute and attr_accessor(t, attribute) or t
  1432. local sum = 0
  1433. for i = 1, #t do sum = sum + ta[i] end
  1434. return sum
  1435. end
  1436. ---
  1437. -- Returns a copy of all words in string *s* in titlecase.
  1438. -- @param s The string to titlecase.
  1439. -- @usage expand('{{ "foo bar"|title }}') --> Foo Bar
  1440. -- @name filters.title
  1441. function M.filters.title(s)
  1442. assert(s, 'input to filter "title" was nil instead of a string')
  1443. return s:gsub('[^-%s]+', M.filters.capitalize)
  1444. end
  1445. ---
  1446. -- Returns a copy of string *s* truncated to *length* number of characters.
  1447. -- Truncated strings end with '...' or string *delimiter*. If boolean
  1448. -- *partial_words* is `false`, truncation will only happen at word boundaries.
  1449. -- @param s The string to truncate.
  1450. -- @param length The length to truncate the string to.
  1451. -- @param partial_words Optional flag indicating whether or not to allow
  1452. -- truncation within word boundaries. The default value is `false`.
  1453. -- @param delimiter Optional delimiter text. The default value is '...'.
  1454. -- @usage expand('{{ "foo bar"|truncate(4) }}') --> "foo ..."
  1455. -- @name filters.truncate
  1456. function M.filters.truncate(s, length, partial_words, delimiter)
  1457. assert(s, 'input to filter "truncate" was nil instead of a string')
  1458. if #s <= length then return s end
  1459. local truncated = s:sub(1, length)
  1460. if s:find('[%w_]', length) and not partial_words then
  1461. truncated = truncated:match('^(.-)[%w_]*$') -- drop partial word
  1462. end
  1463. return truncated..(delimiter or '...')
  1464. end
  1465. ---
  1466. -- Returns a copy of string *s* with all uppercase characters.
  1467. -- @param s The string to uppercase.
  1468. -- @usage expand('{{ "foo"|upper }}') --> FOO
  1469. -- @name filters.upper
  1470. function M.filters.upper(s)
  1471. assert(s, 'input to filter "upper" was nil instead of a string')
  1472. return string.upper(s)
  1473. end
  1474. ---
  1475. -- Returns a string suitably encoded to be used in a URL from value *value*.
  1476. -- *value* may be a string, table of key-value query parameters, or table of
  1477. -- lists of key-value query parameters (for order).
  1478. -- @param value Value to URL-encode.
  1479. -- @usage expand('{{ {{'f', 1}, {'z', 2}}|urlencode }}') --> f=1&z=2
  1480. -- @name filters.urlencode
  1481. function M.filters.urlencode(value)
  1482. assert(value,
  1483. 'input to filter "urlencode" was nil instead of a string or table')
  1484. if type(value) ~= 'table' then
  1485. return tostring(value):gsub('[^%w.-]', function(c)
  1486. return string.format('%%%X', string.byte(c))
  1487. end)
  1488. end
  1489. local params = {}
  1490. if #value > 0 then
  1491. for i = 1, #value do
  1492. local k = M.filters.urlencode(value[i][1])
  1493. local v = M.filters.urlencode(value[i][2])
  1494. params[#params + 1] = k..'='..v
  1495. end
  1496. else
  1497. for k, v in pairs(value) do
  1498. params[#params + 1] = M.filters.urlencode(k)..'='..M.filters.urlencode(v)
  1499. end
  1500. end
  1501. return table.concat(params, '&')
  1502. end
  1503. ---
  1504. -- Replaces any URLs in string *s* with HTML links, limiting link text to
  1505. -- *length* characters.
  1506. -- @param s The string to replace URLs with HTML links in.
  1507. -- @param length Optional maximum number of characters to include in link text.
  1508. -- The default value is `nil`, which imposes no limit.
  1509. -- @param nofollow Optional flag indicating whether or not HTML links will get a
  1510. -- "nofollow" attribute.
  1511. -- @usage expand('{{ "example.com"|urlize }}') -->
  1512. -- <a href="http://example.com">example.com</a>
  1513. -- @name filters.urlize
  1514. function M.filters.urlize(s, length, nofollow)
  1515. assert(s, 'input to filter "urlize" was nil instead of a string')
  1516. -- Trims the given url.
  1517. local function trim_url(url)
  1518. return length and s:sub(1, length)..(#s > length and '...' or '') or url
  1519. end
  1520. local nofollow_attr = nofollow and ' rel="nofollow"' or ''
  1521. local lead, trail = C((S('(<') + '&lt;')^0), C((S('.,)>\n') + '&gt;')^0) * -1
  1522. local middle = C((1 - trail)^0)
  1523. local patt = lpeg.Cs(lead * middle * trail / function(lead, middle, trail)
  1524. local linked
  1525. if middle:find('^www%.') or (not middle:find('@') and
  1526. not middle:find('^https?://') and
  1527. #middle > 0 and middle:find('^%w') and (
  1528. middle:find('%.com$') or
  1529. middle:find('%.net$') or
  1530. middle:find('%.org$')
  1531. )) then
  1532. middle, linked = string.format('<a href="http://%s"%s>%s</a>', middle,
  1533. nofollow_attr, trim_url(middle)), true
  1534. end
  1535. if middle:find('^https?://') then
  1536. middle, linked = string.format('<a href="%s"%s>%s</a>', middle,
  1537. nofollow_attr, trim_url(middle)), true
  1538. end
  1539. if middle:find('@') and not middle:find('^www%.') and
  1540. not middle:find(':') and middle:find('^%S+@[%w._-]+%.[%w._-]+$') then
  1541. middle, linked = string.format('<a href="mailto:%s">%s</a>', middle,
  1542. middle), true
  1543. end
  1544. if linked then return lead..middle..trail end
  1545. end)
  1546. return M.filters.escape(s):gsub('%S+', function(word)
  1547. return lpeg.match(patt, word)
  1548. end)
  1549. end
  1550. ---
  1551. -- Returns the number of words in string *s*.
  1552. -- A word is a sequence of non-space characters.
  1553. -- @param s The string to count words in.
  1554. -- @usage expand('{{ "foo bar baz"|wordcount }}') --> 3
  1555. -- @name filters.wordcount
  1556. function M.filters.wordcount(s)
  1557. assert(s, 'input to filter "wordcount" was nil instead of a string')
  1558. return select(2, s:gsub('%S+', ''))
  1559. end
  1560. ---
  1561. -- Interprets table *t* as a list of XML attribute-value pairs, returning them
  1562. -- as a properly formatted, space-separated string.
  1563. -- @param t The table of XML attribute-value pairs.
  1564. -- @usage expand('<data {{ {foo = 42, bar = 23}|xmlattr }} />')
  1565. -- @name filters.xmlattr
  1566. function M.filters.xmlattr(t)
  1567. assert(t, 'input to filter "xmlattr" was nil instead of a table')
  1568. local attributes = {}
  1569. for k, v in pairs(t) do
  1570. attributes[#attributes + 1] = string.format('%s="%s"', k,
  1571. M.filters.escape(tostring(v)))
  1572. end
  1573. return table.concat(attributes, ' ')
  1574. end
  1575. -- Lupa tests.
  1576. ---
  1577. -- Returns whether or not number *n* is odd.
  1578. -- @param n The number to test.
  1579. -- @usage expand('{% for x in range(10) if is_odd(x) %}...{% endif %}')
  1580. -- @name tests.is_odd
  1581. function M.tests.is_odd(n) return n % 2 == 1 end
  1582. ---
  1583. -- Returns whether or not number *n* is even.
  1584. -- @param n The number to test.
  1585. -- @usage expand('{% for x in range(10) if is_even(x) %}...{% endif %}')
  1586. -- @name tests.is_even
  1587. function M.tests.is_even(n) return n % 2 == 0 end
  1588. ---
  1589. -- Returns whether or not number *n* is evenly divisible by number *num*.
  1590. -- @param n The dividend to test.
  1591. -- @param num The divisor to use.
  1592. -- @usage expand('{% if is_divisibleby(x, y) %}...{% endif %}')
  1593. -- @name tests.is_divisibleby
  1594. function M.tests.is_divisibleby(n, num) return n % num == 0 end
  1595. ---
  1596. -- Returns whether or not value *value* is non-nil, and thus defined.
  1597. -- @param value The value to test.
  1598. -- @usage expand('{% if is_defined(x) %}...{% endif %}')
  1599. -- @name tests.is_defined
  1600. function M.tests.is_defined(value) return value ~= nil end
  1601. ---
  1602. -- Returns whether or not value *value* is nil, and thus effectively undefined.
  1603. -- @param value The value to test.
  1604. -- @usage expand('{% if is_undefined(x) %}...{% endif %}')
  1605. -- @name tests.is_undefined
  1606. function M.tests.is_undefined(value) return value == nil end
  1607. ---
  1608. -- Returns whether or not value *value* is nil.
  1609. -- @param value The value to test.
  1610. -- @usage expand('{% if is_none(x) %}...{% endif %}')
  1611. -- @name tests.is_none
  1612. function M.tests.is_none(value) return value == nil end
  1613. ---
  1614. -- Returns whether or not value *value* is nil.
  1615. -- @param value The value to test.
  1616. -- @usage expand('{% if is_nil(x) %}...{% endif %}')
  1617. -- @name tests.is_nil
  1618. function M.tests.is_nil(value) return value == nil end
  1619. ---
  1620. -- Returns whether or not string *s* is in all lower-case characters.
  1621. -- @param s The string to test.
  1622. -- @usage expand('{% if is_lower(s) %}...{% endif %}')
  1623. -- @name tests.is_lower
  1624. function M.tests.is_lower(s) return s:lower() == s end
  1625. ---
  1626. -- Returns whether or not string *s* is in all upper-case characters.
  1627. -- @param s The string to test.
  1628. -- @usage expand('{% if is_upper(s) %}...{% endif %}')
  1629. -- @name tests.is_upper
  1630. function M.tests.is_upper(s) return s:upper() == s end
  1631. ---
  1632. -- Returns whether or not value *value* is a string.
  1633. -- @param value The value to test.
  1634. -- @usage expand('{% if is_string(x) %}...{% endif %}')
  1635. -- @name tests.is_string
  1636. function M.tests.is_string(value) return type(value) == 'string' end
  1637. ---
  1638. -- Returns whether or not value *value* is a table.
  1639. -- @param value The value to test.
  1640. -- @usage expand('{% if is_mapping(x) %}...{% endif %}')
  1641. -- @name tests.is_mapping
  1642. function M.tests.is_mapping(value) return type(value) == 'table' end
  1643. ---
  1644. -- Returns whether or not value *value* is a table.
  1645. -- @param value The value to test.
  1646. -- @usage expand('{% if is_table(x) %}...{% endif %}')
  1647. -- @name tests.is_table
  1648. function M.tests.is_table(value) return type(value) == 'table' end
  1649. ---
  1650. -- Returns whether or not value *value* is a number.
  1651. -- @param value The value to test.
  1652. -- @usage expand('{% if is_number(x) %}...{% endif %}')
  1653. -- @name tests.is_number
  1654. function M.tests.is_number(value) return type(value) == 'number' end
  1655. ---
  1656. -- Returns whether or not value *value* is a sequence, namely a table with
  1657. -- non-zero length.
  1658. -- @param value The value to test.
  1659. -- @usage expand('{% if is_sequence(x) %}...{% endif %}')
  1660. -- @name tests.is_sequence
  1661. function M.tests.is_sequence(value)
  1662. return type(value) == 'table' and #value > 0
  1663. end
  1664. ---
  1665. -- Returns whether or not value *value* is a sequence (a table with non-zero
  1666. -- length) or a generator.
  1667. -- At the moment, all functions are considered generators.
  1668. -- @param value The value to test.
  1669. -- @usage expand('{% if is_iterable(x) %}...{% endif %}')
  1670. -- @name tests.is_iterable
  1671. function M.tests.is_iterable(value)
  1672. return M.tests.is_sequence(value) or type(value) == 'function'
  1673. end
  1674. ---
  1675. -- Returns whether or not value *value* is a function.
  1676. -- @param value The value to test.
  1677. -- @usage expand('{% if is_callable(x) %}...{% endif %}')
  1678. -- @name tests.is_callable
  1679. function M.tests.is_callable(value) return type(value) == 'function' end
  1680. ---
  1681. -- Returns whether or not value *value* is the same as value *other*.
  1682. -- @param value The value to test.
  1683. -- @param other The value to compare with.
  1684. -- @usage expand('{% if is_sameas(x, y) %}...{% endif %}')
  1685. -- @name tests.is_sameas
  1686. function M.tests.is_sameas(value, other) return value == other end
  1687. ---
  1688. -- Returns whether or not value *value* is HTML-safe.
  1689. -- @param value The value to test.
  1690. -- @usage expand('{% if is_escaped(x) %}...{% endif %}')
  1691. -- @name tests.is_escaped
  1692. function M.tests.is_escaped(value)
  1693. return getmetatable(value) and getmetatable(value).__tostring ~= nil
  1694. end
  1695. return M