A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

148 rindas
5.1 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2016 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. from __future__ import unicode_literals
  23. from ...compat import str
  24. from ...string_mixin import StringMixIn
  25. from ...utils import parse_anything
  26. __all__ = ["Attribute"]
  27. class Attribute(StringMixIn):
  28. """Represents an attribute of an HTML tag.
  29. This is used by :class:`.Tag` objects. For example, the tag
  30. ``<ref name="foo">`` contains an Attribute whose name is ``"name"`` and
  31. whose value is ``"foo"``.
  32. """
  33. def __init__(self, name, value=None, quotes='"', pad_first=" ",
  34. pad_before_eq="", pad_after_eq="", check_quotes=True):
  35. super(Attribute, self).__init__()
  36. if check_quotes and not quotes and self._value_needs_quotes(value):
  37. raise ValueError("given value {!r} requires quotes".format(value))
  38. self._name = name
  39. self._value = value
  40. self._quotes = quotes
  41. self._pad_first = pad_first
  42. self._pad_before_eq = pad_before_eq
  43. self._pad_after_eq = pad_after_eq
  44. def __unicode__(self):
  45. result = self.pad_first + str(self.name) + self.pad_before_eq
  46. if self.value is not None:
  47. result += "=" + self.pad_after_eq
  48. if self.quotes:
  49. return result + self.quotes + str(self.value) + self.quotes
  50. return result + str(self.value)
  51. return result
  52. @staticmethod
  53. def _value_needs_quotes(val):
  54. """Return the preferred quotes for the given value, or None."""
  55. if val and any(char.isspace() for char in val):
  56. return ('"' in val and "'" in val) or ("'" if '"' in val else '"')
  57. return None
  58. def _set_padding(self, attr, value):
  59. """Setter for the value of a padding attribute."""
  60. if not value:
  61. setattr(self, attr, "")
  62. else:
  63. value = str(value)
  64. if not value.isspace():
  65. raise ValueError("padding must be entirely whitespace")
  66. setattr(self, attr, value)
  67. @staticmethod
  68. def coerce_quotes(quotes):
  69. """Coerce a quote type into an acceptable value, or raise an error."""
  70. orig, quotes = quotes, str(quotes) if quotes else None
  71. if quotes not in [None, '"', "'"]:
  72. raise ValueError("{!r} is not a valid quote type".format(orig))
  73. return quotes
  74. @property
  75. def name(self):
  76. """The name of the attribute as a :class:`.Wikicode` object."""
  77. return self._name
  78. @property
  79. def value(self):
  80. """The value of the attribute as a :class:`.Wikicode` object."""
  81. return self._value
  82. @property
  83. def quotes(self):
  84. """How to enclose the attribute value. ``"``, ``'``, or ``None``."""
  85. return self._quotes
  86. @property
  87. def pad_first(self):
  88. """Spacing to insert right before the attribute."""
  89. return self._pad_first
  90. @property
  91. def pad_before_eq(self):
  92. """Spacing to insert right before the equal sign."""
  93. return self._pad_before_eq
  94. @property
  95. def pad_after_eq(self):
  96. """Spacing to insert right after the equal sign."""
  97. return self._pad_after_eq
  98. @name.setter
  99. def name(self, value):
  100. self._name = parse_anything(value)
  101. @value.setter
  102. def value(self, newval):
  103. if newval is None:
  104. self._value = None
  105. else:
  106. code = parse_anything(newval)
  107. quotes = self._value_needs_quotes(code)
  108. if quotes in ['"', "'"] or (quotes is True and not self.quotes):
  109. self._quotes = quotes
  110. self._value = code
  111. @quotes.setter
  112. def quotes(self, value):
  113. value = self.coerce_quotes(value)
  114. if not value and self._value_needs_quotes(self.value):
  115. raise ValueError("attribute value requires quotes")
  116. self._quotes = value
  117. @pad_first.setter
  118. def pad_first(self, value):
  119. self._set_padding("_pad_first", value)
  120. @pad_before_eq.setter
  121. def pad_before_eq(self, value):
  122. self._set_padding("_pad_before_eq", value)
  123. @pad_after_eq.setter
  124. def pad_after_eq(self, value):
  125. self._set_padding("_pad_after_eq", value)