示例#1
0
 def handleTinyMCEmath(
     self,
     client,
     tinyMCEwin,
     tinyMCEwin_name,
     tinyMCEfield,
     latex_source,
     math_fontsize,
     preview_image_filename,
     preview_math_srcfile,
 ):
     """
     Based off of handleTinyMCEimageChoice(), 
     handleTinyMCEmath() is similar in that it places a .gif math image 
     (and a corresponding .tex LaTeX source file) into the previews dir.
     Rather than copying the image from a user-selected directory, though,
     this routine actually generates the math image using mimetex.
     """
     server_filename = ""
     callback_errors = ""
     errors = 0
     webDir = Path(G.application.tempWebDir)
     previewDir = webDir.joinpath("previews")
     if not previewDir.exists():
         log.debug("image previews directory does not yet exist; " + "creating as %s " % previewDir)
         previewDir.makedirs()
     elif not previewDir.isdir():
         client.alert(_(u"Preview directory %s is a file, cannot replace it") % previewDir)
         log.error(
             "Couldn't preview tinyMCE-chosen image: " + "Preview dir %s is a file, cannot replace it" % previewDir
         )
         callback_errors = "Preview dir is a file, cannot replace"
         errors += 1
     if latex_source <> "":
         math_filename = previewDir.joinpath(preview_math_srcfile)
         math_filename_str = math_filename.abspath().encode("utf-8")
         log.info("handleTinyMCEmath: using LaTeX source: " + latex_source)
         log.debug("writing LaTeX source into '" + math_filename_str + "'.")
         math_file = open(math_filename, "wb")
         math_file.write(latex_source)
         math_file.flush()
         math_file.close()
         try:
             use_latex_sourcefile = math_filename_str
             tempFileName = compile(use_latex_sourcefile, math_fontsize, latex_is_file=True)
         except Exception, e:
             client.alert(_("MimeTeX compile failed!\n%s" % str(e)))
             log.error("handleTinyMCEmath unable to compile LaTeX using " + "mimetex, error = " + str(e))
             raise
         server_filename = previewDir.joinpath(preview_image_filename)
         log.debug(
             "handleTinyMCEmath copying math image from '"
             + tempFileName
             + "' to '"
             + server_filename.abspath().encode("utf-8")
             + "'."
         )
         shutil.copyfile(tempFileName, server_filename.abspath().encode("utf-8"))
         Path(tempFileName).remove()
示例#2
0
 def set_latex(self, latex):
     """
     Replaces current gifResource
     """
     if self.gifResource is not None:
         self.gifResource.delete()
         self.gifResource = None
     if latex <> "":
         tempFileName = compile(latex, self.fontsize)
         self.gifResource = Resource(self.idevice, tempFileName)
         Path(tempFileName).remove()
     self._latex = latex
    def handleTinyMCEmath(self, client, tinyMCEwin, tinyMCEwin_name, \
                             tinyMCEfield, latex_source, math_fontsize, \
                             preview_image_filename, preview_math_srcfile):
        """
        Based off of handleTinyMCEimageChoice(), 
        handleTinyMCEmath() is similar in that it places a .gif math image 
        (and a corresponding .tex LaTeX source file) into the previews dir.
        Rather than copying the image from a user-selected directory, though,
        this routine actually generates the math image using mimetex.
        """
        server_filename = ""
        callback_errors = ""
        errors = 0

        webDir = Path(G.application.tempWebDir)
        previewDir = webDir.joinpath('previews')

        if not previewDir.exists():
            log.debug("image previews directory does not yet exist; " \
                    + "creating as %s " % previewDir)
            previewDir.makedirs()
        elif not previewDir.isdir():
            client.alert( \
                _(u'Preview directory %s is a file, cannot replace it') \
                % previewDir)
            log.error("Couldn't preview tinyMCE-chosen image: "+
                      "Preview dir %s is a file, cannot replace it" \
                      % previewDir)
            callback_errors = "Preview dir is a file, cannot replace"
            errors += 1

        #if errors == 0:
        #    localImagePath = Path(local_filename)
        #    if not localImagePath.exists() or not localImagePath.isfile():
        #        client.alert( \
        #             _(u'Image file %s is not found, cannot preview it') \
        #             % localImagePath)
        #        log.error("Couldn't find tinyMCE-chosen image: %s" \
        #                % localImagePath)
        #        callback_errors = "Image file %s not found, cannot preview" \
        #                % localImagePath
        #        errors += 1

        # the mimetex usage code was swiped from the Math iDevice:
        if latex_source <> "":

            # first write the latex_source out into the preview_math_srcfile,
            # such that it can then be passed into the compile command:
            math_filename = previewDir.joinpath(preview_math_srcfile)
            math_filename_str = math_filename.abspath().encode('utf-8')
            log.info("handleTinyMCEmath: using LaTeX source: " + latex_source)
            log.debug("writing LaTeX source into \'" \
                    + math_filename_str + "\'.")
            math_file = open(math_filename, 'wb')
            # do we need to append a \n here?:
            math_file.write(latex_source)
            math_file.flush()
            math_file.close()

            try:
                use_latex_sourcefile = math_filename_str
                tempFileName = compile(use_latex_sourcefile, math_fontsize, \
                        latex_is_file=True)
            except Exception, e:
                client.alert(_('MimeTeX compile failed!\n%s' % str(e)))
                log.error("handleTinyMCEmath unable to compile LaTeX using "\
                    +"mimetex, error = " + str(e))
                raise

            # copy the file into previews
            server_filename = previewDir.joinpath(preview_image_filename)
            log.debug("handleTinyMCEmath copying math image from \'"\
                    + tempFileName + "\' to \'" \
                    + server_filename.abspath().encode('utf-8') + "\'.")
            shutil.copyfile(tempFileName, \
                    server_filename.abspath().encode('utf-8'))

            # Delete the temp file made by compile
            Path(tempFileName).remove()
示例#4
0
文件: mainpage.py 项目: giorgil2/eXe
    def handleTinyMCEmath(self, client, tinyMCEwin, tinyMCEwin_name, \
                             tinyMCEfield, latex_source, math_fontsize, \
                             preview_image_filename, preview_math_srcfile):
        """
        Based off of handleTinyMCEimageChoice(), 
        handleTinyMCEmath() is similar in that it places a .gif math image 
        (and a corresponding .tex LaTeX source file) into the previews dir.
        Rather than copying the image from a user-selected directory, though,
        this routine actually generates the math image using mimetex.
        """
        server_filename = ""
        callback_errors = ""
        errors = 0

        webDir     = Path(G.application.tempWebDir)
        previewDir  = webDir.joinpath('previews')

        if not previewDir.exists():
            log.debug("image previews directory does not yet exist; " \
                    + "creating as %s " % previewDir)
            previewDir.makedirs()
        elif not previewDir.isdir():
            client.alert( \
                _(u'Preview directory %s is a file, cannot replace it') \
                % previewDir)
            log.error("Couldn't preview tinyMCE-chosen image: "+
                      "Preview dir %s is a file, cannot replace it" \
                      % previewDir)
            callback_errors =  "Preview dir is a file, cannot replace"
            errors += 1

        #if errors == 0:
        #    localImagePath = Path(local_filename)
        #    if not localImagePath.exists() or not localImagePath.isfile():
        #        client.alert( \
        #             _(u'Image file %s is not found, cannot preview it') \
        #             % localImagePath)
        #        log.error("Couldn't find tinyMCE-chosen image: %s" \
        #                % localImagePath)
        #        callback_errors = "Image file %s not found, cannot preview" \
        #                % localImagePath
        #        errors += 1

        # the mimetex usage code was swiped from the Math iDevice:
        if latex_source <> "":

            # first write the latex_source out into the preview_math_srcfile,
            # such that it can then be passed into the compile command:
            math_filename = previewDir.joinpath(preview_math_srcfile)
            math_filename_str = math_filename.abspath().encode('utf-8')
            log.info("handleTinyMCEmath: using LaTeX source: " + latex_source)
            log.debug("writing LaTeX source into \'" \
                    + math_filename_str + "\'.")
            math_file = open(math_filename, 'wb')
            # do we need to append a \n here?:
            math_file.write(latex_source)
            math_file.flush()
            math_file.close()


            try: 
                use_latex_sourcefile = math_filename_str
                tempFileName = compile(use_latex_sourcefile, math_fontsize, \
                        latex_is_file=True)
            except Exception, e:
                client.alert(_('MimeTeX compile failed!\n%s' % str(e)))
                log.error("handleTinyMCEmath unable to compile LaTeX using "\
                    +"mimetex, error = " + str(e))
                raise

            # copy the file into previews
            server_filename = previewDir.joinpath(preview_image_filename);
            log.debug("handleTinyMCEmath copying math image from \'"\
                    + tempFileName + "\' to \'" \
                    + server_filename.abspath().encode('utf-8') + "\'.");
            shutil.copyfile(tempFileName, \
                    server_filename.abspath().encode('utf-8'));

            # Delete the temp file made by compile 
            Path(tempFileName).remove()