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.
 
 
 
 

80 lines
2.6 KiB

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