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.
 
 
 
 

87 lines
2.9 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2019 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. import re
  24. from ...compat import str
  25. from ...string_mixin import StringMixIn
  26. from ...utils import parse_anything
  27. __all__ = ["Parameter"]
  28. class Parameter(StringMixIn):
  29. """Represents a paramater of a template.
  30. For example, the template ``{{foo|bar|spam=eggs}}`` contains two
  31. Parameters: one whose name is ``"1"``, value is ``"bar"``, and ``showkey``
  32. is ``False``, and one whose name is ``"spam"``, value is ``"eggs"``, and
  33. ``showkey`` is ``True``.
  34. """
  35. def __init__(self, name, value, showkey=True):
  36. super(Parameter, self).__init__()
  37. self.name = name
  38. self.value = value
  39. self.showkey = showkey
  40. def __unicode__(self):
  41. if self.showkey:
  42. return str(self.name) + "=" + str(self.value)
  43. return str(self.value)
  44. @staticmethod
  45. def can_hide_key(key):
  46. """Return whether or not the given key can be hidden."""
  47. return re.match(r"[1-9][0-9]*$", str(key).strip())
  48. @property
  49. def name(self):
  50. """The name of the parameter as a :class:`.Wikicode` object."""
  51. return self._name
  52. @property
  53. def value(self):
  54. """The value of the parameter as a :class:`.Wikicode` object."""
  55. return self._value
  56. @property
  57. def showkey(self):
  58. """Whether to show the parameter's key (i.e., its "name")."""
  59. return self._showkey
  60. @name.setter
  61. def name(self, newval):
  62. self._name = parse_anything(newval)
  63. @value.setter
  64. def value(self, newval):
  65. self._value = parse_anything(newval)
  66. @showkey.setter
  67. def showkey(self, newval):
  68. newval = bool(newval)
  69. if not newval and not self.can_hide_key(self.name):
  70. raise ValueError("parameter key {!r} cannot be hidden".format(
  71. self.name))
  72. self._showkey = newval