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.
 
 
 
 

85 lines
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012 by 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 collections import OrderedDict
  23. __all__ = ["Template"]
  24. class Template(object):
  25. def __init__(self, name, params=None):
  26. self._name = name
  27. self._params = OrderedDict()
  28. if params:
  29. for param in params:
  30. self._params[param.name] = param
  31. def __repr__(self):
  32. paramlist = []
  33. for name, param in self._params.iteritems():
  34. paramlist.append('"{0}": "{1}"'.format(name, str(param)))
  35. params = "{" + ", ".join(paramlist) + "}"
  36. return "Template(name={0}, params={1})".format(self.name, params)
  37. def __eq__(self, other):
  38. if isinstance(other, Template):
  39. return self.name == other.name and self.params == other.params
  40. return self.render() == other
  41. def __ne__(self, other):
  42. if isinstance(other, Template):
  43. return self.name != other.name or self.params != other.params
  44. return self.render() != other
  45. def __getitem__(self, key):
  46. try:
  47. return self._params[key]
  48. except KeyError: # Try lookup by order in param list
  49. return self._params.values()[key]
  50. def __setitem__(self, key, value):
  51. if isinstance(key, int):
  52. if key > len(self._params):
  53. raise IndexError("Index is too large")
  54. elif key == len(self._params): # Simple addition to the end
  55. self._params[key] = value
  56. else: # We'll need to rebuild the OrderedDict
  57. self._params
  58. else:
  59. self._params[key] = value
  60. @property
  61. def name(self):
  62. return self._name
  63. @property
  64. def params(self):
  65. return self._params.values()
  66. def render(self):
  67. params = ""
  68. for param in self.params:
  69. if param.name.isdigit() and "=" not in param.value:
  70. params += "|" + param.value
  71. else:
  72. params += "|" + param.name + "=" + param.value
  73. return "{{" + self.name + params + "}}"