A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

96 行
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012 by 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__ = ["Parameter"]
  23. class Parameter(object):
  24. def __init__(self, name, value, templates=None):
  25. self._name = name
  26. self._value = value
  27. if templates:
  28. self._templates = templates
  29. else:
  30. self._templates = []
  31. def __repr__(self):
  32. return repr(self.value)
  33. def __str__(self):
  34. return self.value
  35. def __lt__(self, other):
  36. if isinstance(other, Parameter):
  37. return self.value < other.value
  38. return self.value < other
  39. def __le__(self, other):
  40. if isinstance(other, Parameter):
  41. return self.value <= other.value
  42. return self.value <= other
  43. def __eq__(self, other):
  44. if isinstance(other, Parameter):
  45. return (self.value == other.value and
  46. self.templates == other.templates)
  47. return self.value == other
  48. def __ne__(self, other):
  49. if isinstance(other, Parameter):
  50. return (self.value != other.value or
  51. self.templates != other.templates)
  52. return self.value != other
  53. def __gt__(self, other):
  54. if isinstance(other, Parameter):
  55. return self.value > other.value
  56. return self.value > other
  57. def __ge__(self, other):
  58. if isinstance(other, Parameter):
  59. return self.value >= other.value
  60. return self.value >= other
  61. def __nonzero__(self):
  62. return bool(self.value)
  63. def __len__(self):
  64. return len(self.value)
  65. def __iter__(self):
  66. for char in self.value:
  67. yield char
  68. def __contains__(self, item):
  69. return item in self.value or item in self.templates
  70. @property
  71. def name(self):
  72. return self._name
  73. @property
  74. def value(self):
  75. return self._value
  76. @property
  77. def templates(self):
  78. return self._templates