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.
 
 
 
 

116 lines
3.7 KiB

  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(list(a for a in self.args if len(a)))-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. arg = parse_anything(value)
  75. if arg:
  76. self._args = [node for node in str(arg.nodes[0]).split('|')]
  77. if len(self._args) > 0:
  78. self._text = str(arg)[len('|'.join(str(a) for a in self._args))-1:]
  79. self._args.pop()
  80. if len(self._text) == 0:
  81. self._text = None
  82. if len(self._args) == 0:
  83. self._args = None
  84. elif not hasattr(arg, 'nodes'):
  85. self._args = None
  86. else:
  87. self._args = None
  88. @title.setter
  89. def title(self, value):
  90. if value is not None:
  91. self._title = parse_anything(value)
  92. else:
  93. self._title = None
  94. @text.setter
  95. def text(self, value):
  96. if value is not None:
  97. self._text = parse_anything(value)
  98. else:
  99. self._text = None