Browse Source

Merge 55a0971149 into 537e1e19e3

pull/301/merge
Fredrick Brennan 4 months ago
committed by GitHub
parent
commit
0c4333e2b9
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
1 changed files with 42 additions and 6 deletions
  1. +42
    -6
      src/mwparserfromhell/nodes/wikilink.py

+ 42
- 6
src/mwparserfromhell/nodes/wikilink.py View File

@@ -28,10 +28,17 @@ __all__ = ["Wikilink"]
class Wikilink(Node): class Wikilink(Node):
"""Represents an internal wikilink, like ``[[Foo|Bar]]``.""" """Represents an internal wikilink, like ``[[Foo|Bar]]``."""


def __init__(self, title, text=None):
def __init__(self, title, args=None):
super().__init__() super().__init__()
self.title = title 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(list(a for a in self.args if len(a)))-1:]
else:
self.text = None


def __str__(self): def __str__(self):
if self.text is not None: if self.text is not None:
@@ -51,6 +58,10 @@ class Wikilink(Node):
def __showtree__(self, write, get, mark): def __showtree__(self, write, get, mark):
write("[[") write("[[")
get(self.title) get(self.title)
if self.args is not None:
write(" | ")
mark()
get(self.args)
if self.text is not None: if self.text is not None:
write(" | ") write(" | ")
mark() mark()
@@ -63,17 +74,42 @@ class Wikilink(Node):
return self._title return self._title


@property @property
def args(self):
"""The args (if any), as a :class:`.list` object."""
return self._args

@property
def text(self): def text(self):
"""The text to display (if any), as a :class:`.Wikicode` object.""" """The text to display (if any), as a :class:`.Wikicode` object."""
return self._text return self._text


@args.setter
def args(self, 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:]
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 @title.setter
def title(self, value): 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 @text.setter
def text(self, value): def text(self, value):
if value is None:
self._text = None
else:
if value is not None:
self._text = parse_anything(value) self._text = parse_anything(value)
else:
self._text = None

Loading…
Cancel
Save