Download:
child 22:5a13f638a6ef
parent 20:8bd991e9b7ff
21:4470846e7c0c
Anton Shestakov <av6@dwimlabs.net>, Mon, 04 Jul 2016 22:56:48 +0800
rollbot: simple markup for coloring/styling messages

1 файлов изменено, 41 вставок(+), 10 удалений(-) [+]
rollbot.py file | annotate | diff | comparison | revisions
--- a/rollbot.py Fri Jun 24 07:36:35 2016 +0800
+++ b/rollbot.py Mon Jul 04 22:56:48 2016 +0800
@@ -1,4 +1,5 @@
#!/usr/bin/env python
+import cgi
import logging
import random
import re
@@ -14,6 +15,28 @@
setdefaultencoding('utf8')
+def simple_markup(string):
+ plain = ''
+ html = ''
+ tagre = re.compile(r'\{(\w+)\}(.*?)\{/\1\}')
+ plain = tagre.sub(r'\2', string)
+
+ def get_replacement(match):
+ code = match.group(1)
+ text = match.group(2)
+ codes = {
+ 'B': 'color: blue;',
+ 'G': 'color: green;',
+ 'i': 'font-style: italic;',
+ 't': 'font-family: monospace;'
+ }
+ style = codes.get(code, '')
+ return '<span style="{}">{}</span>'.format(style, text)
+
+ html = tagre.sub(get_replacement, cgi.escape(string))
+ return plain, html
+
+
class Command(object):
name = 'command'
@@ -29,7 +52,9 @@
regex = re.compile('^(?P<times>\d+)?d(?P<sides>\d+)$', re.IGNORECASE)
def usage(self):
- return '%s <X>D<Y> means roll Y-sided die X times, if X is 1 it can be omitted' % self.name
+ return (
+ '{t}%s <X>D<Y>{/t} to roll {t}Y{/t}-sided die {t}X{/t} times,'
+ ' if {t}X{/t} is 1 it can be omitted' % self.name)
def respond(self, words, message):
if len(words) == 2:
@@ -54,16 +79,17 @@
def format(self, rolls, total):
if len(rolls) == 1:
- return '%d' % total
+ return '{G}%d{/G}' % total
else:
- return '(%s) = %d' % (' + '.join('%d' % r for r in rolls), total)
+ sequence = ' + '.join('{B}%d{/B}' % r for r in rolls)
+ return '(%s) = {G}%d{/G}' % (sequence, total)
class Flip(Command):
name = 'flip'
def usage(self):
- return '%s or %s <1st-option>[ or] <2nd-option>' % (self.name, self.name)
+ return '{t}%s{/t} {i}or{/i} {t}%s <1st-option>[ or] <2nd-option>{/t}' % (self.name, self.name)
def respond(self, words, message):
if len(words) == 1:
@@ -81,14 +107,14 @@
return random.choice(options)
def format(self, option):
- return option
+ return '{G}%s{/G}' % option
class Help(Command):
name = 'help'
def usage(self):
- return '%s or %s <command>' % (self.name, self.name)
+ return '{t}%s{/t} {i}or{/i} {t}%s <command>{/t}' % (self.name, self.name)
def respond(self, words, message):
if len(words) == 2:
@@ -139,9 +165,10 @@
mbody = mbody[len(self.prefix):]
mbody = mbody.lstrip(' ')
- rbody = self.respond(mbody)
+ rbody, rxbody = self.respond(mbody)
if rbody:
msg.reply(rbody)
+ msg['html']['body'] = rxbody
msg.send()
def muc_message(self, msg):
@@ -157,10 +184,12 @@
return
mbody = mbody.lstrip(':, ')
- rbody = self.respond(mbody)
+ rbody, rxbody = self.respond(mbody)
if rbody:
rbody = '%s: %s' % (msg['mucnick'], rbody)
+ rxbody = '%s: %s' % (cgi.escape(msg['mucnick']), rxbody)
msg.reply(rbody)
+ msg['html']['body'] = rxbody
msg.send()
def muc_invite(self, invite):
@@ -177,14 +206,16 @@
self.plugin['xep_0045'].joinMUC(room, self.nick, password=password, wait=True)
def respond(self, message, prefix=None):
- response = ''
+ plain = ''
+ html = ''
words = message.split()
if words:
cmdname = words[0].lower()
if cmdname in self.commands:
command = self.commands[cmdname]
response = command.respond(words, message)
- return response
+ plain, html = simple_markup(response)
+ return plain, html
def lookup(key, args, config, default=None):