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.
 
 
 
 

411 lines
16 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. import re
  24. from .compat import maxsize, str
  25. from .nodes import Heading, Node, Tag, Template, Text
  26. from .string_mixin import StringMixIn
  27. from .utils import parse_anything
  28. __all__ = ["Wikicode"]
  29. FLAGS = re.IGNORECASE | re.DOTALL | re.UNICODE
  30. class Wikicode(StringMixIn):
  31. """A ``Wikicode`` is a container for nodes that functions like a string.
  32. """
  33. def __init__(self, nodes):
  34. super(Wikicode, self).__init__()
  35. self._nodes = nodes
  36. def __unicode__(self):
  37. return "".join([str(node) for node in self.nodes])
  38. def _get_children(self, node):
  39. """Iterate over all descendants of a given *node*, including itself.
  40. This is implemented by the ``__iternodes__()`` generator of ``Node``
  41. classes, which by default yields itself and nothing more.
  42. """
  43. for context, child in node.__iternodes__(self._get_all_nodes):
  44. yield child
  45. def _get_context(self, node, obj):
  46. """Return a ``Wikicode`` that contains *obj* in its descendants.
  47. The closest (shortest distance from *node*) suitable ``Wikicode`` will
  48. be returned, or ``None`` if the *obj* is the *node* itself.
  49. Raises ``ValueError`` if *obj* is not within *node*.
  50. """
  51. for context, child in node.__iternodes__(self._get_all_nodes):
  52. if child is obj:
  53. return context
  54. raise ValueError(obj)
  55. def _get_all_nodes(self, code):
  56. """Iterate over all of our descendant nodes.
  57. This is implemented by calling :py:meth:`_get_children` on every node
  58. in our node list (:py:attr:`self.nodes <nodes>`).
  59. """
  60. for node in code.nodes:
  61. for child in self._get_children(node):
  62. yield child
  63. def _is_equivalent(self, obj, node):
  64. """Return ``True`` if *obj* and *node* are equivalent, else ``False``.
  65. If *obj* is a ``Node``, the function will test whether they are the
  66. same object, otherwise it will compare them with ``==``.
  67. """
  68. if isinstance(obj, Node):
  69. if node is obj:
  70. return True
  71. else:
  72. if node == obj:
  73. return True
  74. return False
  75. def _contains(self, nodes, obj):
  76. """Return ``True`` if *obj* is inside of *nodes*, else ``False``.
  77. If *obj* is a ``Node``, we will only return ``True`` if *obj* is
  78. actually in the list (and not just a node that equals it). Otherwise,
  79. the test is simply ``obj in nodes``.
  80. """
  81. if isinstance(obj, Node):
  82. for node in nodes:
  83. if node is obj:
  84. return True
  85. return False
  86. return obj in nodes
  87. def _do_search(self, obj, recursive, callback, context, *args, **kwargs):
  88. """Look within *context* for *obj*, executing *callback* if found.
  89. If *recursive* is ``True``, we'll look within context and its
  90. descendants, otherwise we'll just execute callback. We raise
  91. :py:exc:`ValueError` if *obj* isn't in our node list or context. If
  92. found, *callback* is passed the context, the index of the node within
  93. the context, and whatever were passed as ``*args`` and ``**kwargs``.
  94. """
  95. if recursive:
  96. for i, node in enumerate(context.nodes):
  97. if self._is_equivalent(obj, node):
  98. return callback(context, i, *args, **kwargs)
  99. if self._contains(self._get_children(node), obj):
  100. context = self._get_context(node, obj)
  101. return self._do_search(obj, recursive, callback, context,
  102. *args, **kwargs)
  103. raise ValueError(obj)
  104. callback(context, self.index(obj, recursive=False), *args, **kwargs)
  105. def _get_tree(self, code, lines, marker, indent):
  106. """Build a tree to illustrate the way the Wikicode object was parsed.
  107. The method that builds the actual tree is ``__showtree__`` of ``Node``
  108. objects. *code* is the ``Wikicode`` object to build a tree for. *lines*
  109. is the list to append the tree to, which is returned at the end of the
  110. method. *marker* is some object to be used to indicate that the builder
  111. should continue on from the last line instead of starting a new one; it
  112. should be any object that can be tested for with ``is``. *indent* is
  113. the starting indentation.
  114. """
  115. def write(*args):
  116. if lines and lines[-1] is marker: # Continue from the last line
  117. lines.pop() # Remove the marker
  118. last = lines.pop()
  119. lines.append(last + " ".join(args))
  120. else:
  121. lines.append(" " * 6 * indent + " ".join(args))
  122. get = lambda code: self._get_tree(code, lines, marker, indent + 1)
  123. mark = lambda: lines.append(marker)
  124. for node in code.nodes:
  125. node.__showtree__(write, get, mark)
  126. return lines
  127. @property
  128. def nodes(self):
  129. """A list of :py:class:`~mwparserfromhell.nodes.Node` objects.
  130. This is the internal data actually stored within a
  131. :py:class:`~mwparserfromhell.wikicode.Wikicode` object.
  132. """
  133. return self._nodes
  134. @nodes.setter
  135. def nodes(self, value):
  136. self._nodes = value
  137. def get(self, index):
  138. """Return the *index*\ th node within the list of nodes."""
  139. return self.nodes[index]
  140. def set(self, index, value):
  141. """Set the ``Node`` at *index* to *value*.
  142. Raises :py:exc:`IndexError` if *index* is out of range, or
  143. :py:exc:`ValueError` if *value* cannot be coerced into one
  144. :py:class:`~mwparserfromhell.nodes.Node`. To insert multiple nodes at
  145. an index, use :py:meth:`get` with either :py:meth:`remove` and
  146. :py:meth:`insert` or :py:meth:`replace`.
  147. """
  148. nodes = parse_anything(value).nodes
  149. if len(nodes) > 1:
  150. raise ValueError("Cannot coerce multiple nodes into one index")
  151. if index >= len(self.nodes) or -1 * index > len(self.nodes):
  152. raise IndexError("List assignment index out of range")
  153. self.nodes.pop(index)
  154. if nodes:
  155. self.nodes[index] = nodes[0]
  156. def index(self, obj, recursive=False):
  157. """Return the index of *obj* in the list of nodes.
  158. Raises :py:exc:`ValueError` if *obj* is not found. If *recursive* is
  159. ``True``, we will look in all nodes of ours and their descendants, and
  160. return the index of our direct descendant node within *our* list of
  161. nodes. Otherwise, the lookup is done only on direct descendants.
  162. """
  163. if recursive:
  164. for i, node in enumerate(self.nodes):
  165. if self._contains(self._get_children(node), obj):
  166. return i
  167. raise ValueError(obj)
  168. for i, node in enumerate(self.nodes):
  169. if self._is_equivalent(obj, node):
  170. return i
  171. raise ValueError(obj)
  172. def insert(self, index, value):
  173. """Insert *value* at *index* in the list of nodes.
  174. *value* can be anything parasable by
  175. :py:func:`mwparserfromhell.utils.parse_anything`, which includes
  176. strings or other :py:class:`~mwparserfromhell.wikicode.Wikicode` or
  177. :py:class:`~mwparserfromhell.nodes.Node` objects.
  178. """
  179. nodes = parse_anything(value).nodes
  180. for node in reversed(nodes):
  181. self.nodes.insert(index, node)
  182. def insert_before(self, obj, value, recursive=True):
  183. """Insert *value* immediately before *obj* in the list of nodes.
  184. *obj* can be either a string or a
  185. :py:class:`~mwparserfromhell.nodes.Node`. *value* can be anything
  186. parasable by :py:func:`mwparserfromhell.utils.parse_anything`. If
  187. *recursive* is ``True``, we will try to find *obj* within our child
  188. nodes even if it is not a direct descendant of this
  189. :py:class:`~mwparserfromhell.wikicode.Wikicode` object. If *obj* is not
  190. in the node list, :py:exc:`ValueError` is raised.
  191. """
  192. callback = lambda self, i, value: self.insert(i, value)
  193. self._do_search(obj, recursive, callback, self, value)
  194. def insert_after(self, obj, value, recursive=True):
  195. """Insert *value* immediately after *obj* in the list of nodes.
  196. *obj* can be either a string or a
  197. :py:class:`~mwparserfromhell.nodes.Node`. *value* can be anything
  198. parasable by :py:func:`mwparserfromhell.utils.parse_anything`. If
  199. *recursive* is ``True``, we will try to find *obj* within our child
  200. nodes even if it is not a direct descendant of this
  201. :py:class:`~mwparserfromhell.wikicode.Wikicode` object. If *obj* is not
  202. in the node list, :py:exc:`ValueError` is raised.
  203. """
  204. callback = lambda self, i, value: self.insert(i + 1, value)
  205. self._do_search(obj, recursive, callback, self, value)
  206. def replace(self, obj, value, recursive=True):
  207. """Replace *obj* with *value* in the list of nodes.
  208. *obj* can be either a string or a
  209. :py:class:`~mwparserfromhell.nodes.Node`. *value* can be anything
  210. parasable by :py:func:`mwparserfromhell.utils.parse_anything`. If
  211. *recursive* is ``True``, we will try to find *obj* within our child
  212. nodes even if it is not a direct descendant of this
  213. :py:class:`~mwparserfromhell.wikicode.Wikicode` object. If *obj* is not
  214. in the node list, :py:exc:`ValueError` is raised.
  215. """
  216. def callback(self, i, value):
  217. self.nodes.pop(i)
  218. self.insert(i, value)
  219. self._do_search(obj, recursive, callback, self, value)
  220. def append(self, value):
  221. """Insert *value* at the end of the list of nodes.
  222. *value* can be anything parasable by
  223. :py:func:`mwparserfromhell.utils.parse_anything`.
  224. """
  225. nodes = parse_anything(value).nodes
  226. for node in nodes:
  227. self.nodes.append(node)
  228. def remove(self, obj, recursive=True):
  229. """Remove *obj* from the list of nodes.
  230. *obj* can be either a string or a
  231. :py:class:`~mwparserfromhell.nodes.Node`. If *recursive* is ``True``,
  232. we will try to find *obj* within our child nodes even if it is not a
  233. direct descendant of this
  234. :py:class:`~mwparserfromhell.wikicode.Wikicode` object. If *obj* is not
  235. in the node list, :py:exc:`ValueError` is raised.
  236. """
  237. callback = lambda self, i: self.nodes.pop(i)
  238. self._do_search(obj, recursive, callback, self)
  239. def ifilter(self, recursive=False, matches=None, flags=FLAGS,
  240. forcetype=None):
  241. """Iterate over nodes in our list matching certain conditions.
  242. If *recursive* is ``True``, we will iterate over our children and all
  243. descendants of our children, otherwise just our immediate children. If
  244. *matches* is given, we will only yield the nodes that match the given
  245. regular expression (with :py:func:`re.search`). The default flags used
  246. are :py:const:`re.IGNORECASE`, :py:const:`re.DOTALL`, and
  247. :py:const:`re.UNICODE`, but custom flags can be specified by passing
  248. *flags*. If *forcetype* is given, only nodes that are instances of this
  249. type are yielded.
  250. """
  251. if recursive:
  252. nodes = self._get_all_nodes(self)
  253. else:
  254. nodes = self.nodes
  255. for node in nodes:
  256. if not forcetype or isinstance(node, forcetype):
  257. if not matches or re.search(matches, str(node), flags):
  258. yield node
  259. def ifilter_templates(self, recursive=False, matches=None, flags=FLAGS):
  260. """Iterate over template nodes.
  261. This is equivalent to :py:meth:`ifilter` with *forcetype* set to
  262. :py:class:`~mwparserfromhell.nodes.template.Template`. It takes all
  263. other arguments passable to :py:meth:`ifilter`.
  264. """
  265. return self.filter(recursive, matches, flags, forcetype=Template)
  266. def ifilter_text(self, recursive=False, matches=None, flags=FLAGS):
  267. """Iterate over text nodes.
  268. This is equivalent to :py:meth:`ifilter` with *forcetype* set to
  269. :py:class:`~mwparserfromhell.nodes.text.Text`.
  270. """
  271. return self.filter(recursive, matches, flags, forcetype=Text)
  272. def ifilter_tags(self, recursive=False, matches=None, flags=FLAGS):
  273. """Iterate over tag nodes.
  274. This is equivalent to :py:meth:`ifilter` with *forcetype* set to
  275. :py:class:`~mwparserfromhell.nodes.tag.Tag`.
  276. """
  277. return self.ifilter(recursive, matches, flags, forcetype=Tag)
  278. def filter(self, recursive=False, matches=None, flags=FLAGS,
  279. forcetype=None):
  280. """Return a list of nodes within our list matching certain conditions.
  281. This is equivalent to calling :py:func:`list` on :py:meth:`ifilter`.
  282. """
  283. return list(self.ifilter(recursive, matches, flags, forcetype))
  284. def filter_templates(self, recursive=False, matches=None, flags=FLAGS):
  285. """Return a list of template nodes.
  286. This is equivalent to calling :py:func:`list` on
  287. :py:meth:`ifilter_templates`.
  288. """
  289. return list(self.ifilter_templates(recursive, matches, flags))
  290. def filter_text(self, recursive=False, matches=None, flags=FLAGS):
  291. """Return a list of text nodes.
  292. This is equivalent to calling :py:func:`list` on
  293. :py:meth:`ifilter_text`.
  294. """
  295. return list(self.ifilter_text(recursive, matches, flags))
  296. def filter_tags(self, recursive=False, matches=None, flags=FLAGS):
  297. """Return a list of tag nodes.
  298. This is equivalent to calling :py:func:`list` on
  299. :py:meth:`ifilter_tags`.
  300. """
  301. return list(self.ifilter_tags(recursive, matches, flags))
  302. def get_sections(self, flat=True, matches=None, levels=None, flags=FLAGS,
  303. include_headings=True):
  304. if matches:
  305. matches = r"^(=+?)\s*" + matches + r"\s*\1$"
  306. headings = self.filter(recursive=True, matches=matches, flags=flags,
  307. forcetype=Heading)
  308. if levels:
  309. headings = [head for head in headings if head.level in levels]
  310. sections = []
  311. buffers = [[maxsize, 0]]
  312. i = 0
  313. while i < len(self.nodes):
  314. if self.nodes[i] in headings:
  315. this = self.nodes[i].level
  316. for (level, start) in buffers:
  317. if not flat or this <= level:
  318. buffers.remove([level, start])
  319. sections.append(self.nodes[start:i])
  320. buffers.append([this, i])
  321. if not include_headings:
  322. i += 1
  323. i += 1
  324. for (level, start) in buffers:
  325. if start != i:
  326. sections.append(self.nodes[start:i])
  327. return sections
  328. def strip_code(self, normalize=True, collapse=True):
  329. nodes = []
  330. for node in self.nodes:
  331. stripped = node.__strip__(normalize, collapse)
  332. if stripped:
  333. nodes.append(str(stripped))
  334. if collapse:
  335. stripped = "".join(nodes).strip("\n")
  336. while "\n\n\n" in stripped:
  337. stripped = stripped.replace("\n\n\n", "\n\n")
  338. return stripped
  339. else:
  340. return "".join(nodes)
  341. def get_tree(self):
  342. marker = object() # Random object we can find with certainty in a list
  343. return "\n".join(self._get_tree(self, [], marker, 0))