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 """
2.1 --- a/parsers/MoinShare.py Sun Jul 29 00:07:45 2012 +0200
2.2 +++ b/parsers/MoinShare.py Sat Sep 29 17:53:04 2012 +0200
2.3 @@ -6,6 +6,7 @@
2.4 @license: GNU GPL (v2 or later), see COPYING.txt for details.
2.5 """
2.6
2.7 +from MoinSupport import parseAttributes
2.8 from MoinMoin import wikiutil
2.9
2.10 try:
2.11 @@ -38,19 +39,14 @@
2.12 self.raw = raw
2.13 self.request = request
2.14
2.15 - attrs, msg = wikiutil.parseAttributes(request, kw.get("format_args", ""))
2.16 -
2.17 - # The attributes returned from the formatting arguments are encoded like
2.18 - # strings.
2.19 -
2.20 - quotes = '"' + "'"
2.21 + attrs = parseAttributes(request, False)
2.22
2.23 # Get any reference to this fragment, another fragment and the content
2.24 # format.
2.25
2.26 - self.fragment = (attrs.get("fragment") or '').strip(quotes)
2.27 - self.ref = (attrs.get("ref") or '').strip(quotes)
2.28 - self.format_type = (attrs.get("format") or '').strip(quotes)
2.29 + self.fragment = attrs.get("fragment", '')
2.30 + self.ref = attrs.get("ref", '')
2.31 + self.format_type = attrs.get("format", '')
2.32
2.33 def getDigest(self):
2.34
3.1 --- a/tests/test_regions.py Sun Jul 29 00:07:45 2012 +0200
3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
3.3 @@ -1,38 +0,0 @@
3.4 -#!/usr/bin/env python
3.5 -
3.6 -from MoinShare import getRegions, getFragments
3.7 -
3.8 -s = """
3.9 -Test some shared content...
3.10 -
3.11 -{{{#!MoinShare fragment=101
3.12 -Hello!
3.13 -
3.14 -This is ''testing'' shared content.
3.15 -}}}
3.16 -
3.17 -{{{#!html fragment=100 moinshare
3.18 -<table>
3.19 -<tr>
3.20 -<td>Test some <strong>HTML</strong> content.</td>
3.21 -<td>This is a table.</td>
3.22 -</tr>
3.23 -</table>
3.24 -}}}
3.25 -
3.26 -Some trailing content.
3.27 -"""
3.28 -
3.29 -regions = getRegions(s)
3.30 -fragments = getFragments(s)
3.31 -expected = 2
3.32 -
3.33 -print regions
3.34 -print
3.35 -print len(regions) == expected, ": length is", len(regions), "==", expected
3.36 -print
3.37 -print fragments
3.38 -print
3.39 -print len(fragments) == expected, ": length is", len(fragments), "==", expected
3.40 -
3.41 -# vim: tabstop=4 expandtab shiftwidth=4