added substitute commands

This commit is contained in:
Daniel Knüttel 2018-01-18 22:03:40 +01:00
parent cdf1823bf6
commit f259878556
3 changed files with 38 additions and 1 deletions

View File

@ -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")

View File

@ -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()

View File

@ -26,6 +26,8 @@ Usage:
- `/rmath LATEX` render __LATEX__ and return the resulting PNG with the input text
- `/rcmath <CAPTION> LATEX` render __LATEX__ and return the resulting PNG with the input text
and caption __CAPTION__
- `/rsmath LATEX` render __LATEX__ after substituting `/` -> `\\`.
- `/rscmath <CAPTION> LATEX` like `/rcmath` after substituting `/` -> `\\` in __LATEX__.
'''