import re
+from colorsys import rgb_to_hls, hls_to_rgb
+from cgi import escape as html_escape
from datetime import datetime
-# Map of special chars to ascii from Quake's console.c.
-qfont_table = [
+# Map of special chars to ascii from Darkplace's console.c.
+_qfont_table = [
'\0', '#', '#', '#', '#', '.', '#', '#',
'#', '\t', '\n', '#', ' ', '\r', '.', '.',
'[', ']', '0', '1', '2', '3', '4', '5',
'x', 'y', 'z', '{', '|', '}', '~', '<'
]
+# Hex-colored spans for decimal color codes ^0 - ^9
+_dec_spans = [
+ "<span style='color:rgb(128,128,128)'>",
+ "<span style='color:rgb(255,0,0)'>",
+ "<span style='color:rgb(51,255,0)'>",
+ "<span style='color:rgb(255,255,0)'>",
+ "<span style='color:rgb(51,102,255)'>",
+ "<span style='color:rgb(51,255,255)'>",
+ "<span style='color:rgb(255,51,102)'>",
+ "<span style='color:rgb(255,255,255)'>",
+ "<span style='color:rgb(153,153,153)'>",
+ "<span style='color:rgb(128,128,128)'>"
+]
+
+# Color code patterns
+_all_colors = re.compile(r'\^(\d|x[\dA-Fa-f]{3})')
+_dec_colors = re.compile(r'\^(\d)')
+_hex_colors = re.compile(r'\^x([\dA-Fa-f])([\dA-Fa-f])([\dA-Fa-f])')
+
+# On a light scale of 0 (black) to 1.0 (white)
+_contrast_threshold = 0.5
+
def qfont_decode(qstr=''):
+ """ Convert the qfont characters in a string to ascii.
"""
- Convert the qfont characters in a string to ascii.
- """
+ if qstr == None:
+ qstr = ''
chars = []
for c in qstr:
- if c >= u'\ue000' and c <= u'\ue0ff':
- c = qfont_table[ord(c) - 0xe000]
+ if u'\ue000' <= c <= u'\ue0ff':
+ c = _qfont_table[ord(c) - 0xe000]
chars.append(c)
return ''.join(chars)
-def strip_colors(str=''):
- if str is None:
- str = ''
+def strip_colors(qstr=''):
+ if qstr == None:
+ qstr = ''
+ return _all_colors.sub('', qstr)
+
- str = re.sub(r'\^x\w\w\w', '', str)
- str = re.sub(r'\^\d', '', str)
- return str
+def hex_repl(match):
+ """Convert Darkplaces hex color codes to CSS rgb.
+ Brighten colors with HSL light value less than 50%"""
+ # Extend hex char to 8 bits and to 0.0-1.0 scale
+ r = int(match.group(1) * 2, 16) / 255.0
+ g = int(match.group(2) * 2, 16) / 255.0
+ b = int(match.group(3) * 2, 16) / 255.0
-def html_colors(str=''):
- if str is None:
- str = ''
+ # Check if color is too dark
+ hue, light, satur = rgb_to_hls(r, g, b)
+ if light < _contrast_threshold:
+ light = _contrast_threshold
+ r, g, b = hls_to_rgb(hue, light, satur)
- orig = str
- str = re.sub(r'\^x(\w)(\w)(\w)',
- "<span style='color:#\g<1>\g<1>\g<2>\g<2>\g<3>\g<3>'>", str)
- str = re.sub(r'\^1', "<span style='color:#FF9900'>", str)
- str = re.sub(r'\^2', "<span style='color:#33FF00'>", str)
- str = re.sub(r'\^3', "<span style='color:#FFFF00'>", str)
- str = re.sub(r'\^4', "<span style='color:#3366FF'>", str)
- str = re.sub(r'\^5', "<span style='color:#33FFFF'>", str)
- str = re.sub(r'\^6', "<span style='color:#FF3366'>", str)
- str = re.sub(r'\^7', "<span style='color:#FFFFFF'>", str)
- str = re.sub(r'\^8', "<span style='color:#999999'>", str)
- str = re.sub(r'\^9', "<span style='color:#666666'>", str)
- str = re.sub(r'\^0', "<span style='color:#333333'>", str)
+ # Convert back to 0-255 scale for css
+ return '<span style="color:rgb(%d,%d,%d)">' % (255 * r, 255 * g, 255 * b)
- for span in range(len(re.findall(r'\^x\w\w\w|\^\d', orig))):
- str += "</span>"
- return str
+def html_colors(qstr=''):
+ qstr = html_escape(qfont_decode(qstr).replace('^^', '^'))
+ html = _dec_colors.sub(lambda match: _dec_spans[int(match.group(1))], qstr)
+ html = _hex_colors.sub(hex_repl, html)
+ return html + "</span>" * len(_all_colors.findall(qstr))
def page_url(page):
pretty string like 'an hour ago', 'Yesterday', '3 months ago',
'just now', etc
"""
- now = datetime.now()
+ now = datetime.utcnow()
if type(time) is int:
diff = now - datetime.fromtimestamp(time)
elif isinstance(time,datetime):