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.
 
 
 
 

79 lines
2.6 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, text=None):
  26. super().__init__()
  27. self.title = title
  28. self.text = text
  29. def __str__(self):
  30. if self.text is not None:
  31. return "[[" + str(self.title) + "|" + str(self.text) + "]]"
  32. return "[[" + str(self.title) + "]]"
  33. def __children__(self):
  34. yield self.title
  35. if self.text is not None:
  36. yield self.text
  37. def __strip__(self, **kwargs):
  38. if self.text is not None:
  39. return self.text.strip_code(**kwargs)
  40. return self.title.strip_code(**kwargs)
  41. def __showtree__(self, write, get, mark):
  42. write("[[")
  43. get(self.title)
  44. if self.text is not None:
  45. write(" | ")
  46. mark()
  47. get(self.text)
  48. write("]]")
  49. @property
  50. def title(self):
  51. """The title of the linked page, as a :class:`.Wikicode` object."""
  52. return self._title
  53. @property
  54. def text(self):
  55. """The text to display (if any), as a :class:`.Wikicode` object."""
  56. return self._text
  57. @title.setter
  58. def title(self, value):
  59. self._title = parse_anything(value)
  60. @text.setter
  61. def text(self, value):
  62. if value is None:
  63. self._text = None
  64. else:
  65. self._text = parse_anything(value)