Attachment 'latex-1.2.3.py'
Download 1 """
2 MoinMoin - Processor for a LaTeX syntax
3 @license: GNU GPL, see COPYING for details.
4
5 @copyright: 2002 by Won-kyu Park <wkpark@kldp.org>
6 @copyright: 2003 by Benny Siegert (added config.latex_header)
7 Modified:
8 2004-03-22 R.Bauer replaced config. by config_ and imported AttachFile
9 2004-04-09 imported patch from Daniel Hottinger (pagestyle{empty}, -E switch of dvips,
10 input of latex text replaced)
11 2004-04-09 R.Bauer improvement in speed, because only if the image name is different
12 a new image must be created
13 2004-07-31 imported patch from Phil West (imgname = imgname + "_latex")
14 2004-07-31 R.Bauer improvement to handle multipage postscript files as result of
15 dvips. The number of pages is read from the resulting postscript file
16 The last extension must be png to show the resulting image files.
17 Therefore we need to move/rename the png files given from convert.
18 2004-08-16 R.Bauer bug removed if only wiki text is changed
19 in previous version a single page latex image was not shown again; reported first by Peter Kleiweg.
20 2004-08-16 R.Bauer added break to the for loop
21
22
23 """
24
25 Dependencies = []
26
27 import sys, os, re, sha
28 from MoinMoin.Page import Page
29 from MoinMoin.action import AttachFile
30 from string import split
31
32 config_external_latex = "/usr/bin/latex"
33 config_external_convert = "/usr/bin/convert"
34 config_external_dvips = "/usr/bin/dvips"
35 config_external_mv = "/bin/mv"
36 config_umask = 022
37
38 config_latex_cache_dir = "./data"
39 config_latex_cache_url = "./data"
40 config_latex_header = "\documentclass[a4paper,12pt]{article}\n\pagestyle{empty}"
41 config_latex_vartmp_dir = "./data/tmp"
42
43 def process(request, formatter, lines):
44 if not config_latex_cache_dir:
45 return
46 if lines[0].strip() == "#!latex":
47 del lines[0]
48
49 texstr = '\n'.join(lines).strip()
50
51 imgname = re.sub('\s+', ' ', texstr)
52 imgname = sha.new(imgname).hexdigest()
53 imgname = imgname + "_latex"
54
55 attdir = config_latex_cache_dir + "/LaTex/attachments"
56 atturl = config_latex_cache_url + "/LaTex/attachments"
57 outpath = "%s/%s.png" % (attdir, imgname)
58 outurl = "%s/%s.png" % (atturl, imgname)
59 if not os.path.isdir(attdir):
60 os.makedirs(attdir, 0775)
61
62 pagename=formatter.page.page_name
63
64 url=AttachFile.getAttachUrl(pagename,imgname+'.png',request)
65 attach_dir=AttachFile.getAttachDir(pagename,create=1)
66
67
68 if not os.path.isfile(attach_dir+'/'+imgname+'.png'):
69 if not os.path.exists(outpath):
70 vartmp = config_latex_vartmp_dir
71 data = open("%s/%s.tex" % (vartmp, imgname), "w")
72 data.write('%s\n\\begin{document}\n%s\n\\end{document}' % (config_latex_header, texstr))
73 data.close()
74
75 cmd = "cd %(vartmp)s;%(latex)s %(options)s %(tex)s >/dev/null" % {
76 "latex": config_external_latex,
77 "vartmp": vartmp,
78 "options": '-interaction=batchmode',
79 "tex": imgname
80 }
81 os.umask(config_umask)
82 os.system(cmd)
83 os.system("cd %(vartmp)s; %(dvips)s -E %(imgname)s.dvi -o %(imgname)s.ps >/dev/null" % {
84 "dvips": config_external_dvips,
85 "vartmp": vartmp,
86 "imgname": imgname,
87 })
88
89 os.system("%(convert)s -crop 0x0 -density 120x120 %(vartmp)s/%(imgname)s.ps %(outpath)s" % {
90 "convert": config_external_convert,
91 "vartmp": vartmp,
92 "outpath": attach_dir+'/'+imgname+'.png',
93 "imgname": imgname,
94 })
95
96 # How many pages ?
97 id = open("%s/%s.ps" % (vartmp, imgname), "r")
98 txt=id.readlines()
99 id.close()
100
101 name="%%Pages:"
102 i=0
103 result= -1
104 for line in txt:
105 if (line.count(name) == 1):
106 result=i
107 break
108 i=i+1
109
110
111 n_pages=1
112 if (result > -1):
113 page=txt[result]
114 n_pages=int((split(page,name))[1])
115
116
117 if n_pages == 1: # this is the old case
118 request.write(formatter.image(src="%s" % url, alt=texstr, align='absmiddle'))
119 else: # and this is for more as one page
120 pages=range(n_pages)
121 for i in pages:
122 old_img_name=imgname+'.png.'+str(i)
123
124 new_img_name=imgname+'_'+str(i)+'.png'
125 new_url=AttachFile.getAttachUrl(pagename,new_img_name,request)
126
127 cmd="%(config_external_mv)s %(old_img_name)s %(new_img_name)s " %{
128
129 "config_external_mv": config_external_mv,
130 "old_img_name":attach_dir+'/'+old_img_name,
131 "new_img_name":attach_dir+'/'+new_img_name
132 }
133 os.system(cmd)
134
135
136
137 request.write(formatter.image(src="%s" % new_url, alt=texstr, align='absmiddle'))
138
139
140 os.system("rm -f %(vartmp)s/%(imgname)s.*" % {
141 "vartmp": vartmp,
142 "imgname": imgname,
143 })
144
145
146 else:
147 request.write(formatter.image(src="%s" % url, alt=texstr, align='absmiddle'))
148 ## multipage images always created
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.