2 from colorsys import rgb_to_hls, hls_to_rgb
3 from cgi import escape as html_escape
4 from datetime import datetime, timedelta
6 # Map of special chars to ascii from Darkplace's console.c.
8 '\0', '#', '#', '#', '#', '.', '#', '#',
9 '#', '\t', '\n', '#', ' ', '\r', '.', '.',
10 '[', ']', '0', '1', '2', '3', '4', '5',
11 '6', '7', '8', '9', '.', '<', '=', '>',
12 ' ', '!', '"', '#', '$', '%', '&', '\'',
13 '(', ')', '*', '+', ',', '-', '.', '/',
14 '0', '1', '2', '3', '4', '5', '6', '7',
15 '8', '9', ':', ';', '<', '=', '>', '?',
16 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
17 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
18 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
19 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
20 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
21 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
22 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
23 'x', 'y', 'z', '{', '|', '}', '~', '<',
25 '<', '=', '>', '#', '#', '.', '#', '#',
26 '#', '#', ' ', '#', ' ', '>', '.', '.',
27 '[', ']', '0', '1', '2', '3', '4', '5',
28 '6', '7', '8', '9', '.', '<', '=', '>',
29 ' ', '!', '"', '#', '$', '%', '&', '\'',
30 '(', ')', '*', '+', ',', '-', '.', '/',
31 '0', '1', '2', '3', '4', '5', '6', '7',
32 '8', '9', ':', ';', '<', '=', '>', '?',
33 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
34 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
35 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
36 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
37 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
38 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
39 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
40 'x', 'y', 'z', '{', '|', '}', '~', '<'
43 # Hex-colored spans for decimal color codes ^0 - ^9
45 "<span style='color:rgb(128,128,128)'>",
46 "<span style='color:rgb(255,0,0)'>",
47 "<span style='color:rgb(51,255,0)'>",
48 "<span style='color:rgb(255,255,0)'>",
49 "<span style='color:rgb(51,102,255)'>",
50 "<span style='color:rgb(51,255,255)'>",
51 "<span style='color:rgb(255,51,102)'>",
52 "<span style='color:rgb(255,255,255)'>",
53 "<span style='color:rgb(153,153,153)'>",
54 "<span style='color:rgb(128,128,128)'>"
58 _all_colors = re.compile(r'\^(\d|x[\dA-Fa-f]{3})')
59 _dec_colors = re.compile(r'\^(\d)')
60 _hex_colors = re.compile(r'\^x([\dA-Fa-f])([\dA-Fa-f])([\dA-Fa-f])')
62 # On a light scale of 0 (black) to 1.0 (white)
63 _contrast_threshold = 0.5
66 def qfont_decode(qstr=''):
67 """ Convert the qfont characters in a string to ascii.
73 if u'\ue000' <= c <= u'\ue0ff':
74 c = _qfont_table[ord(c) - 0xe000]
79 def strip_colors(qstr=''):
82 return _all_colors.sub('', qstr)
86 """Convert Darkplaces hex color codes to CSS rgb.
87 Brighten colors with HSL light value less than 50%"""
89 # Extend hex char to 8 bits and to 0.0-1.0 scale
90 r = int(match.group(1) * 2, 16) / 255.0
91 g = int(match.group(2) * 2, 16) / 255.0
92 b = int(match.group(3) * 2, 16) / 255.0
94 # Check if color is too dark
95 hue, light, satur = rgb_to_hls(r, g, b)
96 if light < _contrast_threshold:
97 light = _contrast_threshold
98 r, g, b = hls_to_rgb(hue, light, satur)
100 # Convert back to 0-255 scale for css
101 return '<span style="color:rgb(%d,%d,%d)">' % (255 * r, 255 * g, 255 * b)
104 def html_colors(qstr=''):
105 qstr = html_escape(qfont_decode(qstr).replace('^^', '^'))
106 html = _dec_colors.sub(lambda match: _dec_spans[int(match.group(1))], qstr)
107 html = _hex_colors.sub(hex_repl, html)
108 return html + "</span>" * len(_all_colors.findall(qstr))
112 return current_route_url(request, page=page, _query=request.GET)
115 def pretty_date(time=False):
116 '''Returns a human-readable relative date.'''
117 now = datetime.utcnow()
118 if type(time) is int:
119 diff = now - datetime.fromtimestamp(time)
120 elif isinstance(time,datetime):
123 print "not a time value"
126 dim = round(diff.seconds/60.0 + diff.days*1440.0)
129 return "less than a minute ago"
131 return "1 minute ago"
132 elif dim >= 2 and dim <= 44:
133 return "{0} minutes ago".format(int(dim))
134 elif dim >= 45 and dim <= 89:
135 return "about 1 hour ago"
136 elif dim >= 90 and dim <= 1439:
137 return "about {0} hours ago".format(int(round(dim/60.0)))
138 elif dim >= 1440 and dim <= 2519:
140 elif dim >= 2520 and dim <= 43199:
141 return "{0} days ago".format(int(round(dim/1440.0)))
142 elif dim >= 43200 and dim <= 86399:
143 return "about 1 month ago"
144 elif dim >= 86400 and dim <= 525599:
145 return "{0} months ago".format(int(round(dim/43200.0)))
146 elif dim >= 525600 and dim <= 655199:
147 return "about 1 year ago"
148 elif dim >= 655200 and dim <= 914399:
149 return "over 1 year ago"
150 elif dim >= 914400 and dim <= 1051199:
151 return "almost 2 years ago"
153 return "about {0} years ago".format(int(round(dim/525600.0)))