paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - IncludeComments Macro |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 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 | Code from the Include macro: |
paul@0 | 9 | |
paul@0 | 10 | @copyright: 2000-2004 Juergen Hermann <jh@web.de>, |
paul@0 | 11 | 2000-2001 Richard Jones <richard@bizarsoftware.com.au> |
paul@0 | 12 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 13 | """ |
paul@0 | 14 | |
paul@0 | 15 | from MoinMoin.Page import Page |
paul@0 | 16 | from MoinMoin.macro import Include |
paul@0 | 17 | from MoinMoin.user import User |
paul@0 | 18 | from MoinMoin.wikiutil import escape |
paul@5 | 19 | from SubpageComments import get_comment_numbers |
paul@0 | 20 | import re |
paul@0 | 21 | import codecs |
paul@0 | 22 | |
paul@0 | 23 | try: |
paul@0 | 24 | from cStringIO import StringIO |
paul@0 | 25 | except ImportError: |
paul@0 | 26 | from StringIO import StringIO |
paul@0 | 27 | |
paul@3 | 28 | __version__ = "0.1" |
paul@3 | 29 | |
paul@0 | 30 | Dependencies = ['pages'] |
paul@0 | 31 | |
paul@0 | 32 | # Macro functions. |
paul@0 | 33 | |
paul@0 | 34 | def execute(macro, text): |
paul@0 | 35 | request = macro.request |
paul@0 | 36 | fmt = request.formatter |
paul@0 | 37 | page = request.page |
paul@0 | 38 | pagename = page.page_name |
paul@0 | 39 | _ = request.getText |
paul@0 | 40 | |
paul@0 | 41 | output = [] |
paul@0 | 42 | append = output.append |
paul@0 | 43 | |
paul@0 | 44 | # Add a heading. |
paul@0 | 45 | |
paul@0 | 46 | append(fmt.heading(on=1, depth=1)) |
paul@0 | 47 | append(fmt.text(_("Comments"))) |
paul@0 | 48 | append(fmt.heading(on=0, depth=1)) |
paul@0 | 49 | |
paul@0 | 50 | # Provide a form for adding new comments. |
paul@0 | 51 | |
paul@0 | 52 | if request.user.valid and request.user.may.write(pagename): |
paul@0 | 53 | |
paul@0 | 54 | d = { |
paul@0 | 55 | "show_form" : escape(_("Add a comment to this page.")), |
paul@0 | 56 | "comment_label" : escape(_("Write a comment in the box.")), |
paul@0 | 57 | "comment_default" : "", |
paul@0 | 58 | "submit" : escape(_("Submit this comment")), |
paul@0 | 59 | } |
paul@0 | 60 | |
paul@0 | 61 | append("""\ |
paul@0 | 62 | <div id="includecomments-anchor"> |
paul@0 | 63 | <a href="#includecomments-anchor">%(show_form)s</a> |
paul@0 | 64 | <form action="?action=PostComment" method="post" class="includecomments-form"> |
paul@0 | 65 | <p>%(comment_label)s</p> |
paul@0 | 66 | <textarea name="comment" cols="60" rows="10">%(comment_default)s</textarea> |
paul@0 | 67 | <p><input name="submit" type="submit" value="%(submit)s" /></p> |
paul@0 | 68 | </form> |
paul@0 | 69 | </div> |
paul@0 | 70 | """ % d) |
paul@0 | 71 | |
paul@5 | 72 | # Add included comments. |
paul@5 | 73 | |
paul@5 | 74 | pages = get_comment_numbers(pagename, request) |
paul@5 | 75 | |
paul@5 | 76 | ownerfn = re.compile("^#pragma comment-owner (.*?)$", re.MULTILINE | re.UNICODE).search |
paul@5 | 77 | |
paul@0 | 78 | # NOTE: Much of the code below originates from the Include macro, but it |
paul@0 | 79 | # NOTE: excludes various options of that macro and adds comment-related |
paul@0 | 80 | # NOTE: output. |
paul@0 | 81 | |
paul@0 | 82 | # Track included pages. |
paul@0 | 83 | |
paul@0 | 84 | if not hasattr(page, '_macroInclude_pagelist'): |
paul@0 | 85 | page._macroInclude_pagelist = {} |
paul@0 | 86 | |
paul@0 | 87 | # Visit each comment page. |
paul@0 | 88 | |
paul@0 | 89 | for inc_name in pages: |
paul@5 | 90 | inc_name = "%s/%04d" % (pagename, inc_name) |
paul@0 | 91 | |
paul@0 | 92 | # Skip unreadable or already included pages. |
paul@0 | 93 | |
paul@0 | 94 | if not request.user.may.read(inc_name): |
paul@0 | 95 | continue |
paul@0 | 96 | if inc_name in page._macroInclude_pagelist: |
paul@0 | 97 | continue |
paul@0 | 98 | |
paul@0 | 99 | # Obtain a separate formatter for the included page. |
paul@0 | 100 | |
paul@0 | 101 | inc_fmt = macro.formatter.__class__(request, is_included=True) |
paul@0 | 102 | inc_fmt._base_depth = macro.formatter._base_depth |
paul@0 | 103 | |
paul@0 | 104 | # Obtain the included page. |
paul@0 | 105 | |
paul@0 | 106 | inc_page = Page(request, inc_name, formatter=inc_fmt) |
paul@0 | 107 | if not inc_page.exists(): |
paul@0 | 108 | continue |
paul@0 | 109 | inc_page._macroInclude_pagelist = page._macroInclude_pagelist |
paul@0 | 110 | |
paul@0 | 111 | if not hasattr(request, "_Include_backto"): |
paul@0 | 112 | request._Include_backto = pagename |
paul@0 | 113 | |
paul@0 | 114 | # Output a container for the included page. |
paul@0 | 115 | |
paul@0 | 116 | append(fmt.div(1, id=inc_name, css_class="included-comment")) |
paul@0 | 117 | |
paul@0 | 118 | # Add a label indicating a comment. |
paul@0 | 119 | |
paul@0 | 120 | match = ownerfn(inc_page.get_raw_body()) |
paul@0 | 121 | if match: |
paul@0 | 122 | user = User(request, auth_username=match.group(1)) |
paul@0 | 123 | if user.exists(): |
paul@0 | 124 | append(fmt.div(1, css_class="included-comment-owner")) |
paul@0 | 125 | append(fmt.text(user.aliasname or user.name)) |
paul@0 | 126 | append(fmt.div(0)) |
paul@0 | 127 | |
paul@0 | 128 | # Set or increment include marker. |
paul@0 | 129 | |
paul@0 | 130 | page._macroInclude_pagelist[inc_name] = \ |
paul@0 | 131 | page._macroInclude_pagelist.get(inc_name, 0) + 1 |
paul@0 | 132 | |
paul@0 | 133 | # Output the included page. |
paul@0 | 134 | |
paul@0 | 135 | strfile = codecs.getwriter("utf-8")(StringIO()) |
paul@0 | 136 | request.redirect(strfile) |
paul@0 | 137 | try: |
paul@0 | 138 | inc_page.send_page(content_only=True, |
paul@0 | 139 | omit_footnotes=True, |
paul@0 | 140 | count_hit=False) |
paul@0 | 141 | append(unicode(strfile.getvalue(), "utf-8")) |
paul@0 | 142 | finally: |
paul@0 | 143 | request.redirect() |
paul@0 | 144 | |
paul@0 | 145 | # Decrement or remove include marker. |
paul@0 | 146 | |
paul@0 | 147 | if page._macroInclude_pagelist[inc_name] > 1: |
paul@0 | 148 | page._macroInclude_pagelist[inc_name] = \ |
paul@0 | 149 | page._macroInclude_pagelist[inc_name] - 1 |
paul@0 | 150 | else: |
paul@0 | 151 | del page._macroInclude_pagelist[inc_name] |
paul@0 | 152 | |
paul@0 | 153 | # Close the container for the included page. |
paul@0 | 154 | |
paul@0 | 155 | append(fmt.div(0)) |
paul@0 | 156 | |
paul@0 | 157 | return u"".join(output) |
paul@0 | 158 | |
paul@0 | 159 | # vim: tabstop=4 expandtab shiftwidth=4 |