-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
72 lines (58 loc) · 1.89 KB
/
utils.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
import re
# code_regexp = r'[\w+-\.]+/([^/]*/[\d]{2})'
code_regexp = r'[^/]+/([^/]*/[\d]{2})'
def parse_code(code):
"""
Get the short course code from the full course code.
"""
if re.search(r'/.+/', code):
# ak obsahuje FMFI, tak skracuj
m=re.match(r'[^/]*FMFI[^/]*/([^/]+/[\d]{2})',code)
if m:
return m.group(1)
else:
return code
else:
return False
def replace_codes(text, lang='sk', add_links=False, courses={}, and_symbol=', '):
"""
Replace all occurences of full course codes with short course codes.
"""
if not text:
return ""
# AIS2 bugs
text=text.replace('alebo',' alebo ')
text=text.replace('and lebo',' or ')
m=re.split('([()\s]+)',text,flags=re.UNICODE)
result=""
for w in m:
shortcode = parse_code(w)
if shortcode:
if add_links:
title = '%s %s' % (shortcode, courses[shortcode]) if shortcode in courses else shortcode
result+=make_link_from_code(shortcode, lang=lang, title=title)
else:
result+=shortcode
else:
result+=w
return result
def get_text(node):
"""
Get text from elements with multiple <p> tags.
"""
if not node.findall('p'):
return node.text
return ''.join('<p>'+n.text+'</p>' for n in node.findall('p'))
def get_url(code, lang='sk'):
if code == '' or not lang in ['sk', 'en']:
return False
# return "https://sluzby.fmph.uniba.sk/infolist/%s/%s.html" % (lang, code)
link = code.replace("/","_")
return "%s.html" % (link)
def make_link_from_code(code, title='', lang='sk'):
url = get_url(code, lang)
return make_link(url, text=title)
def make_link(href, text, title=''):
if not title == '':
title = ' title="%s"' % title
return '<a href="%s"%s>%s</a>' % (href, title, text)