pesterchum/parsetools.py

273 lines
8.4 KiB
Python
Raw Normal View History

2011-02-04 16:17:27 -05:00
import re
2011-02-11 04:07:07 -05:00
from copy import copy
from datetime import timedelta
2011-02-04 16:17:27 -05:00
from PyQt4 import QtGui
2011-02-13 20:32:02 -05:00
from generic import mysteryTime
2011-02-10 13:00:06 -05:00
_ctag_begin = re.compile(r'(?i)<c=(.*?)>')
2011-02-13 04:27:12 -05:00
_gtag_begin = re.compile(r'(?i)<g[a-f]>')
2011-02-11 18:37:31 -05:00
_ctag_end = re.compile(r'(?i)</c>')
2011-02-04 16:17:27 -05:00
_ctag_rgb = re.compile(r'\d+,\d+,\d+')
2011-02-11 18:37:31 -05:00
_urlre = re.compile(r"(?i)http://[^\s]+")
2011-02-13 04:27:12 -05:00
_memore = re.compile(r"(\s|^)(#[A-Za-z0-9_]+)")
_imgre = re.compile(r"""(?i)<img src=['"](\S+)['"]\s*/>""")
_mecmdre = re.compile(r"^/me(\S*)")
2011-02-04 16:17:27 -05:00
2011-02-11 04:07:07 -05:00
def lexer(string, objlist):
"""objlist is a list: [(objecttype, re),...] list is in order of preference"""
stringlist = [string]
for (oType, regexp) in objlist:
2011-02-11 18:37:31 -05:00
newstringlist = []
2011-02-11 04:07:07 -05:00
for (stri, s) in enumerate(stringlist):
2011-02-11 18:37:31 -05:00
if type(s) not in [str, unicode]:
newstringlist.append(s)
continue
2011-02-11 04:07:07 -05:00
lasti = 0
2011-02-11 18:37:31 -05:00
for m in regexp.finditer(s):
2011-02-11 04:07:07 -05:00
start = m.start()
end = m.end()
2011-02-11 18:37:31 -05:00
tag = oType(m.group(0), *m.groups())
if lasti != start:
newstringlist.append(s[lasti:start])
newstringlist.append(tag)
lasti = end
2011-02-11 04:07:07 -05:00
if lasti < len(string):
2011-02-11 18:37:31 -05:00
newstringlist.append(s[lasti:])
2011-02-11 04:07:07 -05:00
stringlist = copy(newstringlist)
return stringlist
2011-02-11 18:37:31 -05:00
class colorBegin(object):
def __init__(self, string, color):
self.string = string
self.color = color
def convert(self, format):
color = self.color
2011-02-13 04:27:12 -05:00
if format == "text":
return ""
2011-02-04 16:17:27 -05:00
if _ctag_rgb.match(color) is not None:
if format=='ctag':
2011-02-04 19:50:56 -05:00
return "<c=%s>" % (color)
2011-02-04 16:17:27 -05:00
try:
qc = QtGui.QColor(*[int(c) for c in color.split(",")])
except ValueError:
qc = QtGui.QColor("black")
else:
qc = QtGui.QColor(color)
if not qc.isValid():
qc = QtGui.QColor("black")
if format == "html":
return '<span style="color:%s">' % (qc.name())
elif format == "bbcode":
return '[color=%s]' % (qc.name())
elif format == "ctag":
(r,g,b,a) = qc.getRgb()
return '<c=%s,%s,%s>' % (r,g,b)
2011-02-11 18:37:31 -05:00
class colorEnd(object):
def __init__(self, string):
self.string = string
def convert(self, format):
if format == "html":
return "</span>"
elif format == "bbcode":
return "[/color]"
2011-02-13 04:27:12 -05:00
elif format == "text":
return ""
2011-02-11 18:37:31 -05:00
else:
return self.string
class hyperlink(object):
def __init__(self, string):
self.string = string
def convert(self, format):
if format == "html":
return "<a href='%s'>%s</a>" % (self.string, self.string)
elif format == "bbcode":
return "[url]%s[/url]" % (self.string)
else:
return self.string
2011-02-13 04:27:12 -05:00
class imagelink(object):
def __init__(self, string, img):
self.string = string
self.img = img
def convert(self, format):
if format == "html":
return self.string
elif format == "bbcode":
if self.img[0:7] == "http://":
return "[img]%s[/img]" % (img)
else:
return ""
else:
return ""
class memolex(object):
def __init__(self, string, space, channel):
self.string = string
self.space = space
self.channel = channel
def convert(self, format):
if format == "html":
return "%s<a href='%s'>%s</a>" % (self.space, self.channel, self.channel)
else:
return self.string
2011-02-11 18:37:31 -05:00
class smiley(object):
def __init__(self, string):
self.string = string
def convert(self, format):
if format == "html":
return "<img src='smilies/%s' />" % (smiledict[self.string])
else:
return self.string
2011-02-13 04:27:12 -05:00
class mecmd(object):
def __init__(self, string, suffix):
self.string = string
self.suffix = suffix
def convert(self, format):
return self.string
2011-02-04 16:17:27 -05:00
2011-02-13 04:27:12 -05:00
def lexMessage(string):
lexlist = [(mecmd, _mecmdre),
(colorBegin, _ctag_begin), (colorBegin, _gtag_begin),
(colorEnd, _ctag_end), (imagelink, _imgre),
(hyperlink, _urlre), (memolex, _memore),
2011-02-11 18:37:31 -05:00
(smiley, _smilere)]
2011-02-13 04:27:12 -05:00
lexed = lexer(unicode(string), lexlist)
2011-02-11 18:37:31 -05:00
balanced = []
beginc = 0
endc = 0
for o in lexed:
if type(o) is colorBegin:
beginc += 1
balanced.append(o)
elif type(o) is colorEnd:
if beginc >= endc:
endc += 1
balanced.append(o)
else:
balanced.append(o.string)
2011-02-04 16:17:27 -05:00
else:
2011-02-11 18:37:31 -05:00
balanced.append(o)
if beginc > endc:
for i in range(0, beginc-endc):
balanced.append(colorEnd("</c>"))
2011-02-21 14:07:59 -05:00
if len(balanced) == 0:
balanced.append("")
2011-02-13 04:27:12 -05:00
if type(balanced[len(balanced)-1]) not in [str, unicode]:
balanced.append("")
return balanced
def convertTags(lexed, format="html"):
if format not in ["html", "bbcode", "ctag", "text"]:
raise ValueError("Color format not recognized")
2011-02-11 18:37:31 -05:00
2011-02-13 04:27:12 -05:00
if type(lexed) in [str, unicode]:
lexed = lexMessage(lexed)
2011-02-11 18:37:31 -05:00
escaped = ""
2011-02-13 04:27:12 -05:00
firststr = True
for (i, o) in enumerate(lexed):
2011-02-11 18:37:31 -05:00
if type(o) in [str, unicode]:
if format == "html":
escaped += o.replace("&", "&amp;").replace(">", "&gt;").replace("<","&lt;")
2011-02-04 16:17:27 -05:00
else:
2011-02-11 18:37:31 -05:00
escaped += o
2011-02-04 16:17:27 -05:00
else:
2011-02-11 18:37:31 -05:00
escaped += o.convert(format)
2011-02-13 04:27:12 -05:00
2011-02-11 18:37:31 -05:00
return escaped
def addTimeInitial(string, grammar):
endofi = string.find(":")
endoftag = string.find(">")
2011-02-13 04:27:12 -05:00
# support Doc Scratch mode
if (endoftag < 0 or endoftag > 16) or (endofi < 0 or endofi > 17):
return string
return string[0:endoftag+1]+grammar.pcf+string[endoftag+1:endofi]+grammar.number+string[endofi:]
def timeProtocol(cmd):
dir = cmd[0]
2011-02-13 20:32:02 -05:00
if dir == "?":
return mysteryTime(0)
cmd = cmd[1:]
cmd = re.sub("[^0-9:]", "", cmd)
try:
l = [int(x) for x in cmd.split(":")]
except ValueError:
l = [0,0]
timed = timedelta(0, l[0]*3600+l[1]*60)
if dir == "P":
timed = timed*-1
return timed
2011-02-05 13:56:25 -05:00
def timeDifference(td):
2011-02-13 20:32:02 -05:00
if type(td) is mysteryTime:
return "??:?? FROM ????"
2011-02-05 13:56:25 -05:00
if td < timedelta(0):
when = "AGO"
else:
when = "FROM NOW"
atd = abs(td)
minutes = (atd.days*86400 + atd.seconds) // 60
hours = minutes // 60
leftoverminutes = minutes % 60
if atd == timedelta(0):
timetext = "RIGHT NOW"
elif atd < timedelta(0,3600):
2011-02-06 01:02:39 -05:00
if minutes == 1:
timetext = "%d MINUTE %s" % (minutes, when)
else:
timetext = "%d MINUTES %s" % (minutes, when)
2011-02-05 13:56:25 -05:00
elif atd < timedelta(0,3600*100):
2011-02-06 01:02:39 -05:00
if hours == 1 and leftoverminutes == 0:
timetext = "%d:%02d HOUR %s" % (hours, leftoverminutes, when)
else:
timetext = "%d:%02d HOURS %s" % (hours, leftoverminutes, when)
2011-02-05 13:56:25 -05:00
else:
timetext = "%d HOURS %s" % (hours, when)
return timetext
2011-02-08 17:47:07 -05:00
smiledict = {
":rancorous:": "pc_rancorous.gif",
":apple:": "apple.gif",
2011-02-11 14:08:45 -05:00
":bathearst:": "bathearst.gif",
2011-02-22 19:49:47 -05:00
":cathearst:": "cathearst.png",
":woeful:": "pc_bemused.gif",
":sorrow:": "blacktear.gif",
2011-02-08 17:47:07 -05:00
":pleasant:": "pc_pleasant.gif",
":blueghost:": "blueslimer.gif",
2011-02-14 15:04:57 -05:00
":slimer:": "slimer.gif",
2011-02-08 17:47:07 -05:00
":candycorn:": "candycorn.gif",
":cheer:": "cheer.gif",
":duhjohn:": "confusedjohn.gif",
":datrump:": "datrump.gif",
":facepalm:": "facepalm.gif",
":bonk:": "headbonk.gif",
":mspa:": "mspa_face.gif",
":gun:": "mspa_reader.gif",
":cal:": "lilcal.png",
":amazedfirman:": "pc_amazedfirman.gif",
":amazed:": "pc_amazed.gif",
":chummy:": "pc_chummy.gif",
":cool:": "pccool.gif",
":smooth:": "pccool.gif",
":distraughtfirman": "pc_distraughtfirman.gif",
":distraught:": "pc_distraught.gif",
":insolent:": "pc_insolent.gif",
2011-02-14 15:04:57 -05:00
":bemused:": "pc_bemused.gif",
2011-02-08 17:47:07 -05:00
":3:": "pckitty.gif",
":mystified:": "pc_mystified.gif",
":pranky:": "pc_pranky.gif",
":tense:": "pc_tense.gif",
":record:": "record.gif",
":squiddle:": "squiddle.gif",
":tab:": "tab.gif",
":beetip:": "theprofessor.gif",
":flipout:": "weasel.gif",
":befuddled:": "what.gif",
":pumpkin:": "whatpumpkin.gif",
":trollcool:": "trollcool.gif"}
2011-02-11 18:37:31 -05:00
_smilere = re.compile("|".join(smiledict.keys()))