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 # Fragments employ a "moinshare" attribute. 31 32 fragment_prelude = "#!" 33 fragment_attribute = "moinshare" 34 35 def getRegions(s): 36 37 "Parse the string 's', returning a list of shared regions." 38 39 regions = [] 40 marker = None 41 is_region = True 42 43 for match_text in marker_regexp.split(s): 44 45 # Capture section text. 46 47 if is_region and marker: 48 regions[-1] += match_text 49 50 # Handle section markers. 51 52 elif not is_region: 53 54 # Close any open sections, returning to exposed text regions. 55 56 if marker: 57 if match_text.startswith("}") and len(marker) == len(match_text): 58 marker = None 59 60 # Without a current marker, start a section if an appropriate marker 61 # is given. 62 63 elif match_text.startswith("{"): 64 marker = match_text 65 regions.append("") 66 67 # Markers and section text are added to the current region. 68 69 regions[-1] += match_text 70 71 # Exposed text is ignored. 72 73 # The match text alternates between text between markers and the markers 74 # themselves. 75 76 is_region = not is_region 77 78 return regions 79 80 def getFragmentsFromRegions(regions): 81 82 """ 83 Return fragments from the given 'regions', each having the form 84 (format, arguments, body text). 85 """ 86 87 fragments = [] 88 89 for region in regions: 90 body = region.lstrip("{").rstrip("}").lstrip() 91 if body.startswith(fragment_prelude): 92 arguments, body = body[len(fragment_prelude):].split("\n", 1) 93 94 # Get any parser/format declaration. 95 96 if arguments and not arguments[0].isspace(): 97 format, arguments = arguments.split(None, 1) 98 else: 99 format = None 100 101 # Get the attributes/arguments for the region. 102 103 attributes = parseAttributes(arguments, False) 104 105 # If the format is the MoinShare attribute, move it into the 106 # dictionary. 107 108 if format.lower() == fragment_attribute: 109 attributes[fragment_attribute] = True 110 format = None 111 112 # Only collect appropriate regions. 113 114 if attributes.has_key(fragment_attribute): 115 fragments.append((format, attributes, body)) 116 117 return fragments 118 119 def getFragments(s): 120 121 """ 122 Return fragments for the given string 's', each having the form 123 (arguments, body text). 124 """ 125 126 return getFragmentsFromRegions(getRegions(s)) 127 128 # vim: tabstop=4 expandtab shiftwidth=4