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