forked from jdossgollin/my-cv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.py
48 lines (37 loc) · 1.36 KB
/
filters.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
"""
Filters for printing TeX and Markdown to jinja templates.
See http://flask.pocoo.org/snippets/55/ for more info.
"""
import re
LATEX_SUBS = (
(re.compile(r'\\'), r'\\textbackslash'),
(re.compile(r'([#%&{}])'), r'\\\1'),
(re.compile(r'~'), r'\~{}'),
(re.compile(r'\^'), r'\^{}'),
(re.compile(r'^"'), r"``"),
(re.compile(r'"$'), r"''"),
(re.compile(r'\.\.\.+'), r'\\ldots')
)
def escape_tex(value):
"""
Escape TeX special characters
"""
newval = value
for pattern, replacement in LATEX_SUBS:
newval = pattern.sub(replacement, newval, re.MULTILINE)
return newval
def select_by_attr_name(array, attr, value):
for d in array:
if d[attr] == value:
return d
def sort_by_attr(array, attr, reverse=False):
if type(attr) is list:
sorted_array = sorted(array, key=lambda x: tuple(str(x[a]) for a in attr), reverse=reverse)
else:
sorted_array = sorted(array, key=lambda x: str(x[a] for a in attr), reverse=reverse)
return sorted_array
def sort_first_year(array, attr, reverse=False):
return sorted(array, key=lambda x: int(re.findall(r'^\d{4}', str(x[attr]))[0]), reverse=reverse)
def sort_advisees(array, reverse=False):
sort_order = {"Postdoctoral": 0, "Graduate": 1, "Undergraduate": 2}
return sorted(array, key=lambda val: sort_order[val[0]], reverse=reverse)