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