1 """
   2     MoinMoin - "application/pdf" Formatter
   3     Uses the text/latex formatter and then pdflatex.
   4     
   5     NOT SECURE. USE ONLY ON VERY MUCH TRUSTED WIKIS LIKE A LOCAL ONE.
   6     
   7     No error checking, no support. Works For Me (TM)
   8     Needs my latex parser and text/latex formatter installed.
   9 
  10     Copyright 2005 Johannes Berg <johannes@sipsolutions.net>
  11 
  12     All rights reserved, see COPYING for details.
  13 """
  14 
  15 # Imports
  16 
  17 from MoinMoin import wikiutil
  18 from MoinMoin.action import AttachFile
  19 import tempfile, os
  20 from shutil import copyfile
  21 
  22 def Formatter(request, **kw):
  23   LatexFormatter = wikiutil.importPlugin(request.cfg, 'formatter', 'text_latex', 'Formatter')
  24   call_command_in_dir = wikiutil.importPlugin(request.cfg, 'parser', 'latex', 'call_command_in_dir')
  25   class PDFFormatter(LatexFormatter):
  26     def __init__(self, request, **kw):
  27         apply(LatexFormatter.__init__, (self, request), kw)
  28         self.verbatim = False
  29         self.itemized = False
  30         self.request = request
  31         self._write = request.write
  32         request.write = self.write
  33 
  34     def write(self, txt):
  35         self.texfile.write(txt.encode('utf-8'))
  36 
  37     def startDocument(self, pagename):
  38         self.pagename = pagename
  39         self.tmpdir = tempfile.mkdtemp()
  40         self.texfile = open("%s/document.tex" % self.tmpdir, "w")
  41         return LatexFormatter.startDocument(self, pagename)
  42 
  43     def endDocument(self):
  44         self.request.write = self._write
  45         self.texfile.write(LatexFormatter.endDocument(self).encode('utf-8'))
  46         self.texfile.close()
  47         for img in self.images:
  48             copyfile(img, "%s/%s" % (self.tmpdir, img.split('/')[-1]))
  49         res = call_command_in_dir("pdflatex", ["document.tex"], self.tmpdir)
  50         try:
  51             if not res is None:
  52                 self._write(res)
  53                 return ''
  54             else:
  55                 f = open("%s/document.pdf" % self.tmpdir, "rb")
  56                 data = f.read(8192)
  57                 while data != '':
  58                     self._write(data)
  59                     data = f.read(8192)
  60                 f.close()
  61         finally:
  62               for root,dirs,files in os.walk(self.tmpdir, topdown=False):
  63                   for name in files:
  64                       os.remove(os.path.join(root,name))
  65                   for name in dirs:
  66                       os.rmdir(os.path.join(root,name))
  67               os.rmdir(self.tmpdir)
  68               return ''
  69 
  70 
  71         
  72 
  73     images = []
  74     def image(self, **kw):
  75         imgname = kw['src'].split('=')[-1]
  76         self.images += [AttachFile.getFilename(self.request, self.pagename, imgname)]
  77         return '\\includegraphics{%s}' % imgname
  78 
  79   return PDFFormatter(request, **kw)

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