mirror of
https://github.com/webrecorder/pywb.git
synced 2025-03-15 00:03:28 +01:00
jinja2: add decorator for adding custom filters
This commit is contained in:
parent
83e07442f0
commit
e2f7777c7d
@ -10,6 +10,50 @@ from itertools import imap
|
||||
from jinja2 import Environment, FileSystemLoader, PackageLoader
|
||||
|
||||
|
||||
FILTERS = {}
|
||||
|
||||
|
||||
#=================================================================
|
||||
class template_filter(object):
|
||||
"""
|
||||
Decorator for registering a function as a jinja2 filter
|
||||
If optional argument is supplied, it is used as the filter name
|
||||
Otherwise, the func name is the filter name
|
||||
"""
|
||||
def __init__(self, param=None):
|
||||
if hasattr(param, '__call__'):
|
||||
self.name = None
|
||||
self.__call__(param)
|
||||
else:
|
||||
self.name = param
|
||||
|
||||
def __call__(self, func):
|
||||
name = self.name
|
||||
if not name:
|
||||
name = func.__name__
|
||||
|
||||
FILTERS[name] = func
|
||||
return func
|
||||
|
||||
|
||||
#=================================================================
|
||||
# Filters
|
||||
@template_filter
|
||||
def format_ts(value, format_='%a, %b %d %Y %H:%M:%S'):
|
||||
value = timestamp_to_datetime(value)
|
||||
return value.strftime(format_)
|
||||
|
||||
|
||||
@template_filter('host')
|
||||
def get_hostname(url):
|
||||
return urlparse.urlsplit(url).netloc
|
||||
|
||||
|
||||
@template_filter()
|
||||
def request_hostname(env):
|
||||
return env.get('HTTP_HOST', 'localhost')
|
||||
|
||||
|
||||
#=================================================================
|
||||
class J2TemplateView:
|
||||
def __init__(self, filename):
|
||||
@ -26,9 +70,7 @@ class J2TemplateView:
|
||||
loader = PackageLoader('pywb', template_dir)
|
||||
|
||||
jinja_env = Environment(loader=loader, trim_blocks=True)
|
||||
jinja_env.filters['format_ts'] = J2TemplateView.format_ts
|
||||
jinja_env.filters['host'] = J2TemplateView.get_host
|
||||
jinja_env.filters['request_hostname'] = J2TemplateView.request_hostname
|
||||
jinja_env.filters.update(FILTERS)
|
||||
return jinja_env
|
||||
|
||||
def render_to_string(self, **kwargs):
|
||||
@ -46,20 +88,6 @@ class J2TemplateView:
|
||||
status=status,
|
||||
content_type=content_type)
|
||||
|
||||
# Filters
|
||||
@staticmethod
|
||||
def format_ts(value, format_='%a, %b %d %Y %H:%M:%S'):
|
||||
value = timestamp_to_datetime(value)
|
||||
return value.strftime(format_)
|
||||
|
||||
@staticmethod
|
||||
def get_host(url):
|
||||
return urlparse.urlsplit(url).netloc
|
||||
|
||||
@staticmethod
|
||||
def request_hostname(env):
|
||||
return env.get('HTTP_HOST', 'localhost')
|
||||
|
||||
|
||||
#=================================================================
|
||||
def load_template_file(file, desc=None, view_class=J2TemplateView):
|
||||
@ -83,6 +111,7 @@ class J2HtmlCapturesView(J2TemplateView):
|
||||
return J2TemplateView.render_response(self,
|
||||
cdx_lines=list(cdx_lines),
|
||||
url=wbrequest.wb_url.url,
|
||||
type=wbrequest.wb_url.type,
|
||||
prefix=wbrequest.wb_prefix)
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user