1.1 --- a/macros/IncludeComments.py Sun Nov 03 01:03:35 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,157 +0,0 @@
1.4 -# -*- coding: iso-8859-1 -*-
1.5 -"""
1.6 - MoinMoin - IncludeComments Macro
1.7 -
1.8 - @copyright: 2013 by Paul Boddie <paul@boddie.org.uk>
1.9 - @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.10 -
1.11 - Code from the Include macro:
1.12 -
1.13 - @copyright: 2000-2004 Juergen Hermann <jh@web.de>,
1.14 - 2000-2001 Richard Jones <richard@bizarsoftware.com.au>
1.15 - @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.16 -"""
1.17 -
1.18 -from MoinMoin.Page import Page
1.19 -from MoinMoin.macro import Include
1.20 -from MoinMoin.user import User
1.21 -from MoinMoin.wikiutil import escape
1.22 -import re
1.23 -import codecs
1.24 -
1.25 -try:
1.26 - from cStringIO import StringIO
1.27 -except ImportError:
1.28 - from StringIO import StringIO
1.29 -
1.30 -Dependencies = ['pages']
1.31 -
1.32 -# Macro functions.
1.33 -
1.34 -def execute(macro, text):
1.35 - request = macro.request
1.36 - fmt = request.formatter
1.37 - page = request.page
1.38 - pagename = page.page_name
1.39 - _ = request.getText
1.40 -
1.41 - output = []
1.42 - append = output.append
1.43 -
1.44 - # Add a heading.
1.45 -
1.46 - append(fmt.heading(on=1, depth=1))
1.47 - append(fmt.text(_("Comments")))
1.48 - append(fmt.heading(on=0, depth=1))
1.49 -
1.50 - # Provide a form for adding new comments.
1.51 -
1.52 - if request.user.valid and request.user.may.write(pagename):
1.53 -
1.54 - d = {
1.55 - "show_form" : escape(_("Add a comment to this page.")),
1.56 - "comment_label" : escape(_("Write a comment in the box.")),
1.57 - "comment_default" : "",
1.58 - "submit" : escape(_("Submit this comment")),
1.59 - }
1.60 -
1.61 - append("""\
1.62 -<div id="includecomments-anchor">
1.63 -<a href="#includecomments-anchor">%(show_form)s</a>
1.64 -<form action="?action=PostComment" method="post" class="includecomments-form">
1.65 -<p>%(comment_label)s</p>
1.66 -<textarea name="comment" cols="60" rows="10">%(comment_default)s</textarea>
1.67 -<p><input name="submit" type="submit" value="%(submit)s" /></p>
1.68 -</form>
1.69 -</div>
1.70 -""" % d)
1.71 -
1.72 - # NOTE: Much of the code below originates from the Include macro, but it
1.73 - # NOTE: excludes various options of that macro and adds comment-related
1.74 - # NOTE: output.
1.75 -
1.76 - # Add included comments.
1.77 -
1.78 - filterfn = re.compile(ur"^%s/" % re.escape(pagename), re.U).match
1.79 - pages = request.rootpage.getPageList(filter=filterfn)
1.80 - pages.sort()
1.81 -
1.82 - ownerfn = re.compile("^#pragma comment-owner (.*?)$", re.MULTILINE | re.UNICODE).search
1.83 -
1.84 - # Track included pages.
1.85 -
1.86 - if not hasattr(page, '_macroInclude_pagelist'):
1.87 - page._macroInclude_pagelist = {}
1.88 -
1.89 - # Visit each comment page.
1.90 -
1.91 - for inc_name in pages:
1.92 -
1.93 - # Skip unreadable or already included pages.
1.94 -
1.95 - if not request.user.may.read(inc_name):
1.96 - continue
1.97 - if inc_name in page._macroInclude_pagelist:
1.98 - continue
1.99 -
1.100 - # Obtain a separate formatter for the included page.
1.101 -
1.102 - inc_fmt = macro.formatter.__class__(request, is_included=True)
1.103 - inc_fmt._base_depth = macro.formatter._base_depth
1.104 -
1.105 - # Obtain the included page.
1.106 -
1.107 - inc_page = Page(request, inc_name, formatter=inc_fmt)
1.108 - if not inc_page.exists():
1.109 - continue
1.110 - inc_page._macroInclude_pagelist = page._macroInclude_pagelist
1.111 -
1.112 - if not hasattr(request, "_Include_backto"):
1.113 - request._Include_backto = pagename
1.114 -
1.115 - # Output a container for the included page.
1.116 -
1.117 - append(fmt.div(1, id=inc_name, css_class="included-comment"))
1.118 -
1.119 - # Add a label indicating a comment.
1.120 -
1.121 - match = ownerfn(inc_page.get_raw_body())
1.122 - if match:
1.123 - user = User(request, auth_username=match.group(1))
1.124 - if user.exists():
1.125 - append(fmt.div(1, css_class="included-comment-owner"))
1.126 - append(fmt.text(user.aliasname or user.name))
1.127 - append(fmt.div(0))
1.128 -
1.129 - # Set or increment include marker.
1.130 -
1.131 - page._macroInclude_pagelist[inc_name] = \
1.132 - page._macroInclude_pagelist.get(inc_name, 0) + 1
1.133 -
1.134 - # Output the included page.
1.135 -
1.136 - strfile = codecs.getwriter("utf-8")(StringIO())
1.137 - request.redirect(strfile)
1.138 - try:
1.139 - inc_page.send_page(content_only=True,
1.140 - omit_footnotes=True,
1.141 - count_hit=False)
1.142 - append(unicode(strfile.getvalue(), "utf-8"))
1.143 - finally:
1.144 - request.redirect()
1.145 -
1.146 - # Decrement or remove include marker.
1.147 -
1.148 - if page._macroInclude_pagelist[inc_name] > 1:
1.149 - page._macroInclude_pagelist[inc_name] = \
1.150 - page._macroInclude_pagelist[inc_name] - 1
1.151 - else:
1.152 - del page._macroInclude_pagelist[inc_name]
1.153 -
1.154 - # Close the container for the included page.
1.155 -
1.156 - append(fmt.div(0))
1.157 -
1.158 - return u"".join(output)
1.159 -
1.160 -# vim: tabstop=4 expandtab shiftwidth=4