paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - MoinShare library |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 2011, 2012 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 | |
paul@0 | 9 | from MoinSupport import * |
paul@0 | 10 | from MoinMoin import wikiutil |
paul@0 | 11 | import re |
paul@0 | 12 | |
paul@0 | 13 | escape = wikiutil.escape |
paul@0 | 14 | |
paul@0 | 15 | __version__ = "0.1" |
paul@0 | 16 | |
paul@0 | 17 | # More Moin 1.9 compatibility functions. |
paul@0 | 18 | |
paul@0 | 19 | def has_member(request, groupname, username): |
paul@0 | 20 | if hasattr(request.dicts, "has_member"): |
paul@0 | 21 | return request.dicts.has_member(groupname, username) |
paul@0 | 22 | else: |
paul@0 | 23 | return username in request.dicts.get(groupname, []) |
paul@0 | 24 | |
paul@0 | 25 | # Extraction of shared fragments. |
paul@0 | 26 | |
paul@0 | 27 | marker_regexp_str = r"([{]{3,}|[}]{3,})" |
paul@0 | 28 | marker_regexp = re.compile(marker_regexp_str, re.MULTILINE | re.DOTALL) # {{{... or }}}... |
paul@0 | 29 | |
paul@0 | 30 | def getRegions(s): |
paul@0 | 31 | |
paul@0 | 32 | "Parse the string 's', returning a list of shared regions." |
paul@0 | 33 | |
paul@0 | 34 | regions = [] |
paul@0 | 35 | marker = None |
paul@0 | 36 | is_region = True |
paul@0 | 37 | |
paul@0 | 38 | for match_text in marker_regexp.split(s): |
paul@0 | 39 | |
paul@0 | 40 | # Capture section text. |
paul@0 | 41 | |
paul@0 | 42 | if is_region and marker: |
paul@0 | 43 | regions[-1] += match_text |
paul@0 | 44 | |
paul@0 | 45 | # Handle section markers. |
paul@0 | 46 | |
paul@0 | 47 | elif not is_region: |
paul@0 | 48 | |
paul@0 | 49 | # Close any open sections, returning to exposed text regions. |
paul@0 | 50 | |
paul@0 | 51 | if marker: |
paul@0 | 52 | if match_text.startswith("}") and len(marker) == len(match_text): |
paul@0 | 53 | marker = None |
paul@0 | 54 | |
paul@0 | 55 | # Without a current marker, start a section if an appropriate marker |
paul@0 | 56 | # is given. |
paul@0 | 57 | |
paul@0 | 58 | elif match_text.startswith("{"): |
paul@0 | 59 | marker = match_text |
paul@0 | 60 | regions.append("") |
paul@0 | 61 | |
paul@0 | 62 | # Markers and section text are added to the current region. |
paul@0 | 63 | |
paul@0 | 64 | regions[-1] += match_text |
paul@0 | 65 | |
paul@0 | 66 | # Exposed text is ignored. |
paul@0 | 67 | |
paul@0 | 68 | # The match text alternates between text between markers and the markers |
paul@0 | 69 | # themselves. |
paul@0 | 70 | |
paul@0 | 71 | is_region = not is_region |
paul@0 | 72 | |
paul@0 | 73 | return regions |
paul@0 | 74 | |
paul@0 | 75 | # vim: tabstop=4 expandtab shiftwidth=4 |