1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/parsers/text_plain.py Thu Apr 03 22:25:14 2014 +0200
1.3 @@ -0,0 +1,78 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - text_plain (MoinShare)
1.7 +
1.8 + @copyright: 2012, 2013, 2014 by Paul Boddie <paul@boddie.org.uk>
1.9 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.10 +"""
1.11 +
1.12 +from MoinSupport import parseAttributes
1.13 +
1.14 +Dependencies = ["pages"]
1.15 +
1.16 +# Parser support.
1.17 +
1.18 +class Parser:
1.19 +
1.20 + "Display data in text/plain format."
1.21 +
1.22 + Dependencies = Dependencies
1.23 + extensions = [".txt"]
1.24 +
1.25 + # Input content types understood by this parser.
1.26 +
1.27 + input_mimetypes = ["text/plain"]
1.28 +
1.29 + # Output content types preferred by this parser.
1.30 +
1.31 + output_mimetypes = ["text/plain", "text/html"]
1.32 +
1.33 + def __init__(self, raw, request, **kw):
1.34 +
1.35 + """
1.36 + Initialise the parser with the given 'raw' data, 'request' and any
1.37 + keyword arguments that may have been supplied.
1.38 + """
1.39 +
1.40 + self.raw = raw
1.41 + self.request = request
1.42 + self.attrs = parseAttributes(kw.get("format_args", ""), False)
1.43 +
1.44 + def format(self, fmt, write=None):
1.45 +
1.46 + """
1.47 + Format plain text using the given formatter 'fmt'. If the 'write'
1.48 + parameter is specified, use it to write output; otherwise, write output
1.49 + using the request.
1.50 + """
1.51 +
1.52 + (write or self.request.write)(fmt.text(self.raw))
1.53 +
1.54 + # Extra API methods.
1.55 +
1.56 + def formatForOutputType(self, mimetype, write=None):
1.57 +
1.58 + """
1.59 + Format plain text for the given 'mimetype'. If the 'write' parameter is
1.60 + specified, use it to write output; otherwise, write output using the
1.61 + request.
1.62 + """
1.63 +
1.64 + request = self.request
1.65 + write = write or request.write
1.66 + fmt = request.html_formatter
1.67 + _ = request.getText
1.68 +
1.69 + if mimetype == "text/html":
1.70 + self.format(fmt, write)
1.71 + elif mimetype == "text/plain":
1.72 + write(self.raw)
1.73 +
1.74 + # Class methods.
1.75 +
1.76 + def getOutputTypes(self):
1.77 + return self.output_mimetypes
1.78 +
1.79 + getOutputTypes = classmethod(getOutputTypes)
1.80 +
1.81 +# vim: tabstop=4 expandtab shiftwidth=4