1.1 --- a/macros/SharedContent.py Fri Jun 14 18:01:52 2013 +0200
1.2 +++ b/macros/SharedContent.py Sat Jun 15 00:45:10 2013 +0200
1.3 @@ -11,7 +11,8 @@
1.4 from MoinRemoteSupport import *
1.5 from MoinSupport import parseMacroArguments, getParsersForContentType, formatText
1.6 from MoinShare import getUpdateSources, getUpdatesFromPage, \
1.7 - getUpdatesFromStore, Update, get_make_parser
1.8 + getUpdatesFromStore, getUpdateFromPart, \
1.9 + Update, get_make_parser
1.10 from email.utils import parsedate
1.11 import xml.dom.pulldom
1.12
1.13 @@ -206,6 +207,119 @@
1.14
1.15 return (feed_type, channel_title, channel_link), feed_updates
1.16
1.17 +# Update formatting.
1.18 +
1.19 +def getUpdatesForFormatting(update):
1.20 +
1.21 + "Get a list of updates for formatting given 'update'."
1.22 +
1.23 + updates = []
1.24 +
1.25 + # Handle multipart/alternative.
1.26 +
1.27 + if update.parts:
1.28 + for n, part in enumerate(update.parts):
1.29 + update_part = getUpdateFromPart(update, part, n)
1.30 + updates += getUpdatesForFormatting(update_part)
1.31 + else:
1.32 + updates.append(update)
1.33 +
1.34 + return updates
1.35 +
1.36 +def getFormattedUpdate(update, request, fmt):
1.37 +
1.38 + """
1.39 + Return the formatted form of the given 'update' using the given 'request'
1.40 + and 'fmt'.
1.41 + """
1.42 +
1.43 + # NOTE: Some control over the HTML and XHTML should be exercised.
1.44 +
1.45 + if update.content:
1.46 + if update.content_type == "text/html" and update.message_number is not None:
1.47 + parsers = [get_make_parser(update.page, update.message_number)]
1.48 + else:
1.49 + parsers = getParsersForContentType(request.cfg, update.content_type)
1.50 +
1.51 + if parsers:
1.52 + for parser_cls in parsers:
1.53 + return formatText(update.content, request, fmt, parser_cls=parser_cls)
1.54 + break
1.55 + else:
1.56 + return None
1.57 + else:
1.58 + return None
1.59 +
1.60 +def formatUpdate(update, request, fmt):
1.61 +
1.62 + "Format the given 'update' using the given 'request' and 'fmt'."
1.63 +
1.64 + result = []
1.65 + append = result.append
1.66 +
1.67 + updates = getUpdatesForFormatting(update)
1.68 + single = len(updates) == 1
1.69 +
1.70 + # Format some navigation tabs.
1.71 +
1.72 + if not single:
1.73 + append(fmt.div(on=1, css_class="moinshare-alternatives"))
1.74 +
1.75 + first = True
1.76 +
1.77 + for update_part in updates:
1.78 + append(fmt.url(1, "#%s" % update_part.unique_id()))
1.79 + append(fmt.text(update_part.content_type))
1.80 + append(fmt.url(0))
1.81 +
1.82 + first = False
1.83 +
1.84 + append(fmt.div(on=0))
1.85 +
1.86 + # Format the content.
1.87 +
1.88 + first = True
1.89 +
1.90 + for update_part in updates:
1.91 +
1.92 + # Encapsulate each alternative if many exist.
1.93 +
1.94 + if not single:
1.95 + css_class = first and "moinshare-default" or "moinshare-other"
1.96 + append(fmt.div(on=1, css_class="moinshare-alternative %s" % css_class, id=update_part.unique_id()))
1.97 +
1.98 + # Include the content.
1.99 +
1.100 + append(formatUpdatePart(update_part, request, fmt))
1.101 +
1.102 + if not single:
1.103 + append(fmt.div(on=0))
1.104 +
1.105 + first = False
1.106 +
1.107 + return "".join(result)
1.108 +
1.109 +def formatUpdatePart(update, request, fmt):
1.110 +
1.111 + "Format the given 'update' using the given 'request' and 'fmt'."
1.112 +
1.113 + _ = request.getText
1.114 +
1.115 + result = []
1.116 + append = result.append
1.117 +
1.118 + # Encapsulate the content.
1.119 +
1.120 + append(fmt.div(on=1, css_class="moinshare-content"))
1.121 + text = getFormattedUpdate(update, request, fmt)
1.122 + if text:
1.123 + append(text)
1.124 + else:
1.125 + append(fmt.text(_("Update cannot be shown for content of type %s.") % update.content_type))
1.126 + append(fmt.div(on=0))
1.127 +
1.128 + return "".join(result)
1.129 +
1.130 # The macro itself.
1.131
1.132 def execute(macro, args):
1.133 @@ -326,31 +440,16 @@
1.134 for update in updates:
1.135
1.136 # Emit content where appropriate.
1.137 - # NOTE: Some control over the HTML and XHTML should be exercised.
1.138
1.139 if show_content:
1.140 append(fmt.div(on=1, css_class="moinshare-update"))
1.141 - append(fmt.div(on=1, css_class="moinshare-content"))
1.142 -
1.143 - # NOTE: Handle multipart/alternative.
1.144
1.145 - if update.content:
1.146 - if update.content_type == "text/html" and update.message_number is not None:
1.147 - parsers = [get_make_parser(update.page, update.message_number)]
1.148 - else:
1.149 - parsers = getParsersForContentType(request.cfg, update.content_type)
1.150 + append(formatUpdate(update, request, fmt))
1.151
1.152 - if parsers:
1.153 - for parser_cls in parsers:
1.154 - append(formatText(update.content, request, fmt, parser_cls=parser_cls))
1.155 - break
1.156 - else:
1.157 - append(fmt.text(_("Update cannot be shown for content of type %s.") % update.content_type))
1.158 -
1.159 - append(fmt.div(on=0))
1.160 append(fmt.div(on=1, css_class="moinshare-date"))
1.161 append(fmt.text(str(update.updated)))
1.162 append(fmt.div(on=0))
1.163 +
1.164 append(fmt.div(on=0))
1.165
1.166 # Or emit title and link information for items.