paul@63 | 1 | # -*- coding: iso-8859-1 -*- |
paul@63 | 2 | """ |
paul@63 | 3 | MoinMoin - text_plain (MoinShare) |
paul@63 | 4 | |
paul@63 | 5 | @copyright: 2012, 2013, 2014 by Paul Boddie <paul@boddie.org.uk> |
paul@63 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@63 | 7 | """ |
paul@63 | 8 | |
paul@63 | 9 | from MoinSupport import parseAttributes |
paul@63 | 10 | |
paul@63 | 11 | Dependencies = ["pages"] |
paul@63 | 12 | |
paul@63 | 13 | # Parser support. |
paul@63 | 14 | |
paul@63 | 15 | class Parser: |
paul@63 | 16 | |
paul@63 | 17 | "Display data in text/plain format." |
paul@63 | 18 | |
paul@63 | 19 | Dependencies = Dependencies |
paul@63 | 20 | extensions = [".txt"] |
paul@63 | 21 | |
paul@63 | 22 | # Input content types understood by this parser. |
paul@63 | 23 | |
paul@63 | 24 | input_mimetypes = ["text/plain"] |
paul@63 | 25 | |
paul@63 | 26 | # Output content types preferred by this parser. |
paul@63 | 27 | |
paul@63 | 28 | output_mimetypes = ["text/plain", "text/html"] |
paul@63 | 29 | |
paul@63 | 30 | def __init__(self, raw, request, **kw): |
paul@63 | 31 | |
paul@63 | 32 | """ |
paul@63 | 33 | Initialise the parser with the given 'raw' data, 'request' and any |
paul@63 | 34 | keyword arguments that may have been supplied. |
paul@63 | 35 | """ |
paul@63 | 36 | |
paul@63 | 37 | self.raw = raw |
paul@63 | 38 | self.request = request |
paul@63 | 39 | self.attrs = parseAttributes(kw.get("format_args", ""), False) |
paul@63 | 40 | |
paul@63 | 41 | def format(self, fmt, write=None): |
paul@63 | 42 | |
paul@63 | 43 | """ |
paul@63 | 44 | Format plain text using the given formatter 'fmt'. If the 'write' |
paul@63 | 45 | parameter is specified, use it to write output; otherwise, write output |
paul@63 | 46 | using the request. |
paul@63 | 47 | """ |
paul@63 | 48 | |
paul@65 | 49 | write = write or self.request.write |
paul@65 | 50 | write(fmt.preformatted(1)) |
paul@65 | 51 | write(fmt.text(self.raw.expandtabs())) |
paul@65 | 52 | write(fmt.preformatted(0)) |
paul@63 | 53 | |
paul@63 | 54 | # Extra API methods. |
paul@63 | 55 | |
paul@63 | 56 | def formatForOutputType(self, mimetype, write=None): |
paul@63 | 57 | |
paul@63 | 58 | """ |
paul@63 | 59 | Format plain text for the given 'mimetype'. If the 'write' parameter is |
paul@63 | 60 | specified, use it to write output; otherwise, write output using the |
paul@63 | 61 | request. |
paul@63 | 62 | """ |
paul@63 | 63 | |
paul@63 | 64 | request = self.request |
paul@63 | 65 | write = write or request.write |
paul@63 | 66 | fmt = request.html_formatter |
paul@63 | 67 | _ = request.getText |
paul@63 | 68 | |
paul@63 | 69 | if mimetype == "text/html": |
paul@63 | 70 | self.format(fmt, write) |
paul@63 | 71 | elif mimetype == "text/plain": |
paul@63 | 72 | write(self.raw) |
paul@63 | 73 | |
paul@63 | 74 | # Class methods. |
paul@63 | 75 | |
paul@63 | 76 | def getOutputTypes(self): |
paul@63 | 77 | return self.output_mimetypes |
paul@63 | 78 | |
paul@63 | 79 | getOutputTypes = classmethod(getOutputTypes) |
paul@63 | 80 | |
paul@63 | 81 | # vim: tabstop=4 expandtab shiftwidth=4 |