2002 WkPark has developed this parser which gives moinmoin the functionality to use latex formulars. We often have used this parser. Over the years a lot of features were added or improved and we have learned from that what all is needed.

At the end of 2004 JohannesBerg gots the idea to use the program dvipng instead of dvips and convert. Based on this idea a new latex parser was developed by him. The new parser could also be called by a wrapper as macro and it is much more secure as this one.

By now we should move to use the new parser. The old one is still here if we like to compare. -- ReimarBauer 2005-02-26 09:11:35


   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;
  20                reported first by Peter Kleiweg.
  21     2004-08-16 R.Bauer added break to the for loop
  22 
  23     2004-11-07 R.Bauer changed to PARSER
  24     2004-12-22 R.Bauer patch for unicode names 
  25 
  26 """
  27 
  28 
  29 Dependencies = []
  30 
  31 import sys, os, re, sha
  32 from MoinMoin.Page import Page
  33 from MoinMoin.action import AttachFile
  34 from string import split
  35 
  36 config_external_latex = "/usr/bin/latex"
  37 config_external_convert = "/usr/bin/convert"
  38 config_external_dvips = "/usr/bin/dvips"
  39 config_external_mv = "/bin/mv"
  40 config_umask = 022
  41 
  42 config_latex_cache_dir = "./data"
  43 config_latex_cache_url = "./data"
  44 config_latex_header = "\documentclass[a4paper,12pt]{article}\n\pagestyle{empty}"
  45 config_latex_vartmp_dir = "./data/tmp"
  46 class Parser:
  47     """ Format CSV data as table
  48     """
  49 
  50     extensions = ['.tex']
  51 
  52     def __init__(self, raw, request, **kw):
  53         """ Store the source text.
  54         """
  55         self.raw = raw
  56         self.request = request
  57         self.form = request.form
  58         self._ = request.getText
  59 
  60         # parse ex1tra arguments for excludes
  61         self.exclude = []
  62 
  63 
  64     def format(self, formatter):
  65         if not config_latex_cache_dir:
  66                 return
  67         lines = self.raw.split('\n')
  68         if lines[0].strip() == "#!latex":
  69                 del lines[0]
  70 
  71         texstr = '\n'.join(lines).strip()
  72 
  73         imgname = re.sub('\s+', ' ', texstr)
  74         imgname = sha.new(imgname).hexdigest()
  75         imgname = imgname + "_latex"
  76 
  77         attdir = config_latex_cache_dir + "/LaTex/attachments"
  78         atturl = config_latex_cache_url + "/LaTex/attachments"
  79         outpath = "%s/%s.png" % (attdir, imgname)
  80         outurl = "%s/%s.png" % (atturl, imgname)
  81         if not os.path.isdir(attdir):
  82          os.makedirs(attdir, 0775)
  83 
  84         pagename=formatter.page.page_name
  85 
  86         url=AttachFile.getAttachUrl(pagename,imgname+'.png',self.request)
  87         attach_dir=AttachFile.getAttachDir(self.request,pagename,create=1)
  88 
  89 
  90         if not os.path.isfile(attach_dir+'/'+imgname+'.png'):
  91                 if not os.path.exists(outpath):
  92                         vartmp = config_latex_vartmp_dir
  93                         data = open("%s/%s.tex" % (vartmp, imgname), "w")
  94                         data.write('%s\n\\begin{document}\n%s\n\\end{document}' % (config_latex_header, texstr))
  95                         data.close()
  96 
  97                         cmd = "cd %(vartmp)s;%(latex)s %(options)s %(tex)s >/dev/null" % {
  98                                 "latex": config_external_latex,
  99                                 "vartmp": vartmp,
 100                                 "options": '-interaction=batchmode',
 101                                 "tex": imgname
 102                         }
 103                         os.umask(config_umask)
 104                         os.system(cmd)
 105                         os.system("cd %(vartmp)s; %(dvips)s -E %(imgname)s.dvi -o %(imgname)s.ps >/dev/null" % {
 106                                 "dvips": config_external_dvips,
 107                                 "vartmp": vartmp,
 108                                 "imgname": imgname,
 109                         })
 110 
 111                         os.system('%(convert)s -crop 0x0 -density 120x120 "%(vartmp)s/%(imgname)s".ps "%(outpath)s"'                            % {
 112                                 "convert": config_external_convert,
 113                                 "vartmp": vartmp,
 114                                 "outpath": attach_dir+'/'+imgname+'.png',
 115                                 "imgname": imgname,
 116                         })
 117 
 118 #       How many pages ?
 119                         id = open("%s/%s.ps" % (vartmp, imgname), "r")
 120                         txt=id.readlines()
 121                         id.close()
 122 
 123                         name="%%Pages:"
 124                         i=0
 125                         result= -1
 126                         for line in txt:
 127                                 if (line.count(name) == 1):
 128                                         result=i
 129                                         break
 130                                 i=i+1
 131 
 132 
 133                         n_pages=1
 134                         if (result > -1):
 135                                 page=txt[result]
 136                                 n_pages=int((split(page,name))[1])
 137 
 138 
 139                         if n_pages == 1: # this is the old case
 140                                 self.request.write(formatter.image(src="%s" % url, alt=texstr, align='absmiddle'))
 141                         else: # and this is for more as one page
 142                                 pages=range(n_pages)
 143                                 for i in pages:
 144                                         old_img_name=imgname+'.png.'+str(i)
 145 
 146                                         new_img_name=imgname+'_'+str(i)+'.png'
 147                                         new_url=AttachFile.getAttachUrl(pagename,new_img_name,self.request)
 148 
 149                                         cmd='%(config_external_mv)s "%(old_img_name)s" "%(new_img_name)s" ' %{
 150                                                 "config_external_mv": config_external_mv,
 151                                                 "old_img_name":attach_dir+'/'+old_img_name,
 152                                                 "new_img_name":attach_dir+'/'+new_img_name
 153                                         }
 154                                         os.system(cmd)
 155 
 156 
 157                                         self.request.write(formatter.image(src="%s" % new_url, alt=texstr, align='absmiddle'))
 158 
 159 
 160                         os.system("rm -f %(vartmp)s/%(imgname)s.*" % {
 161                                 "vartmp": vartmp,
 162                                 "imgname": imgname,
 163                         })
 164 
 165 
 166         else:
 167                 self.request.write(formatter.image(src="%s" % url, alt=texstr, align='absmiddle'))
 168       ## multipage images always created

MoinMoin: ParserMarket/obsolete latex-1.3.1-2.py (last edited 2007-10-29 19:07:59 by localhost)