1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
from __future__ import nested_scopes
8
from twisted.web import domhelpers, microdom
9
import latex, tree, lint, default
11
class MathLatexSpitter(latex.LatexSpitter):
13
start_html = '\\documentclass{amsart}\n'
15
def visitNode_div_latexmacros(self, node):
16
self.writer(domhelpers.getNodeText(node))
18
def visitNode_span_latexformula(self, node):
20
self.writer(domhelpers.getNodeText(node))
23
def formulaeToImages(document, dir):
26
for node in domhelpers.findElementsWithAttribute(document, 'class',
28
macros += domhelpers.getNodeText(node)
29
node.parentNode.removeChild(node)
31
for node in domhelpers.findElementsWithAttribute(document, 'class',
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)
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>' %
47
node.parentNode.replaceChild(newNode, node)
50
def doFile(fn, docsdir, ext, url, templ, linkrel='', d=None):
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'))
59
class ProcessingFunctionFactory(default.ProcessingFunctionFactory):
61
latexSpitters = {None: MathLatexSpitter}
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
76
factory = ProcessingFunctionFactory()