[thecurse] blacklist of latex commands. fixes #2796
This commit is contained in:
parent
c1c30d20de
commit
1114b4c946
|
@ -578,36 +578,59 @@ 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()
|
# filter latex code with bad commands
|
||||||
tmpfile = os.path.join(gettempdir(), "gajimtex_" + random.randint(0, 100).__str__())
|
for word in blacklist:
|
||||||
|
if word in str:
|
||||||
# build latex string
|
exitcode = 1
|
||||||
texstr = "\\documentclass[12pt]{article}\\usepackage[dvips]{graphicx}\\usepackage{amsmath}\\usepackage{amssymb}\\pagestyle{empty}"
|
break
|
||||||
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())
|
random.seed()
|
||||||
|
tmpfile = os.path.join(gettempdir(), "gajimtex_" + random.randint(0,
|
||||||
|
100).__str__())
|
||||||
|
|
||||||
|
# build latex string
|
||||||
|
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()
|
exitcode = p.wait()
|
||||||
|
|
||||||
if exitcode == 0:
|
if exitcode == 0:
|
||||||
p = Popen(['convert', tmpfile + '.ps', tmpfile + '.png'], cwd=gettempdir())
|
p = Popen(['dvips', '-E', '-o', tmpfile + '.ps', tmpfile + '.dvi'],
|
||||||
exitcode = p.wait()
|
cwd=gettempdir())
|
||||||
|
exitcode = p.wait()
|
||||||
|
|
||||||
|
if exitcode == 0:
|
||||||
|
p = Popen(['convert', tmpfile + '.ps', tmpfile + '.png'],
|
||||||
|
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)
|
||||||
|
@ -619,7 +642,6 @@ class ConversationTextview:
|
||||||
|
|
||||||
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
|
||||||
special text (emots, links, formatting)'''
|
special text (emots, links, formatting)'''
|
||||||
|
|
Loading…
Reference in New Issue