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.
 
 
 
 

134 wiersze
6.1 KiB

  1. #
  2. # Copyright (C) 2012-2019 Ben Kurtovic <ben.kurtovic@gmail.com>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. from mwparserfromhell.nodes import (Argument, Comment, Heading, HTMLEntity,
  22. Tag, Template, Text, Wikilink)
  23. from mwparserfromhell.smart_list import SmartList
  24. from mwparserfromhell.wikicode import Wikicode
  25. wrap = lambda L: Wikicode(SmartList(L))
  26. wraptext = lambda *args: wrap([Text(t) for t in args])
  27. class TreeEqualityTestCase:
  28. """A base test case with support for comparing the equality of node trees.
  29. This adds a number of type equality functions, for Wikicode, Text,
  30. Templates, and Wikilinks.
  31. """
  32. def assertNodeEqual(self, expected, actual):
  33. """Assert that two Nodes have the same type and have the same data."""
  34. registry = {
  35. Argument: self.assertArgumentNodeEqual,
  36. Comment: self.assertCommentNodeEqual,
  37. Heading: self.assertHeadingNodeEqual,
  38. HTMLEntity: self.assertHTMLEntityNodeEqual,
  39. Tag: self.assertTagNodeEqual,
  40. Template: self.assertTemplateNodeEqual,
  41. Text: self.assertTextNodeEqual,
  42. Wikilink: self.assertWikilinkNodeEqual
  43. }
  44. for nodetype in registry:
  45. if isinstance(expected, nodetype):
  46. assert isinstance(actual, nodetype)
  47. registry[nodetype](expected, actual)
  48. def assertArgumentNodeEqual(self, expected, actual):
  49. """Assert that two Argument nodes have the same data."""
  50. self.assertWikicodeEqual(expected.name, actual.name)
  51. if expected.default is not None:
  52. self.assertWikicodeEqual(expected.default, actual.default)
  53. else:
  54. assert actual.default is None
  55. def assertCommentNodeEqual(self, expected, actual):
  56. """Assert that two Comment nodes have the same data."""
  57. assert expected.contents == actual.contents
  58. def assertHeadingNodeEqual(self, expected, actual):
  59. """Assert that two Heading nodes have the same data."""
  60. self.assertWikicodeEqual(expected.title, actual.title)
  61. assert expected.level == actual.level
  62. def assertHTMLEntityNodeEqual(self, expected, actual):
  63. """Assert that two HTMLEntity nodes have the same data."""
  64. assert expected.value == actual.value
  65. assert expected.named is actual.named
  66. assert expected.hexadecimal is actual.hexadecimal
  67. assert expected.hex_char == actual.hex_char
  68. def assertTagNodeEqual(self, expected, actual):
  69. """Assert that two Tag nodes have the same data."""
  70. self.assertWikicodeEqual(expected.tag, actual.tag)
  71. if expected.contents is not None:
  72. self.assertWikicodeEqual(expected.contents, actual.contents)
  73. length = len(expected.attributes)
  74. assert length == len(actual.attributes)
  75. for i in range(length):
  76. exp_attr = expected.attributes[i]
  77. act_attr = actual.attributes[i]
  78. self.assertWikicodeEqual(exp_attr.name, act_attr.name)
  79. if exp_attr.value is not None:
  80. self.assertWikicodeEqual(exp_attr.value, act_attr.value)
  81. assert exp_attr.quotes == act_attr.quotes
  82. assert exp_attr.pad_first == act_attr.pad_first
  83. assert exp_attr.pad_before_eq == act_attr.pad_before_eq
  84. assert exp_attr.pad_after_eq == act_attr.pad_after_eq
  85. assert expected.wiki_markup == actual.wiki_markup
  86. assert expected.self_closing is actual.self_closing
  87. assert expected.invalid is actual.invalid
  88. assert expected.implicit is actual.implicit
  89. assert expected.padding == actual.padding
  90. self.assertWikicodeEqual(expected.closing_tag, actual.closing_tag)
  91. def assertTemplateNodeEqual(self, expected, actual):
  92. """Assert that two Template nodes have the same data."""
  93. self.assertWikicodeEqual(expected.name, actual.name)
  94. length = len(expected.params)
  95. assert length == len(actual.params)
  96. for i in range(length):
  97. exp_param = expected.params[i]
  98. act_param = actual.params[i]
  99. self.assertWikicodeEqual(exp_param.name, act_param.name)
  100. self.assertWikicodeEqual(exp_param.value, act_param.value)
  101. assert exp_param.showkey is act_param.showkey
  102. def assertTextNodeEqual(self, expected, actual):
  103. """Assert that two Text nodes have the same data."""
  104. assert expected.value == actual.value
  105. def assertWikilinkNodeEqual(self, expected, actual):
  106. """Assert that two Wikilink nodes have the same data."""
  107. self.assertWikicodeEqual(expected.title, actual.title)
  108. if expected.text is not None:
  109. self.assertWikicodeEqual(expected.text, actual.text)
  110. else:
  111. assert None is actual.text
  112. def assertWikicodeEqual(self, expected, actual):
  113. """Assert that two Wikicode objects have the same data."""
  114. assert isinstance(actual, Wikicode)
  115. length = len(expected.nodes)
  116. assert length == len(actual.nodes)
  117. for i in range(length):
  118. self.assertNodeEqual(expected.get(i), actual.get(i))