Attachment 'latex.py'
Download 1 """
2 MoinMoin - Processor for a LaTeX syntax
3
4 @copyright: 2002 by Won-kyu Park <wkpark@kldp.org>
5 @copyright: 2003 by Benny Siegert (added config.latex_header)
6 @license: GNU GPL, see COPYING for details.
7 """
8
9 Dependencies = []
10
11 import sys, os, re, sha
12 from MoinMoin import config
13
14 config_external_latex = "/usr/bin/latex"
15 config_external_convert = "/usr/bin/convert"
16 config_external_dvips = "/usr/bin/dvips"
17 config_umask = 022
18
19 def process(request, formatter, lines):
20 if not config.latex_cache_dir:
21 return
22 if lines[0].strip() == "#!latex":
23 del lines[0]
24
25 texstr = '\n'.join(lines).strip()
26
27 imgname = re.sub('\s+', ' ', texstr)
28 imgname = sha.new(imgname).hexdigest()
29
30 attdir = config.latex_cache_dir + "/LaTex/attachments"
31 atturl = config.latex_cache_url + "/LaTex/attachments"
32 outpath = "%s/%s.png" % (attdir, imgname)
33 outurl = "%s/%s.png" % (atturl, imgname)
34 if not os.path.isdir(attdir):
35 os.makedirs(attdir, 0775)
36
37 if not os.path.exists(outpath):
38 vartmp = config.latex_vartmp_dir
39 data = open("%s/%s.tex" % (vartmp, imgname), "w")
40 data.write('\\input %s\n%s\n\\end{document}' % (config.latex_header, texstr))
41 data.close()
42
43 cmd = "cd %(vartmp)s;%(latex)s %(options)s %(tex)s >/dev/null" % {
44 "latex": config_external_latex,
45 "vartmp": vartmp,
46 "options": '-interaction=batchmode',
47 "tex": imgname
48 }
49 os.umask(config_umask)
50 os.system(cmd)
51 os.system("cd %(vartmp)s; %(dvips)s %(imgname)s.dvi -o %(imgname)s.ps" % {
52 "dvips": config_external_dvips,
53 "vartmp": vartmp,
54 "imgname": imgname,
55 })
56 os.system("%(convert)s -crop 0x0 -density 120x120 %(vartmp)s/%(imgname)s.ps %(outpath)s" % {
57 "convert": config_external_convert,
58 "vartmp": vartmp,
59 "outpath": outpath,
60 })
61 os.system("rm -f %(vartmp)s/%(imgname)s.*" % {
62 "vartmp": vartmp,
63 "imgname": imgname,
64 })
65
66 request.write(formatter.image(src="%s" % outurl, alt=texstr, align='absmiddle'))
67
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.