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