@@ -1,6 +1,6 @@ | |||||
v0.7 (unreleased): | v0.7 (unreleased): | ||||
- ... | |||||
- Added 'after' parameter to Template.add(). (#281, #315) | |||||
v0.6.6 (released January 4, 2024): | v0.6.6 (released January 4, 2024): | ||||
@@ -7,7 +7,9 @@ v0.7 | |||||
Unreleased | Unreleased | ||||
(`changes <https://github.com/earwig/mwparserfromhell/compare/v0.6.6...main>`__): | (`changes <https://github.com/earwig/mwparserfromhell/compare/v0.6.6...main>`__): | ||||
- ... | |||||
- Added 'after' parameter to Template.add(). | |||||
(`#281 <https://github.com/earwig/mwparserfromhell/issues/281>`_, | |||||
`#315 <https://github.com/earwig/mwparserfromhell/pull/315>`_) | |||||
v0.6.6 | v0.6.6 | ||||
------ | ------ | ||||
@@ -114,7 +114,7 @@ class MemoryTest: | |||||
def run(self): | def run(self): | ||||
"""Run the memory test suite.""" | """Run the memory test suite.""" | ||||
width = 1 | width = 1 | ||||
for (name, _) in self._tests: | |||||
for name, _ in self._tests: | |||||
if len(name) > width: | if len(name) > width: | ||||
width = len(name) | width = len(name) | ||||
@@ -237,8 +237,9 @@ class Template(Node): | |||||
def __getitem__(self, name): | def __getitem__(self, name): | ||||
return self.get(name) | return self.get(name) | ||||
def add(self, name, value, showkey=None, before=None, after=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*. | """Add a parameter to the template with a given *name* and *value*. | ||||
*name* and *value* can be anything parsable by | *name* and *value* can be anything parsable by | ||||
@@ -317,13 +318,14 @@ class Template(Node): | |||||
param = Parameter(name, value, showkey) | param = Parameter(name, value, showkey) | ||||
if before: | if before: | ||||
assert after is None, "Cannot set a value for both 'before' and 'after'" | |||||
if not isinstance(before, Parameter): | if not isinstance(before, Parameter): | ||||
before = self.get(before) | before = self.get(before) | ||||
self.params.insert(self.params.index(before), param) | self.params.insert(self.params.index(before), param) | ||||
elif after: | elif after: | ||||
if not isinstance(after, Parameter): | if not isinstance(after, Parameter): | ||||
after = self.get(after) | after = self.get(after) | ||||
self.params.insert(self.params.index(after)+1, param) | |||||
self.params.insert(self.params.index(after) + 1, param) | |||||
else: | else: | ||||
self.params.append(param) | self.params.append(param) | ||||
return param | return param | ||||
@@ -19,7 +19,7 @@ | |||||
*/ | */ | ||||
#define false 0 | #define false 0 | ||||
#define true 1 | |||||
#define true 1 | |||||
typedef int bool; | typedef int bool; | ||||
@@ -334,8 +334,12 @@ def test_add(): | |||||
node40.add("3", "d") | node40.add("3", "d") | ||||
node41.add("3", "d") | node41.add("3", "d") | ||||
node42.add("b", "hello") | 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 = 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") | node43.add("new_param", "value", after="d") | ||||
node44.add("new_param", "value", after="f") | node44.add("new_param", "value", after="f") | ||||