Attachment 'text_x_exportfile-1.6.0-2.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - ExportFile parser
   4 
   5     PURPOSE:
   6       This parser is used to download some content of a wiki page to a file. It needs the action exportfile.py
   7 
   8     CALLING SEQUENCE:
   9       {{{
  10       #!ExportFile [file=file]
  11       = Example =
  12       }}}
  13 
  14     OPTIONAL KEYWORD PARAMETERS:
  15         file:              file name to save to
  16 
  17     EXAMPLE:
  18 {{{#!ExportFile
  19 = some wiki mark up =
  20  * A
  21  * B
  22 }}}
  23 
  24 
  25 {{{#!ExportFile file=example.txt
  26 #format plain
  27 AAEE BBAA CCAA
  28 DDDE FFAE BBCD
  29 }}}
  30 
  31 
  32 {{{#!ExportFile file=example.py
  33 #format python
  34 def format(self, formatter):
  35     self.pagename = formatter.page.page_name
  36     self.quoted_pagename = wikiutil.quoteWikinameURL(self.pagename)
  37     attachment_path = AttachFile.getAttachDir(self.request, self.pagename, create=1)
  38 }}}
  39 
  40 
  41     PROCEDURE:
  42       ABOUT:
  43       This parser is the result of a discussion with ThiloPfennig about saving conten of a page to a file 
  44       while editing functionality is done from the wiki. see FeatureRequests/ExportFileAction
  45 
  46     MODIFICATION HISTORY:
  47         Version 1.6.0-1
  48         1.6.0-2 some code cleanup
  49         @copyright: 2007 by Reimar Bauer (R.Bauer@fz-juelich.de)
  50         @license: GNU GPL, see COPYING for details.
  51 
  52 """
  53 Dependencies = ['time'] # do not cache
  54 import StringIO, codecs
  55 from MoinMoin.action import AttachFile
  56 from MoinMoin import wikiutil
  57 from MoinMoin.parser import text_moin_wiki
  58 
  59 class Parser:
  60     extensions = '*'
  61     def __init__(self, raw, request, **kw):
  62         self.file = 'exportfile.txt'
  63         test = kw.get('format_args', '')
  64         if test:
  65             for arg in kw.get('format_args', '').split(','):
  66                 if arg.find('=') > -1:
  67                     key, value = arg.split('=')
  68                     setattr(self, key, wikiutil.escape(value.strip(), quote=1))
  69         self.raw = raw
  70         self.request = request
  71         self.form = request.form
  72         self._ = request.getText
  73 
  74     def get_parser(self):
  75         body = self.raw
  76         pi = wikiutil.get_process_instruction(body)
  77         pi_format = self.request.cfg.default_markup or "wiki"
  78 
  79         # check for XML content
  80         if body and body[:5] == '<?xml':
  81             pi_format = "xslt"
  82 
  83         # check processing instructions
  84         if pi.has_key('format'):
  85             pi_format = (pi["format"])
  86             pi_format = pi_format.lower()
  87 
  88         Parser = wikiutil.searchAndImportPlugin(self.request.cfg, "parser", pi_format)
  89         return Parser
  90 
  91     def format(self, formatter):
  92         self.pagename = formatter.page.page_name
  93         self.quoted_pagename = wikiutil.quoteWikinameURL(self.pagename)
  94         attachment_path = AttachFile.getAttachDir(self.request, self.pagename, create=1)
  95 
  96         Parser = self.get_parser()
  97 
  98         lines = self.raw.split('\n')
  99         raw = []
 100         for line in lines:
 101             is_good = True
 102             for pi in ("#format", "#refresh", "#redirect", "#deprecated",
 103                        "#pragma", "#form", "#acl", "#language"):
 104                 if line.lower().startswith(pi):
 105                    is_good = False
 106                    break
 107             if is_good:
 108                raw.append(line)
 109         raw = '\n'.join(raw)
 110 
 111         out = StringIO.StringIO()
 112         self.request.redirect(out)
 113         wikiizer = Parser(raw, self.request)
 114         wikiizer.format(formatter)
 115         result = out.getvalue()
 116         self.request.redirect()
 117         del out
 118 
 119         self.request.write(result)
 120 
 121         form = """
 122 <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
 123    <input type="hidden" name="action" value="exportfile">
 124    <input type="hidden" name="ticket" value="%(ticket)s">
 125    <input type="hidden" name="file" value="%(file)s">
 126    <input type="hidden" name="raw" value="%(raw)s">
 127    <input type="hidden" name="content-type" value="%(content_type)s"> 
 128    <input type="submit" value="download %(file)s">
 129 </form>""" % {
 130 "baseurl": self.request.getBaseURL(),
 131 "ticket": wikiutil.createTicket(self.request),
 132 "pagename": self.pagename,
 133 "file": self.file,
 134 "content_type": "text/plain",
 135 "raw": raw,
 136 }
 137         self.request.write(form)

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] (2007-01-02 19:13:36, 13.2 KB) [[attachment:ExportFile_1.png]]
  • [get | view] (2007-01-02 19:27:33, 0.2 KB) [[attachment:example.py]]
  • [get | view] (2007-01-02 19:27:41, 0.0 KB) [[attachment:example.txt]]
  • [get | view] (2007-01-02 19:14:06, 1.4 KB) [[attachment:exportfile-1.6.0-1.py]]
  • [get | view] (2007-01-02 19:27:52, 0.0 KB) [[attachment:exportfile.txt]]
  • [get | view] (2007-01-02 19:13:52, 4.4 KB) [[attachment:text_x_exportfile-1.6.0-1.py]]
  • [get | view] (2007-01-04 04:56:37, 4.0 KB) [[attachment:text_x_exportfile-1.6.0-2.py]]
  • [get | view] (2007-01-04 05:16:14, 3.6 KB) [[attachment:text_x_exportfile-1.6.0-3.py]]
  • [get | view] (2007-01-04 04:57:15, 1.1 KB) [[attachment:wikiutil_20070103_patch.txt]]
  • [get | view] (2007-01-04 05:15:49, 1.6 KB) [[attachment:wikiutil_20070104_patch.txt]]
 All files | Selected Files: delete move to page copy to page

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