Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ItemList: use _args_wiki parser #1810

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 34 additions & 41 deletions src/moin/macros/ItemList.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
from moin.i18n import _
from moin.utils.interwiki import split_fqname
from moin.macros._base import MacroPageLinkListBase, get_item_names, fail_message
from moin.converters._args_wiki import parse as parse_arguments


class Macro(MacroPageLinkListBase):
Expand All @@ -83,51 +84,43 @@ def macro(self, content, arguments, page_url, alternative):
skiptag = ""
tag = ""

# process input
args = []
# process arguments
if arguments:
args = arguments[0].split(",")
for arg in args:
try:
key, val = (x.strip() for x in arg.split("="))
except ValueError:
err_msg = _(
'ItemList macro: Argument "{arg}" does not follow <key>=<val> format '
"(arguments, if more than one, must be comma-separated)."
).format(arg=arg)
return fail_message(err_msg, alternative)

if len(val) < 2 or (val[0] != "'" and val[0] != '"') and val[-1] != val[0]:
err_msg = _("The key's value must be bracketed by matching quotes.")
return fail_message(err_msg, alternative)

val = val[1:-1] # strip out the doublequote characters

if key == "item":
item = val
elif key == "startswith":
startswith = val
elif key == "regex":
regex = val
elif key == "ordered":
if val == "False":
ordered = False
elif val == "True":
ordered = True
args = parse_arguments(arguments[0])

for key, val in args.items():
if not key and val:
err_msg = _(
'ItemList macro: Argument "{arg}" does not follow <key>=<val> format '
"(arguments, if more than one, must be comma-separated)."
).format(arg=val)
return fail_message(err_msg, alternative)
if key == "item":
item = val
elif key == "startswith":
startswith = val
elif key == "regex":
regex = val
elif key == "ordered":
if val == "False":
ordered = False
elif val == "True":
ordered = True
else:
err_msg = _('The value for "{key}" must be "True" or "False", got "{val}".').format(
key=key, val=val
)
return fail_message(err_msg, alternative)
elif key == "display":
display = val # let 'create_pagelink_list' throw an exception if needed
elif key == "skiptag":
skiptag = val
elif key == "tag":
tag = val
else:
err_msg = _('The value must be "True" or "False". (got "{val}")').format(val=val)
err_msg = _('Unrecognized key "{key}".').format(key=key)
return fail_message(err_msg, alternative)

elif key == "display":
display = val # let 'create_pagelink_list' throw an exception if needed
elif key == "skiptag":
skiptag = val
elif key == "tag":
tag = val
else:
err_msg = _('Unrecognized key "{key}".').format(key=key)
return fail_message(err_msg, alternative)

# use curr item if not specified
if item is None:
item = request.path[1:]
Expand Down
2 changes: 1 addition & 1 deletion src/moin/macros/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def create_pagelink_list(self, pagenames, alternative, ordered=False, display="F
elif display == "ItemTitle":
linkname = extract_h1(pagename.fullname)
else:
err_msg = _('Unrecognized display value "{display}".').format(display=display)
err_msg = _('Unrecognized "display" value "{display}".').format(display=display)
return fail_message(err_msg, alternative)

pagelink = moin_page.a(attrib={xlink.href: url}, children=[linkname])
Expand Down