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.
 
 
 
 

50 lines
2.2 KiB

  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.parameter import Parameter
  23. from mwparserfromhell.template import Template
  24. from mwparserfromhell.text import Text
  25. from mwparserfromhell.wikicode import Wikicode
  26. __all__ = ["Parser"]
  27. class Parser(object):
  28. def _tokenize(self, text):
  29. return text
  30. def parse(self, text):
  31. text = u"This is a {{test}} message with a {{template|with|foo={{params}}}}."
  32. node1 = Text(u"This is a ")
  33. node2 = Template(Wikicode([Text(u"test")]))
  34. node3 = Text(u" message with a ")
  35. node4_param1_name = Wikicode([Text(u"1")])
  36. node4_param1_value = Wikicode([Text(u"with")])
  37. node4_param1 = Parameter(node4_param1_name, node4_param1_value, showkey=False)
  38. node4_param2_name = Wikicode([Text(u"foo")])
  39. node4_param2_value = Wikicode([Template(Wikicode([Text(u"params")]))])
  40. node4_param2 = Parameter(node4_param2_name, node4_param2_value, showkey=True)
  41. node4 = Template(Wikicode([Text(u"template")]), [node4_param1, node4_param2])
  42. node5 = Text(u".")
  43. parsed = Wikicode([node1, node2, node3, node4, node5])
  44. return parsed