From 849746be4b36ff688586556e83f58a3948d7e895 Mon Sep 17 00:00:00 2001 From: Fred Brennan Date: Sun, 29 Jan 2023 22:26:54 -0500 Subject: [PATCH 1/3] Add support for Wikilink.args for `File:` links --- src/mwparserfromhell/nodes/wikilink.py | 47 +++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/src/mwparserfromhell/nodes/wikilink.py b/src/mwparserfromhell/nodes/wikilink.py index 870cba9..9d9d2bc 100644 --- a/src/mwparserfromhell/nodes/wikilink.py +++ b/src/mwparserfromhell/nodes/wikilink.py @@ -28,10 +28,17 @@ __all__ = ["Wikilink"] class Wikilink(Node): """Represents an internal wikilink, like ``[[Foo|Bar]]``.""" - def __init__(self, title, text=None): + def __init__(self, title, args=None): super().__init__() self.title = title - self.text = text + self.args = args + if args is not None: + if not '|' in args: + self.text = args + else: + self.text = parse_anything(args).nodes[len(self.args)-1:] + else: + self.text = None def __str__(self): if self.text is not None: @@ -51,6 +58,10 @@ class Wikilink(Node): def __showtree__(self, write, get, mark): write("[[") get(self.title) + if self.args is not None: + write(" | ") + mark() + get(self.args) if self.text is not None: write(" | ") mark() @@ -63,17 +74,41 @@ class Wikilink(Node): return self._title @property + def args(self): + """The args (if any), as a :class:`.list` object.""" + return self._args + + @property def text(self): """The text to display (if any), as a :class:`.Wikicode` object.""" return self._text + @args.setter + def args(self, value): + if arg := parse_anything(value): + self._args = [node for node in str(arg.nodes[0]).split('|')] + if len(self._args) > 0: + self._text = str(arg)[len('|'.join(str(a) for a in self._args)):] + self._args.pop() + if len(self._text) == 0: + self._text = None + if len(self._args) == 0: + self._args = None + elif not hasattr(arg, 'nodes'): + self._args = None + else: + self._args = None + @title.setter def title(self, value): - self._title = parse_anything(value) + if value is not None: + self._title = parse_anything(value) + else: + self._title = None @text.setter def text(self, value): - if value is None: - self._text = None - else: + if value is not None: self._text = parse_anything(value) + else: + self._text = None From bd36dd2b6df4f908047af5d26a09dc47d085c317 Mon Sep 17 00:00:00 2001 From: Fred Brennan Date: Sun, 29 Jan 2023 23:32:12 -0500 Subject: [PATCH 2/3] Now passes wikilinks:blank_with_multiple_texts --- src/mwparserfromhell/nodes/wikilink.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mwparserfromhell/nodes/wikilink.py b/src/mwparserfromhell/nodes/wikilink.py index 9d9d2bc..de700d6 100644 --- a/src/mwparserfromhell/nodes/wikilink.py +++ b/src/mwparserfromhell/nodes/wikilink.py @@ -36,7 +36,7 @@ class Wikilink(Node): if not '|' in args: self.text = args else: - self.text = parse_anything(args).nodes[len(self.args)-1:] + self.text = parse_anything(args).nodes[len(list(a for a in self.args if len(a)))-1:] else: self.text = None @@ -88,7 +88,7 @@ class Wikilink(Node): if arg := parse_anything(value): self._args = [node for node in str(arg.nodes[0]).split('|')] if len(self._args) > 0: - self._text = str(arg)[len('|'.join(str(a) for a in self._args)):] + self._text = str(arg)[len('|'.join(str(a) for a in self._args))-1:] self._args.pop() if len(self._text) == 0: self._text = None From 55a0971149d4e2e236c952f3881e96f70ee78fe7 Mon Sep 17 00:00:00 2001 From: Fred Brennan Date: Sun, 29 Jan 2023 23:34:10 -0500 Subject: [PATCH 3/3] Fix for old Py3k versions --- src/mwparserfromhell/nodes/wikilink.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mwparserfromhell/nodes/wikilink.py b/src/mwparserfromhell/nodes/wikilink.py index de700d6..7bfafdb 100644 --- a/src/mwparserfromhell/nodes/wikilink.py +++ b/src/mwparserfromhell/nodes/wikilink.py @@ -85,7 +85,8 @@ class Wikilink(Node): @args.setter def args(self, value): - if arg := parse_anything(value): + arg = parse_anything(value) + if arg: self._args = [node for node in str(arg.nodes[0]).split('|')] if len(self._args) > 0: self._text = str(arg)[len('|'.join(str(a) for a in self._args))-1:]