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.
 
 
 
 

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