1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - ShowMessages Macro 4 5 @copyright: 2012, 2013 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 \ 10 ItemStore, get_form, parseMacroArguments, getParsersForContentType, formatText 11 from email.parser import Parser 12 13 try: 14 from cStringIO import StringIO 15 except ImportError: 16 from StringIO import StringIO 17 18 Dependencies = ['pages'] 19 20 # Macro functions. 21 22 def execute(macro, args): 23 24 """ 25 Execute the 'macro' with the given 'args' to produce a list of messages from 26 a message store associated with the current page. 27 """ 28 29 request = macro.request 30 fmt = macro.formatter 31 page = fmt.page 32 _ = request.getText 33 34 # Interpret the arguments. 35 36 parsed_args = dict(parseMacroArguments(args)) 37 fragment = parsed_args.get("fragment") 38 39 # Obtain any request parameters corresponding to the field. 40 # NOTE: Get selection of representations for display. 41 42 form = get_form(request) 43 44 # Show the messages. 45 # NOTE: Support additional access control. 46 47 store = ItemStore(page, "messages", "message-locks") 48 49 output = [] 50 output.append(fmt.div(on=1, css_class="showmessages-messages")) 51 52 for message_text in iter(store).reversed(): 53 message = Parser().parse(StringIO(message_text)) 54 55 # Determine whether the message has several representations. 56 57 if not message.is_multipart(): 58 parts = [message] 59 else: 60 parts = message.get_payload() 61 62 # Show the parts. 63 64 output.append(fmt.div(on=1, css_class="showmessages-message")) 65 first = True 66 67 for part in parts: 68 69 # Set the appearance of the part. 70 71 extra_flags = first and " showmessages-shown-part" or "" 72 css_class = "showmessages-message-part%s" % extra_flags 73 74 output.append(fmt.div(on=1, css_class=css_class)) 75 76 # Find a parser for the content type. 77 78 mimetype = part.get_content_type() 79 parsers = getParsersForContentType(request.cfg, mimetype) 80 81 if parsers: 82 for parser_cls in parsers: 83 84 # NOTE: Could check for output_mimetypes and use formatForOutputType. 85 # Emit the parsed content. 86 87 output.append(formatText(part.get_payload(), request, fmt, parser_cls=parser_cls)) 88 break 89 else: 90 output.append(fmt.text(_("Update cannot be shown for content of type %s.") % mimetype)) 91 92 # End message part. 93 94 output.append(fmt.div(on=0)) 95 first = False 96 97 # End message. 98 99 output.append(fmt.div(on=0)) 100 101 # End message list. 102 103 output.append(fmt.div(on=0)) 104 return u"".join(output) 105 106 # vim: tabstop=4 expandtab shiftwidth=4