A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

90 wiersze
3.0 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. from . import Node
  24. from ..compat import str
  25. from ..utils import parse_anything
  26. __all__ = ["Argument"]
  27. class Argument(Node):
  28. """Represents a template argument substitution, like ``{{{foo}}}``."""
  29. def __init__(self, name, default=None):
  30. super(Argument, self).__init__()
  31. self.name = name
  32. self.default = default
  33. def __unicode__(self):
  34. start = "{{{" + str(self.name)
  35. if self.default is not None:
  36. return start + "|" + str(self.default) + "}}}"
  37. return start + "}}}"
  38. def __children__(self):
  39. yield self.name
  40. if self.default is not None:
  41. yield self.default
  42. def __strip__(self, **kwargs):
  43. if self.default is not None:
  44. return self.default.strip_code(**kwargs)
  45. return None
  46. def __showtree__(self, write, get, mark):
  47. write("{{{")
  48. get(self.name)
  49. if self.default is not None:
  50. write(" | ")
  51. mark()
  52. get(self.default)
  53. write("}}}")
  54. @property
  55. def name(self):
  56. """The name of the argument to substitute."""
  57. return self._name
  58. @property
  59. def default(self):
  60. """The default value to substitute if none is passed.
  61. This will be ``None`` if the argument wasn't defined with one. The
  62. MediaWiki parser handles this by rendering the argument itself in the
  63. result, complete braces. To have the argument render as nothing, set
  64. default to ``""`` (``{{{arg}}}`` vs. ``{{{arg|}}}``).
  65. """
  66. return self._default
  67. @name.setter
  68. def name(self, value):
  69. self._name = parse_anything(value)
  70. @default.setter
  71. def default(self, default):
  72. if default is None:
  73. self._default = None
  74. else:
  75. self._default = parse_anything(default)