A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wikilink.py 3.6 KiB

11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
11 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright (C) 2012-2020 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. from ._base import Node
  21. from ..utils import parse_anything
  22. __all__ = ["Wikilink"]
  23. class Wikilink(Node):
  24. """Represents an internal wikilink, like ``[[Foo|Bar]]``."""
  25. def __init__(self, title, args=None):
  26. super().__init__()
  27. self.title = title
  28. self.args = args
  29. if args is not None:
  30. if not '|' in args:
  31. self.text = args
  32. else:
  33. self.text = parse_anything(args).nodes[len(self.args)-1:]
  34. else:
  35. self.text = None
  36. def __str__(self):
  37. if self.text is not None:
  38. return "[[" + str(self.title) + "|" + str(self.text) + "]]"
  39. return "[[" + str(self.title) + "]]"
  40. def __children__(self):
  41. yield self.title
  42. if self.text is not None:
  43. yield self.text
  44. def __strip__(self, **kwargs):
  45. if self.text is not None:
  46. return self.text.strip_code(**kwargs)
  47. return self.title.strip_code(**kwargs)
  48. def __showtree__(self, write, get, mark):
  49. write("[[")
  50. get(self.title)
  51. if self.args is not None:
  52. write(" | ")
  53. mark()
  54. get(self.args)
  55. if self.text is not None:
  56. write(" | ")
  57. mark()
  58. get(self.text)
  59. write("]]")
  60. @property
  61. def title(self):
  62. """The title of the linked page, as a :class:`.Wikicode` object."""
  63. return self._title
  64. @property
  65. def args(self):
  66. """The args (if any), as a :class:`.list` object."""
  67. return self._args
  68. @property
  69. def text(self):
  70. """The text to display (if any), as a :class:`.Wikicode` object."""
  71. return self._text
  72. @args.setter
  73. def args(self, value):
  74. if arg := parse_anything(value):
  75. self._args = [node for node in str(arg.nodes[0]).split('|')]
  76. if len(self._args) > 0:
  77. self._text = str(arg)[len('|'.join(str(a) for a in self._args)):]
  78. self._args.pop()
  79. if len(self._text) == 0:
  80. self._text = None
  81. if len(self._args) == 0:
  82. self._args = None
  83. elif not hasattr(arg, 'nodes'):
  84. self._args = None
  85. else:
  86. self._args = None
  87. @title.setter
  88. def title(self, value):
  89. if value is not None:
  90. self._title = parse_anything(value)
  91. else:
  92. self._title = None
  93. @text.setter
  94. def text(self, value):
  95. if value is not None:
  96. self._text = parse_anything(value)
  97. else:
  98. self._text = None