Skip to content

Commit

Permalink
Template.add after= parameter (#315)
Browse files Browse the repository at this point in the history
* +after param to Template.add

also updates docstring

* Template.add param after test
  • Loading branch information
eseiver committed Jan 18, 2024
1 parent 74e0be1 commit e24c6e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/mwparserfromhell/nodes/template.py
Expand Up @@ -237,7 +237,8 @@ def get(self, name, default=_UNSET):
def __getitem__(self, name):
return self.get(name)

def add(self, name, value, showkey=None, before=None, preserve_spacing=True):
def add(self, name, value, showkey=None, before=None, after=None,
preserve_spacing=True):
"""Add a parameter to the template with a given *name* and *value*.
*name* and *value* can be anything parsable by
Expand All @@ -259,6 +260,13 @@ def add(self, name, value, showkey=None, before=None, preserve_spacing=True):
occurrence. If *before* is not in the template, :exc:`ValueError` is
raised. The argument is ignored if *name* is an existing parameter.
If *after* is given (either a :class:`.Parameter` object or a name),
then we will place the parameter immediately after this one. If *after*
is a name and exists multiple times in the template, we will place it
after the last occurrence. If *after* is not in the template,
:exc:`ValueError` is raised. The argument is ignored if *name* is an
existing parameter or if a value is passed to *before*.
If *preserve_spacing* is ``True``, we will try to preserve whitespace
conventions around the parameter, whether it is new or we are updating
an existing value. It is disabled for parameters with hidden keys,
Expand Down Expand Up @@ -312,6 +320,10 @@ def add(self, name, value, showkey=None, before=None, preserve_spacing=True):
if not isinstance(before, Parameter):
before = self.get(before)
self.params.insert(self.params.index(before), param)
elif after:
if not isinstance(after, Parameter):
after = self.get(after)
self.params.insert(self.params.index(after)+1, param)
else:
self.params.append(param)
return param
Expand Down
6 changes: 6 additions & 0 deletions tests/test_template.py
Expand Up @@ -334,6 +334,10 @@ def test_add():
node40.add("3", "d")
node41.add("3", "d")
node42.add("b", "hello")
node43 = Template(wraptext("a"), [pgens("b", "c"), pgens("d", "e"), pgens("f", "g")])
node44 = Template(wraptext("a"), [pgens("b", "c"), pgens("d", "e"), pgens("f", "g")])
node43.add("new_param", "value", after="d")
node44.add("new_param", "value", after="f")

assert "{{a|b=c|d|e=f}}" == node1
assert "{{a|b=c|d|g}}" == node2
Expand Down Expand Up @@ -382,6 +386,8 @@ def test_add():
assert "{{a| b| c|d}}" == node40
assert "{{a|1= b|2= c|3= d}}" == node41
assert "{{a|b=hello \n}}" == node42
assert "{{a|b=c|d=e|new_param=value|f=g}}" == node43
assert "{{a|b=c|d=e|f=g|new_param=value}}" == node44


def test_remove():
Expand Down

0 comments on commit e24c6e4

Please sign in to comment.