1.1 --- a/MoinSupport.py Mon Apr 02 20:17:59 2012 +0200
1.2 +++ b/MoinSupport.py Wed Apr 18 01:17:58 2012 +0200
1.3 @@ -11,10 +11,12 @@
1.4 from DateSupport import *
1.5 from MoinMoin.Page import Page
1.6 from MoinMoin import wikiutil
1.7 +from StringIO import StringIO
1.8 +from shlex import shlex
1.9 import re
1.10 import time
1.11
1.12 -__version__ = "0.1"
1.13 +__version__ = "0.2"
1.14
1.15 # Content type parsing.
1.16
1.17 @@ -47,6 +49,65 @@
1.18 else:
1.19 return int(x)
1.20
1.21 +def parseAttributes(s, escape=True):
1.22 +
1.23 + """
1.24 + Parse the section attributes string 's', returning a mapping of names to
1.25 + values. If 'escape' is set to a true value, the attributes will be suitable
1.26 + for use with the formatter API. If 'escape' is set to a false value, the
1.27 + attributes will have any quoting removed.
1.28 + """
1.29 +
1.30 + attrs = {}
1.31 + f = StringIO(s)
1.32 + name = None
1.33 + need_value = False
1.34 +
1.35 + for token in shlex(f):
1.36 +
1.37 + # Capture the name if needed.
1.38 +
1.39 + if name is None:
1.40 + name = escape and wikiutil.escape(token) or strip_token(token)
1.41 +
1.42 + # Detect either an equals sign or another name.
1.43 +
1.44 + elif not need_value:
1.45 + if token == "=":
1.46 + need_value = True
1.47 + else:
1.48 + attrs[name.lower()] = escape and "true" or True
1.49 + name = wikiutil.escape(token)
1.50 +
1.51 + # Otherwise, capture a value.
1.52 +
1.53 + else:
1.54 + # Quoting of attributes done similarly to wikiutil.parseAttributes.
1.55 +
1.56 + if token:
1.57 + if escape:
1.58 + if token[0] in ("'", '"'):
1.59 + token = wikiutil.escape(token)
1.60 + else:
1.61 + token = '"%s"' % wikiutil.escape(token, 1)
1.62 + else:
1.63 + token = strip_token(token)
1.64 +
1.65 + attrs[name.lower()] = token
1.66 + name = None
1.67 + need_value = False
1.68 +
1.69 + return attrs
1.70 +
1.71 +def strip_token(token):
1.72 +
1.73 + "Return the given 'token' stripped of quoting."
1.74 +
1.75 + if token[0] in ("'", '"') and token[-1] == token[0]:
1.76 + return token[1:-1]
1.77 + else:
1.78 + return token
1.79 +
1.80 # Utility classes and associated functions.
1.81
1.82 class Form: