-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathtemplates.py
More file actions
41 lines (32 loc) · 1.22 KB
/
Copy pathtemplates.py
File metadata and controls
41 lines (32 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Python Standard Library Imports
import re
# HTK Imports
from htk.utils import htk_setting
from htk.utils import resolve_method_dynamically
def get_renderer():
renderer = resolve_method_dynamically(htk_setting('HTK_TEMPLATE_RENDERER'))
return renderer
def get_template_context_generator():
wrap_data = resolve_method_dynamically(htk_setting('HTK_TEMPLATE_CONTEXT_GENERATOR'))
return wrap_data
def generate_html_from_template(template_name, context_dict):
"""Generate HTML by using `template_name` inflated with `context_dict`
"""
from django.template import TemplateDoesNotExist
from django.template import loader
try:
template = loader.get_template(template_name)
html = template.render(context_dict)
except TemplateDoesNotExist:
html = ''
return html
def rewrite_relative_urls_as_absolute(html, base_url):
"""Rewrite relative URLs in `html` as absolute urls with `base_url`
"""
html = re.sub(
r'<(?P<tag>(?:a|link)[^>]*?(?:href|src))="(?P<path>/[^/][^>"]*?)"(?P<suffix>[^>]*?)>',
#r'<(?P<tag>img[^>]*?src)="(?P<path>/[^/][^>"]*?)"(?P<suffix>[^>]*?)>',
r'<\g<tag>="%s\g<path>"\g<suffix>>' % base_url,
html
)
return html