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.
 
 
 
 

206 lines
6.7 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net>
  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, Text
  24. from ..compat import str
  25. from ..tag_defs import get_wikicode, is_visible
  26. from ..utils import parse_anything
  27. __all__ = ["Tag"]
  28. class Tag(Node):
  29. """Represents an HTML-style tag in wikicode, like ``<ref>``."""
  30. def __init__(self, tag, contents=None, attrs=None, showtag=True,
  31. self_closing=False, invalid=False, implicit=False, padding="",
  32. closing_tag=None):
  33. super(Tag, self).__init__()
  34. self._tag = tag
  35. self._contents = contents
  36. self._attrs = attrs if attrs else []
  37. self._showtag = showtag
  38. self._self_closing = self_closing
  39. self._invalid = invalid
  40. self._implicit = implicit
  41. self._padding = padding
  42. if closing_tag:
  43. self._closing_tag = closing_tag
  44. elif not self_closing:
  45. self._closing_tag = tag
  46. def __unicode__(self):
  47. if not self.showtag:
  48. open_, close = get_wikicode[self.tag]
  49. if self.self_closing:
  50. return open_
  51. else:
  52. return open_ + str(self.contents) + close
  53. result = ("</" if self.invalid else "<") + str(self.tag)
  54. if self.attributes:
  55. result += "".join([str(attr) for attr in self.attributes])
  56. if self.self_closing:
  57. result += self.padding + (">" if self.implicit else "/>")
  58. else:
  59. result += self.padding + ">" + str(self.contents)
  60. result += "</" + str(self.closing_tag) + ">"
  61. return result
  62. def __iternodes__(self, getter):
  63. yield None, self
  64. if self.showtag:
  65. for child in getter(self.tag):
  66. yield self.tag, child
  67. for attr in self.attributes:
  68. for child in getter(attr.name):
  69. yield attr.name, child
  70. if attr.value:
  71. for child in getter(attr.value):
  72. yield attr.value, child
  73. if self.contents:
  74. for child in getter(self.contents):
  75. yield self.contents, child
  76. if not self.self_closing and self.closing_tag:
  77. for child in getter(self.closing_tag):
  78. yield self.closing_tag, child
  79. def __strip__(self, normalize, collapse):
  80. if is_visible(self.tag):
  81. return self.contents.strip_code(normalize, collapse)
  82. return None
  83. def __showtree__(self, write, get, mark):
  84. write("</" if self.invalid else "<")
  85. get(self.tag)
  86. for attr in self.attributes:
  87. get(attr.name)
  88. if not attr.value:
  89. continue
  90. write(" = ")
  91. mark()
  92. get(attr.value)
  93. if self.self_closing:
  94. write(">" if self.implicit else "/>")
  95. else:
  96. write(">")
  97. get(self.contents)
  98. write("</")
  99. get(self.closing_tag)
  100. write(">")
  101. @property
  102. def tag(self):
  103. """The tag itself, as a :py:class:`~.Wikicode` object."""
  104. return self._tag
  105. @property
  106. def contents(self):
  107. """The contents of the tag, as a :py:class:`~.Wikicode` object."""
  108. return self._contents
  109. @property
  110. def attributes(self):
  111. """The list of attributes affecting the tag.
  112. Each attribute is an instance of :py:class:`~.Attribute`.
  113. """
  114. return self._attrs
  115. @property
  116. def showtag(self):
  117. """Whether to show the tag itself instead of a wikicode version."""
  118. return self._showtag
  119. @property
  120. def self_closing(self):
  121. """Whether the tag is self-closing with no content (like ``<br/>``)."""
  122. return self._self_closing
  123. @property
  124. def invalid(self):
  125. """Whether the tag starts with a backslash after the opening bracket.
  126. This makes the tag look like a lone close tag. It is technically
  127. invalid and is only parsable Wikicode when the tag itself is
  128. single-only, like ``<br>`` and ``<img>``. See
  129. :py:func:`.tag_defs.is_single_only`.
  130. """
  131. return self._invalid
  132. @property
  133. def implicit(self):
  134. """Whether the tag is implicitly self-closing, with no ending slash.
  135. This is only possible for specific "single" tags like ``<br>`` and
  136. ``<li>``. See :py:func:`.tag_defs.is_single`. This field only has an
  137. effect if :py:attr:`self_closing` is also ``True``.
  138. """
  139. return self._implicit
  140. @property
  141. def padding(self):
  142. """Spacing to insert before the first closing ``>``."""
  143. return self._padding
  144. @property
  145. def closing_tag(self):
  146. """The closing tag, as a :py:class:`~.Wikicode` object.
  147. This will usually equal :py:attr:`tag`, unless there is additional
  148. spacing, comments, or the like.
  149. """
  150. return self._closing_tag
  151. @tag.setter
  152. def tag(self, value):
  153. self._tag = self._closing_tag = parse_anything(value)
  154. @contents.setter
  155. def contents(self, value):
  156. self._contents = parse_anything(value)
  157. @showtag.setter
  158. def showtag(self, value):
  159. self._showtag = bool(value)
  160. @self_closing.setter
  161. def self_closing(self, value):
  162. self._self_closing = bool(value)
  163. @invalid.setter
  164. def invalid(self, value):
  165. self._invalid = bool(value)
  166. @implicit.setter
  167. def implicit(self, value):
  168. self._implicit = bool(value)
  169. @padding.setter
  170. def padding(self, value):
  171. self._padding = str(value)
  172. @closing_tag.setter
  173. def closing_tag(self, value):
  174. self._closing_tag = parse_anything(value)