mirror of
https://github.com/Threnklyn/dungeon-sheets.git
synced 2026-05-19 04:33:26 +02:00
39 lines
921 B
Python
39 lines
921 B
Python
import re
|
|
|
|
from jinja2 import Environment, PackageLoader
|
|
|
|
|
|
# A dice string, with optional backticks: ``1d6 + 3``
|
|
dice_re = re.compile(r"`*(\d+d\d+(?:\s*\+\s*\d+)?)`*")
|
|
|
|
|
|
def mod_str(modifier):
|
|
"""Converts a modifier to a string, eg 2 -> '+2'."""
|
|
return "{:+d}".format(modifier)
|
|
|
|
|
|
def jinja_environment():
|
|
"""Prepare a new environment for Jinja templates.
|
|
|
|
This function loads filters that are agnostic to the output
|
|
format. Format specific filters (e.g. html, latex, etc.) should be
|
|
added to ``jinja_env.filters``.
|
|
|
|
Returns
|
|
-------
|
|
jinja_env
|
|
The newly created jinja environment.
|
|
|
|
"""
|
|
jinja_env = Environment(
|
|
loader=PackageLoader("dungeonsheets", "forms"),
|
|
block_start_string="[%",
|
|
block_end_string="%]",
|
|
variable_start_string="[[",
|
|
variable_end_string="]]",
|
|
)
|
|
jinja_env.filters["mod_str"] = mod_str
|
|
return jinja_env
|
|
|
|
|