[thecurse] blacklist of latex commands. fixes #2796

This commit is contained in:
Yann Leboulanger 2007-04-26 17:06:05 +00:00
parent c1c30d20de
commit 1114b4c946
1 changed files with 45 additions and 23 deletions

View File

@ -578,47 +578,69 @@ class ConversationTextview:
def latex_to_image(self, str): def latex_to_image(self, str):
result = None result = None
exitcode = 0
# some latex commands are really bad
blacklist = ["\\def", "\\let", "\\futurelet",
"\\newcommand", "\\renewcomment", "\\else", "\\fi", "\\write",
"\\input", "\\include", "\\chardef", "\\catcode", "\\makeatletter",
"\\noexpand", "\\toksdef", "\\every", "\\errhelp", "\\errorstopmode",
"\\scrollmode", "\\nonstopmode", "\\batchmode", "\\read", "\\csname",
"\\newhelp", "\\relax", "\\afterground", "\\afterassignment",
"\\expandafter", "\\noexpand", "\\special", "\\command", "\\loop",
"\\repeat", "\\toks", "\\output", "\\line", "\\mathcode", "\\name",
"\\item", "\\section", "\\mbox", "\\DeclareRobustCommand", "\\[",
"\\]"]
str = str[2:len(str)-2] str = str[2:len(str)-2]
random.seed()
tmpfile = os.path.join(gettempdir(), "gajimtex_" + random.randint(0, 100).__str__())
# build latex string # filter latex code with bad commands
texstr = "\\documentclass[12pt]{article}\\usepackage[dvips]{graphicx}\\usepackage{amsmath}\\usepackage{amssymb}\\pagestyle{empty}" for word in blacklist:
texstr += "\\begin{document}\\begin{large}\\begin{gather*}" if word in str:
texstr += str exitcode = 1
texstr += "\\end{gather*}\\end{large}\\end{document}" break
file = open(os.path.join(tmpfile + ".tex"), "w+") if exitcode == 0:
file.write(texstr) random.seed()
file.flush() tmpfile = os.path.join(gettempdir(), "gajimtex_" + random.randint(0,
file.close() 100).__str__())
p = Popen(['latex', '--interaction=nonstopmode', tmpfile + '.tex'], cwd=gettempdir()) # build latex string
exitcode = p.wait() texstr = "\\documentclass[12pt]{article}\\usepackage[dvips]{graphicx}\\usepackage{amsmath}\\usepackage{amssymb}\\pagestyle{empty}"
texstr += "\\begin{document}\\begin{large}\\begin{gather*}"
texstr += str
texstr += "\\end{gather*}\\end{large}\\end{document}"
file = open(os.path.join(tmpfile + ".tex"), "w+")
file.write(texstr)
file.flush()
file.close()
p = Popen(['latex', '--interaction=nonstopmode', tmpfile + '.tex'],
cwd=gettempdir())
exitcode = p.wait()
if exitcode == 0: if exitcode == 0:
p = Popen(['dvips', '-E', '-o', tmpfile + '.ps', tmpfile + '.dvi'], cwd=gettempdir()) p = Popen(['dvips', '-E', '-o', tmpfile + '.ps', tmpfile + '.dvi'],
cwd=gettempdir())
exitcode = p.wait() exitcode = p.wait()
if exitcode == 0: if exitcode == 0:
p = Popen(['convert', tmpfile + '.ps', tmpfile + '.png'], cwd=gettempdir()) p = Popen(['convert', tmpfile + '.ps', tmpfile + '.png'],
exitcode = p.wait() cwd=gettempdir())
exitcode = p.wait()
extensions = [".tex", ".log", ".aux", ".dvi", ".ps"] extensions = [".tex", ".log", ".aux", ".dvi", ".ps"]
for ext in extensions: for ext in extensions:
try: try:
os.remove(tmpfile + ext) os.remove(tmpfile + ext)
except Exception: except Exception:
pass pass
if exitcode == 0: if exitcode == 0:
result = tmpfile + '.png' result = tmpfile + '.png'
return result
return result
def print_special_text(self, special_text, other_tags): def print_special_text(self, special_text, other_tags):
'''is called by detect_and_print_special_text and prints '''is called by detect_and_print_special_text and prints