1 """
   2     MoinMoin - "multipart/latex" Formatter
   3     This parser handles images by adding them in a multipart/related
   4     MIME message.
   5 
   6     Copyright 2005 Johannes Berg <johannes@sipsolutions.net>
   7 
   8     All rights reserved, see COPYING for details.
   9 """
  10 
  11 # Imports
  12 
  13 from MoinMoin import wikiutil
  14 from MoinMoin.action import AttachFile
  15 from cStringIO import StringIO
  16 import base64
  17 
  18 def Formatter(request, **kw):
  19   LatexFormatter = wikiutil.importPlugin(request.cfg, 'formatter', 'text_latex', 'Formatter')
  20   class MultipartFormatter(LatexFormatter):
  21     images = []
  22     def startDocument(self, pagename):
  23         self.pagename = pagename
  24         return """
  25 Content-Type: multipart/mixed;  boundary="----=_Part_1713_32419636.1123709120859"
  26 Mime-Version: 1.0
  27 
  28 ------=_Part_1713_32419636.1123709120859
  29 Content-Type: text/latex; charset=utf-8; name="document.tex"
  30 Content-Transfer-Encoding: 8bit
  31 
  32 """ + LatexFormatter.startDocument(self, pagename)
  33 
  34     def endDocument(self):
  35         result = LatexFormatter.endDocument(self) + '------=_Part_1713_32419636.1123709120859'
  36         for img in self.images:
  37           b64 = StringIO()
  38           base64.encode(open(img), b64)
  39           b64 = b64.getvalue()
  40           result += '\nContent-Type: application/octet-stream; name="%s"\nContent-Transfer-Encoding: base64\n\n%s\n------=_Part_1713_32419636.1123709120859' % (img.split('/')[-1], b64)
  41         result+='--\n'
  42         return result
  43 
  44     def image(self, **kw):
  45         imgname = kw['src'].split('=')[-1]
  46         self.images += [AttachFile.getFilename(self.request, self.pagename, imgname)]
  47         return '\\includegraphics{%s}' % imgname
  48 
  49   return MultipartFormatter(request, **kw)

MoinMoin: FormatterMarket/multipart_latex.py (last edited 2007-10-29 19:19:45 by localhost)