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.
 
 
 
 

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