template

Note

This module requires html2text, Jinja2, and Pygments. pip users can install them with the jnrbase[template] requirement.

Constants

jnrbase.template.FILTERS = {<name>: <filter function>, …}

Collection of custom filters to add to Jinja environment

Functions

jnrbase.template.jinja_filter(__func)[source]

Simple decorator to add a new filter to Jinja environment.

See also: FILTERS

Parameters

__func (Callable[…, str]) – Function to add to Jinja environment

Return type

Callable[…, str]

Returns

Unmodified function

jnrbase.template.colourise(__text, *args, **kwargs)[source]

Colourise text using click’s style function.

Returns text untouched if colour output is not enabled, or stdout is not a tty.

See click.style() for parameters

Parameters

__text (str) – Text to colourise

Return type

str

Returns

Colourised text, when possible

jnrbase.template.highlight(__text, *, lexer='diff', formatter='terminal')[source]

Highlight text highlighted using pygments.

Returns text untouched if colour output is not enabled.

See also: Pygments

Parameters
  • __text (str) – Text to highlight

  • lexer (str) – Jinja lexer to use

  • formatter (str) – Jinja formatter to use

Return type

str

Returns

Syntax highlighted output, when possible

jnrbase.template.html2text(__html, *, width=80, ascii_replacements=False)[source]

HTML to plain text renderer.

See also: html2text

Parameters
  • __html (str) – Text to process

  • width (int) – Paragraph width

  • ascii_replacements (bool) – Use pseudo-ASCII replacements for Unicode

Return type

str

Returns

Rendered text

jnrbase.template.regexp(__string, __pattern, __repl, *, count=0, flags=0)[source]

Jinja filter for regexp replacements.

See re.sub() for documentation.

Return type

str

Returns

Text with substitutions applied

jnrbase.template.relative_time(__timestamp)[source]

Format a relative time.

See human_timestamp()

Parameters

__timestamp (datetime) – Event to generate relative timestamp against

Return type

str

Returns

Human readable date and time offset

jnrbase.template.setup(__pkg)[source]

Configure a new Jinja environment with our filters.

Parameters

__pkg (str) – Package name to use as base for templates searches

Return type

Environment

Returns

Configured Jinja environment

Examples

>>> FILTERS['regexp']('hello', 'l', 'L')
'heLLo'
>>> regexp('hello', 'l', 'L')
'heLLo'