Attachment 'latex-cygwin.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 2004-08-06 Yaroslav Bulatov modified to work under Cygwin (only)
13 """
14
15 Dependencies = []
16
17 import sys, os, re, sha
18 from MoinMoin.Page import Page
19 from MoinMoin.action import AttachFile
20
21 config_external_latex = "latex"
22 config_external_convert = "convert"
23 config_external_dvips = "dvips"
24 config_umask = 022
25
26 config_latex_cache_dir = "./data"
27 config_latex_cache_url = "./data"
28 config_latex_header = "\documentclass[a4paper,12pt]{article}\n\pagestyle{empty}"
29 config_latex_vartmp_dir = "./data/tmp"
30
31 # Executes command using cygwin, captures output
32 external_shell = 'C:/cygwin/bin/tcsh.exe -c '
33 def ex(cmd):
34 dummy, out = os.popen4(external_shell+"'"+cmd+"'")
35 if 0:
36 import time
37 debug = open(r'G:\public_html\moin\ai\data\tmp\debug.txt', 'a')
38 print >>debug, time.asctime()
39 print >>debug, cmd
40 for line in out:
41 print >>debug, line
42 dummy.close()
43 out.close()
44 debug.close()
45
46 def process(request, formatter, lines):
47 if not config_latex_cache_dir:
48 return
49 if lines[0].strip() == "#!latex":
50 del lines[0]
51
52 texstr = '\n'.join(lines).strip()
53
54 imgname = re.sub('\s+', ' ', texstr)
55 imgname = sha.new(imgname).hexdigest()
56
57 attdir = config_latex_cache_dir + "/LaTex/attachments"
58 atturl = config_latex_cache_url + "/LaTex/attachments"
59 outpath = "%s/%s.gif" % (attdir, imgname)
60 outurl = "%s/%s.gif" % (atturl, imgname)
61 if not os.path.isdir(attdir):
62 os.makedirs(attdir, 0775)
63
64 pagename=formatter.page.page_name
65
66 url=AttachFile.getAttachUrl(pagename,imgname+'.png',request)
67 attach_dir=AttachFile.getAttachDir(pagename,create=1)
68
69 if not os.path.isfile(attach_dir+'/'+imgname+'.png'):
70 if not os.path.exists(outpath):
71 vartmp = config_latex_vartmp_dir
72 data = open("%s/%s.tex" % (vartmp, imgname), "w")
73
74 data.write('%s\n\\begin{document}\n%s\n\\end{document}' % (config_latex_header, texstr))
75 data.close()
76
77 cmd = r"cd %(vartmp)s;%(latex)s %(options)s %(tex)s" % {
78 "latex": config_external_latex,
79 "vartmp": vartmp,
80 "options": '-interaction=batchmode',
81 "tex": imgname
82 }
83 os.umask(config_umask)
84 ex(cmd)
85
86 cmd = r"cd %(vartmp)s; %(dvips)s -E %(imgname)s.dvi -o %(imgname)s.ps" % {
87 "dvips": config_external_dvips,
88 "vartmp": vartmp,
89 "imgname": imgname,
90 }
91 ex(cmd)
92
93 # Convert ps file into true color png
94 cmd = r"gs -q -dBATCH -dNOPAUSE -dNOPLATFONTS -sDEVICE=png16m -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dMaxBitmap=50000000 -r120x120 -sOutputFile=%(pngfile)s -- %(psfile)s -c quit"%{
95 "pngfile": vartmp+"/"+imgname+".png",
96 "psfile": vartmp+"/"+imgname+".ps"
97 }
98 ex(cmd)
99
100 # convert -crop 0x0 -density 300x300 a.png a.gif
101
102 # Crop and convert png into gif
103 cmd = r"%(convert)s -crop 0x0 %(inpath)s %(outpath)s" % {
104 "convert": config_external_convert,
105 "vartmp": vartmp,
106 "outpath": attach_dir.replace('\\','/')+'/'+imgname+'.png',
107 "inpath": vartmp+"/"+imgname+".png"
108 }
109 ex(cmd)
110
111 cmd = r"rm -f %(vartmp)s/%(imgname)s.*" % {
112 "vartmp": vartmp,
113 "imgname": imgname,
114 }
115 ex(cmd)
116
117 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.