Changeset 8111

Show
Ignore:
Timestamp:
04/25/07 23:51:08 (20 months ago)
Author:
asterix
Message:

[thecurse] add latex support (run dvips and convert externaly) through use_latex ACE option. fixes #2796

Location:
trunk/src
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/common/config.py

    r8015 r8111  
    226226                'treat_incoming_messages': [ opt_str, '', _('Can be empty, \'chat\' or \'normal\'. If not empty, treat all incoming messages as if they were of this type')], 
    227227                'scroll_roster_to_last_message': [opt_bool, True, _('If True, Gajim will scroll and select the contact who sent you the last message, if chat window is not already opened.')], 
     228                'use_latex': [opt_bool, False, _('If True, Gajim will convert string between $$ and $$ to an image using dvips and convert before insterting it in chat window.')], 
    228229        } 
    229230 
  • trunk/src/conversation_textview.py

    r7959 r8111  
    1414## GNU General Public License for more details. 
    1515## 
     16 
     17import random 
     18from tempfile import gettempdir 
     19from subprocess import Popen 
    1620 
    1721import gtk 
     
    572576 
    573577                return index # the position after *last* special text 
     578 
     579        def latex_to_image(self, str): 
     580                result = None 
     581                 
     582                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() 
     600 
     601                if exitcode == 0:            
     602                        p = Popen(['dvips', '-E', '-o', tmpfile + '.ps', tmpfile + '.dvi'], cwd=gettempdir()) 
     603                        exitcode = p.wait() 
     604 
     605                if exitcode == 0: 
     606                    p = Popen(['convert', tmpfile + '.ps', tmpfile + '.png'], cwd=gettempdir()) 
     607                    exitcode = p.wait() 
     608             
     609                extensions = [".tex", ".log", ".aux", ".dvi", ".ps"] 
     610             
     611                for ext in extensions: 
     612                        try: 
     613                                os.remove(tmpfile + ext) 
     614                        except Exception: 
     615                                pass 
     616             
     617                if exitcode == 0: 
     618                        result = tmpfile + '.png' 
     619                 
     620                return result 
     621 
    574622 
    575623        def print_special_text(self, special_text, other_tags): 
     
    657705                                if not show_ascii_formatting_chars: 
    658706                                        special_text = special_text[1:-1] # remove _ _ 
     707                elif special_text.startswith('$$') and special_text.endswith('$$'): 
     708                        imagepath = self.latex_to_image(special_text) 
     709                        end_iter = buffer.get_end_iter() 
     710                        anchor = buffer.create_child_anchor(end_iter) 
     711                        if imagepath != None: 
     712                                img = gtk.Image() 
     713                                img.set_from_file(imagepath) 
     714                                img.show() 
     715                                # add 
     716                                self.tv.add_child_at_anchor(img, anchor) 
     717                                # delete old file 
     718                                try: 
     719                                        os.remove(imagepath) 
     720                                except Exception: 
     721                                        pass 
     722                        else: 
     723                                buffer.insert(end_iter, special_text) 
     724                        use_other_tags = False 
    659725                else: 
    660726                        #it's a url 
  • trunk/src/gajim.py

    r8100 r8111  
    18241824                        r'(?<!\w)' r'_[^\s_]' r'([^_]*[^\s_])?' r'_(?!\w)' 
    18251825 
     1826                latex = r'|\$\$.*\$\$' 
     1827                 
    18261828                basic_pattern = links + mail 
     1829                 
     1830                if gajim.config.get('use_latex'): 
     1831                        basic_pattern += latex 
     1832                 
    18271833                if gajim.config.get('ascii_formatting'): 
    18281834                        basic_pattern += formatting