A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

293 řádky
8.7 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012 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 ..utils import parse_anything
  26. __all__ = ["Tag"]
  27. class Tag(Node):
  28. """Represents an HTML-style tag in wikicode, like ``<ref>``."""
  29. TAG_UNKNOWN = 0
  30. # Basic HTML:
  31. TAG_ITALIC = 1
  32. TAG_BOLD = 2
  33. TAG_UNDERLINE = 3
  34. TAG_STRIKETHROUGH = 4
  35. TAG_UNORDERED_LIST = 5
  36. TAG_ORDERED_LIST = 6
  37. TAG_DEF_TERM = 7
  38. TAG_DEF_ITEM = 8
  39. TAG_BLOCKQUOTE = 9
  40. TAG_RULE = 10
  41. TAG_BREAK = 11
  42. TAG_ABBR = 12
  43. TAG_PRE = 13
  44. TAG_MONOSPACE = 14
  45. TAG_CODE = 15
  46. TAG_SPAN = 16
  47. TAG_DIV = 17
  48. TAG_FONT = 18
  49. TAG_SMALL = 19
  50. TAG_BIG = 20
  51. TAG_CENTER = 21
  52. # MediaWiki parser hooks:
  53. TAG_REF = 101
  54. TAG_GALLERY = 102
  55. TAG_MATH = 103
  56. TAG_NOWIKI = 104
  57. TAG_NOINCLUDE = 105
  58. TAG_INCLUDEONLY = 106
  59. TAG_ONLYINCLUDE = 107
  60. # Additional parser hooks:
  61. TAG_SYNTAXHIGHLIGHT = 201
  62. TAG_POEM = 202
  63. # Lists of tags:
  64. TAGS_ALL = set(range(300))
  65. TAGS_INVISIBLE = set((TAG_REF, TAG_GALLERY, TAG_MATH, TAG_NOINCLUDE))
  66. TAGS_VISIBLE = TAGS_ALL - TAGS_INVISIBLE
  67. TRANSLATIONS = {
  68. "i": TAG_ITALIC,
  69. "em": TAG_ITALIC,
  70. "b": TAG_BOLD,
  71. "strong": TAG_BOLD,
  72. "u": TAG_UNDERLINE,
  73. "s": TAG_STRIKETHROUGH,
  74. "ul": TAG_UNORDERED_LIST,
  75. "ol": TAG_ORDERED_LIST,
  76. "dt": TAG_DEF_TERM,
  77. "dd": TAG_DEF_ITEM,
  78. "blockquote": TAG_BLOCKQUOTE,
  79. "hl": TAG_RULE,
  80. "br": TAG_BREAK,
  81. "abbr": TAG_ABBR,
  82. "pre": TAG_PRE,
  83. "tt": TAG_MONOSPACE,
  84. "code": TAG_CODE,
  85. "span": TAG_SPAN,
  86. "div": TAG_DIV,
  87. "font": TAG_FONT,
  88. "small": TAG_SMALL,
  89. "big": TAG_BIG,
  90. "center": TAG_CENTER,
  91. "ref": TAG_REF,
  92. "gallery": TAG_GALLERY,
  93. "math": TAG_MATH,
  94. "nowiki": TAG_NOWIKI,
  95. "noinclude": TAG_NOINCLUDE,
  96. "includeonly": TAG_INCLUDEONLY,
  97. "onlyinclude": TAG_ONLYINCLUDE,
  98. "syntaxhighlight": TAG_SYNTAXHIGHLIGHT,
  99. "source": TAG_SYNTAXHIGHLIGHT,
  100. "poem": TAG_POEM,
  101. }
  102. def __init__(self, type_, tag, contents=None, attrs=None, showtag=True,
  103. self_closing=False, open_padding="", closing_tag=None):
  104. super(Tag, self).__init__()
  105. self._type = type_
  106. self._tag = tag
  107. self._contents = contents
  108. if attrs:
  109. self._attrs = attrs
  110. else:
  111. self._attrs = []
  112. self._showtag = showtag
  113. self._self_closing = self_closing
  114. self._open_padding = open_padding
  115. if closing_tag:
  116. self._closing_tag = closing_tag
  117. else:
  118. self._closing_tag = tag
  119. def __unicode__(self):
  120. if not self.showtag:
  121. open_, close = self._translate()
  122. if self.self_closing:
  123. return open_
  124. else:
  125. return open_ + str(self.contents) + close
  126. result = "<" + str(self.tag)
  127. if self.attrs:
  128. result += " " + " ".join([str(attr) for attr in self.attrs])
  129. if self.self_closing:
  130. result += self.open_padding + "/>"
  131. else:
  132. result += self.open_padding + ">" + str(self.contents)
  133. result += "</" + self.closing_tag + ">"
  134. return result
  135. def __iternodes__(self, getter):
  136. yield None, self
  137. if self.showtag:
  138. for child in getter(self.tag):
  139. yield self.tag, child
  140. for attr in self.attrs:
  141. for child in getter(attr.name):
  142. yield attr.name, child
  143. if attr.value:
  144. for child in getter(attr.value):
  145. yield attr.value, child
  146. for child in getter(self.contents):
  147. yield self.contents, child
  148. def __strip__(self, normalize, collapse):
  149. if self.type in self.TAGS_VISIBLE:
  150. return self.contents.strip_code(normalize, collapse)
  151. return None
  152. def __showtree__(self, write, get, mark):
  153. tagnodes = self.tag.nodes
  154. if (not self.attrs and len(tagnodes) == 1 and isinstance(tagnodes[0], Text)):
  155. write("<" + str(tagnodes[0]) + ">")
  156. else:
  157. write("<")
  158. get(self.tag)
  159. for attr in self.attrs:
  160. get(attr.name)
  161. if not attr.value:
  162. continue
  163. write(" = ")
  164. mark()
  165. get(attr.value)
  166. write(">")
  167. get(self.contents)
  168. if len(tagnodes) == 1 and isinstance(tagnodes[0], Text):
  169. write("</" + str(tagnodes[0]) + ">")
  170. else:
  171. write("</")
  172. get(self.tag)
  173. write(">")
  174. def _translate(self):
  175. """If the HTML-style tag has a wikicode representation, return that.
  176. For example, ``<b>Foo</b>`` can be represented as ``'''Foo'''``. This
  177. returns a tuple of the character starting the sequence and the
  178. character ending it.
  179. """
  180. translations = {
  181. self.TAG_ITALIC: ("''", "''"),
  182. self.TAG_BOLD: ("'''", "'''"),
  183. self.TAG_UNORDERED_LIST: ("*", ""),
  184. self.TAG_ORDERED_LIST: ("#", ""),
  185. self.TAG_DEF_TERM: (";", ""),
  186. self.TAG_DEF_ITEM: (":", ""),
  187. self.TAG_RULE: ("----", ""),
  188. }
  189. return translations[self.type]
  190. @property
  191. def type(self):
  192. """The tag type."""
  193. return self._type
  194. @property
  195. def tag(self):
  196. """The tag itself, as a :py:class:`~.Wikicode` object."""
  197. return self._tag
  198. @property
  199. def contents(self):
  200. """The contents of the tag, as a :py:class:`~.Wikicode` object."""
  201. return self._contents
  202. @property
  203. def attrs(self):
  204. """The list of attributes affecting the tag.
  205. Each attribute is an instance of :py:class:`~.Attribute`.
  206. """
  207. return self._attrs
  208. @property
  209. def showtag(self):
  210. """Whether to show the tag itself instead of a wikicode version."""
  211. return self._showtag
  212. @property
  213. def self_closing(self):
  214. """Whether the tag is self-closing with no content (like ``<br/>``)."""
  215. return self._self_closing
  216. @property
  217. def open_padding(self):
  218. """Spacing to insert before the first closing >."""
  219. return self._open_padding
  220. @property
  221. def closing_tag(self):
  222. """The closing tag, as a :py:class:`~.Wikicode` object.
  223. This will usually equal :py:attr:`tag`, unless there is additional
  224. spacing, comments, or the like.
  225. """
  226. return self._closing_tag
  227. @type.setter
  228. def type(self, value):
  229. value = int(value)
  230. if value not in self.TAGS_ALL:
  231. raise ValueError(value)
  232. self._type = value
  233. for key in self.TRANSLATIONS:
  234. if self.TRANSLATIONS[key] == value:
  235. self._tag = self._closing_tag = parse_anything(key)
  236. @tag.setter
  237. def tag(self, value):
  238. self._tag = self._closing_tag = parse_anything(value)
  239. try:
  240. self._type = self.TRANSLATIONS[text]
  241. except KeyError:
  242. self._type = self.TAG_UNKNOWN
  243. @contents.setter
  244. def contents(self, value):
  245. self._contents = parse_anything(value)
  246. @showtag.setter
  247. def showtag(self, value):
  248. self._showtag = bool(value)
  249. @self_closing.setter
  250. def self_closing(self, value):
  251. self._self_closing = bool(value)
  252. @open_padding.setter
  253. def open_padding(self, value):
  254. self._open_padding = str(value)
  255. @closing_tag.setter
  256. def closing_tag(self, value):
  257. self._closing_tag = parse_anything(value)