3 from datetime import datetime
5 # Map of special chars to ascii from Darkplace's console.c.
7 '\0', '#', '#', '#', '#', '.', '#', '#',
8 '#', '\t', '\n', '#', ' ', '\r', '.', '.',
9 '[', ']', '0', '1', '2', '3', '4', '5',
10 '6', '7', '8', '9', '.', '<', '=', '>',
11 ' ', '!', '"', '#', '$', '%', '&', '\'',
12 '(', ')', '*', '+', ',', '-', '.', '/',
13 '0', '1', '2', '3', '4', '5', '6', '7',
14 '8', '9', ':', ';', '<', '=', '>', '?',
15 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
16 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
17 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
18 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
19 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
20 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
21 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
22 'x', 'y', 'z', '{', '|', '}', '~', '<',
24 '<', '=', '>', '#', '#', '.', '#', '#',
25 '#', '#', ' ', '#', ' ', '>', '.', '.',
26 '[', ']', '0', '1', '2', '3', '4', '5',
27 '6', '7', '8', '9', '.', '<', '=', '>',
28 ' ', '!', '"', '#', '$', '%', '&', '\'',
29 '(', ')', '*', '+', ',', '-', '.', '/',
30 '0', '1', '2', '3', '4', '5', '6', '7',
31 '8', '9', ':', ';', '<', '=', '>', '?',
32 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
33 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
34 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
35 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
36 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
37 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
38 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
39 'x', 'y', 'z', '{', '|', '}', '~', '<'
42 # Hex-colored spans for decimal color codes ^0 - ^9
44 "<span style='color:#333333'>",
45 "<span style='color:#FF0000'>",
46 "<span style='color:#33FF00'>",
47 "<span style='color:#FFFF00'>",
48 "<span style='color:#3366FF'>",
49 "<span style='color:#33FFFF'>",
50 "<span style='color:#FF3366'>",
51 "<span style='color:#FFFFFF'>",
52 "<span style='color:#999999'>",
53 "<span style='color:#666666'>"
57 _all_colors = re.compile(r'\^(\d|x[\dA-Fa-f]{3})')
58 _dec_colors = re.compile(r'\^(\d)')
59 _hex_colors = re.compile(r'\^x([\dA-Fa-f])([\dA-Fa-f])([\dA-Fa-f])')
62 def qfont_decode(qstr=''):
63 """ Convert the qfont characters in a string to ascii.
69 if u'\ue000' <= c <= u'\ue0ff':
70 c = _qfont_table[ord(c) - 0xe000]
75 def strip_colors(qstr=''):
78 return _all_colors.sub('', qstr)
81 def html_colors(qstr=''):
82 qstr = escape(qfont_decode(qstr))
84 return _dec_spans[int(match.group(1))]
85 qstr = qstr.replace('^^', '^')
86 html = _dec_colors.sub(dec_repl, qstr)
87 html = _hex_colors.sub(r"<span style='color:#\1\1\2\2\3\3'>", html)
88 return html + "</span>" * len(_all_colors.findall(qstr))
92 return current_route_url(request, page=page, _query=request.GET)
95 def pretty_date(time=False):
97 Get a datetime object or a int() Epoch timestamp and return a
98 pretty string like 'an hour ago', 'Yesterday', '3 months ago',
102 if type(time) is int:
103 diff = now - datetime.fromtimestamp(time)
104 elif isinstance(time,datetime):
108 second_diff = diff.seconds
118 return str(second_diff) + " seconds ago"
119 if second_diff < 120:
120 return "a minute ago"
121 if second_diff < 3600:
122 return str( second_diff / 60 ) + " minutes ago"
123 if second_diff < 7200:
125 if second_diff < 86400:
126 return str( second_diff / 3600 ) + " hours ago"
130 return str(day_diff) + " days ago"
135 return str(day_diff/7) + " weeks ago"
140 return str(day_diff/30) + " months ago"
142 if day_diff/365 == 1:
145 return str(day_diff/365) + " years ago"