1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - MoinShare library 4 5 @copyright: 2011, 2012 by Paul Boddie <paul@boddie.org.uk> 6 @license: GNU GPL (v2 or later), see COPYING.txt for details. 7 """ 8 9 from MoinSupport import * 10 from MoinMoin import wikiutil 11 import re 12 13 escape = wikiutil.escape 14 15 __version__ = "0.1" 16 17 # More Moin 1.9 compatibility functions. 18 19 def has_member(request, groupname, username): 20 if hasattr(request.dicts, "has_member"): 21 return request.dicts.has_member(groupname, username) 22 else: 23 return username in request.dicts.get(groupname, []) 24 25 # Extraction of shared fragments. 26 27 marker_regexp_str = r"([{]{3,}|[}]{3,})" 28 marker_regexp = re.compile(marker_regexp_str, re.MULTILINE | re.DOTALL) # {{{... or }}}... 29 30 fragment_prelude = "#!MoinShare " 31 32 def getRegions(s): 33 34 "Parse the string 's', returning a list of shared regions." 35 36 regions = [] 37 marker = None 38 is_region = True 39 40 for match_text in marker_regexp.split(s): 41 42 # Capture section text. 43 44 if is_region and marker: 45 regions[-1] += match_text 46 47 # Handle section markers. 48 49 elif not is_region: 50 51 # Close any open sections, returning to exposed text regions. 52 53 if marker: 54 if match_text.startswith("}") and len(marker) == len(match_text): 55 marker = None 56 57 # Without a current marker, start a section if an appropriate marker 58 # is given. 59 60 elif match_text.startswith("{"): 61 marker = match_text 62 regions.append("") 63 64 # Markers and section text are added to the current region. 65 66 regions[-1] += match_text 67 68 # Exposed text is ignored. 69 70 # The match text alternates between text between markers and the markers 71 # themselves. 72 73 is_region = not is_region 74 75 return regions 76 77 def getFragmentsFromRegions(regions): 78 79 """ 80 Return fragments from the given 'regions', each having the form 81 (arguments, body text). 82 """ 83 84 fragments = [] 85 86 for region in regions: 87 body = region.lstrip("{").rstrip("}") 88 if body.startswith(fragment_prelude): 89 arguments, body = body[len(fragment_prelude):].split("\n", 1) 90 fragments.append((parseAttributes(arguments, False), body)) 91 92 return fragments 93 94 def getFragments(s): 95 96 """ 97 Return fragments for the given string 's', each having the form 98 (arguments, body text). 99 """ 100 101 return getFragmentsFromRegions(getRegions(s)) 102 103 # vim: tabstop=4 expandtab shiftwidth=4