def formulaeToImages(document, dir, _system=os.system):
    # gather all macros
    macros = ''
    for node in domhelpers.findElementsWithAttribute(document, 'class',
                                                     'latexmacros'):
        macros += domhelpers.getNodeText(node)
        node.parentNode.removeChild(node)
    i = 0
    for node in domhelpers.findElementsWithAttribute(document, 'class',
                                                    'latexformula'):
        latexText='''\\documentclass[12pt]{amsart}%s
                     \\begin{document}\[%s\]
                     \\end{document}''' % (macros, domhelpers.getNodeText(node))
        # This file really should be cleaned up by this function, or placed
        # somewhere such that the calling code can find it and clean it up.
        file = tempfile.mktemp()
        f = open(file+'.tex', 'w')
        f.write(latexText)
        f.close()
        _system('latex %s.tex' % file)
        _system('dvips %s.dvi -o %s.ps' % (os.path.basename(file), file))
        baseimgname = 'latexformula%d.png' % i
        imgname = os.path.join(dir, baseimgname)
        i += 1
        _system('pstoimg -type png -crop a -trans -interlace -out '
                  '%s %s.ps' % (imgname, file))
        newNode = dom.parseString(
            '<span><br /><img src="%s" /><br /></span>' % (
                baseimgname,)).documentElement
        node.parentNode.replaceChild(newNode, node)
dataset/ETHPy150Open kuri65536/python-for-android/python-modules/twisted/twisted/lore/lmath.py/formulaeToImages/original