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.

template.py 6.8 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. self._name = name
  32. if params:
  33. self._params = params
  34. else:
  35. self._params = []
  36. def __unicode__(self):
  37. if self.params:
  38. params = u"|".join([unicode(param) for param in self.params])
  39. return "{{" + unicode(self.name) + "|" + params + "}}"
  40. else:
  41. return "{{" + unicode(self.name) + "}}"
  42. def __iternodes__(self, getter):
  43. yield None, self
  44. for child in getter(self.name):
  45. yield self.name, child
  46. for param in self.params:
  47. if param.showkey:
  48. for child in getter(param.name):
  49. yield param.name, child
  50. for child in getter(param.value):
  51. yield param.value, child
  52. def __showtree__(self, write, get, mark):
  53. write("{{")
  54. get(self.name)
  55. for param in self.params:
  56. write(" | ")
  57. mark()
  58. get(param.name)
  59. write(" = ")
  60. mark()
  61. get(param.value)
  62. write("}}")
  63. def _surface_escape(self, code, char):
  64. replacement = HTMLEntity(value=ord(char))
  65. for node in code.filter_text(recursive=False):
  66. if char in node:
  67. code.replace(node, node.replace(char, replacement))
  68. def _blank_param_value(self, value):
  69. match = re.search(r"^(\s*).*?(\s*)$", unicode(value), FLAGS)
  70. value.nodes = [Text(match.group(1)), Text(match.group(2))]
  71. def _select_theory(self, theories):
  72. if theories:
  73. best = max(theories.values())
  74. confidence = float(best) / sum(theories.values())
  75. if confidence > 0.75:
  76. return theories.keys()[theories.values().index(best)]
  77. def _get_spacing_conventions(self):
  78. before_theories = defaultdict(lambda: 0)
  79. after_theories = defaultdict(lambda: 0)
  80. for param in self.params:
  81. match = re.search(r"^(\s*).*?(\s*)$", unicode(param.value), FLAGS)
  82. before, after = match.group(1), match.group(2)
  83. before_theories[before] += 1
  84. after_theories[after] += 1
  85. before = self._select_theory(before_theories)
  86. after = self._select_theory(after_theories)
  87. return before, after
  88. @property
  89. def name(self):
  90. return self._name
  91. @property
  92. def params(self):
  93. return self._params
  94. def has_param(self, name, ignore_empty=True):
  95. name = name.strip() if isinstance(name, basestring) else unicode(name)
  96. for param in self.params:
  97. if param.name.strip() == name:
  98. if ignore_empty and not param.value.strip():
  99. continue
  100. return True
  101. return False
  102. def get(self, name):
  103. name = name.strip() if isinstance(name, basestring) else unicode(name)
  104. for param in reversed(self.params):
  105. if param.name.strip() == name:
  106. return param
  107. raise ValueError(name)
  108. def add(self, name, value, showkey=None, force_nonconformity=False):
  109. name, value = parse_anything(name), parse_anything(value)
  110. self._surface_escape(value, "|")
  111. if self.has_param(name):
  112. self.remove(name, keep_field=True)
  113. existing = self.get(name)
  114. if showkey is not None:
  115. if not showkey:
  116. self._surface_escape(value, "=")
  117. existing.showkey = showkey
  118. nodes = existing.value.nodes
  119. if force_nonconformity:
  120. existing.value = value
  121. else:
  122. existing.value = parse_anything([nodes[0], value, nodes[1]])
  123. return existing
  124. if showkey is None:
  125. try:
  126. int_name = int(unicode(name))
  127. except ValueError:
  128. showkey = True
  129. else:
  130. int_keys = set()
  131. for param in self.params:
  132. if not param.showkey:
  133. if re.match(r"[1-9][0-9]*$", param.name.strip()):
  134. int_keys.add(int(unicode(param.name)))
  135. expected = min(set(range(1, len(int_keys) + 2)) - int_keys)
  136. if expected == int_name:
  137. showkey = False
  138. else:
  139. showkey = True
  140. if not showkey:
  141. self._surface_escape(value, "=")
  142. if not force_nonconformity:
  143. before, after = self._get_spacing_conventions()
  144. if before and after:
  145. value = parse_anything([before, value, after])
  146. elif before:
  147. value = parse_anything([before, value])
  148. elif after:
  149. value = parse_anything([value, after])
  150. param = Parameter(name, value, showkey)
  151. self.params.append(param)
  152. return param
  153. def remove(self, name, keep_field=False, force_no_field=False): # KEEP FIRST FIELD, REMOVE ALL AFTER
  154. name = name.strip() if isinstance(name, basestring) else unicode(name)
  155. for i, param in enumerate(self.params):
  156. if param.name.strip() == name:
  157. if keep_field:
  158. return self._blank_param_value(param.value)
  159. dependent = [not after.showkey for after in self.params[i+1:]]
  160. if any(dependent) and not param.showkey and not force_no_field:
  161. return self._blank_param_value(param.value)
  162. return self.params.remove(param)
  163. raise ValueError(name)