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.
 
 
 
 

121 lines
4.0 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 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. 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 :py: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, quoted=True, pad_first=" ",
  34. pad_before_eq="", pad_after_eq=""):
  35. super(Attribute, self).__init__()
  36. self._name = name
  37. self._value = value
  38. self._quoted = quoted
  39. self._pad_first = pad_first
  40. self._pad_before_eq = pad_before_eq
  41. self._pad_after_eq = pad_after_eq
  42. def __unicode__(self):
  43. result = self.pad_first + str(self.name) + self.pad_before_eq
  44. if self.value is not None:
  45. result += "=" + self.pad_after_eq
  46. if self.quoted:
  47. return result + '"' + str(self.value) + '"'
  48. return result + str(self.value)
  49. return result
  50. def _set_padding(self, attr, value):
  51. """Setter for the value of a padding attribute."""
  52. if not value:
  53. setattr(self, attr, "")
  54. else:
  55. value = str(value)
  56. if not value.isspace():
  57. raise ValueError("padding must be entirely whitespace")
  58. setattr(self, attr, value)
  59. @property
  60. def name(self):
  61. """The name of the attribute as a :py:class:`~.Wikicode` object."""
  62. return self._name
  63. @property
  64. def value(self):
  65. """The value of the attribute as a :py:class:`~.Wikicode` object."""
  66. return self._value
  67. @property
  68. def quoted(self):
  69. """Whether the attribute's value is quoted with double quotes."""
  70. return self._quoted
  71. @property
  72. def pad_first(self):
  73. """Spacing to insert right before the attribute."""
  74. return self._pad_first
  75. @property
  76. def pad_before_eq(self):
  77. """Spacing to insert right before the equal sign."""
  78. return self._pad_before_eq
  79. @property
  80. def pad_after_eq(self):
  81. """Spacing to insert right after the equal sign."""
  82. return self._pad_after_eq
  83. @name.setter
  84. def name(self, value):
  85. self._name = parse_anything(value)
  86. @value.setter
  87. def value(self, newval):
  88. self._value = None if newval is None else parse_anything(newval)
  89. @quoted.setter
  90. def quoted(self, value):
  91. self._quoted = bool(value)
  92. @pad_first.setter
  93. def pad_first(self, value):
  94. self._set_padding("_pad_first", value)
  95. @pad_before_eq.setter
  96. def pad_before_eq(self, value):
  97. self._set_padding("_pad_before_eq", value)
  98. @pad_after_eq.setter
  99. def pad_after_eq(self, value):
  100. self._set_padding("_pad_after_eq", value)