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