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.
 
 
 
 

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