A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

110 lignes
3.6 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:
  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. @property
  50. def name(self):
  51. """The name of the attribute as a :py:class:`~.Wikicode` object."""
  52. return self._name
  53. @property
  54. def value(self):
  55. """The value of the attribute as a :py:class:`~.Wikicode` object."""
  56. return self._value
  57. @property
  58. def quoted(self):
  59. """Whether the attribute's value is quoted with double quotes."""
  60. return self._quoted
  61. @property
  62. def pad_first(self):
  63. """Spacing to insert right before the attribute."""
  64. return self._pad_first
  65. @property
  66. def pad_before_eq(self):
  67. """Spacing to insert right before the equal sign."""
  68. return self._pad_before_eq
  69. @property
  70. def pad_after_eq(self):
  71. """Spacing to insert right after the equal sign."""
  72. return self._pad_after_eq
  73. @name.setter
  74. def name(self, value):
  75. self._name = parse_anything(value)
  76. @value.setter
  77. def value(self, newval):
  78. self._value = parse_anything(newval)
  79. @quoted.setter
  80. def quoted(self, value):
  81. self._quoted = bool(value)
  82. @pad_first.setter
  83. def pad_first(self, value):
  84. self._pad_first = str(value)
  85. @pad_before_eq.setter
  86. def pad_before_eq(self, value):
  87. self._pad_before_eq = str(value)
  88. @pad_after_eq.setter
  89. def pad_after_eq(self, value):
  90. self._pad_after_eq = str(value)