98 lines
3.3 KiB
Python
98 lines
3.3 KiB
Python
# Copyright (c) 2018 Daniel Knüttel #
|
|
# #
|
|
# This file is part of mathbot. #
|
|
# #
|
|
# mathbot is free software: you can redistribute it and/or modify #
|
|
# it under the terms of the GNU General Public License as published by #
|
|
# the Free Software Foundation, either version 3 of the License, or #
|
|
# (at your option) any later version. #
|
|
# #
|
|
# mathbot is distributed in the hope that it will be useful, #
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
|
|
# GNU General Public License for more details. #
|
|
# #
|
|
# You should have received a copy of the GNU General Public License #
|
|
# along with mathbot. If not, see <http://www.gnu.org/licenses/>. #
|
|
# #
|
|
|
|
from telegram.ext import RegexHandler
|
|
from .rendering import get_png
|
|
from .static import static_content
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def handle_exception_in_get_png(bot, update, exception, text):
|
|
update.message.reply_text("**Error** while processing '__{}__'.\n\nException was: **{}**.".format(text, exception), parse_mode = "Markdown")
|
|
|
|
def render_math(bot, update):
|
|
text = update.message.text[len("/rmath"):]
|
|
tex = text
|
|
|
|
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(text))
|
|
|
|
def render_math_caption(bot, update):
|
|
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")
|
|
return
|
|
|
|
caption = text[text.index("<") + 1:text.index(">")]
|
|
text = text[text.index(">") + 1:]
|
|
|
|
tex = text
|
|
|
|
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, 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")
|
|
|