# HG changeset patch # User Paul Boddie # Date 1354146536 -3600 # Node ID 2bd2ac851c905e4bf5beb138ab39974d89d49258 # Parent a8fd39bc4d997e09cae5e9b527b5b12a417a38c2 Added a separate function for getting the properties of individual text regions. Added an items method to the form dictionary wrapper class. diff -r a8fd39bc4d99 -r 2bd2ac851c90 MoinSupport.py --- a/MoinSupport.py Mon Nov 26 23:08:38 2012 +0100 +++ b/MoinSupport.py Thu Nov 29 00:48:56 2012 +0100 @@ -258,57 +258,73 @@ """ Return fragments from the given 'regions', each having the form - (format, arguments, body text). + (format, attributes, body text). """ fragments = [] for region in regions: - if region.startswith("{{{"): + format, attributes, body, header, close = getFragmentFromRegion(region) + fragments.append((format, attributes, body)) - body = region.lstrip("{").rstrip("}").lstrip() + return fragments - # Remove any prelude and process metadata. +def getFragmentFromRegion(region): - if body.startswith("#!"): - body = body[2:] + """ + Return a fragment for the given 'region' having the form (format, + attributes, body text, header, close), where the 'header' is the original + declaration of the 'region' or None if no explicit region is defined, and + 'close' is the closing marker of the 'region' or None if no explicit region + is defined. + """ - try: - arguments, body = body.split("\n", 1) - except ValueError: - arguments = body - body = "" + if region.startswith("{{{"): - # Get any parser/format declaration. + body = region.lstrip("{") + level = len(region) - len(body) + body = body.rstrip("}").lstrip() + + # Remove any prelude and process metadata. + + if body.startswith("#!"): - if arguments and not arguments[0].isspace(): - details = arguments.split(None, 1) - if len(details) == 2: - format, arguments = details - else: - format = details[0] - arguments = "" - else: - format = None + try: + declaration, body = body.split("\n", 1) + except ValueError: + declaration = body + body = "" - # Get the attributes/arguments for the region. + arguments = declaration[2:] + + # Get any parser/format declaration. - attributes = parseAttributes(arguments, False) - - # Add an entry for the format in the attribute dictionary. + if arguments and not arguments[0].isspace(): + details = arguments.split(None, 1) + if len(details) == 2: + format, arguments = details + else: + format = details[0] + arguments = "" + else: + format = None - if format and not attributes.has_key(format): - attributes[format] = True + # Get the attributes/arguments for the region. + + attributes = parseAttributes(arguments, False) - fragments.append((format, attributes, body)) + # Add an entry for the format in the attribute dictionary. - else: - fragments.append((None, {}, body)) + if format and not attributes.has_key(format): + attributes[format] = True + + return format, attributes, body, level * "{" + declaration + "\n", level * "}" else: - fragments.append((None, {}, region)) + return None, {}, body, level * "{" + "\n", level * "}" - return fragments + else: + return None, {}, region, None, None def getFragments(s, include_non_regions=False): @@ -432,6 +448,9 @@ def __getitem__(self, name): return self.form.getlist(name) + def items(self): + return self.form.items(True) + class ActionSupport: """