Attachment 'latex.1.2.1.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 Modified:
7 2004-03-22 R.Bauer replaced config. by config_ and imported AttachFile
8 @license: GNU GPL, see COPYING for details.
9 2004-04-09 imported patch from Daniel Hottinger (pagestyle{empty}, -E switch of dvips, input of latex text replaced)
10 2004-04-09 R.Bauer improvement in speed, because only if the image name is different a new image must be created
11
12 """
13
14 Dependencies = []
15
16 import sys, os, re, sha
17 from MoinMoin.Page import Page
18 from MoinMoin.action import AttachFile
19
20 config_external_latex = "/usr/bin/latex"
21 config_external_convert = "/usr/bin/convert"
22 config_external_dvips = "/usr/bin/dvips"
23 config_umask = 022
24
25 config_latex_cache_dir = "./data"
26 config_latex_cache_url = "./data"
27 config_latex_header = "\documentclass[a4paper,12pt]{article}\n\pagestyle{empty}"
28 config_latex_vartmp_dir = "./data/tmp"
29
30 def process(request, formatter, lines):
31 if not config_latex_cache_dir:
32 return
33 if lines[0].strip() == "#!latex":
34 del lines[0]
35
36 texstr = '\n'.join(lines).strip()
37
38 imgname = re.sub('\s+', ' ', texstr)
39 imgname = sha.new(imgname).hexdigest()
40
41 attdir = config_latex_cache_dir + "/LaTex/attachments"
42 atturl = config_latex_cache_url + "/LaTex/attachments"
43 outpath = "%s/%s.png" % (attdir, imgname)
44 outurl = "%s/%s.png" % (atturl, imgname)
45 if not os.path.isdir(attdir):
46 os.makedirs(attdir, 0775)
47
48 pagename=formatter.page.page_name
49
50 url=AttachFile.getAttachUrl(pagename,imgname+'.png',request)
51 attach_dir=AttachFile.getAttachDir(pagename,create=1)
52
53 if not os.path.isfile(attach_dir+'/'+imgname+'.png'):
54 if not os.path.exists(outpath):
55 vartmp = config_latex_vartmp_dir
56 data = open("%s/%s.tex" % (vartmp, imgname), "w")
57 data.write('%s\n\\begin{document}\n%s\n\\end{document}' % (config_latex_header, texstr))
58 data.close()
59
60 cmd = "cd %(vartmp)s;%(latex)s %(options)s %(tex)s >/dev/null" % {
61 "latex": config_external_latex,
62 "vartmp": vartmp,
63 "options": '-interaction=batchmode',
64 "tex": imgname
65 }
66 os.umask(config_umask)
67 os.system(cmd)
68 os.system("cd %(vartmp)s; %(dvips)s -E %(imgname)s.dvi -o %(imgname)s.ps >/dev/null" % {
69 "dvips": config_external_dvips,
70 "vartmp": vartmp,
71 "imgname": imgname,
72 })
73
74
75
76 os.system("%(convert)s -crop 0x0 -density 120x120 %(vartmp)s/%(imgname)s.ps %(outpath)s" % {
77 "convert": config_external_convert,
78 "vartmp": vartmp,
79 "outpath": attach_dir+'/'+imgname+'.png',
80 "imgname": imgname,
81 })
82
83
84 os.system("rm -f %(vartmp)s/%(imgname)s.*" % {
85 "vartmp": vartmp,
86 "imgname": imgname,
87 })
88
89
90 request.write(formatter.image(src="%s" % url, alt=texstr, align='absmiddle'))
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.