예제 #1
0
def run_latex(pictype, codehash, codetext, cachepath, dpi=300, pdflatexpath=None):
    """Run the image generation for pstricks and tikz images."""
    # try and find pdflatex
    if pdflatexpath is None:
        path = os.environ.get('LATEX_PATH', os.environ.get('PATH'))
        texpath = [p for p in path.split(':') if 'tex' in p]
        if texpath:
            pdflatexpath = texpath[0] + '/pdflatex'
        else:
            # no custom latex installed. Try /usr/local/bin and /usr/local
            for path in ['/usr/local/bin/', '/usr/bin/']:
                texpath = os.path.join(path, 'pdflatex')
                if os.path.exists(texpath):
                    pdflatexpath = texpath
                    break

    # copy to local image cache in .bookbuilder/images
    image_cache_path = os.path.join(cachepath, pictype, codehash + '.png')
    rendered = False
    # skip image generation if it exists
    if os.path.exists(image_cache_path):
        rendered = True
        sys.stdout.write('s')

    if not rendered:
        sys.stdout.write('.')
        if pictype == 'pspicture':
            latex_code = pspicture2png(codetext)
            preamble = PsPicture_preamble()
        elif pictype == 'tikzpicture':
            latex_code = tikzpicture2png(codetext)
            preamble = tikz_preamble()
        elif pictype == 'equation':
            latex_code = equation2png(codetext)
            preamble = equation_preamble()
        try:
            figpath = latex2png(latex_code, preamble, dpi=dpi, pdflatexpath=pdflatexpath)
        except LatexPictureError as lpe:
            print(colored("\nLaTeX failure", "red"))
            print(unicode(lpe))
            return None

        if figpath:
            # done. copy to image cache
            copy_if_newer(figpath, image_cache_path)
            # copy the pdf also but run pdfcrop first
            copy_if_newer(figpath.replace('.png', '.pdf'),
                          image_cache_path.replace('.png', '.pdf'))

            cleanup_after_latex(figpath)
    else:
        figpath = image_cache_path

    sys.stdout.flush()
    return image_cache_path
예제 #2
0
    def __copy_html_images(self, build_folder, output_path):
        ''' Find all images referenced in the converted html document and copy
        them to their correct relative places in the build/tex folder.

        '''
        success = True
        # copy images directly included in cnxmlplus to the output folder
        with open(output_path, 'r') as f_in:
            html = etree.HTML(f_in.read())

        for img in html.findall('.//img'):
            src = img.attrib['src']
            dest = os.path.join(os.path.dirname(output_path), src)
            if not os.path.exists(src) and (not os.path.exists(dest)):
                print_debug_msg(src + " doesn't exist")

            success = copy_if_newer(src, dest)

        return success
예제 #3
0
    def __copy_tex_images(self, build_folder, output_path):
        """
        Copy tex images.

        Find all images referenced in the cnxmlplus document and copy them
        to their correct relative places in the build/tex folder.

        """
        success = True
        with open(self.file) as f_in:
            xml = etree.XML(f_in.read())

        # if it is tex, we can copy the images referenced in the cnxmlplus
        # directly to the build/tex folder
        for image in xml.findall('.//image'):
            # find the src, it may be an attribute or a child element
            if 'src' in image.attrib:
                src = image.attrib['src'].strip()
            else:
                src = image.find('.//src').text.strip()

            # check for paths starting with /
            if src.startswith('/'):
                print(colored("ERROR! image paths may not start with /: ",
                              "red") + src)
                success = False
                continue

            dest = os.path.join(build_folder, 'tex', src)
            if not os.path.exists(dest):
                try:
                    mkdir_p(os.path.dirname(dest))
                except OSError:
                    msg = colored("WARNING! {dest} is not allowed!"
                                  .format(dest=dest),
                                  "magenta")
                    success = False
                    print(msg)
            success = copy_if_newer(src, dest)

        return success