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.
 
 
 
 

285 rivejä
8.2 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. """
  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. return self.__unicode__().center(width, fillchar)
  103. @inheritdoc
  104. def count(self, sub=None, start=None, end=None):
  105. return self.__unicode__().count(sub, start, end)
  106. if not py3k:
  107. @inheritdoc
  108. def decode(self, encoding=None, errors=None):
  109. return self.__unicode__().decode(encoding, errors)
  110. @inheritdoc
  111. def encode(self, encoding=None, errors=None):
  112. return self.__unicode__().encode(encoding, errors)
  113. @inheritdoc
  114. def endswith(self, prefix, start=None, end=None):
  115. return self.__unicode__().endswith(prefix, start, end)
  116. @inheritdoc
  117. def expandtabs(self, tabsize=None):
  118. return self.__unicode__().expandtabs(tabsize)
  119. @inheritdoc
  120. def find(self, sub=None, start=None, end=None):
  121. return self.__unicode__().find(sub, start, end)
  122. @inheritdoc
  123. def format(self, *args, **kwargs):
  124. return self.__unicode__().format(*args, **kwargs)
  125. @inheritdoc
  126. def index(self, sub=None, start=None, end=None):
  127. return self.__unicode__().index(sub, start, end)
  128. @inheritdoc
  129. def isalnum(self):
  130. return self.__unicode__().isalnum()
  131. @inheritdoc
  132. def isalpha(self):
  133. return self.__unicode__().isalpha()
  134. @inheritdoc
  135. def isdecimal(self):
  136. return self.__unicode__().isdecimal()
  137. @inheritdoc
  138. def isdigit(self):
  139. return self.__unicode__().isdigit()
  140. @inheritdoc
  141. def islower(self):
  142. return self.__unicode__().islower()
  143. @inheritdoc
  144. def isnumeric(self):
  145. return self.__unicode__().isnumeric()
  146. @inheritdoc
  147. def isspace(self):
  148. return self.__unicode__().isspace()
  149. @inheritdoc
  150. def istitle(self):
  151. return self.__unicode__().istitle()
  152. @inheritdoc
  153. def isupper(self):
  154. return self.__unicode__().isupper()
  155. @inheritdoc
  156. def join(self, iterable):
  157. return self.__unicode__().join(iterable)
  158. @inheritdoc
  159. def ljust(self, width, fillchar=None):
  160. return self.__unicode__().ljust(width, fillchar)
  161. @inheritdoc
  162. def lower(self):
  163. return self.__unicode__().lower()
  164. @inheritdoc
  165. def lstrip(self, chars=None):
  166. return self.__unicode__().lstrip(chars)
  167. @inheritdoc
  168. def partition(self, sep):
  169. return self.__unicode__().partition(sep)
  170. @inheritdoc
  171. def replace(self, old, new, count):
  172. return self.__unicode__().replace(old, new, count)
  173. @inheritdoc
  174. def rfind(self, sub=None, start=None, end=None):
  175. return self.__unicode__().rfind(sub, start, end)
  176. @inheritdoc
  177. def rindex(self, sub=None, start=None, end=None):
  178. return self.__unicode__().rindex(sub, start, end)
  179. @inheritdoc
  180. def rjust(self, width, fillchar=None):
  181. return self.__unicode__().rjust(width, fillchar)
  182. @inheritdoc
  183. def rpartition(self, sep):
  184. return self.__unicode__().rpartition(sep)
  185. @inheritdoc
  186. def rsplit(self, sep=None, maxsplit=None):
  187. return self.__unicode__().rsplit(sep, maxsplit)
  188. @inheritdoc
  189. def rstrip(self, chars=None):
  190. return self.__unicode__().rstrip(chars)
  191. @inheritdoc
  192. def split(self, sep=None, maxsplit=None):
  193. return self.__unicode__().split(sep, maxsplit)
  194. @inheritdoc
  195. def splitlines(self, keepends=None):
  196. return self.__unicode__().splitlines(keepends)
  197. @inheritdoc
  198. def startswith(self, prefix, start=None, end=None):
  199. return self.__unicode__().startswith(prefix, start, end)
  200. @inheritdoc
  201. def strip(self, chars=None):
  202. return self.__unicode__().strip(chars)
  203. @inheritdoc
  204. def swapcase(self):
  205. return self.__unicode__().swapcase()
  206. @inheritdoc
  207. def title(self):
  208. return self.__unicode__().title()
  209. @inheritdoc
  210. def translate(self, table, deletechars=None):
  211. return self.__unicode__().translate(table, deletechars)
  212. @inheritdoc
  213. def upper(self):
  214. return self.__unicode__().upper()
  215. @inheritdoc
  216. def zfill(self, width):
  217. return self.__unicode__().zfill(width)
  218. del inheritdoc