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.
 
 
 
 

303 lines
12 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 collections import defaultdict
  24. import re
  25. from . import HTMLEntity, Node, Text
  26. from .extras import Parameter
  27. from ..compat import basestring, str
  28. from ..utils import parse_anything
  29. __all__ = ["Template"]
  30. FLAGS = re.DOTALL | re.UNICODE
  31. class Template(Node):
  32. """Represents a template in wikicode, like ``{{foo}}``."""
  33. def __init__(self, name, params=None):
  34. super(Template, self).__init__()
  35. self._name = name
  36. if params:
  37. self._params = params
  38. else:
  39. self._params = []
  40. def __unicode__(self):
  41. if self.params:
  42. params = "|".join([str(param) for param in self.params])
  43. return "{{" + str(self.name) + "|" + params + "}}"
  44. else:
  45. return "{{" + str(self.name) + "}}"
  46. def __iternodes__(self, getter):
  47. yield None, self
  48. for child in getter(self.name):
  49. yield self.name, child
  50. for param in self.params:
  51. if param.showkey:
  52. for child in getter(param.name):
  53. yield param.name, child
  54. for child in getter(param.value):
  55. yield param.value, child
  56. def __showtree__(self, write, get, mark):
  57. write("{{")
  58. get(self.name)
  59. for param in self.params:
  60. write(" | ")
  61. mark()
  62. get(param.name)
  63. write(" = ")
  64. mark()
  65. get(param.value)
  66. write("}}")
  67. def _surface_escape(self, code, char):
  68. """Return *code* with *char* escaped as an HTML entity.
  69. The main use of this is to escape pipes (``|``) or equal signs (``=``)
  70. in parameter names or values so they are not mistaken for new
  71. parameters.
  72. """
  73. replacement = HTMLEntity(value=ord(char))
  74. for node in code.filter_text(recursive=False):
  75. if char in node:
  76. code.replace(node, node.replace(char, replacement))
  77. def _blank_param_value(self, value):
  78. """Remove the content from *value* while keeping its whitespace.
  79. Replace *value*\ 's nodes with two text nodes, the first containing
  80. whitespace from before its content and the second containing whitespace
  81. from after its content.
  82. """
  83. match = re.search(r"^(\s*).*?(\s*)$", str(value), FLAGS)
  84. value.nodes = [Text(match.group(1)), Text(match.group(2))]
  85. def _select_theory(self, theories):
  86. """Return the most likely spacing convention given different options.
  87. Given a dictionary of convention options as keys and their occurance as
  88. values, return the convention that occurs the most, or ``None`` if
  89. there is no clear preferred style.
  90. """
  91. if theories:
  92. values = tuple(theories.values())
  93. best = max(values)
  94. confidence = float(best) / sum(values)
  95. if confidence > 0.75:
  96. return tuple(theories.keys())[values.index(best)]
  97. def _get_spacing_conventions(self, use_names):
  98. """Try to determine the whitespace conventions for parameters.
  99. This will examine the existing parameters and use
  100. :py:meth:`_select_theory` to determine if there are any preferred
  101. styles for how much whitespace to put before or after the value.
  102. """
  103. before_theories = defaultdict(lambda: 0)
  104. after_theories = defaultdict(lambda: 0)
  105. for param in self.params:
  106. if use_names:
  107. component = str(param.name)
  108. else:
  109. component = str(param.value)
  110. match = re.search(r"^(\s*).*?(\s*)$", component, FLAGS)
  111. before, after = match.group(1), match.group(2)
  112. before_theories[before] += 1
  113. after_theories[after] += 1
  114. before = self._select_theory(before_theories)
  115. after = self._select_theory(after_theories)
  116. return before, after
  117. def _remove_with_field(self, param, i, name):
  118. """Return True if a parameter name should be kept, otherwise False."""
  119. if param.showkey:
  120. following = self.params[i+1:]
  121. better_matches = [after.name.strip() == name and not after.showkey for after in following]
  122. if any(better_matches):
  123. return False
  124. return True
  125. def _remove_without_field(self, param, i, force_no_field):
  126. """Return False if a parameter name should be kept, otherwise True."""
  127. if not param.showkey and not force_no_field:
  128. dependents = [not after.showkey for after in self.params[i+1:]]
  129. if any(dependents):
  130. return False
  131. return True
  132. @property
  133. def name(self):
  134. """The name of the template, as a :py:class:`~.Wikicode` object."""
  135. return self._name
  136. @property
  137. def params(self):
  138. """The list of parameters contained within the template."""
  139. return self._params
  140. @name.setter
  141. def name(self, value):
  142. self._name = parse_anything(value)
  143. def has_param(self, name, ignore_empty=True):
  144. """Return ``True`` if any parameter in the template is named *name*.
  145. With *ignore_empty*, ``False`` will be returned even if the template
  146. contains a parameter with the name *name*, if the parameter's value
  147. is empty. Note that a template may have multiple parameters with the
  148. same name.
  149. """
  150. name = name.strip() if isinstance(name, basestring) else str(name)
  151. for param in self.params:
  152. if param.name.strip() == name:
  153. if ignore_empty and not param.value.strip():
  154. continue
  155. return True
  156. return False
  157. def get(self, name):
  158. """Get the parameter whose name is *name*.
  159. The returned object is a
  160. :py:class:`~.Parameter` instance. Raises :py:exc:`ValueError` if no
  161. parameter has this name. Since multiple parameters can have the same
  162. name, we'll return the last match, since the last parameter is the only
  163. one read by the MediaWiki parser.
  164. """
  165. name = name.strip() if isinstance(name, basestring) else str(name)
  166. for param in reversed(self.params):
  167. if param.name.strip() == name:
  168. return param
  169. raise ValueError(name)
  170. def add(self, name, value, showkey=None, force_nonconformity=False):
  171. """Add a parameter to the template with a given *name* and *value*.
  172. *name* and *value* can be anything parasable by
  173. :py:func:`.utils.parse_anything`; pipes (and equal signs, if
  174. appropriate) are automatically escaped from *value* where applicable.
  175. If *showkey* is given, this will determine whether or not to show the
  176. parameter's name (e.g., ``{{foo|bar}}``'s parameter has a name of
  177. ``"1"`` but it is hidden); otherwise, we'll make a safe and intelligent
  178. guess. If *name* is already a parameter, we'll replace its value while
  179. keeping the same spacing rules unless *force_nonconformity* is
  180. ``True``. We will also try to guess the dominant spacing convention
  181. when adding a new parameter using :py:meth:`_get_spacing_conventions`
  182. unless *force_nonconformity* is ``True``.
  183. """
  184. name, value = parse_anything(name), parse_anything(value)
  185. self._surface_escape(value, "|")
  186. if self.has_param(name):
  187. self.remove(name, keep_field=True)
  188. existing = self.get(name)
  189. if showkey is not None:
  190. if not showkey:
  191. self._surface_escape(value, "=")
  192. existing.showkey = showkey
  193. nodes = existing.value.nodes
  194. if force_nonconformity:
  195. existing.value = value
  196. else:
  197. existing.value = parse_anything([nodes[0], value, nodes[1]])
  198. return existing
  199. if showkey is None:
  200. try:
  201. int_name = int(str(name))
  202. except ValueError:
  203. showkey = True
  204. else:
  205. int_keys = set()
  206. for param in self.params:
  207. if not param.showkey:
  208. if re.match(r"[1-9][0-9]*$", param.name.strip()):
  209. int_keys.add(int(str(param.name)))
  210. expected = min(set(range(1, len(int_keys) + 2)) - int_keys)
  211. if expected == int_name:
  212. showkey = False
  213. else:
  214. showkey = True
  215. if not showkey:
  216. self._surface_escape(value, "=")
  217. if not force_nonconformity:
  218. before_n, after_n = self._get_spacing_conventions(use_names=True)
  219. if before_n and after_n:
  220. name = parse_anything([before_n, name, after_n])
  221. elif before_n:
  222. name = parse_anything([before_n, name])
  223. elif after_n:
  224. name = parse_anything([name, after_n])
  225. before_v, after_v = self._get_spacing_conventions(use_names=False)
  226. if before_v and after_v:
  227. value = parse_anything([before_v, value, after_v])
  228. elif before_v:
  229. value = parse_anything([before_v, value])
  230. elif after_v:
  231. value = parse_anything([value, after_v])
  232. param = Parameter(name, value, showkey)
  233. self.params.append(param)
  234. return param
  235. def remove(self, name, keep_field=False, force_no_field=False):
  236. """Remove a parameter from the template whose name is *name*.
  237. If *keep_field* is ``True``, we will keep the parameter's name, but
  238. blank its value. Otherwise, we will remove the parameter completely
  239. *unless* other parameters are dependent on it (e.g. removing ``bar``
  240. from ``{{foo|bar|baz}}`` is unsafe because ``{{foo|baz}}`` is not what
  241. we expected, so ``{{foo||baz}}`` will be produced instead), unless
  242. *force_no_field* is also ``True``. If the parameter shows up multiple
  243. times in the template, we will remove all instances of it (and keep
  244. one if *keep_field* is ``True`` - that being the first instance if
  245. none of the instances have dependents, otherwise that instance will be
  246. kept).
  247. """
  248. name = name.strip() if isinstance(name, basestring) else str(name)
  249. removed = False
  250. for i, param in enumerate(self.params):
  251. if param.name.strip() == name:
  252. if keep_field:
  253. if self._remove_with_field(param, i, name):
  254. self._blank_param_value(param.value)
  255. keep_field = False
  256. else:
  257. self.params.remove(param)
  258. else:
  259. if self._remove_without_field(param, i, force_no_field):
  260. self.params.remove(param)
  261. else:
  262. self._blank_param_value(param.value)
  263. if not removed:
  264. removed = True
  265. if not removed:
  266. raise ValueError(name)