-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.py
76 lines (62 loc) · 2.17 KB
/
process.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
import bleach
import re
def clean_input(string):
"""
Sanatises invalid tags/attributes & strip white space from strings
"""
allowed_tags = ['\r', '\n',
'a', 'abbr', 'acronym', 'address', 'b', 'br', 'div', 'dl', 'dt',
'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img',
'li', 'ol', 'p', 'pre', 'q', 's', 'small', 'strike', 'strong',
'span', 'sub', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th',
'thead', 'tr', 'tt', 'u', 'ul']
allowed_attrs = {
'a': ['href', 'target', 'title'],
'img': ['src', 'alt', 'width', 'height'],
}
string = bleach.clean(string, tags=allowed_tags, attributes=allowed_attrs)
string = string.rstrip().lstrip()
return string
def title_process(title):
"""
Process title field.
"""
title = clean_input(title)
if len(title) == 0:
title = "your wall of text"
output_title = ""
list_of_words = title.split()
for elem in list_of_words:
if len(output_title) > 0:
output_title = output_title + " " + elem.strip().capitalize()
else:
output_title = elem.capitalize()
if not output_title:
return title
else:
return output_title
def text_process(text):
"""
Process user input field.
text, takes user sumitted plain text and formats it as per the WoT
specification.
"""
if (text[-1] != "."):
text = text + "."
text = re.sub("\r\n\r\n", "<hr>", text)
text = clean_input(text)
text_list = re.findall('.*?[.!\?]+', text)
# TODO: also need to check for speach in text ""
new_list = []
for item in text_list:
new_string = item.lstrip()
new_string = new_string.capitalize()
new_string = "<li>" + new_string + "</li>"
new_string = re.sub('<li><hr>', '<hr><br><li>', new_string)
# FIXME: edge case, this does not capatalize first letter on new line
# due to additional <hr> tags at start of list item.:w
new_list.append(new_string)
str1 = ""
for ele in new_list:
str1 += ele
return str1