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.4 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
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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__(self)
  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("^(\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("^(\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. @property
  90. def name(self):
  91. return self._name
  92. @property
  93. def params(self):
  94. return self._params
  95. def has_param(self, name, ignore_empty=True):
  96. name = name.strip() if isinstance(name, basestring) else unicode(name)
  97. for param in self.params:
  98. if param.name.strip() == name:
  99. if ignore_empty and not param.value.strip():
  100. continue
  101. return True
  102. return False
  103. def get(self, name):
  104. name = name.strip() if isinstance(name, basestring) else unicode(name)
  105. for param in self.params:
  106. if param.name.strip() == name:
  107. return param
  108. raise ValueError(name)
  109. def add(self, name, value, showkey=None, force_nonconformity=False):
  110. name, value = parse_anything(name), parse_anything(value)
  111. self._surface_escape(value, "|")
  112. if self.has_param(name):
  113. self.remove(name, keep_field=True)
  114. existing = self.get(name)
  115. if showkey is None: # Infer showkey from current value
  116. showkey = existing.showkey
  117. if not showkey:
  118. self._surface_escape(value, "=")
  119. nodes = existing.value.nodes
  120. if force_nonconformity:
  121. existing.value = value
  122. else:
  123. existing.value = parse_anything([nodes[0], value, nodes[1]])
  124. return existing
  125. if showkey is None:
  126. try:
  127. int(name)
  128. showkey = True
  129. except ValueError:
  130. showkey = False
  131. if not showkey:
  132. self._surface_escape(value, "=")
  133. if not force_nonconformity:
  134. before, after = self._get_spacing_conventions()
  135. if before and after:
  136. value = parse_anything([before, value, after])
  137. elif before:
  138. value = parse_anything([before, value])
  139. elif after:
  140. value = parse_anything([value, after])
  141. param = Parameter(name, value, showkey)
  142. self.params.append(param)
  143. return param
  144. def remove(self, name, keep_field=False, force_no_field=False):
  145. name = name.strip() if isinstance(name, basestring) else unicode(name)
  146. for i, param in enumerate(self.params):
  147. if param.name.strip() == name:
  148. if keep_field:
  149. return self._blank_param_value(param.value)
  150. dependent = [not after.showkey for after in self.params[i+1:]]
  151. if any(dependent) and not param.showkey and not force_no_field:
  152. return self._blank_param_value(param.value)
  153. return self.params.remove(param)
  154. raise ValueError(name)