Changeset 8114

Show
Ignore:
Timestamp:
04/26/07 19:06:05 (20 months ago)
Author:
asterix
Message:

[thecurse] blacklist of latex commands. fixes #2796

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/conversation_textview.py

    r8111 r8114  
    579579        def latex_to_image(self, str): 
    580580                result = None 
    581                  
     581                exitcode = 0 
     582 
     583                # some latex commands are really bad 
     584                blacklist = ["\\def", "\\let", "\\futurelet", 
     585                        "\\newcommand", "\\renewcomment", "\\else", "\\fi", "\\write", 
     586                        "\\input", "\\include", "\\chardef", "\\catcode", "\\makeatletter", 
     587                        "\\noexpand", "\\toksdef", "\\every", "\\errhelp", "\\errorstopmode", 
     588                        "\\scrollmode", "\\nonstopmode", "\\batchmode", "\\read", "\\csname", 
     589                        "\\newhelp", "\\relax", "\\afterground", "\\afterassignment", 
     590                        "\\expandafter", "\\noexpand", "\\special", "\\command", "\\loop", 
     591                        "\\repeat", "\\toks", "\\output", "\\line", "\\mathcode", "\\name", 
     592                        "\\item", "\\section", "\\mbox", "\\DeclareRobustCommand", "\\[", 
     593                        "\\]"] 
     594 
    582595                str = str[2:len(str)-2] 
    583              
    584                 random.seed() 
    585                 tmpfile = os.path.join(gettempdir(), "gajimtex_" + random.randint(0, 100).__str__()) 
    586  
    587                 # build latex string 
    588                 texstr = "\\documentclass[12pt]{article}\\usepackage[dvips]{graphicx}\\usepackage{amsmath}\\usepackage{amssymb}\\pagestyle{empty}" 
    589                 texstr += "\\begin{document}\\begin{large}\\begin{gather*}" 
    590                 texstr += str 
    591                 texstr += "\\end{gather*}\\end{large}\\end{document}" 
    592  
    593                 file = open(os.path.join(tmpfile + ".tex"), "w+") 
    594                 file.write(texstr) 
    595                 file.flush() 
    596                 file.close() 
    597  
    598                 p = Popen(['latex', '--interaction=nonstopmode', tmpfile + '.tex'], cwd=gettempdir()) 
    599                 exitcode = p.wait() 
     596 
     597                # filter latex code with bad commands 
     598                for word in blacklist: 
     599                        if word in str: 
     600                                exitcode = 1 
     601                                break 
     602 
     603                if exitcode == 0: 
     604                        random.seed() 
     605                        tmpfile = os.path.join(gettempdir(), "gajimtex_" + random.randint(0, 
     606                                100).__str__()) 
     607 
     608                        # build latex string 
     609                        texstr = "\\documentclass[12pt]{article}\\usepackage[dvips]{graphicx}\\usepackage{amsmath}\\usepackage{amssymb}\\pagestyle{empty}" 
     610                        texstr += "\\begin{document}\\begin{large}\\begin{gather*}" 
     611                        texstr += str 
     612                        texstr += "\\end{gather*}\\end{large}\\end{document}" 
     613 
     614                        file = open(os.path.join(tmpfile + ".tex"), "w+") 
     615                        file.write(texstr) 
     616                        file.flush() 
     617                        file.close() 
     618 
     619                        p = Popen(['latex', '--interaction=nonstopmode', tmpfile + '.tex'], 
     620                                cwd=gettempdir()) 
     621                        exitcode = p.wait() 
    600622 
    601623                if exitcode == 0:            
    602                         p = Popen(['dvips', '-E', '-o', tmpfile + '.ps', tmpfile + '.dvi'], cwd=gettempdir()) 
     624                        p = Popen(['dvips', '-E', '-o', tmpfile + '.ps', tmpfile + '.dvi'], 
     625                                cwd=gettempdir()) 
    603626                        exitcode = p.wait() 
    604627 
    605628                if exitcode == 0: 
    606                     p = Popen(['convert', tmpfile + '.ps', tmpfile + '.png'], cwd=gettempdir()) 
    607                     exitcode = p.wait() 
    608              
     629                        p = Popen(['convert', tmpfile + '.ps', tmpfile + '.png'], 
     630                                cwd=gettempdir()) 
     631                        exitcode = p.wait() 
     632 
    609633                extensions = [".tex", ".log", ".aux", ".dvi", ".ps"] 
    610              
    611634                for ext in extensions: 
    612635                        try: 
     
    614637                        except Exception: 
    615638                                pass 
    616              
     639 
    617640                if exitcode == 0: 
    618641                        result = tmpfile + '.png' 
    619                  
     642 
    620643                return result 
    621  
    622644 
    623645        def print_special_text(self, special_text, other_tags):