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.

template.py 4.0 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012 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 mwparserfromhell.nodes import Node
  23. from mwparserfromhell.nodes.extras import Parameter
  24. from mwparserfromhell.utils import parse_anything
  25. __all__ = ["Template"]
  26. class Template(Node):
  27. def __init__(self, name, params=None):
  28. self._name = name
  29. if params:
  30. self._params = params
  31. else:
  32. self._params = []
  33. def __unicode__(self):
  34. if self.params:
  35. params = u"|".join([unicode(param) for param in self.params])
  36. return "{{" + unicode(self.name) + "|" + params + "}}"
  37. else:
  38. return "{{" + unicode(self.name) + "}}"
  39. def _blank_param_value(self, value): # TODO
  40. pass
  41. @property
  42. def name(self):
  43. return self._name
  44. @property
  45. def params(self):
  46. return self._params
  47. def has_param(self, name):
  48. name = name.strip() if isinstance(name, basestring) else unicode(name)
  49. for param in self.params:
  50. if param.name.strip() == name:
  51. return True
  52. return False
  53. def get_param(self, name):
  54. name = name.strip() if isinstance(name, basestring) else unicode(name)
  55. for param in self.params:
  56. if param.name.strip() == name:
  57. return param
  58. raise ValueError(name)
  59. def add_param(self, name, value, showkey=None):
  60. name, value = parse_anything(name), parse_anything(value)
  61. surface_text = value.filter_text(recursive=False)
  62. for node in surface_text:
  63. value.replace(node, node.replace("|", "&#124;"))
  64. if showkey is None:
  65. if any(["=" in node for node in surface_text]):
  66. showkey = True
  67. else:
  68. try:
  69. int(name)
  70. except ValueError:
  71. showkey = False
  72. else:
  73. showkey = True
  74. elif not showkey:
  75. for node in surface_text:
  76. value.replace(node, node.replace("=", "&#124;"))
  77. if self.has_param(name):
  78. self.remove_param(name, keep_field=True)
  79. existing = self.get_param(name).value
  80. self.get_param(name).value = value # CONFORM TO FORMATTING?
  81. else:
  82. self.params.append(Parameter(name, value, showkey)) # CONFORM TO FORMATTING CONVENTIONS?
  83. def remove_param(self, name, keep_field=False): # DON'T MESS UP NUMBERING WITH show_key = False AND keep_field = False
  84. name = name.strip() if isinstance(name, basestring) else unicode(name)
  85. for param in self.params:
  86. if param.name.strip() == name:
  87. if keep_field:
  88. return self._blank_param_value(param.value)
  89. else:
  90. return self.params.remove(param)
  91. raise ValueError(name)