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@25 | 103 | def getUpdateFragmentsFromPage(page, request): |
paul@25 | 104 | |
paul@25 | 105 | """ |
paul@25 | 106 | Get update fragments from the given 'page' using the 'request'. A list of |
paul@25 | 107 | update descriptions is returned, each being a tuple of the form... |
paul@25 | 108 | |
paul@25 | 109 | (fragment identifer, summary text, preferred mimetypes, textual content, |
paul@25 | 110 | updated datetime) |
paul@25 | 111 | """ |
paul@25 | 112 | |
paul@25 | 113 | updates = [] |
paul@25 | 114 | |
paul@25 | 115 | # NOTE: Use the updated datetime from the page for updates. |
paul@25 | 116 | # NOTE: The published and updated details would need to be deduced from |
paul@25 | 117 | # NOTE: the page history instead of being taken from the page as a whole. |
paul@25 | 118 | |
paul@25 | 119 | metadata = getMetadata(page) |
paul@25 | 120 | updated = getUpdatedTime(metadata) |
paul@25 | 121 | |
paul@25 | 122 | # Get the fragment regions for the page. |
paul@25 | 123 | |
paul@25 | 124 | for n, (format, attributes, body) in enumerate(getFragments(page.get_raw_body())): |
paul@25 | 125 | |
paul@25 | 126 | # Produce a fragment identifier. |
paul@25 | 127 | # NOTE: Choose a more robust identifier where none is explicitly given. |
paul@25 | 128 | |
paul@25 | 129 | fragment = attributes.get("fragment", str(n)) |
paul@25 | 130 | summary = attributes.get("summary", "Update #%d" % n) |
paul@25 | 131 | |
paul@25 | 132 | # Get the preferred content types available for the fragment. |
paul@25 | 133 | |
paul@25 | 134 | preferred = getPreferredOutputTypes(request, getOutputTypes(request, format)) |
paul@25 | 135 | |
paul@25 | 136 | # Try and obtain some suitable content for the entry. |
paul@25 | 137 | # NOTE: Could potentially get a summary for the fragment. |
paul@25 | 138 | |
paul@25 | 139 | content = None |
paul@25 | 140 | |
paul@25 | 141 | if "text/html" in preferred: |
paul@25 | 142 | parser_cls = getParserClass(request, format) |
paul@25 | 143 | parser = parser_cls(body, request) |
paul@25 | 144 | |
paul@25 | 145 | if format == "html": |
paul@25 | 146 | content = body |
paul@25 | 147 | elif hasattr(parser, "formatForOutputType"): |
paul@25 | 148 | s = StringIO() |
paul@25 | 149 | parser.formatForOutputType("text/html", write=s.write) |
paul@25 | 150 | content = s.getvalue() |
paul@25 | 151 | else: |
paul@25 | 152 | fmt = request.html_formatter |
paul@25 | 153 | fmt.setPage(page) |
paul@25 | 154 | content = formatText(body, request, fmt, parser_cls) |
paul@25 | 155 | |
paul@25 | 156 | updates.append((fragment, summary, preferred, content, updated)) |
paul@25 | 157 | |
paul@25 | 158 | return updates |
paul@25 | 159 | |
paul@0 | 160 | # vim: tabstop=4 expandtab shiftwidth=4 |