From f259878556367968a674acae0e0b719e4460b41e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kn=C3=BCttel?= Date: Thu, 18 Jan 2018 22:03:40 +0100 Subject: [PATCH] added substitute commands --- mathbot/bot.py | 35 ++++++++++++++++++++++++++++++++++- mathbot/main.py | 2 ++ mathbot/static.py | 2 ++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/mathbot/bot.py b/mathbot/bot.py index 2068ab2..a756654 100644 --- a/mathbot/bot.py +++ b/mathbot/bot.py @@ -40,7 +40,7 @@ def render_math(bot, update): update.message.reply_photo(png, caption = "{}".format(text)) def render_math_caption(bot, update): - text = update.message.text[len("/rmath"):] + text = update.message.text[len("/rcmath"):] if( not ("<" in text and ">" in text)): update.message.reply_text("**Error**: missing caption seperators: < and >", parse_mode = "Markdown") @@ -59,6 +59,39 @@ def render_math_caption(bot, update): update.message.reply_photo(png, caption = "{}\n\nLaTeX: {}".format(caption, text)) +def substitute_render_math(bot, update): + text = update.message.text[len("/rsmath"):] + tex = text.replace("/", "\\") + + try: + png = get_png(tex) + except Exception as e: + handle_exception_in_get_png(bot, update, e, tex) + return + + update.message.reply_photo(png, caption = "{}".format(tex)) + +def substitute_render_math_caption(bot, update): + text = update.message.text[len("/rscmath"):] + + if( not ("<" in text and ">" in text)): + update.message.reply_text("**Error**: missing caption seperators: < and >", parse_mode = "Markdown") + return + + caption = text[text.index("<") + 1:text.index(">")] + text = text[text.index(">") + 1:] + + tex = text.replace("/", "\\") + + try: + png = get_png(tex) + except Exception as e: + handle_exception_in_get_png(bot, update, e, tex) + return + + update.message.reply_photo(png, caption = "{}\n\nLaTeX: {}".format(caption, tex)) + + def help(bot, update): update.message.reply_text(static_content.help_text, parse_mode = "Markdown") diff --git a/mathbot/main.py b/mathbot/main.py index dc9a9d9..0d30a49 100644 --- a/mathbot/main.py +++ b/mathbot/main.py @@ -28,6 +28,8 @@ def main(api_token): dispatcher.add_handler(CommandHandler("help", bot.help)) dispatcher.add_handler(CommandHandler("rmath", bot.render_math)) dispatcher.add_handler(CommandHandler("rcmath", bot.render_math_caption)) + dispatcher.add_handler(CommandHandler("rsmath", bot.substitute_render_math)) + dispatcher.add_handler(CommandHandler("rscmath", bot.substitute_render_math_caption)) updater.start_polling() diff --git a/mathbot/static.py b/mathbot/static.py index 93fb125..cafbfe5 100644 --- a/mathbot/static.py +++ b/mathbot/static.py @@ -26,6 +26,8 @@ Usage: - `/rmath LATEX` render __LATEX__ and return the resulting PNG with the input text - `/rcmath LATEX` render __LATEX__ and return the resulting PNG with the input text and caption __CAPTION__ +- `/rsmath LATEX` render __LATEX__ after substituting `/` -> `\\`. +- `/rscmath LATEX` like `/rcmath` after substituting `/` -> `\\` in __LATEX__. '''