Show More
@@ -58,11 +58,11 b' In addition to filters, there are some b' | |||||
58 |
|
58 | |||
59 | - label(label, expr) |
|
59 | - label(label, expr) | |
60 |
|
60 | |||
61 | - sub(pat, repl, expr) |
|
|||
62 |
|
||||
63 | - rstdoc(text, style) |
|
61 | - rstdoc(text, style) | |
64 |
|
62 | |||
65 | - strip(text, chars) |
|
63 | - strip(text[, chars]) | |
|
64 | ||||
|
65 | - sub(pat, repl, expr) | |||
66 |
|
66 | |||
67 | Also, for any expression that returns a list, there is a list operator: |
|
67 | Also, for any expression that returns a list, there is a list operator: | |
68 |
|
68 |
@@ -205,6 +205,41 b' def buildfunc(exp, context):' | |||||
205 | f = context._filters[n] |
|
205 | f = context._filters[n] | |
206 | return (runfilter, (args[0][0], args[0][1], f)) |
|
206 | return (runfilter, (args[0][0], args[0][1], f)) | |
207 |
|
207 | |||
|
208 | def date(context, mapping, args): | |||
|
209 | if not (1 <= len(args) <= 2): | |||
|
210 | raise error.ParseError(_("date expects one or two arguments")) | |||
|
211 | ||||
|
212 | date = args[0][0](context, mapping, args[0][1]) | |||
|
213 | if len(args) == 2: | |||
|
214 | fmt = stringify(args[1][0](context, mapping, args[1][1])) | |||
|
215 | return util.datestr(date, fmt) | |||
|
216 | return util.datestr(date) | |||
|
217 | ||||
|
218 | def fill(context, mapping, args): | |||
|
219 | if not (1 <= len(args) <= 4): | |||
|
220 | raise error.ParseError(_("fill expects one to four arguments")) | |||
|
221 | ||||
|
222 | text = stringify(args[0][0](context, mapping, args[0][1])) | |||
|
223 | width = 76 | |||
|
224 | initindent = '' | |||
|
225 | hangindent = '' | |||
|
226 | if 2 <= len(args) <= 4: | |||
|
227 | try: | |||
|
228 | width = int(stringify(args[1][0](context, mapping, args[1][1]))) | |||
|
229 | except ValueError: | |||
|
230 | raise error.ParseError(_("fill expects an integer width")) | |||
|
231 | try: | |||
|
232 | initindent = stringify(args[2][0](context, mapping, args[2][1])) | |||
|
233 | initindent = stringify(runtemplate(context, mapping, | |||
|
234 | compiletemplate(initindent, context))) | |||
|
235 | hangindent = stringify(args[3][0](context, mapping, args[3][1])) | |||
|
236 | hangindent = stringify(runtemplate(context, mapping, | |||
|
237 | compiletemplate(hangindent, context))) | |||
|
238 | except IndexError: | |||
|
239 | pass | |||
|
240 | ||||
|
241 | return templatefilters.fill(text, width, initindent, hangindent) | |||
|
242 | ||||
208 | def get(context, mapping, args): |
|
243 | def get(context, mapping, args): | |
209 | if len(args) != 2: |
|
244 | if len(args) != 2: | |
210 | # i18n: "get" is a keyword |
|
245 | # i18n: "get" is a keyword | |
@@ -218,40 +253,6 b' def get(context, mapping, args):' | |||||
218 | key = args[1][0](context, mapping, args[1][1]) |
|
253 | key = args[1][0](context, mapping, args[1][1]) | |
219 | yield dictarg.get(key) |
|
254 | yield dictarg.get(key) | |
220 |
|
255 | |||
221 | def join(context, mapping, args): |
|
|||
222 | if not (1 <= len(args) <= 2): |
|
|||
223 | # i18n: "join" is a keyword |
|
|||
224 | raise error.ParseError(_("join expects one or two arguments")) |
|
|||
225 |
|
||||
226 | joinset = args[0][0](context, mapping, args[0][1]) |
|
|||
227 | if util.safehasattr(joinset, '__call__'): |
|
|||
228 | jf = joinset.joinfmt |
|
|||
229 | joinset = [jf(x) for x in joinset()] |
|
|||
230 |
|
||||
231 | joiner = " " |
|
|||
232 | if len(args) > 1: |
|
|||
233 | joiner = args[1][0](context, mapping, args[1][1]) |
|
|||
234 |
|
||||
235 | first = True |
|
|||
236 | for x in joinset: |
|
|||
237 | if first: |
|
|||
238 | first = False |
|
|||
239 | else: |
|
|||
240 | yield joiner |
|
|||
241 | yield x |
|
|||
242 |
|
||||
243 | def sub(context, mapping, args): |
|
|||
244 | if len(args) != 3: |
|
|||
245 | # i18n: "sub" is a keyword |
|
|||
246 | raise error.ParseError(_("sub expects three arguments")) |
|
|||
247 |
|
||||
248 | pat = stringify(args[0][0](context, mapping, args[0][1])) |
|
|||
249 | rpl = stringify(args[1][0](context, mapping, args[1][1])) |
|
|||
250 | src = stringify(args[2][0](context, mapping, args[2][1])) |
|
|||
251 | src = stringify(runtemplate(context, mapping, |
|
|||
252 | compiletemplate(src, context))) |
|
|||
253 | yield re.sub(pat, rpl, src) |
|
|||
254 |
|
||||
255 | def if_(context, mapping, args): |
|
256 | def if_(context, mapping, args): | |
256 | if not (2 <= len(args) <= 3): |
|
257 | if not (2 <= len(args) <= 3): | |
257 | # i18n: "if" is a keyword |
|
258 | # i18n: "if" is a keyword | |
@@ -279,6 +280,28 b' def ifeq(context, mapping, args):' | |||||
279 | t = stringify(args[3][0](context, mapping, args[3][1])) |
|
280 | t = stringify(args[3][0](context, mapping, args[3][1])) | |
280 | yield runtemplate(context, mapping, compiletemplate(t, context)) |
|
281 | yield runtemplate(context, mapping, compiletemplate(t, context)) | |
281 |
|
282 | |||
|
283 | def join(context, mapping, args): | |||
|
284 | if not (1 <= len(args) <= 2): | |||
|
285 | # i18n: "join" is a keyword | |||
|
286 | raise error.ParseError(_("join expects one or two arguments")) | |||
|
287 | ||||
|
288 | joinset = args[0][0](context, mapping, args[0][1]) | |||
|
289 | if util.safehasattr(joinset, '__call__'): | |||
|
290 | jf = joinset.joinfmt | |||
|
291 | joinset = [jf(x) for x in joinset()] | |||
|
292 | ||||
|
293 | joiner = " " | |||
|
294 | if len(args) > 1: | |||
|
295 | joiner = args[1][0](context, mapping, args[1][1]) | |||
|
296 | ||||
|
297 | first = True | |||
|
298 | for x in joinset: | |||
|
299 | if first: | |||
|
300 | first = False | |||
|
301 | else: | |||
|
302 | yield joiner | |||
|
303 | yield x | |||
|
304 | ||||
282 | def label(context, mapping, args): |
|
305 | def label(context, mapping, args): | |
283 | if len(args) != 2: |
|
306 | if len(args) != 2: | |
284 | # i18n: "label" is a keyword |
|
307 | # i18n: "label" is a keyword | |
@@ -298,41 +321,6 b' def rstdoc(context, mapping, args):' | |||||
298 |
|
321 | |||
299 | return minirst.format(text, style=style, keep=['verbose']) |
|
322 | return minirst.format(text, style=style, keep=['verbose']) | |
300 |
|
323 | |||
301 | def fill(context, mapping, args): |
|
|||
302 | if not (1 <= len(args) <= 4): |
|
|||
303 | raise error.ParseError(_("fill expects one to four arguments")) |
|
|||
304 |
|
||||
305 | text = stringify(args[0][0](context, mapping, args[0][1])) |
|
|||
306 | width = 76 |
|
|||
307 | initindent = '' |
|
|||
308 | hangindent = '' |
|
|||
309 | if 2 <= len(args) <= 4: |
|
|||
310 | try: |
|
|||
311 | width = int(stringify(args[1][0](context, mapping, args[1][1]))) |
|
|||
312 | except ValueError: |
|
|||
313 | raise error.ParseError(_("fill expects an integer width")) |
|
|||
314 | try: |
|
|||
315 | initindent = stringify(args[2][0](context, mapping, args[2][1])) |
|
|||
316 | initindent = stringify(runtemplate(context, mapping, |
|
|||
317 | compiletemplate(initindent, context))) |
|
|||
318 | hangindent = stringify(args[3][0](context, mapping, args[3][1])) |
|
|||
319 | hangindent = stringify(runtemplate(context, mapping, |
|
|||
320 | compiletemplate(hangindent, context))) |
|
|||
321 | except IndexError: |
|
|||
322 | pass |
|
|||
323 |
|
||||
324 | return templatefilters.fill(text, width, initindent, hangindent) |
|
|||
325 |
|
||||
326 | def date(context, mapping, args): |
|
|||
327 | if not (1 <= len(args) <= 2): |
|
|||
328 | raise error.ParseError(_("date expects one or two arguments")) |
|
|||
329 |
|
||||
330 | date = args[0][0](context, mapping, args[0][1]) |
|
|||
331 | if len(args) == 2: |
|
|||
332 | fmt = stringify(args[1][0](context, mapping, args[1][1])) |
|
|||
333 | return util.datestr(date, fmt) |
|
|||
334 | return util.datestr(date) |
|
|||
335 |
|
||||
336 | def strip(context, mapping, args): |
|
324 | def strip(context, mapping, args): | |
337 | if not (1 <= len(args) <= 2): |
|
325 | if not (1 <= len(args) <= 2): | |
338 | raise error.ParseError(_("strip expects one or two arguments")) |
|
326 | raise error.ParseError(_("strip expects one or two arguments")) | |
@@ -343,6 +331,18 b' def strip(context, mapping, args):' | |||||
343 | return text.strip(chars) |
|
331 | return text.strip(chars) | |
344 | return text.strip() |
|
332 | return text.strip() | |
345 |
|
333 | |||
|
334 | def sub(context, mapping, args): | |||
|
335 | if len(args) != 3: | |||
|
336 | # i18n: "sub" is a keyword | |||
|
337 | raise error.ParseError(_("sub expects three arguments")) | |||
|
338 | ||||
|
339 | pat = stringify(args[0][0](context, mapping, args[0][1])) | |||
|
340 | rpl = stringify(args[1][0](context, mapping, args[1][1])) | |||
|
341 | src = stringify(args[2][0](context, mapping, args[2][1])) | |||
|
342 | src = stringify(runtemplate(context, mapping, | |||
|
343 | compiletemplate(src, context))) | |||
|
344 | yield re.sub(pat, rpl, src) | |||
|
345 | ||||
346 | methods = { |
|
346 | methods = { | |
347 | "string": lambda e, c: (runstring, e[1]), |
|
347 | "string": lambda e, c: (runstring, e[1]), | |
348 | "symbol": lambda e, c: (runsymbol, e[1]), |
|
348 | "symbol": lambda e, c: (runsymbol, e[1]), | |
@@ -354,16 +354,16 b' methods = {' | |||||
354 | } |
|
354 | } | |
355 |
|
355 | |||
356 | funcs = { |
|
356 | funcs = { | |
|
357 | "date": date, | |||
|
358 | "fill": fill, | |||
357 | "get": get, |
|
359 | "get": get, | |
358 | "if": if_, |
|
360 | "if": if_, | |
359 | "ifeq": ifeq, |
|
361 | "ifeq": ifeq, | |
360 | "join": join, |
|
362 | "join": join, | |
361 | "label": label, |
|
363 | "label": label, | |
362 | "rstdoc": rstdoc, |
|
364 | "rstdoc": rstdoc, | |
|
365 | "strip": strip, | |||
363 | "sub": sub, |
|
366 | "sub": sub, | |
364 | "fill": fill, |
|
|||
365 | "date": date, |
|
|||
366 | "strip": strip, |
|
|||
367 | } |
|
367 | } | |
368 |
|
368 | |||
369 | # template engine |
|
369 | # template engine |
General Comments 0
You need to be logged in to leave comments.
Login now