Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Wikilink.args for File: links #301

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions src/mwparserfromhell/nodes/wikilink.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@
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:
Copy link

@adrianfagerland adrianfagerland Mar 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using self.args instead of args for the check of ...is not None
At least, that solved an error I was getting when using your code.

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):
if self.text is not None:
Expand All @@ -51,6 +58,10 @@ def __strip__(self, **kwargs):
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()
Expand All @@ -62,18 +73,43 @@ def title(self):
"""The title of the linked page, as a :class:`.Wikicode` object."""
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):
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
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