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.
 
 
 
 

214 lines
7.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 collections import defaultdict
  23. import re
  24. from . import HTMLEntity, Node, Text
  25. from .extras import Parameter
  26. from ..utils import parse_anything
  27. __all__ = ["Template"]
  28. FLAGS = re.DOTALL | re.UNICODE
  29. class Template(Node):
  30. def __init__(self, name, params=None):
  31. super(Template, self).__init__()
  32. self._name = name
  33. if params:
  34. self._params = params
  35. else:
  36. self._params = []
  37. def __unicode__(self):
  38. if self.params:
  39. params = u"|".join([unicode(param) for param in self.params])
  40. return "{{" + unicode(self.name) + "|" + params + "}}"
  41. else:
  42. return "{{" + unicode(self.name) + "}}"
  43. def __iternodes__(self, getter):
  44. yield None, self
  45. for child in getter(self.name):
  46. yield self.name, child
  47. for param in self.params:
  48. if param.showkey:
  49. for child in getter(param.name):
  50. yield param.name, child
  51. for child in getter(param.value):
  52. yield param.value, child
  53. def __showtree__(self, write, get, mark):
  54. write("{{")
  55. get(self.name)
  56. for param in self.params:
  57. write(" | ")
  58. mark()
  59. get(param.name)
  60. write(" = ")
  61. mark()
  62. get(param.value)
  63. write("}}")
  64. def _surface_escape(self, code, char):
  65. replacement = HTMLEntity(value=ord(char))
  66. for node in code.filter_text(recursive=False):
  67. if char in node:
  68. code.replace(node, node.replace(char, replacement))
  69. def _blank_param_value(self, value):
  70. match = re.search(r"^(\s*).*?(\s*)$", unicode(value), FLAGS)
  71. value.nodes = [Text(match.group(1)), Text(match.group(2))]
  72. def _select_theory(self, theories):
  73. if theories:
  74. best = max(theories.values())
  75. confidence = float(best) / sum(theories.values())
  76. if confidence > 0.75:
  77. return theories.keys()[theories.values().index(best)]
  78. def _get_spacing_conventions(self):
  79. before_theories = defaultdict(lambda: 0)
  80. after_theories = defaultdict(lambda: 0)
  81. for param in self.params:
  82. match = re.search(r"^(\s*).*?(\s*)$", unicode(param.value), FLAGS)
  83. before, after = match.group(1), match.group(2)
  84. before_theories[before] += 1
  85. after_theories[after] += 1
  86. before = self._select_theory(before_theories)
  87. after = self._select_theory(after_theories)
  88. return before, after
  89. def _remove_with_field(self, param, i, name):
  90. if param.showkey:
  91. following = self.params[i+1:]
  92. better_matches = [after.name.strip() == name and not after.showkey for after in following]
  93. if any(better_matches):
  94. return False
  95. return True
  96. def _remove_without_field(self, param, i, force_no_field):
  97. if not param.showkey and not force_no_field:
  98. dependents = [not after.showkey for after in self.params[i+1:]]
  99. if any(dependents):
  100. return False
  101. return True
  102. @property
  103. def name(self):
  104. return self._name
  105. @property
  106. def params(self):
  107. return self._params
  108. @name.setter
  109. def name(self, value):
  110. self._name = parse_anything(value)
  111. def has_param(self, name, ignore_empty=True):
  112. name = name.strip() if isinstance(name, basestring) else unicode(name)
  113. for param in self.params:
  114. if param.name.strip() == name:
  115. if ignore_empty and not param.value.strip():
  116. continue
  117. return True
  118. return False
  119. def get(self, name):
  120. name = name.strip() if isinstance(name, basestring) else unicode(name)
  121. for param in reversed(self.params):
  122. if param.name.strip() == name:
  123. return param
  124. raise ValueError(name)
  125. def add(self, name, value, showkey=None, force_nonconformity=False):
  126. name, value = parse_anything(name), parse_anything(value)
  127. self._surface_escape(value, "|")
  128. if self.has_param(name):
  129. self.remove(name, keep_field=True)
  130. existing = self.get(name)
  131. if showkey is not None:
  132. if not showkey:
  133. self._surface_escape(value, "=")
  134. existing.showkey = showkey
  135. nodes = existing.value.nodes
  136. if force_nonconformity:
  137. existing.value = value
  138. else:
  139. existing.value = parse_anything([nodes[0], value, nodes[1]])
  140. return existing
  141. if showkey is None:
  142. try:
  143. int_name = int(unicode(name))
  144. except ValueError:
  145. showkey = True
  146. else:
  147. int_keys = set()
  148. for param in self.params:
  149. if not param.showkey:
  150. if re.match(r"[1-9][0-9]*$", param.name.strip()):
  151. int_keys.add(int(unicode(param.name)))
  152. expected = min(set(range(1, len(int_keys) + 2)) - int_keys)
  153. if expected == int_name:
  154. showkey = False
  155. else:
  156. showkey = True
  157. if not showkey:
  158. self._surface_escape(value, "=")
  159. if not force_nonconformity:
  160. before, after = self._get_spacing_conventions()
  161. if before and after:
  162. value = parse_anything([before, value, after])
  163. elif before:
  164. value = parse_anything([before, value])
  165. elif after:
  166. value = parse_anything([value, after])
  167. param = Parameter(name, value, showkey)
  168. self.params.append(param)
  169. return param
  170. def remove(self, name, keep_field=False, force_no_field=False):
  171. name = name.strip() if isinstance(name, basestring) else unicode(name)
  172. removed = False
  173. for i, param in enumerate(self.params):
  174. if param.name.strip() == name:
  175. if keep_field:
  176. if self._remove_with_field(param, i, name):
  177. self._blank_param_value(param.value)
  178. keep_field = False
  179. else:
  180. self.params.remove(param)
  181. else:
  182. if self._remove_without_field(param, i, force_no_field):
  183. self.params.remove(param)
  184. else:
  185. self._blank_param_value(param.value)
  186. if not removed:
  187. removed = True
  188. if not removed:
  189. raise ValueError(name)