1.1 --- a/MoinShare.py Sun Jul 29 00:07:45 2012 +0200
1.2 +++ b/MoinShare.py Sat Sep 29 17:53:04 2012 +0200
1.3 @@ -12,6 +12,7 @@
1.4 import re
1.5
1.6 escape = wikiutil.escape
1.7 +_getFragments = getFragments
1.8
1.9 __version__ = "0.1"
1.10
1.11 @@ -23,109 +24,17 @@
1.12 else:
1.13 return username in request.dicts.get(groupname, [])
1.14
1.15 -# Extraction of shared fragments.
1.16 -
1.17 -marker_regexp_str = r"([{]{3,}|[}]{3,})"
1.18 -marker_regexp = re.compile(marker_regexp_str, re.MULTILINE | re.DOTALL) # {{{... or }}}...
1.19 -
1.20 # Fragments employ a "moinshare" attribute.
1.21
1.22 -fragment_prelude = "#!"
1.23 fragment_attribute = "moinshare"
1.24
1.25 -def getRegions(s):
1.26 -
1.27 - "Parse the string 's', returning a list of shared regions."
1.28 -
1.29 - regions = []
1.30 - marker = None
1.31 - is_region = True
1.32 -
1.33 - for match_text in marker_regexp.split(s):
1.34 -
1.35 - # Capture section text.
1.36 -
1.37 - if is_region and marker:
1.38 - regions[-1] += match_text
1.39 -
1.40 - # Handle section markers.
1.41 -
1.42 - elif not is_region:
1.43 -
1.44 - # Close any open sections, returning to exposed text regions.
1.45 -
1.46 - if marker:
1.47 - if match_text.startswith("}") and len(marker) == len(match_text):
1.48 - marker = None
1.49 -
1.50 - # Without a current marker, start a section if an appropriate marker
1.51 - # is given.
1.52 -
1.53 - elif match_text.startswith("{"):
1.54 - marker = match_text
1.55 - regions.append("")
1.56 -
1.57 - # Markers and section text are added to the current region.
1.58 -
1.59 - regions[-1] += match_text
1.60 -
1.61 - # Exposed text is ignored.
1.62 -
1.63 - # The match text alternates between text between markers and the markers
1.64 - # themselves.
1.65 -
1.66 - is_region = not is_region
1.67 -
1.68 - return regions
1.69 -
1.70 -def getFragmentsFromRegions(regions):
1.71 -
1.72 - """
1.73 - Return fragments from the given 'regions', each having the form
1.74 - (format, arguments, body text).
1.75 - """
1.76 -
1.77 +def getFragments(s):
1.78 fragments = []
1.79 -
1.80 - for region in regions:
1.81 - body = region.lstrip("{").rstrip("}").lstrip()
1.82 - if body.startswith(fragment_prelude):
1.83 - arguments, body = body[len(fragment_prelude):].split("\n", 1)
1.84 -
1.85 - # Get any parser/format declaration.
1.86 -
1.87 - if arguments and not arguments[0].isspace():
1.88 - format, arguments = arguments.split(None, 1)
1.89 - else:
1.90 - format = None
1.91 -
1.92 - # Get the attributes/arguments for the region.
1.93 -
1.94 - attributes = parseAttributes(arguments, False)
1.95 -
1.96 - # If the format is the MoinShare attribute, move it into the
1.97 - # dictionary.
1.98 -
1.99 - if format.lower() == fragment_attribute:
1.100 - attributes[fragment_attribute] = True
1.101 - format = None
1.102 -
1.103 - # Only collect appropriate regions.
1.104 -
1.105 - if attributes.has_key(fragment_attribute):
1.106 - fragments.append((format, attributes, body))
1.107 -
1.108 + for format, attributes, body in _getFragments(s):
1.109 + if attributes.has_key(fragment_attribute):
1.110 + fragments.append((format, attributes, body))
1.111 return fragments
1.112
1.113 -def getFragments(s):
1.114 -
1.115 - """
1.116 - Return fragments for the given string 's', each having the form
1.117 - (arguments, body text).
1.118 - """
1.119 -
1.120 - return getFragmentsFromRegions(getRegions(s))
1.121 -
1.122 def getOutputTypes(request, format):
1.123
1.124 """