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 = write or self.request.write 50 write(fmt.preformatted(1)) 51 write(fmt.text(self.raw.expandtabs())) 52 write(fmt.preformatted(0)) 53 54 # Extra API methods. 55 56 def formatForOutputType(self, mimetype, write=None): 57 58 """ 59 Format plain text for the given 'mimetype'. If the 'write' parameter is 60 specified, use it to write output; otherwise, write output using the 61 request. 62 """ 63 64 request = self.request 65 write = write or request.write 66 fmt = request.html_formatter 67 _ = request.getText 68 69 if mimetype == "text/html": 70 self.format(fmt, write) 71 elif mimetype == "text/plain": 72 write(self.raw) 73 74 # Class methods. 75 76 def getOutputTypes(self): 77 return self.output_mimetypes 78 79 getOutputTypes = classmethod(getOutputTypes) 80 81 # vim: tabstop=4 expandtab shiftwidth=4