# HG changeset patch # User Paul Boddie # Date 1348933984 -7200 # Node ID f6853712771a123f889adcf6ca9b316839833c44 # Parent 5989ec91c5a1dc741b09754a1af7f55b0bea86e3 Moved fragment parsing to MoinSupport. Added usage of the MoinSupport.parseAttributes function. diff -r 5989ec91c5a1 -r f6853712771a MoinShare.py --- a/MoinShare.py Sun Jul 29 00:07:45 2012 +0200 +++ b/MoinShare.py Sat Sep 29 17:53:04 2012 +0200 @@ -12,6 +12,7 @@ import re escape = wikiutil.escape +_getFragments = getFragments __version__ = "0.1" @@ -23,109 +24,17 @@ else: return username in request.dicts.get(groupname, []) -# Extraction of shared fragments. - -marker_regexp_str = r"([{]{3,}|[}]{3,})" -marker_regexp = re.compile(marker_regexp_str, re.MULTILINE | re.DOTALL) # {{{... or }}}... - # Fragments employ a "moinshare" attribute. -fragment_prelude = "#!" fragment_attribute = "moinshare" -def getRegions(s): - - "Parse the string 's', returning a list of shared regions." - - regions = [] - marker = None - is_region = True - - for match_text in marker_regexp.split(s): - - # Capture section text. - - if is_region and marker: - regions[-1] += match_text - - # Handle section markers. - - elif not is_region: - - # Close any open sections, returning to exposed text regions. - - if marker: - if match_text.startswith("}") and len(marker) == len(match_text): - marker = None - - # Without a current marker, start a section if an appropriate marker - # is given. - - elif match_text.startswith("{"): - marker = match_text - regions.append("") - - # Markers and section text are added to the current region. - - regions[-1] += match_text - - # Exposed text is ignored. - - # The match text alternates between text between markers and the markers - # themselves. - - is_region = not is_region - - return regions - -def getFragmentsFromRegions(regions): - - """ - Return fragments from the given 'regions', each having the form - (format, arguments, body text). - """ - +def getFragments(s): fragments = [] - - for region in regions: - body = region.lstrip("{").rstrip("}").lstrip() - if body.startswith(fragment_prelude): - arguments, body = body[len(fragment_prelude):].split("\n", 1) - - # Get any parser/format declaration. - - if arguments and not arguments[0].isspace(): - format, arguments = arguments.split(None, 1) - else: - format = None - - # Get the attributes/arguments for the region. - - attributes = parseAttributes(arguments, False) - - # If the format is the MoinShare attribute, move it into the - # dictionary. - - if format.lower() == fragment_attribute: - attributes[fragment_attribute] = True - format = None - - # Only collect appropriate regions. - - if attributes.has_key(fragment_attribute): - fragments.append((format, attributes, body)) - + for format, attributes, body in _getFragments(s): + if attributes.has_key(fragment_attribute): + fragments.append((format, attributes, body)) return fragments -def getFragments(s): - - """ - Return fragments for the given string 's', each having the form - (arguments, body text). - """ - - return getFragmentsFromRegions(getRegions(s)) - def getOutputTypes(request, format): """ diff -r 5989ec91c5a1 -r f6853712771a parsers/MoinShare.py --- a/parsers/MoinShare.py Sun Jul 29 00:07:45 2012 +0200 +++ b/parsers/MoinShare.py Sat Sep 29 17:53:04 2012 +0200 @@ -6,6 +6,7 @@ @license: GNU GPL (v2 or later), see COPYING.txt for details. """ +from MoinSupport import parseAttributes from MoinMoin import wikiutil try: @@ -38,19 +39,14 @@ self.raw = raw self.request = request - attrs, msg = wikiutil.parseAttributes(request, kw.get("format_args", "")) - - # The attributes returned from the formatting arguments are encoded like - # strings. - - quotes = '"' + "'" + attrs = parseAttributes(request, False) # Get any reference to this fragment, another fragment and the content # format. - self.fragment = (attrs.get("fragment") or '').strip(quotes) - self.ref = (attrs.get("ref") or '').strip(quotes) - self.format_type = (attrs.get("format") or '').strip(quotes) + self.fragment = attrs.get("fragment", '') + self.ref = attrs.get("ref", '') + self.format_type = attrs.get("format", '') def getDigest(self): diff -r 5989ec91c5a1 -r f6853712771a tests/test_regions.py --- a/tests/test_regions.py Sun Jul 29 00:07:45 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,38 +0,0 @@ -#!/usr/bin/env python - -from MoinShare import getRegions, getFragments - -s = """ -Test some shared content... - -{{{#!MoinShare fragment=101 -Hello! - -This is ''testing'' shared content. -}}} - -{{{#!html fragment=100 moinshare - - - - - -
Test some HTML content.This is a table.
-}}} - -Some trailing content. -""" - -regions = getRegions(s) -fragments = getFragments(s) -expected = 2 - -print regions -print -print len(regions) == expected, ": length is", len(regions), "==", expected -print -print fragments -print -print len(fragments) == expected, ": length is", len(fragments), "==", expected - -# vim: tabstop=4 expandtab shiftwidth=4