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.
 
 
 
 

311 lines
9.2 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. """
  23. This module contains the :py:class:`~.StringMixIn` type, which implements the
  24. interface for the ``unicode`` type (``str`` on py3k) in a dynamic manner.
  25. """
  26. from __future__ import unicode_literals
  27. from .compat import py3k, str
  28. __all__ = ["StringMixIn"]
  29. def inheritdoc(method):
  30. """Set __doc__ of *method* to __doc__ of *method* in its parent class.
  31. Since this is used on :py:class:`~.StringMixIn`, the "parent class" used is
  32. ``str``. This function can be used as a decorator.
  33. """
  34. method.__doc__ = getattr(str, method.__name__).__doc__
  35. return method
  36. class StringMixIn(object):
  37. """Implement the interface for ``unicode``/``str`` in a dynamic manner.
  38. To use this class, inherit from it and override the :py:meth:`__unicode__`
  39. method (same on py3k) to return the string representation of the object.
  40. The various string methods will operate on the value of
  41. :py:meth:`__unicode__` instead of the immutable ``self`` like the regular
  42. ``str`` type.
  43. """
  44. if py3k:
  45. def __str__(self):
  46. return self.__unicode__()
  47. def __bytes__(self):
  48. return self.__unicode__().encode("utf8")
  49. else:
  50. def __str__(self):
  51. return self.__unicode__().encode("utf8")
  52. def __unicode__(self):
  53. raise NotImplementedError()
  54. def __repr__(self):
  55. return repr(self.__unicode__())
  56. def __lt__(self, other):
  57. if isinstance(other, StringMixIn):
  58. return self.__unicode__() < other.__unicode__()
  59. return self.__unicode__() < other
  60. def __le__(self, other):
  61. if isinstance(other, StringMixIn):
  62. return self.__unicode__() <= other.__unicode__()
  63. return self.__unicode__() <= other
  64. def __eq__(self, other):
  65. if isinstance(other, StringMixIn):
  66. return self.__unicode__() == other.__unicode__()
  67. return self.__unicode__() == other
  68. def __ne__(self, other):
  69. if isinstance(other, StringMixIn):
  70. return self.__unicode__() != other.__unicode__()
  71. return self.__unicode__() != other
  72. def __gt__(self, other):
  73. if isinstance(other, StringMixIn):
  74. return self.__unicode__() > other.__unicode__()
  75. return self.__unicode__() > other
  76. def __ge__(self, other):
  77. if isinstance(other, StringMixIn):
  78. return self.__unicode__() >= other.__unicode__()
  79. return self.__unicode__() >= other
  80. if py3k:
  81. def __bool__(self):
  82. return bool(self.__unicode__())
  83. else:
  84. def __nonzero__(self):
  85. return bool(self.__unicode__())
  86. def __len__(self):
  87. return len(self.__unicode__())
  88. def __iter__(self):
  89. for char in self.__unicode__():
  90. yield char
  91. def __getitem__(self, key):
  92. return self.__unicode__()[key]
  93. def __contains__(self, item):
  94. if isinstance(item, StringMixIn):
  95. return str(item) in self.__unicode__()
  96. return item in self.__unicode__()
  97. @inheritdoc
  98. def capitalize(self):
  99. return self.__unicode__().capitalize()
  100. @inheritdoc
  101. def center(self, width, fillchar=None):
  102. if fillchar is None:
  103. return self.__unicode__().center(width)
  104. return self.__unicode__().center(width, fillchar)
  105. @inheritdoc
  106. def count(self, sub, start=None, end=None):
  107. return self.__unicode__().count(sub, start, end)
  108. if not py3k:
  109. @inheritdoc
  110. def decode(self, encoding=None, errors=None):
  111. if errors is None:
  112. if encoding is None:
  113. return self.__unicode__().decode()
  114. return self.__unicode__().decode(encoding)
  115. return self.__unicode__().decode(encoding, errors)
  116. @inheritdoc
  117. def encode(self, encoding=None, errors=None):
  118. if errors is None:
  119. if encoding is None:
  120. return self.__unicode__().encode()
  121. return self.__unicode__().encode(encoding)
  122. return self.__unicode__().encode(encoding, errors)
  123. @inheritdoc
  124. def endswith(self, prefix, start=None, end=None):
  125. return self.__unicode__().endswith(prefix, start, end)
  126. @inheritdoc
  127. def expandtabs(self, tabsize=None):
  128. if tabsize is None:
  129. return self.__unicode__().expandtabs()
  130. return self.__unicode__().expandtabs(tabsize)
  131. @inheritdoc
  132. def find(self, sub, start=None, end=None):
  133. return self.__unicode__().find(sub, start, end)
  134. @inheritdoc
  135. def format(self, *args, **kwargs):
  136. return self.__unicode__().format(*args, **kwargs)
  137. @inheritdoc
  138. def index(self, sub, start=None, end=None):
  139. return self.__unicode__().index(sub, start, end)
  140. @inheritdoc
  141. def isalnum(self):
  142. return self.__unicode__().isalnum()
  143. @inheritdoc
  144. def isalpha(self):
  145. return self.__unicode__().isalpha()
  146. @inheritdoc
  147. def isdecimal(self):
  148. return self.__unicode__().isdecimal()
  149. @inheritdoc
  150. def isdigit(self):
  151. return self.__unicode__().isdigit()
  152. @inheritdoc
  153. def islower(self):
  154. return self.__unicode__().islower()
  155. @inheritdoc
  156. def isnumeric(self):
  157. return self.__unicode__().isnumeric()
  158. @inheritdoc
  159. def isspace(self):
  160. return self.__unicode__().isspace()
  161. @inheritdoc
  162. def istitle(self):
  163. return self.__unicode__().istitle()
  164. @inheritdoc
  165. def isupper(self):
  166. return self.__unicode__().isupper()
  167. @inheritdoc
  168. def join(self, iterable):
  169. return self.__unicode__().join(iterable)
  170. @inheritdoc
  171. def ljust(self, width, fillchar=None):
  172. if fillchar is None:
  173. return self.__unicode__().ljust(width)
  174. return self.__unicode__().ljust(width, fillchar)
  175. @inheritdoc
  176. def lower(self):
  177. return self.__unicode__().lower()
  178. @inheritdoc
  179. def lstrip(self, chars=None):
  180. return self.__unicode__().lstrip(chars)
  181. @inheritdoc
  182. def partition(self, sep):
  183. return self.__unicode__().partition(sep)
  184. @inheritdoc
  185. def replace(self, old, new, count):
  186. return self.__unicode__().replace(old, new, count)
  187. @inheritdoc
  188. def rfind(self, sub, start=None, end=None):
  189. return self.__unicode__().rfind(sub, start, end)
  190. @inheritdoc
  191. def rindex(self, sub, start=None, end=None):
  192. return self.__unicode__().rindex(sub, start, end)
  193. @inheritdoc
  194. def rjust(self, width, fillchar=None):
  195. if fillchar is None:
  196. return self.__unicode__().rjust(width)
  197. return self.__unicode__().rjust(width, fillchar)
  198. @inheritdoc
  199. def rpartition(self, sep):
  200. return self.__unicode__().rpartition(sep)
  201. @inheritdoc
  202. def rsplit(self, sep=None, maxsplit=None):
  203. if maxsplit is None:
  204. if sep is None:
  205. return self.__unicode__().rsplit()
  206. return self.__unicode__().rsplit(sep)
  207. return self.__unicode__().rsplit(sep, maxsplit)
  208. @inheritdoc
  209. def rstrip(self, chars=None):
  210. return self.__unicode__().rstrip(chars)
  211. @inheritdoc
  212. def split(self, sep=None, maxsplit=None):
  213. if maxsplit is None:
  214. if sep is None:
  215. return self.__unicode__().split()
  216. return self.__unicode__().split(sep)
  217. return self.__unicode__().split(sep, maxsplit)
  218. @inheritdoc
  219. def splitlines(self, keepends=None):
  220. if keepends is None:
  221. return self.__unicode__().splitlines()
  222. return self.__unicode__().splitlines(keepends)
  223. @inheritdoc
  224. def startswith(self, prefix, start=None, end=None):
  225. return self.__unicode__().startswith(prefix, start, end)
  226. @inheritdoc
  227. def strip(self, chars=None):
  228. return self.__unicode__().strip(chars)
  229. @inheritdoc
  230. def swapcase(self):
  231. return self.__unicode__().swapcase()
  232. @inheritdoc
  233. def title(self):
  234. return self.__unicode__().title()
  235. @inheritdoc
  236. def translate(self, table):
  237. return self.__unicode__().translate(table)
  238. @inheritdoc
  239. def upper(self):
  240. return self.__unicode__().upper()
  241. @inheritdoc
  242. def zfill(self, width):
  243. return self.__unicode__().zfill(width)
  244. del inheritdoc