A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

250 linhas
6.7 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. __all__ = ["StringMixIn"]
  23. def inheritdoc(method):
  24. method.__doc__ = getattr(unicode, method.func_name).__doc__
  25. return method
  26. class StringMixIn(object):
  27. def __str__(self):
  28. return unicode(self).encode("utf8")
  29. def __repr__(self):
  30. return repr(unicode(self))
  31. def __lt__(self, other):
  32. if isinstance(other, StringMixIn):
  33. return unicode(self) < unicode(other)
  34. return unicode(self) < other
  35. def __le__(self, other):
  36. if isinstance(other, StringMixIn):
  37. return unicode(self) <= unicode(other)
  38. return unicode(self) <= other
  39. def __eq__(self, other):
  40. if isinstance(other, StringMixIn):
  41. return unicode(self) == unicode(other)
  42. return unicode(self) == other
  43. def __ne__(self, other):
  44. if isinstance(other, StringMixIn):
  45. return unicode(self) != unicode(other)
  46. return unicode(self) != other
  47. def __gt__(self, other):
  48. if isinstance(other, StringMixIn):
  49. return unicode(self) > unicode(other)
  50. return unicode(self) > other
  51. def __ge__(self, other):
  52. if isinstance(other, StringMixIn):
  53. return unicode(self) >= unicode(other)
  54. return unicode(self) >= other
  55. def __nonzero__(self):
  56. return bool(unicode(self))
  57. def __unicode__(self):
  58. raise NotImplementedError()
  59. def __len__(self):
  60. return len(unicode(self))
  61. def __iter__(self):
  62. for char in unicode(self):
  63. yield char
  64. def __getitem__(self, key):
  65. return unicode(self)[key]
  66. def __contains__(self, item):
  67. if isinstance(item, StringMixIn):
  68. return unicode(item) in unicode(self)
  69. return item in unicode(self)
  70. @inheritdoc
  71. def capitalize(self):
  72. return unicode(self).capitalize()
  73. @inheritdoc
  74. def center(self, width, fillchar=None):
  75. return unicode(self).center(width, fillchar)
  76. @inheritdoc
  77. def count(self, sub=None, start=None, end=None):
  78. return unicode(self).count(sub, start, end)
  79. @inheritdoc
  80. def decode(self, encoding=None, errors=None):
  81. return unicode(self).decode(encoding, errors)
  82. @inheritdoc
  83. def encode(self, encoding=None, errors=None):
  84. return unicode(self).encode(encoding, errors)
  85. @inheritdoc
  86. def endswith(self, prefix, start=None, end=None):
  87. return unicode(self).endswith(prefix, start, end)
  88. @inheritdoc
  89. def expandtabs(self, tabsize=None):
  90. return unicode(self).expandtabs(tabsize)
  91. @inheritdoc
  92. def find(self, sub=None, start=None, end=None):
  93. return unicode(self).find(sub, start, end)
  94. @inheritdoc
  95. def format(self, *args, **kwargs):
  96. return unicode(self).format(*args, **kwargs)
  97. @inheritdoc
  98. def index(self, sub=None, start=None, end=None):
  99. return unicode(self).index(sub, start, end)
  100. @inheritdoc
  101. def isalnum(self):
  102. return unicode(self).isalnum()
  103. @inheritdoc
  104. def isalpha(self):
  105. return unicode(self).isalpha()
  106. @inheritdoc
  107. def isdecimal(self):
  108. return unicode(self).isdecimal()
  109. @inheritdoc
  110. def isdigit(self):
  111. return unicode(self).isdigit()
  112. @inheritdoc
  113. def islower(self):
  114. return unicode(self).islower()
  115. @inheritdoc
  116. def isnumeric(self):
  117. return unicode(self).isnumeric()
  118. @inheritdoc
  119. def isspace(self):
  120. return unicode(self).isspace()
  121. @inheritdoc
  122. def istitle(self):
  123. return unicode(self).istitle()
  124. @inheritdoc
  125. def isupper(self):
  126. return unicode(self).isupper()
  127. @inheritdoc
  128. def join(self, iterable):
  129. return unicode(self).join(iterable)
  130. @inheritdoc
  131. def ljust(self, width, fillchar=None):
  132. return unicode(self).ljust(width, fillchar)
  133. @inheritdoc
  134. def lower(self):
  135. return unicode(self).lower()
  136. @inheritdoc
  137. def lstrip(self, chars=None):
  138. return unicode(self).lstrip(chars)
  139. @inheritdoc
  140. def partition(self, sep):
  141. return unicode(self).partition(sep)
  142. @inheritdoc
  143. def replace(self, old, new, count):
  144. return unicode(self).replace(old, new, count)
  145. @inheritdoc
  146. def rfind(self, sub=None, start=None, end=None):
  147. return unicode(self).rfind(sub, start, end)
  148. @inheritdoc
  149. def rindex(self, sub=None, start=None, end=None):
  150. return unicode(self).rindex(sub, start, end)
  151. @inheritdoc
  152. def rjust(self, width, fillchar=None):
  153. return unicode(self).rjust(width, fillchar)
  154. @inheritdoc
  155. def rpartition(self, sep):
  156. return unicode(self).rpartition(sep)
  157. @inheritdoc
  158. def rsplit(self, sep=None, maxsplit=None):
  159. return unicode(self).rsplit(sep, maxsplit)
  160. @inheritdoc
  161. def rstrip(self, chars=None):
  162. return unicode(self).rstrip(chars)
  163. @inheritdoc
  164. def split(self, sep=None, maxsplit=None):
  165. return unicode(self).split(sep, maxsplit)
  166. @inheritdoc
  167. def splitlines(self, keepends=None):
  168. return unicode(self).splitlines(keepends)
  169. @inheritdoc
  170. def startswith(self, prefix, start=None, end=None):
  171. return unicode(self).startswith(prefix, start, end)
  172. @inheritdoc
  173. def strip(self, chars=None):
  174. return unicode(self).strip(chars)
  175. @inheritdoc
  176. def swapcase(self):
  177. return unicode(self).swapcase()
  178. @inheritdoc
  179. def title(self):
  180. return unicode(self).title()
  181. @inheritdoc
  182. def translate(self, table, deletechars=None):
  183. return unicode(self).translate(table, deletechars)
  184. @inheritdoc
  185. def upper(self):
  186. return unicode(self).upper()
  187. @inheritdoc
  188. def zfill(self, width):
  189. return unicode(self).zfill(width)
  190. del inheritdoc