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.

пре 12 година
пре 11 година
пре 12 година
пре 12 година
пре 12 година
пре 11 година
пре 12 година
пре 12 година
пре 12 година
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (C) 2012-2020 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. import re
  21. from ...string_mixin import StringMixIn
  22. from ...utils import parse_anything
  23. __all__ = ["Parameter"]
  24. class Parameter(StringMixIn):
  25. """Represents a paramater of a template.
  26. For example, the template ``{{foo|bar|spam=eggs}}`` contains two
  27. Parameters: one whose name is ``"1"``, value is ``"bar"``, and ``showkey``
  28. is ``False``, and one whose name is ``"spam"``, value is ``"eggs"``, and
  29. ``showkey`` is ``True``.
  30. """
  31. def __init__(self, name, value, showkey=True):
  32. super().__init__()
  33. self.name = name
  34. self.value = value
  35. self.showkey = showkey
  36. def __str__(self):
  37. if self.showkey:
  38. return str(self.name) + "=" + str(self.value)
  39. return str(self.value)
  40. @staticmethod
  41. def can_hide_key(key):
  42. """Return whether or not the given key can be hidden."""
  43. return re.match(r"[1-9][0-9]*$", str(key).strip())
  44. @property
  45. def name(self):
  46. """The name of the parameter as a :class:`.Wikicode` object."""
  47. return self._name
  48. @property
  49. def value(self):
  50. """The value of the parameter as a :class:`.Wikicode` object."""
  51. return self._value
  52. @property
  53. def showkey(self):
  54. """Whether to show the parameter's key (i.e., its "name")."""
  55. return self._showkey
  56. @name.setter
  57. def name(self, newval):
  58. self._name = parse_anything(newval)
  59. @value.setter
  60. def value(self, newval):
  61. self._value = parse_anything(newval)
  62. @showkey.setter
  63. def showkey(self, newval):
  64. newval = bool(newval)
  65. if not newval and not self.can_hide_key(self.name):
  66. raise ValueError("parameter key {!r} cannot be hidden".format(
  67. self.name))
  68. self._showkey = newval