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