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.
  • [get | view] (2004-05-05 21:34:20, 10.3 KB) [[attachment:AbcMusic.py]]
  • [get | view] (2004-04-15 07:19:38, 3.4 KB) [[attachment:DataLanguage.py]]
  • [get | view] (2004-04-15 20:23:33, 2.5 KB) [[attachment:GANTT-1.2.1.py]]
  • [get | view] (2003-12-07 18:15:55, 2.4 KB) [[attachment:GANTT.py]]
  • [get | view] (2004-07-29 15:04:28, 12.1 KB) [[attachment:IndentTable.py]]
  • [get | view] (2004-10-05 13:12:16, 10.6 KB) [[attachment:MySQL.py]]
  • [get | view] (2004-03-27 18:55:57, 3.7 KB) [[attachment:SimpleTable.py]]
  • [get | view] (2003-12-07 18:15:55, 1.5 KB) [[attachment:TextOnRight.py]]
  • [get | view] (2004-09-17 06:53:46, 3.0 KB) [[attachment:awktable-1.2.3.py]]
  • [get | view] (2004-10-28 13:55:04, 1.0 KB) [[attachment:colorer.py]]
  • [get | view] (2004-11-04 00:26:37, 1.4 KB) [[attachment:csv_python_module.diff]]
  • [get | view] (2004-04-21 18:29:38, 0.2 KB) [[attachment:html.py]]
  • [get | view] (2004-08-16 10:59:24, 4.8 KB) [[attachment:latex-1.2.3.py]]
  • [get | view] (2004-08-07 07:36:59, 3.9 KB) [[attachment:latex-cygwin.py]]
  • [get | view] (2004-04-09 17:05:33, 2.9 KB) [[attachment:latex.1.2.1.py]]
  • [get | view] (2004-02-29 16:50:13, 2.1 KB) [[attachment:latex.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.