diff options
author | Livio Recchia <recchialivio@libero.it> | 2020-02-10 23:06:34 +0100 |
---|---|---|
committer | Livio Recchia <recchialivio@libero.it> | 2020-02-10 23:06:34 +0100 |
commit | 9a13903a2f7d3a65fdf15a65fb59cccd622e2066 (patch) | |
tree | 9403b7dff39eb5e5d7fa0f79efb69b496add4c4b /textutils.py | |
parent | 11cc316b74d5f3f283413a33e7693b314741aa4a (diff) | |
download | manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.gz manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.bz2 manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.xz manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.zip |
Initial commit
Diffstat (limited to 'textutils.py')
-rw-r--r-- | textutils.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/textutils.py b/textutils.py new file mode 100644 index 0000000..4cd5b2e --- /dev/null +++ b/textutils.py @@ -0,0 +1,69 @@ +import re + +__all__ = ('preprocess', 'remove_formatting', 'replace_emotes', + 'simplify_links', 'expand_links', 'links_to_markup') + +formatting_re = re.compile(r'##[0-9bB]') +emotes_re = re.compile(r'%%[^%]') +mplus_link_re = re.compile(r'@@([^|]+)\|([^@]+)@@') +url_re = re.compile(r'(^|[^@|])((http|https|ftp)://([^\t ]+))') + + +def remove_formatting(text): + return re.sub(formatting_re, '', text) + + +def replace_emotes(text): + + emotes = ( ":-D", ":-)", ";-)", ":-(", ":-o", ":-|", ":-/", "B-)", + ":-D", ":-[", ":-P", "*blush*", ":'-(", "*:-]*", + "*weird emote*", "*ninja*", ":-)", "*star*", "*?*", "*!*", "*idea*", + "*->*", "<3", "^_^", ":-)", ";-)", ":-(", ":-O", ":-(", + "*mimi*", ":-D", ":-D", "*perturbed*", ":-P", + "*shame*", ":-(", ">:-D", "0_o", "*ninja*", "*bad geek*", + "*star*", "*?*", "*!*", "*bubble*", ">_>", "*in love*", + "*disgust*", ">:-D", ":-(", "xD", "u.u", "x_x", + "*facepalm*", ">:-D", "*angry*", ":-D", "*metal*", + ":'-(", "*...*", "*@:=*", ":3", "*zZzZz*", "-.-'", + "*alien*") + + def emote_repl(m): + code = ord(m.group(0)[2]) - 48 + if code > len(emotes): + return m.group(0) + else: + return emotes[code] + + return re.sub(emotes_re, emote_repl, text) + + +def simplify_links(text): + + def simplify(m): + return m.group(2) + + return re.sub(mplus_link_re, simplify, text) + + +def expand_links(text): + + def expand(m): + return '{}[@@{}|{}@@]'.format(m.group(1), m.group(2), m.group(4)) + + # text = ' ' + text + return re.sub(url_re, expand, text) + + +def links_to_markup(text): + return re.sub(mplus_link_re, + r'[ref=\1][color=2F3Fff]\2[/color][/ref]', + text) + + +def preprocess(text, actions=(simplify_links, + remove_formatting, + replace_emotes)): + for f in actions: + text = f(text) + + return text |