Attachment 'text_x_exportfile-1.6.0-1.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         @copyright: 2007 by Reimar Bauer (R.Bauer@fz-juelich.de)
  49         @license: GNU GPL, see COPYING for details.
  50 
  51 """
  52 Dependencies = ['time'] # do not cache
  53 import StringIO, codecs
  54 
  55 from MoinMoin.action import AttachFile
  56 from MoinMoin import wikiutil
  57 
  58 from MoinMoin.parser import text_moin_wiki
  59 
  60 class Parser:
  61     extensions = '*'
  62     def __init__(self, raw, request, **kw):
  63         self.file = 'exportfile.txt'
  64         test = kw.get('format_args', '')
  65         if test:
  66             for arg in kw.get('format_args', '').split(','):
  67                 if arg.find('=') > -1:
  68                     key, value = arg.split('=')
  69                     setattr(self, key, wikiutil.escape(value.strip(), quote=1))
  70         self.raw = raw
  71         self.request = request
  72         self.form = request.form
  73         self._ = request.getText
  74 
  75     def get_parser(self):
  76         body = self.raw
  77         pi_format = self.request.cfg.default_markup or "wiki"
  78         pi_lines = 0
  79 
  80         # check for XML content
  81         if body and body[:5] == '<?xml':
  82             pi_format = "xslt"
  83 
  84         # check processing instructions
  85         while body and body[0] == '#':
  86             pi_lines += 1
  87 
  88             # extract first line
  89             try:
  90                 line, body = body.split('\n', 1)
  91             except ValueError:
  92                 line = body
  93                 body = ''
  94 
  95             # end parsing on empty (invalid) PI
  96             if line == "#":
  97                 body = line + '\n' + body
  98                 break
  99 
 100             # skip comments (lines with two hash marks)
 101             if line[1] == '#': continue
 102 
 103             # parse the PI
 104             verb, args = (line[1:]+' ').split(' ', 1)
 105             verb = verb.lower()
 106             args = args.strip()
 107 
 108             # check the PIs
 109             if verb == "format":
 110                 # markup format
 111                 pi_format, pi_formatargs = (args+' ').split(' ', 1)
 112                 pi_format = pi_format.lower()
 113                 pi_formatargs = pi_formatargs.strip()
 114 
 115         Parser = wikiutil.searchAndImportPlugin(self.request.cfg, "parser", pi_format)
 116         return Parser, pi_lines
 117 
 118     def format(self, formatter):
 119         self.pagename = formatter.page.page_name
 120         self.quoted_pagename = wikiutil.quoteWikinameURL(self.pagename)
 121         attachment_path = AttachFile.getAttachDir(self.request, self.pagename, create=1)
 122 
 123         Parser, pi_lines = self.get_parser()
 124         raw = self.raw.split('\n')
 125         raw = raw[pi_lines:]
 126         raw = '\n'.join(raw)
 127 
 128         out = StringIO.StringIO()
 129         self.request.redirect(out)
 130         wikiizer = Parser(raw, self.request)
 131         wikiizer.format(formatter)
 132         result = out.getvalue()
 133         self.request.redirect()
 134         del out
 135 
 136         self.request.write(result)
 137 
 138         form = """
 139 <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
 140    <input type="hidden" name="action" value="exportfile">
 141    <input type="hidden" name="ticket" value="%(ticket)s">
 142    <input type="hidden" name="file" value="%(file)s">
 143    <input type="hidden" name="raw" value="%(raw)s">
 144    <input type="hidden" name="content-type" value="%(content_type)s"> 
 145    <input type="submit" value="download %(file)s">
 146 </form>""" % {
 147 "baseurl": self.request.getBaseURL(),
 148 "ticket": wikiutil.createTicket(self.request),
 149 "pagename": self.pagename,
 150 "file": self.file,
 151 "content_type": "text/plain",
 152 "raw": raw,
 153 }
 154         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.