Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Oliver <excid3@gmail.com>2012-10-09 07:50:35 -0700
committerChris Oliver <excid3@gmail.com>2012-10-09 07:50:35 -0700
commit11610b1e43d49bcf7bf6b4251b793d7c321685aa (patch)
tree9da7e65dca9b4559ea64787abae5e93931ed9ae8
parentdb3f3ea3086cb48d0f1a5b940d7ee182f6839513 (diff)
parent69bdad2ec5e373f1834fe725997fe169b09ae2d2 (diff)
Merge pull request #19 from jaraco/master
HTML Colors from ANSI codes
-rwxr-xr-xlogbot.py51
1 files changed, 49 insertions, 2 deletions
diff --git a/logbot.py b/logbot.py
index 73a3200..80047c7 100755
--- a/logbot.py
+++ b/logbot.py
@@ -37,6 +37,7 @@ import cgi
import os
import ftplib
import sys
+import itertools
from time import strftime
try:
from datetime import datetime
@@ -156,6 +157,52 @@ def write_string(filename, string):
f.write(string)
f.close()
+color_pattern = re.compile(r'(\[\d{1,2}m)')
+"Pattern that matches ANSI color codes and the text that follows"
+
+def pairs(items):
+ """
+ Return pairs from items
+
+ >>> list(pairs([1,2,3,4]))
+ [(1, 2), (3, 4)]
+ """
+ items = iter(items)
+ while True:
+ yield next(items), next(items)
+
+def html_color(input):
+ """
+ >>> html_color("This is plain but [30m this is in color")
+ 'This is plain but <span style="color: #000316"> this is in color</span>'
+ >>> html_color("[32mtwo[37mcolors")
+ '<span style="color: #00aa00">two</span><span style="color: #F5F1DE">colors</span>'
+ """
+ first = []
+ parts = color_pattern.split(input)
+ if len(parts) % 2:
+ # an odd number of parts occurred - first part is uncolored
+ first = [parts.pop(0)]
+ rest = itertools.starmap(replace_color, pairs(parts))
+ return ''.join(itertools.chain(first, rest))
+
+def replace_color(code, text):
+ code = code.lstrip('[').rstrip('m')
+ colors = {
+ '30': '000316',
+ '31': 'aa0000',
+ '32': '00aa00',
+ '33': 'aa5500',
+ '34': '0000aa',
+ '35': 'E850A8',
+ '36': '00aaaa',
+ '37': 'F5F1DE',
+ }
+ return '<span style="color: #%(color)s">%(text)s</span>' % dict(
+ color = colors[code],
+ text = text,
+ )
+
### Logbot class
@@ -199,7 +246,8 @@ class Logbot(SingleServerIRCBot):
try: msg = msg.replace("%channel%", event.target())
except: pass
msg = msg.replace("%color%", self.color(nm_to_n(event.source())))
- try: msg = msg.replace("%message%", cgi.escape(event.arguments()[0]))
+ user_message = cgi.escape(event.arguments()[0])
+ try: msg = msg.replace("%message%", html_color(user_message))
except: pass
return msg
@@ -211,7 +259,6 @@ class Logbot(SingleServerIRCBot):
else:
chans = event.target()
msg = self.format_event(name, event, params)
- msg = re.sub(r'\[(\d{1,2})m', '', msg)
msg = urlify2(msg)
# In case there are still events that don't supply a channel name (like /quit and /nick did)