# HG changeset patch # User Paul Boddie # Date 1368225303 -7200 # Node ID ef2a98e8bc9ac36e64bf1b82f7366c01ca7cea44 # Parent f18627d8a5dee09a19d9701a67856f4af336c9e5 Moved page update retrieval into the common module since it will be used for direct, internal retrieval of updates by the macro, eliminating the need for the macro to use RSS or Atom feeds to fetch updates from within the same wiki. diff -r f18627d8a5de -r ef2a98e8bc9a MoinShare.py --- a/MoinShare.py Tue May 07 23:44:34 2013 +0200 +++ b/MoinShare.py Sat May 11 00:35:03 2013 +0200 @@ -12,6 +12,11 @@ from MoinMoin import wikiutil import re +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO + escape = wikiutil.escape _getFragments = getFragments @@ -30,6 +35,9 @@ fragment_attribute = "moinshare" def getFragments(s): + + "Return all fragments in 's' having the MoinShare fragment attribute." + fragments = [] for format, attributes, body in _getFragments(s): if attributes.has_key(fragment_attribute): @@ -81,4 +89,72 @@ else: return getCurrentTime().as_ISO8601_datetime_string() +def getUpdateSources(request, sources_page): + + """ + Using the 'request', return the update sources defined on the given + 'sources_page'. + """ + + # Remote sources are accessed via dictionary page definitions. + + return getWikiDict(sources_page, request) + +def getUpdateFragmentsFromPage(page, request): + + """ + Get update fragments from the given 'page' using the 'request'. A list of + update descriptions is returned, each being a tuple of the form... + + (fragment identifer, summary text, preferred mimetypes, textual content, + updated datetime) + """ + + updates = [] + + # NOTE: Use the updated datetime from the page for updates. + # NOTE: The published and updated details would need to be deduced from + # NOTE: the page history instead of being taken from the page as a whole. + + metadata = getMetadata(page) + updated = getUpdatedTime(metadata) + + # Get the fragment regions for the page. + + for n, (format, attributes, body) in enumerate(getFragments(page.get_raw_body())): + + # Produce a fragment identifier. + # NOTE: Choose a more robust identifier where none is explicitly given. + + fragment = attributes.get("fragment", str(n)) + summary = attributes.get("summary", "Update #%d" % n) + + # Get the preferred content types available for the fragment. + + preferred = getPreferredOutputTypes(request, getOutputTypes(request, format)) + + # Try and obtain some suitable content for the entry. + # NOTE: Could potentially get a summary for the fragment. + + content = None + + if "text/html" in preferred: + parser_cls = getParserClass(request, format) + parser = parser_cls(body, request) + + if format == "html": + content = body + elif hasattr(parser, "formatForOutputType"): + s = StringIO() + parser.formatForOutputType("text/html", write=s.write) + content = s.getvalue() + else: + fmt = request.html_formatter + fmt.setPage(page) + content = formatText(body, request, fmt, parser_cls) + + updates.append((fragment, summary, preferred, content, updated)) + + return updates + # vim: tabstop=4 expandtab shiftwidth=4 diff -r f18627d8a5de -r ef2a98e8bc9a actions/SharedUpdates.py --- a/actions/SharedUpdates.py Tue May 07 23:44:34 2013 +0200 +++ b/actions/SharedUpdates.py Sat May 11 00:35:03 2013 +0200 @@ -9,14 +9,9 @@ from MoinMoin.action import ActionBase from MoinMoin.Page import Page from MoinMoin import wikiutil -from MoinShare import getPreferredOutputTypes, getOutputTypes, getUpdatedTime -from MoinSupport import escattr, formatText, get_form, getFragments, \ - getMetadata, getParserClass, getPathInfo, writeHeaders, ActionSupport - -try: - from cStringIO import StringIO -except ImportError: - from StringIO import StringIO +from MoinShare import getUpdatedTime, getUpdateFragmentsFromPage +from MoinSupport import escattr, get_form, getMetadata, getPathInfo, \ + writeHeaders, ActionSupport escape = wikiutil.escape @@ -122,24 +117,13 @@ %(updated)s\r ''' % d) - # Get the fragment regions for the page. - - for n, (format, attributes, body) in enumerate(getFragments(page.get_raw_body())): - - # Produce a fragment identifier. - # NOTE: Choose a more robust identifier where none is explicitly given. - - fragment = attributes.get("fragment", str(n)) - summary = attributes.get("summary", "Update #%d" % n) + for fragment, summary, preferred, content, updated in \ + getUpdateFragmentsFromPage(page, request): # Get the URL that yields only the fragment. fragment_link = "%s?action=SharedUpdate&fragment=%s" % (link, fragment) - # Get the preferred content types available for the fragment. - - preferred = getPreferredOutputTypes(request, getOutputTypes(request, format)) - download_links = [] for mimetype in preferred: @@ -148,29 +132,6 @@ download_links.append('' % ( escattr(mimetype), escattr(specific_link))) - # Try and obtain some suitable content for the entry. - # NOTE: Could potentially get a summary for the fragment. - - content = None - - if "text/html" in preferred: - parser_cls = getParserClass(request, format) - parser = parser_cls(body, request) - - if format == "html": - content = body - elif hasattr(parser, "formatForOutputType"): - s = StringIO() - parser.formatForOutputType("text/html", write=s.write) - content = s.getvalue() - else: - fmt = request.html_formatter - fmt.setPage(page) - content = formatText(body, request, fmt, parser_cls) - - # NOTE: The published and updated details would need to be deduced from - # NOTE: the page history instead of being taken for the page as a whole. - d = { "title" : escape(summary), "fragment_link" : escape(fragment_link),