A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

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