Skip to content

Instantly share code, notes, and snippets.

@morsik
Created February 19, 2013 19:50
Show Gist options
  • Save morsik/4989218 to your computer and use it in GitHub Desktop.
Save morsik/4989218 to your computer and use it in GitHub Desktop.
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("\^\^", "&#94^", 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('&nbsp;', ' ')
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