blob: 1b34e91035fa123942604fe76aad53db7770481e (
plain)
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
42
43
44
45
46
47
48
49
|
import os.path as path
import sys
import codecs
import logging
import jinja2
import resumejson_converter.filters as filters
def generate(resume, template, out_path):
"""
Return html version of a JSON resume based on a template.
`resume` is a JsonObject from resumejson_converter.utils.json.
`template` is the path of the template to use.
`out_path` is the output path of html generate. By default the html is
not writen in a file.
"""
try:
logging.info("HTML generation...")
templates_path = path.dirname(template)
template_name = path.basename(template)
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(templates_path),
autoescape=jinja2.select_autoescape(['hmtl', 'xml']),
)
env.filters['dateedit'] = filters.dateedit
env.filters['datediff'] = filters.datediff
env.filters['birthday'] = filters.birthday
env.filters['clean'] = filters.clean
try:
template = env.get_template(template_name)
except jinja2.exceptions.TemplateNotFound:
logging.exception("File not found: '{}'!"
.format(template))
sys.exit(1)
html = template.render(dict(resume=resume))
except jinja2.exceptions.TemplateSyntaxError as e:
logging.exception("Syntax error in template file '{}' line {}: {}"
.format(e.name, e.lineno, e.message))
sys.exit(1)
else:
logging.info("HTML generation done.")
if out_path:
file = codecs.open(out_path, "w", "utf-8-sig")
file.write(html)
file.close
return html
|