-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreviewtemplate.py
executable file
·358 lines (303 loc) · 12 KB
/
previewtemplate.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python3
import re
import os
import argparse
import logging
try:
import colorlog
except ImportError:
colorlog = None
from typing import Dict, List
import csv
"""Small tool to preview templates for the spaced repetition flashcard program
Anki.
https://github.com/klieret/anki-template-tester
"""
def setup_logger(name="Logger"):
""" Sets up a logging.Logger with the name $name. If the colorlog module
is available, the logger will use colors, otherwise it will be in b/w.
The colorlog module is available at
https://github.com/borntyping/python-colorlog
but can also easily be installed with e.g. 'sudo pip3 colorlog' or similar
commands.
Arguments:
name: name of the logger
Returns:
Logger
"""
if colorlog:
_logger = colorlog.getLogger(name)
else:
_logger = logging.getLogger(name)
if _logger.handlers:
# the logger already has handlers attached to it, even though
# we didn't add it ==> logging.get_logger got us an existing
# logger ==> we don't need to do anything
return _logger
_logger.setLevel(logging.DEBUG)
if colorlog is not None:
sh = colorlog.StreamHandler()
log_colors = {'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red'}
formatter = colorlog.ColoredFormatter(
'%(log_color)s%(name)s:%(levelname)s:%(message)s',
log_colors=log_colors)
else:
# no colorlog available:
sh = logging.StreamHandler()
formatter = logging.Formatter('%(name)s:%(levelname)s:%(message)s')
sh.setFormatter(formatter)
sh.setLevel(logging.DEBUG)
_logger.addHandler(sh)
if colorlog is None:
_logger.debug("Module colorlog not available. Log will be b/w.")
return _logger
logger = setup_logger("TemplateTest")
def import_dict(filename: str) -> Dict[str, str]:
""" Read a csv file and interpret each line as entries for a dictionary.
Used to import the exemplary field values from a text file.
"""
ret = {}
with open(filename) as csvfile:
reader = csv.reader(
csvfile,
delimiter=":",
quotechar='"',
quoting=csv.QUOTE_ALL,
escapechar="\\",
skipinitialspace=True
)
for i, row in enumerate(reader):
if not len(row) == 2:
logger.warning("Row {} of file {} does not seem to have "
"the proper format. Skip. "
"More details in debug.".format(i, filename))
logger.debug("We were expecting to find 2 columns "
"but found {}.".format(len(row)))
logger.debug("Here's that row FYI: {}.".format(row))
logger.debug('Please note that only \'"\' can be used as '
'quotation char (not \'u"\' etc)!')
continue
ret[row[0]] = row[1]
return ret
def get_cli_args():
""" Get arguments from command line. """
_ = "Test Anki templates, i.e. fill in field values and CSS to get a " \
"HTML file that can be displayed in the web browser."
parser = argparse.ArgumentParser(description=_)
parser.add_argument("template", help="The HTML template", type=str)
_ = "python file with dictionary containing field values"
parser.add_argument("fields", help=_, type=str)
parser.add_argument("-s", "--style", help="Include CSS style sheet")
_ = "Output HTML file"
parser.add_argument("-o", "--output", help=_, type=str, default="")
return parser.parse_args()
def next_braces(string: str) -> (str, str, str):
"""Go through the string $string and find the next value contained in
double braces, e.g. "before {{foo}} after" would return the triple
("before ", "foo", " after").
Args:
string: Input string.
Returns: (before, enclosed, after)
before: Everything before the double braces
enclosed: String enclosed in the double braces
after: Everything after the double braces.
"""
match = re.search(r"{{([^}]*)}}", string)
if not match:
before = string
enclosed = ""
after = ""
else:
enclosed = match.groups(0)[0]
before = string[:match.span(0)[0]]
after = string[match.span(0)[1]:]
return before, enclosed, after
def is_pos_conditional(enclosed: str) -> bool:
""" True if {{$enclosed}} corresponds to the beginning of an 'if empty'
statement on the Anki card template.
"""
return enclosed.startswith("#")
def is_neg_conditional(enclosed: str) -> bool:
""" True if {{$enclosed}} corresponds to the beginning of an 'if not empty'
statement on the Anki card template.
"""
return enclosed.startswith("^")
def is_close_conditional(enclosed: str) -> bool:
""" True if {{$enclosed}} corresponds to the end of an 'if' statement
on the Anki card template.
"""
return enclosed.startswith("/")
def is_field(enclosed: str) -> bool:
""" True if {{$enclosed}} corresponds to a field value
on the Anki card template.
"""
return enclosed and not is_pos_conditional(enclosed) and not \
is_neg_conditional(enclosed) and not is_close_conditional(enclosed)
def get_field_name(enclosed: str) -> str:
""" Gets the name of the field which is used in the {{$enclosed}}
statement on the Anki card template."""
if is_pos_conditional(enclosed):
return enclosed[1:]
if is_neg_conditional(enclosed):
return enclosed[1:]
if is_close_conditional(enclosed):
return enclosed[1:]
if is_field(enclosed):
enclosed = enclosed.replace("text:", "")
return enclosed
return ""
def evaluate_conditional(string: str, fields: Dict[str, str]) -> bool:
""" Evaluates a conditional from $string on a Anki card template
using the field values $fields. """
field_name = get_field_name(string)
if field_name not in fields:
logger.warning("Field '{}' from conditional '{}' not defined! "
"Will evaluate conditional to "
"True!".format(field_name, string))
return True
if is_pos_conditional(string):
return bool(fields[field_name].strip())
if is_neg_conditional(string):
return not bool(fields[field_name].strip())
raise ValueError
def evaluate_conditional_chain(conditional_chain: List[str],
fields: Dict[str, str]) -> bool:
""" When we go through the template, we encounter conditionals, which
can be enclosed in each other. This means that at any point of the
template we can be inside a list of conditionals ($conditional_chain).
Here we test if the whole $conditional_chain evaluates to true, i.e.
if the code enclosed would be run.
"""
for conditional in conditional_chain:
if not evaluate_conditional(conditional, fields):
return False
return True
def process_line(line: str, conditional_chain: List[str],
fields: Dict[str, str]):
""" Processes a line in the template, i.e. returns the output html code
after evaluating all if statements and filling the fields. Since we
oftentimes are in the middle of several if statements, we need to pass
the current conditional_chain (i.e. the list of if statments the following
line will be subject to) on (and also need to use it).
Args:
line: Line we are processing
conditional_chain: In which conditionals are we currently enclosed?
fields: field values
Returns:
(html output, conditional_chain)
"""
after = line
out = ""
while after:
before, enclosed, after = next_braces(after)
if evaluate_conditional_chain(conditional_chain, fields):
out += before
if is_pos_conditional(enclosed) or is_neg_conditional(enclosed):
conditional_chain.append(enclosed)
elif is_close_conditional(enclosed):
if not len(conditional_chain) >= 1:
_ = "Closing conditional '{}' found, but we didn't encounter" \
" a conditional before.".format(enclosed)
logger.error(_)
else:
field_name = get_field_name(enclosed)
if field_name not in conditional_chain[-1]:
_ = "Closing conditional '{}' found, but the last opened" \
" conditional was {}. I will " \
"ignore this.".format(enclosed, field_name)
logger.error(_)
else:
conditional_chain.pop()
elif is_field(enclosed):
field_name = get_field_name(enclosed)
if field_name in fields:
out += fields[field_name]
else:
_ = "Could not find value for field '{}'".format(field_name)
logger.error(_)
return out, conditional_chain
class TemplateTester(object):
""" Main object that processes the template, the fields and the style
sheet. """
def __init__(self, template: str, fields: Dict[str, str], css: str):
self.html = ""
self.template = template
self.fields = fields
self.css = css
def render(self) -> str:
""" Parse the template and return the html string. """
self._start_html_file()
self._main_loop()
self._end_html_file()
return self.html
def _start_html_file(self):
""" Start html file. """
self.html = "<html>"
self.html += "<head>"
self.html += '<meta http-equiv="Content-Type" content="text/html; ' \
'charset=utf-8" />'
self.html += "<title>{}</title>".format("Rending")
if self.css:
self.html += "<style>"
self.html += self.css
self.html += "</style>"
self.html += "</head>"
self.html += "<body>"
def _end_html_file(self):
""" End html file. """
self.html += "</body>"
self.html += "</html>"
def _main_loop(self):
""" Process template line by line. """
lines = self.template.split("\n")
conditional_chain = []
for line in lines:
out, conditional_chain = process_line(line, conditional_chain,
self.fields)
self.html += out
self.html += "\n"
if __name__ == "__main__":
args = get_cli_args()
if not args.output:
args.output = os.path.splitext(args.template)[0] + "_out.html"
logger.warning("Output file name was automatically "
"set to {}.".format(args.output))
css_ = ""
if args.style:
try:
with open(args.style) as css_file:
css_ = css_file.read()
except:
logger.error("Could not load css file from {}. "
"Leaving it empty.").format(args.style)
template_ = ""
try:
with open(args.template) as template_file:
template_ = template_file.read()
except:
_ = "Could not load template file from {}. " \
"Abort.".format(args.template)
logger.critical(_)
raise
fields_ = {}
if args.fields:
try:
fields_ = import_dict(args.fields)
except:
_ = "Could not load template file from {}. " \
"Abort.".format(args.template)
raise
tt = TemplateTester(template_, fields_, css_)
html_out = tt.render()
if not os.path.exists(os.path.dirname(args.output)):
logger.debug("Creating folder {}.".format(
os.path.dirname(args.output))
)
os.makedirs(os.path.dirname(args.output))
with open(args.output, "w") as output_file:
output_file.write(html_out)
logger.info("Output written to {}".format(os.path.abspath(args.output)))