Coverage for ligo/followup_advocate/jinja.py: 100%
15 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-20 11:01 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-20 11:01 +0000
1"""Jinja2 environment"""
3import math
4import re
5import textwrap
7import humanize.number
8import humanize.time
9import jinja2
11__all__ = ('env',)
13env = jinja2.Environment(
14 loader=jinja2.PackageLoader(__package__, 'templates'),
15 # The options below are suggested by the Chromium Jinja style guide,
16 # https://www.chromium.org/developers/jinja.
17 keep_trailing_newline=True, # newline-terminate generated files
18 lstrip_blocks=False, # so can preserve whitespace at the state of line
19 trim_blocks=True, # so don't need {%- -%} everywhere
20 extensions=['jinja2.ext.loopcontrols']
21)
23_rewrap_regex = re.compile(
24 r'^[ \t]+.+\n|([^ \t\n]+[^\n]*\n)+|.*',
25 flags=re.MULTILINE)
28def rewrap(text, width=79):
29 """Rewrap the given text, paragraph by paragraph. Treat any line that
30 begins with whitespace as a separate paragraph, and any consecutive
31 sequence of lines that do not begin with whitespace as a paragraph."""
32 return '\n'.join(textwrap.fill(text[match.start():match.end()],
33 width=width)
34 for match in _rewrap_regex.finditer(text))
37env.filters['log10'] = math.log10
38env.filters['rewrap'] = rewrap
39env.filters['apnumber'] = humanize.number.apnumber
40env.filters['naturaldelta'] = humanize.time.naturaldelta