From ce6b6a9595cf9f3f288adea9d152e2e71e7dfdf6 Mon Sep 17 00:00:00 2001 From: UlrichB22 <97119703+UlrichB22@users.noreply.github.com> Date: Fri, 22 Nov 2024 21:14:51 +0100 Subject: [PATCH] ItemList: use _args_wiki parser --- src/moin/macros/ItemList.py | 75 +++++++++++++++++-------------------- src/moin/macros/_base.py | 2 +- 2 files changed, 35 insertions(+), 42 deletions(-) diff --git a/src/moin/macros/ItemList.py b/src/moin/macros/ItemList.py index c09bd3ed1..96d138f4b 100644 --- a/src/moin/macros/ItemList.py +++ b/src/moin/macros/ItemList.py @@ -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): @@ -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 = 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 = 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:] diff --git a/src/moin/macros/_base.py b/src/moin/macros/_base.py index 5a4deae7e..0b982bfb1 100644 --- a/src/moin/macros/_base.py +++ b/src/moin/macros/_base.py @@ -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])