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