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.
 
 
 
 

127 linhas
4.0 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  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 :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 sys import getdefaultencoding
  28. from .compat import bytes, py26, 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 :class:`.StringMixIn`, the "parent class" used is
  33. ``str``. This function can be used as a decorator.
  34. """
  35. method.__doc__ = getattr(str, method.__name__).__doc__
  36. return method
  37. class StringMixIn(object):
  38. """Implement the interface for ``unicode``/``str`` in a dynamic manner.
  39. To use this class, inherit from it and override the :meth:`__unicode__`
  40. method (same on py3k) to return the string representation of the object.
  41. The various string methods will operate on the value of :meth:`__unicode__`
  42. instead of the immutable ``self`` like the regular ``str`` type.
  43. """
  44. if py3k:
  45. def __str__(self):
  46. return self.__unicode__()
  47. def __bytes__(self):
  48. return bytes(self.__unicode__(), getdefaultencoding())
  49. else:
  50. def __str__(self):
  51. return bytes(self.__unicode__())
  52. def __unicode__(self):
  53. raise NotImplementedError()
  54. def __repr__(self):
  55. return repr(self.__unicode__())
  56. def __lt__(self, other):
  57. return self.__unicode__() < other
  58. def __le__(self, other):
  59. return self.__unicode__() <= other
  60. def __eq__(self, other):
  61. return self.__unicode__() == other
  62. def __ne__(self, other):
  63. return self.__unicode__() != other
  64. def __gt__(self, other):
  65. return self.__unicode__() > other
  66. def __ge__(self, other):
  67. return self.__unicode__() >= other
  68. if py3k:
  69. def __bool__(self):
  70. return bool(self.__unicode__())
  71. else:
  72. def __nonzero__(self):
  73. return bool(self.__unicode__())
  74. def __len__(self):
  75. return len(self.__unicode__())
  76. def __iter__(self):
  77. for char in self.__unicode__():
  78. yield char
  79. def __getitem__(self, key):
  80. return self.__unicode__()[key]
  81. def __reversed__(self):
  82. return reversed(self.__unicode__())
  83. def __contains__(self, item):
  84. return str(item) in self.__unicode__()
  85. def __getattr__(self, attr):
  86. return getattr(self.__unicode__(), attr)
  87. if py3k:
  88. maketrans = str.maketrans # Static method can't rely on __getattr__
  89. if py26:
  90. @inheritdoc
  91. def encode(self, encoding=None, errors=None):
  92. if encoding is None:
  93. encoding = getdefaultencoding()
  94. if errors is not None:
  95. return self.__unicode__().encode(encoding, errors)
  96. return self.__unicode__().encode(encoding)
  97. del inheritdoc