~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/lore/lmath.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
#
 
5
from __future__ import nested_scopes
 
6
 
 
7
import os, tempfile
 
8
from twisted.web import domhelpers, microdom
 
9
import latex, tree, lint, default
 
10
 
 
11
class MathLatexSpitter(latex.LatexSpitter):
 
12
 
 
13
    start_html = '\\documentclass{amsart}\n'
 
14
 
 
15
    def visitNode_div_latexmacros(self, node):
 
16
        self.writer(domhelpers.getNodeText(node))
 
17
 
 
18
    def visitNode_span_latexformula(self, node):
 
19
        self.writer('\[')
 
20
        self.writer(domhelpers.getNodeText(node))
 
21
        self.writer('\]')
 
22
 
 
23
def formulaeToImages(document, dir):
 
24
    # gather all macros
 
25
    macros = ''
 
26
    for node in domhelpers.findElementsWithAttribute(document, 'class',
 
27
                                                     'latexmacros'):
 
28
        macros += domhelpers.getNodeText(node)
 
29
        node.parentNode.removeChild(node)
 
30
    i = 0
 
31
    for node in domhelpers.findElementsWithAttribute(document, 'class',
 
32
                                                    'latexformula'):
 
33
        latexText='''\\documentclass[12pt]{amsart}%s
 
34
                     \\begin{document}\[%s\]
 
35
                     \\end{document}''' % (macros, domhelpers.getNodeText(node))
 
36
        file = tempfile.mktemp()
 
37
        open(file+'.tex', 'w').write(latexText)
 
38
        os.system('latex %s.tex' % file)
 
39
        os.system('dvips %s.dvi -o %s.ps' % (os.path.basename(file), file))
 
40
        baseimgname = 'latexformula%d.png' % i
 
41
        imgname = os.path.join(dir, baseimgname)
 
42
        i += 1
 
43
        os.system('pstoimg -type png -crop a -trans -interlace -out '
 
44
                  '%s %s.ps' % (imgname, file))
 
45
        newNode = microdom.parseString('<span><br /><img src="%s" /><br /></span>' %
 
46
                                       baseimgname)
 
47
        node.parentNode.replaceChild(newNode, node)
 
48
 
 
49
 
 
50
def doFile(fn, docsdir, ext, url, templ, linkrel='', d=None):
 
51
    d = d or {}
 
52
    doc = tree.parseFileAndReport(fn)
 
53
    formulaeToImages(doc, os.path.dirname(fn))
 
54
    cn = templ.cloneNode(1)
 
55
    tree.munge(doc, cn, linkrel, docsdir, fn, ext, url, d)
 
56
    cn.writexml(open(os.path.splitext(fn)[0]+ext, 'wb'))
 
57
 
 
58
 
 
59
class ProcessingFunctionFactory(default.ProcessingFunctionFactory):
 
60
 
 
61
    latexSpitters = {None: MathLatexSpitter}
 
62
 
 
63
    def getDoFile(self):
 
64
        return doFile
 
65
 
 
66
    def getLintChecker(self):
 
67
        checker = lint.getDefaultChecker()
 
68
        checker.allowedClasses = checker.allowedClasses.copy()
 
69
        oldDiv = checker.allowedClasses['div']
 
70
        oldSpan = checker.allowedClasses['span']
 
71
        checker.allowedClasses['div'] = lambda x:oldDiv(x) or x=='latexmacros'
 
72
        checker.allowedClasses['span'] = (lambda x:oldSpan(x) or
 
73
                                                     x=='latexformula')
 
74
        return checker
 
75
 
 
76
factory = ProcessingFunctionFactory()