-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathformatting.py
174 lines (147 loc) · 5.48 KB
/
formatting.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
import string
import cgi
import re
from settings import Settings
from database import *
def tripcode(pw):
"""
Calculate tripcode to match output of most imageboards
"""
from crypt import crypt
try:
pw = pw.encode("sjis", "ignore")
except:
pass
pw = pw.replace('"', """) \
.replace("'", "'") \
.replace("<", "<") \
.replace(">", ">") \
.replace(",", ",")
salt = re.sub(r"[^\.-z]", ".", (pw + "H..")[1:3])
salt = salt.translate(string.maketrans(r":;=?@[\]^_`", "ABDFGabcdef"))
return crypt(pw, salt)[-10:]
def nameBlock(post_name, post_tripcode, post_email, post_timestamp_formatted):
"""
Creates a string containing HTML formatted poster name data. This saves quite
a bit of time when templating pages, as it saves the engine a few conditions
per post, which adds up over the time of processing entire pages
"""
board = Settings._.BOARD
nameblock = ""
if post_name == "" and post_tripcode == "":
post_anonymous = True
else:
post_anonymous = False
if board["settings"]["anonymous"] == "" and (post_anonymous or board["settings"]["forced_anonymous"]):
if post_email:
nameblock += '<a href="mailto:' + post_email + '">'
nameblock += post_timestamp_formatted
if post_email:
nameblock += "</a>"
else:
if post_anonymous:
nameblock += '<span class="postername">'
if post_email:
nameblock += '<a href="mailto:' + post_email + '">' + board['settings']['anonymous'] + '</a>'
else:
nameblock += board["settings"]["anonymous"]
nameblock += "</span>"
else:
if post_email:
nameblock += '<a href="mailto:' + post_email + '">'
nameblock += '<span class="postername">'
if post_name:
nameblock += post_name
else:
if not post_tripcode:
nameblock += board["settings"]["anonymous"]
if post_tripcode:
nameblock += '</span><span class="postertrip">' + board['settings']['tripcode_character'] + post_tripcode
nameblock += "</span>"
if post_email:
nameblock += "</a>"
nameblock += " " + post_timestamp_formatted
return nameblock
def cleanString(string, escape=True):
string = string.strip()
if escape:
string = cgi.escape(string)
return string
def clickableURLs(message):
prog = re.compile(r"\b(http|ftp|https)://\S+(\b|/)|\b[-.\w]+@[-.\w]+")
i = 0
urllist = []
while 1:
m = prog.search(message, i)
if not m:
break
j = m.start()
urllist.append(message[i:j])
i = j
url = m.group(0)
while url[-1] in '();:,.?\'"<>':
url = url[:-1]
i = i + len(url)
if ':' in url:
repl = '<a href="%s">%s</a>' % (url, url)
else:
repl = '<a href="mailto:%s"><%s></a>' % (url, url)
urllist.append(repl)
j = len(message)
urllist.append(message[i:j])
return string.join(urllist, "")
def checkRefLinks(message, parentid):
"""
Check for >># links in posts and replace with the HTML to make them clickable
"""
board = Settings._.BOARD
message = re.compile(r">>([0-9]+)").sub('<a href="' + Settings.BOARDS_URL + board['dir'] + '/res/' + str(parentid) + r'.html#\1" onclick="javascript:highlight(' + '\'' + r'\1' + '\'' + r', true);">>>\1</a>', message)
return message
def matchCrossThreadRefLinks(matchobj):
board = Settings._.BOARD
postid = matchobj.group(1)
try:
parentid = FetchOne("SELECT `parentid` FROM `posts` WHERE `id` = '%s' AND `boardid` = '%s' LIMIT 1" % (postid, board['id']))["parentid"]
except:
return matchobj.group(0)
if parentid == "0":
parentid = postid
return '<a href="' + Settings.BOARDS_URL + board['dir'] + '/res/' + str(parentid) + '.html#' + postid + '">>>>­' + postid + '</a>'
def checkCrossThreadRefLinks(message):
"""
Check for >>># links in posts and replace with the HTML to make them clickable
"""
board = Settings._.BOARD
message = re.compile(r">>>([0-9]+)").sub(matchCrossThreadRefLinks, message)
return message
def checkQuotes(message):
"""
Check for >text in posts and add span around it to color according to the css
"""
message = re.compile(r"^>(.*)$", re.MULTILINE).sub(r'<span class="unkfunc">>\1</span>', message)
return message
def escapeHTML(string):
string = string.replace('<', '<')
string = string.replace('>', '>')
return string
def matchPre(matchobj):
return "<pre>" + matchobj.group(1).replace("\n", "") + "</pre>"
def matchAA(matchobj):
return "<div class=\"aa\">" + matchobj.group(1).replace("\n", "") + "</div>"
def onlyAllowedHTML(message):
"""
Allow <b>, <i>, <u>, <strike>, and <pre> in posts, along with the special <aa>
"""
message = re.compile(r"<b>(.+?)</b>", re.DOTALL | re.IGNORECASE).sub(r"<b>\1</b>", message)
message = re.compile(r"<i>(.+?)</i>", re.DOTALL | re.IGNORECASE).sub(r"<i>\1</i>", message)
message = re.compile(r"<u>(.+?)</u>", re.DOTALL | re.IGNORECASE).sub(r"<u>\1</u>", message)
message = re.compile(r"<strike>(.+?)</strike>", re.DOTALL | re.IGNORECASE).sub(r"<strike>\1</strike>", message)
message = re.compile(r"<pre>(.+?)</pre>", re.DOTALL | re.IGNORECASE).sub(matchPre, message)
message = re.compile(r"<aa>(.+?)</aa>", re.DOTALL | re.IGNORECASE).sub(matchAA, message)
return message
def markdown(message):
import markdown
if message.strip() != "":
return markdown.markdown(message).rstrip("\n").rstrip("<br>")
else:
return ""