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