Skip to content

Commit

Permalink
Merge pull request #1810 from UlrichB22/itemlist_regex
Browse files Browse the repository at this point in the history
ItemList: use _args_wiki parser
  • Loading branch information
UlrichB22 authored Nov 26, 2024
2 parents 2213072 + ce6b6a9 commit 2320a83
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 42 deletions.
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

0 comments on commit 2320a83

Please sign in to comment.