Created
February 19, 2013 19:50
-
-
Save morsik/4989218 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import template | |
from django.utils.safestring import mark_safe | |
import re | |
register = template.Library() | |
@register.filter | |
def colorize(t, a='white'): | |
if t == None: | |
return None | |
t = t.replace(" ", " ") | |
t = re.sub("(\^)?$", "^", t) | |
t = re.sub("\^\^", "^^", t) | |
t = re.sub("(\^)?$", "", t) | |
t = re.sub("\^<", "^|", t) | |
t = re.sub("\<", "<", t) | |
t = re.sub("\^>", "^^", t) | |
t = re.sub("\>", ">", t) | |
t = re.sub("\^\^", "^>", t) | |
colors = { | |
'000000' : [ '0', 'P', 'p', ], | |
'ff0000' : [ '1', 'Q', 'q', ], | |
'00ff00' : [ '2', 'R', 'r', ], | |
'ffff00' : [ '3', 'S', 's', ], | |
'0000ff' : [ '4', 'T', 't', ], | |
'00ffff' : [ '5', 'U', 'u', ], | |
'ff00ff' : [ '6', 'V', 'v', ], | |
'ffffff' : [ '7', 'W', 'w', ], | |
'ff7f00' : [ '8', 'X', 'x', ], | |
'7f7f7f' : [ '9', 'Y', 'y', ], | |
'bfbfbf' : [ ':', 'Z', 'z', ';', '[', '{', ], | |
'007f00' : [ '<', '\\', '|', ], | |
'7f7f00' : [ '=', ']', '}', ], | |
'00007f' : [ '>', '^', '~', ], | |
'7f0000' : [ '?', '_', ], | |
'7f3f00' : [ '@', '`', ], | |
'ff9919' : [ 'A', 'a', '!', ], | |
'007f7f' : [ 'B', 'b', '"', ], | |
'7f007f' : [ 'C', 'c', '#', ], | |
'007fff' : [ 'D', 'd', '$', ], | |
'7F00ff' : [ 'E', 'e', '%', ], | |
'3399cc' : [ 'F', 'f', '&', ], | |
'ccffcc' : [ 'G', 'g', '\'',], | |
'006633' : [ 'H', 'h', '(', ], | |
'ff0033' : [ 'I', 'i', ')', ], | |
'b21919' : [ 'J', 'j', '*', ], | |
'993300' : [ 'K', 'k', '+', ], | |
'cc9933' : [ 'L', 'l', ',', ], | |
'999933' : [ 'M', 'm', '-', ], | |
'ffffbf' : [ 'N', 'n', '.', ], | |
'ffff7f' : [ 'O', 'o', '/', ], | |
} | |
for color, opts in colors.iteritems(): | |
for code in opts: | |
t = t.replace('^^%s' % code + code, '</span><span style="color:#%s">' % color) | |
: | |
for color, opts in colors.iteritems(): | |
for code in opts: | |
t = t.replace('^%s' % code, '</span><span style="color:#%s">' % color) | |
return mark_safe('<span style="color:%s">%s</span>' % (a, t)) | |
@register.filter | |
def wrap(t): | |
return t.replace(' ', ' ') | |
colorize.is_safe = True | |
wrap.is_safe = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment