Merge pull request #112 from dougfelt/emoji_html_template

Use standard string.Template mechanism.
This commit is contained in:
dougfelt 2017-04-12 11:02:23 -07:00 committed by GitHub
commit 8f0ab6079e

View file

@ -29,6 +29,7 @@ import os
from os import path from os import path
import re import re
import shutil import shutil
import string
import sys import sys
from nototools import tool_utils from nototools import tool_utils
@ -436,38 +437,30 @@ def _parse_annotation_file(afile):
def _instantiate_template(template, arg_dict): def _instantiate_template(template, arg_dict):
id_regex = re.compile('{{([a-zA-Z0-9_]+)}}') id_regex = re.compile(r'\$([a-zA-Z0-9_]+)')
ids = set(m.group(1) for m in id_regex.finditer(template)) ids = set(m.group(1) for m in id_regex.finditer(template))
keyset = set(arg_dict.keys()) keyset = set(arg_dict.keys())
missing_ids = ids - keyset
extra_args = keyset - ids extra_args = keyset - ids
if extra_args: if extra_args:
print >> sys.stderr, ( print >> sys.stderr, (
'the following %d args are unused:\n%s' % 'the following %d args are unused:\n%s' %
(len(extra_args), ', '.join(sorted(extra_args)))) (len(extra_args), ', '.join(sorted(extra_args))))
text = template return string.Template(template).substitute(arg_dict)
if missing_ids:
raise ValueError(
'the following %d ids in the template have no args:\n%s' %
(len(missing_ids), ', '.join(sorted(missing_ids))))
for arg in ids:
text = re.sub('{{%s}}' % arg, arg_dict[arg], text)
return text
TEMPLATE = """<!DOCTYPE html> TEMPLATE = """<!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>{{title}}</title>{{fontFaceStyle}} <title>$title</title>$fontFaceStyle
<style>{{style}}</style> <style>$style</style>
</head> </head>
<body> <body>
<!-- <!--
{{info}} $info
--> -->
<h3>{{title}}</h3> <h3>$title</h3>
{{content}} $content
</body> </body>
</html> </html>
""" """