A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

159 rindas
6.0 KiB

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