A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

83 wiersze
2.7 KiB

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