paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - MoinShare library |
paul@0 | 4 | |
paul@17 | 5 | @copyright: 2011, 2012, 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 7 | """ |
paul@0 | 8 | |
paul@17 | 9 | from ContentTypeSupport import getContentPreferences |
paul@17 | 10 | from DateSupport import getCurrentTime |
paul@0 | 11 | from MoinSupport import * |
paul@15 | 12 | from MoinMoin import wikiutil |
paul@0 | 13 | import re |
paul@0 | 14 | |
paul@25 | 15 | try: |
paul@25 | 16 | from cStringIO import StringIO |
paul@25 | 17 | except ImportError: |
paul@25 | 18 | from StringIO import StringIO |
paul@25 | 19 | |
paul@0 | 20 | escape = wikiutil.escape |
paul@16 | 21 | _getFragments = getFragments |
paul@0 | 22 | |
paul@0 | 23 | __version__ = "0.1" |
paul@0 | 24 | |
paul@0 | 25 | # More Moin 1.9 compatibility functions. |
paul@0 | 26 | |
paul@0 | 27 | def has_member(request, groupname, username): |
paul@0 | 28 | if hasattr(request.dicts, "has_member"): |
paul@0 | 29 | return request.dicts.has_member(groupname, username) |
paul@0 | 30 | else: |
paul@0 | 31 | return username in request.dicts.get(groupname, []) |
paul@0 | 32 | |
paul@3 | 33 | # Fragments employ a "moinshare" attribute. |
paul@3 | 34 | |
paul@3 | 35 | fragment_attribute = "moinshare" |
paul@2 | 36 | |
paul@16 | 37 | def getFragments(s): |
paul@25 | 38 | |
paul@25 | 39 | "Return all fragments in 's' having the MoinShare fragment attribute." |
paul@25 | 40 | |
paul@2 | 41 | fragments = [] |
paul@16 | 42 | for format, attributes, body in _getFragments(s): |
paul@16 | 43 | if attributes.has_key(fragment_attribute): |
paul@16 | 44 | fragments.append((format, attributes, body)) |
paul@2 | 45 | return fragments |
paul@2 | 46 | |
paul@9 | 47 | def getOutputTypes(request, format): |
paul@9 | 48 | |
paul@9 | 49 | """ |
paul@9 | 50 | Using the 'request' and the 'format' of a fragment, return the media types |
paul@9 | 51 | available for the fragment. |
paul@9 | 52 | """ |
paul@9 | 53 | |
paul@9 | 54 | # This uses an extended parser API method if available. |
paul@9 | 55 | |
paul@9 | 56 | parser = getParserClass(request, format) |
paul@9 | 57 | if hasattr(parser, "getOutputTypes"): |
paul@9 | 58 | return parser.getOutputTypes() |
paul@9 | 59 | else: |
paul@9 | 60 | return ["text/html"] |
paul@9 | 61 | |
paul@9 | 62 | def getPreferredOutputTypes(request, mimetypes): |
paul@9 | 63 | |
paul@9 | 64 | """ |
paul@9 | 65 | Using the 'request', perform content negotiation, obtaining mimetypes common |
paul@9 | 66 | to the fragment (given by 'mimetypes') and the client (found in the Accept |
paul@9 | 67 | header). |
paul@9 | 68 | """ |
paul@9 | 69 | |
paul@9 | 70 | accept = getHeader(request, "Accept", "HTTP") |
paul@12 | 71 | if accept: |
paul@12 | 72 | prefs = getContentPreferences(accept) |
paul@12 | 73 | return prefs.get_preferred_types(mimetypes) |
paul@12 | 74 | else: |
paul@12 | 75 | return mimetypes |
paul@9 | 76 | |
paul@9 | 77 | def getUpdatedTime(metadata): |
paul@9 | 78 | |
paul@9 | 79 | """ |
paul@9 | 80 | Return the last updated time based on the given 'metadata', using the |
paul@9 | 81 | current time if no explicit last modified time is specified. |
paul@9 | 82 | """ |
paul@9 | 83 | |
paul@9 | 84 | # NOTE: We could attempt to get the last edit time of a fragment. |
paul@9 | 85 | |
paul@9 | 86 | latest_timestamp = metadata.get("last-modified") |
paul@9 | 87 | if latest_timestamp: |
paul@9 | 88 | return latest_timestamp.as_ISO8601_datetime_string() |
paul@9 | 89 | else: |
paul@9 | 90 | return getCurrentTime().as_ISO8601_datetime_string() |
paul@9 | 91 | |
paul@25 | 92 | def getUpdateSources(request, sources_page): |
paul@25 | 93 | |
paul@25 | 94 | """ |
paul@25 | 95 | Using the 'request', return the update sources defined on the given |
paul@25 | 96 | 'sources_page'. |
paul@25 | 97 | """ |
paul@25 | 98 | |
paul@25 | 99 | # Remote sources are accessed via dictionary page definitions. |
paul@25 | 100 | |
paul@25 | 101 | return getWikiDict(sources_page, request) |
paul@25 | 102 | |
paul@30 | 103 | # Entry/update classes. |
paul@30 | 104 | |
paul@30 | 105 | class Update: |
paul@30 | 106 | |
paul@30 | 107 | "A feed update entry." |
paul@30 | 108 | |
paul@30 | 109 | def __init__(self): |
paul@30 | 110 | self.title = None |
paul@30 | 111 | self.link = None |
paul@30 | 112 | self.content = None |
paul@30 | 113 | self.content_type = None |
paul@30 | 114 | self.updated = None |
paul@30 | 115 | |
paul@30 | 116 | # Page-related attributes. |
paul@30 | 117 | |
paul@30 | 118 | self.fragment = None |
paul@30 | 119 | self.preferred = None |
paul@30 | 120 | |
paul@30 | 121 | def __cmp__(self, other): |
paul@30 | 122 | if self.updated is None and other.updated is not None: |
paul@30 | 123 | return 1 |
paul@30 | 124 | elif self.updated is not None and other.updated is None: |
paul@30 | 125 | return -1 |
paul@30 | 126 | else: |
paul@30 | 127 | return cmp(self.updated, other.updated) |
paul@30 | 128 | |
paul@30 | 129 | # Update retrieval from pages. |
paul@30 | 130 | |
paul@30 | 131 | def getUpdatesFromPage(page, request): |
paul@25 | 132 | |
paul@25 | 133 | """ |
paul@30 | 134 | Get updates from the given 'page' using the 'request'. A list of update |
paul@30 | 135 | objects is returned. |
paul@25 | 136 | """ |
paul@25 | 137 | |
paul@25 | 138 | updates = [] |
paul@25 | 139 | |
paul@25 | 140 | # NOTE: Use the updated datetime from the page for updates. |
paul@25 | 141 | # NOTE: The published and updated details would need to be deduced from |
paul@25 | 142 | # NOTE: the page history instead of being taken from the page as a whole. |
paul@25 | 143 | |
paul@25 | 144 | metadata = getMetadata(page) |
paul@25 | 145 | updated = getUpdatedTime(metadata) |
paul@25 | 146 | |
paul@25 | 147 | # Get the fragment regions for the page. |
paul@25 | 148 | |
paul@25 | 149 | for n, (format, attributes, body) in enumerate(getFragments(page.get_raw_body())): |
paul@25 | 150 | |
paul@25 | 151 | # Produce a fragment identifier. |
paul@25 | 152 | # NOTE: Choose a more robust identifier where none is explicitly given. |
paul@25 | 153 | |
paul@30 | 154 | update = Update() |
paul@30 | 155 | |
paul@30 | 156 | update.fragment = attributes.get("fragment", str(n)) |
paul@30 | 157 | update.title = attributes.get("summary", "Update #%d" % n) |
paul@25 | 158 | |
paul@25 | 159 | # Get the preferred content types available for the fragment. |
paul@25 | 160 | |
paul@30 | 161 | update.preferred = getPreferredOutputTypes(request, getOutputTypes(request, format)) |
paul@25 | 162 | |
paul@25 | 163 | # Try and obtain some suitable content for the entry. |
paul@25 | 164 | # NOTE: Could potentially get a summary for the fragment. |
paul@25 | 165 | |
paul@30 | 166 | update.content = None |
paul@25 | 167 | |
paul@30 | 168 | if "text/html" in update.preferred: |
paul@25 | 169 | parser_cls = getParserClass(request, format) |
paul@25 | 170 | parser = parser_cls(body, request) |
paul@25 | 171 | |
paul@25 | 172 | if format == "html": |
paul@30 | 173 | update.content = body |
paul@25 | 174 | elif hasattr(parser, "formatForOutputType"): |
paul@25 | 175 | s = StringIO() |
paul@25 | 176 | parser.formatForOutputType("text/html", write=s.write) |
paul@30 | 177 | update.content = s.getvalue() |
paul@25 | 178 | else: |
paul@25 | 179 | fmt = request.html_formatter |
paul@25 | 180 | fmt.setPage(page) |
paul@30 | 181 | update.content = formatText(body, request, fmt, parser_cls) |
paul@30 | 182 | |
paul@30 | 183 | update.content_type = "html" |
paul@25 | 184 | |
paul@30 | 185 | update.link = page.url(request) |
paul@30 | 186 | update.updated = updated |
paul@30 | 187 | |
paul@30 | 188 | updates.append(update) |
paul@25 | 189 | |
paul@25 | 190 | return updates |
paul@25 | 191 | |
paul@0 | 192 | # vim: tabstop=4 expandtab shiftwidth=4 |