Skip to content

Instantly share code, notes, and snippets.

@Kwpolska

Kwpolska/-

Created Dec 23, 2015
Embed
What would you like to do?
def parse_sc(data):
"""Parse shortcode arguments into a tuple.
>>> import pprint
>>> pprint.pprint(parse_sc('hello world Nikola abc=def foo="bar baz" backslashes=work\\ too'))
('hello',
(['world', 'Nikola'],
{'abc': 'def', 'backslashes': 'work too', 'foo': 'bar baz'}))
"""
elements = data.split(' ', 1)
name = elements[0]
if len(elements) == 1:
# No arguments
return name, ([], {})
args = []
kwargs = {}
# "Simple" argument parser.
# flag can be one of:
# 0 name
# 1 value
# 2 value inside quotes
# 5 value inside backslash
# 6 value inside quotes and backslash
flag = 0
cname = ''
cvalue = ''
qc = ''
for char in elements[1]:
if flag > 2:
cvalue += char
flag -= 4
elif char == '=' and flag == 0:
flag = 1
elif char == ' ' and flag == 0:
args.append(cname)
cname = cvalue = qc = ''
elif char == ' ' and flag == 1:
kwargs[cname] = cvalue
flag = 0
cname = cvalue = qc = ''
elif char == ' ' and flag == 2:
cvalue += char
elif char == '\\' and (flag == 1 or flag == 2):
flag += 4
elif char == '"' or char == "'":
qc = char
if flag == 1:
flag = 2
elif flag == 2 and qc == char:
flag = 1
else:
# Unbalanced quotes, reproduce as is
cvalue += char
elif flag == 0:
cname += char
elif flag > 0:
cvalue += char
# Last argument handling
if cvalue:
kwargs[cname] = cvalue
else:
args.append(cname)
return name, (args, kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.