From a63e35d04318578da5758f8df747d4dc31ea90de Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Tue, 21 Feb 2012 08:17:32 +1300 Subject: [PATCH] Added bold, underline and italic/inverted shortcodes to plugins/factoids.py --- plugins/factoids.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/plugins/factoids.py b/plugins/factoids.py index 740f810..71c9a84 100644 --- a/plugins/factoids.py +++ b/plugins/factoids.py @@ -6,6 +6,16 @@ from util import hook import re +# the dictionary has target_word : replacement_word pairs +shortcodes = { +'': '\x02', +'': '\x02', +'': '\x1F', +'': '\x1F', +'': '\x16', +'': '\x16'} + + def db_init(db): db.execute("create table if not exists mem(word, data, nick," " primary key(word))") @@ -20,6 +30,17 @@ def get_memory(db, word): else: return None + +def multiwordReplace(text, wordDic): + """ + take a text and replace words that match a key in a dictionary with + the associated value, return the changed text + """ + rc = re.compile('|'.join(map(re.escape, wordDic))) + def translate(match): + return wordDic[match.group(0)] + return rc.sub(translate, text) + @hook.command("r") def remember(inp, nick='', db=None, say=None, input=None, notice=None): @@ -92,4 +113,5 @@ def question(inp, say=None, db=None): data = get_memory(db, inp.group(1).strip()) if data: - say(data) + out = multiwordReplace(data, shortcodes) + say(out)