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.
 
 
 
 

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