wmltools: refine comma_split function

First: minor change of the argument name passed to the function
from "value" to "csstring", since the string might not be an
attribute value.

Second: more testing pointed out that an empty list would not
meet the "if list:" condition and would not be extended. So
make it explicitly "!= None".

Third: when asking on IRC about trailing whitespace, it was
noted that utils::split also removed empty entries, which
reminded me that I should do that as well.

The biggest change is that the function will now rstrip() by
default, with other options for dealing with trailing
whitespace offered.
This commit is contained in:
Groggy Dice 2015-03-13 04:36:33 -04:00
parent 134df26a0a
commit 13c9885156

View file

@ -52,12 +52,20 @@ def attr_strip(value):
value = value.strip()
return string_strip(value)
def comma_split(value, list=None):
"Split a comma-separated value, and append the entries to a list if specified."
vallist = [x.lstrip()for x in value.split(",")]
# lstrip: wml-tags.lua split function will remove leading whitespace
# of items in comma-separated lists but not trailing whitespace
if list:
def comma_split(csstring, list=None, strip="r"):
"Split a comma-separated string, and append the entries to a list if specified."
vallist = [x.lstrip() for x in csstring.split(",") if x.lstrip()]
# strip=: utils::split will remove trailing whitespace from items in comma-
# separated lists but the wml-tags.lua split function only removes leading
# whitespace. So two flags are offered to change default behavior: one to
# lstrip() only, the other to warn about trailing whitespace.
if 'w' in strip:
for item in vallist:
if re.search('\s$', item):
print 'Trailing whitespace may be problematic: "%s" in "%s"' % (item, csstring)
if 'l' not in strip:
vallist = [x.rstrip() for x in vallist]
if list != None:
list.extend(vallist)
else:
return vallist