A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

163 líneas
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. def wrap(L):
  34. return Wikicode(SmartList(L))
  35. def wraptext(*args):
  36. return wrap([Text(t) for t in args])
  37. def _assert_node_equal(expected, actual):
  38. """Assert that two Nodes have the same type and have the same data."""
  39. registry = {
  40. Argument: _assert_argument_node_equal,
  41. Comment: _assert_comment_node_equal,
  42. ExternalLink: _assert_external_link_node_equal,
  43. Heading: _assert_heading_node_equal,
  44. HTMLEntity: _assert_html_entity_node_equal,
  45. Tag: _assert_tag_node_equal,
  46. Template: _assert_template_node_equal,
  47. Text: _assert_text_node_equal,
  48. Wikilink: _assert_wikilink_node_equal,
  49. }
  50. assert type(expected) is type(actual)
  51. registry[type(expected)](expected, actual)
  52. def _assert_argument_node_equal(expected, actual):
  53. """Assert that two Argument nodes have the same data."""
  54. assert_wikicode_equal(expected.name, actual.name)
  55. if expected.default is not None:
  56. assert_wikicode_equal(expected.default, actual.default)
  57. else:
  58. assert actual.default is None
  59. def _assert_comment_node_equal(expected, actual):
  60. """Assert that two Comment nodes have the same data."""
  61. assert expected.contents == actual.contents
  62. def _assert_external_link_node_equal(expected, actual):
  63. """Assert that two ExternalLink nodes have the same data."""
  64. assert_wikicode_equal(expected.url, actual.url)
  65. if expected.title is not None:
  66. assert_wikicode_equal(expected.title, actual.title)
  67. else:
  68. assert actual.title is None
  69. assert expected.brackets is actual.brackets
  70. assert expected.suppress_space is actual.suppress_space
  71. def _assert_heading_node_equal(expected, actual):
  72. """Assert that two Heading nodes have the same data."""
  73. assert_wikicode_equal(expected.title, actual.title)
  74. assert expected.level == actual.level
  75. def _assert_html_entity_node_equal(expected, actual):
  76. """Assert that two HTMLEntity nodes have the same data."""
  77. assert expected.value == actual.value
  78. assert expected.named is actual.named
  79. assert expected.hexadecimal is actual.hexadecimal
  80. assert expected.hex_char == actual.hex_char
  81. def _assert_tag_node_equal(expected, actual):
  82. """Assert that two Tag nodes have the same data."""
  83. assert_wikicode_equal(expected.tag, actual.tag)
  84. if expected.contents is not None:
  85. assert_wikicode_equal(expected.contents, actual.contents)
  86. else:
  87. assert actual.contents is None
  88. length = len(expected.attributes)
  89. assert length == len(actual.attributes)
  90. for i in range(length):
  91. exp_attr = expected.attributes[i]
  92. act_attr = actual.attributes[i]
  93. assert_wikicode_equal(exp_attr.name, act_attr.name)
  94. if exp_attr.value is not None:
  95. assert_wikicode_equal(exp_attr.value, act_attr.value)
  96. assert exp_attr.quotes == act_attr.quotes
  97. else:
  98. assert act_attr.value is None
  99. assert exp_attr.pad_first == act_attr.pad_first
  100. assert exp_attr.pad_before_eq == act_attr.pad_before_eq
  101. assert exp_attr.pad_after_eq == act_attr.pad_after_eq
  102. assert expected.wiki_markup == actual.wiki_markup
  103. assert expected.self_closing is actual.self_closing
  104. assert expected.invalid is actual.invalid
  105. assert expected.implicit is actual.implicit
  106. assert expected.padding == actual.padding
  107. assert_wikicode_equal(expected.closing_tag, actual.closing_tag)
  108. def _assert_template_node_equal(expected, actual):
  109. """Assert that two Template nodes have the same data."""
  110. assert_wikicode_equal(expected.name, actual.name)
  111. length = len(expected.params)
  112. assert length == len(actual.params)
  113. for i in range(length):
  114. exp_param = expected.params[i]
  115. act_param = actual.params[i]
  116. assert_wikicode_equal(exp_param.name, act_param.name)
  117. assert_wikicode_equal(exp_param.value, act_param.value)
  118. assert exp_param.showkey is act_param.showkey
  119. def _assert_text_node_equal(expected, actual):
  120. """Assert that two Text nodes have the same data."""
  121. assert expected.value == actual.value
  122. def _assert_wikilink_node_equal(expected, actual):
  123. """Assert that two Wikilink nodes have the same data."""
  124. assert_wikicode_equal(expected.title, actual.title)
  125. if expected.text is not None:
  126. assert_wikicode_equal(expected.text, actual.text)
  127. else:
  128. assert actual.text is None
  129. def assert_wikicode_equal(expected, actual):
  130. """Assert that two Wikicode objects have the same data."""
  131. assert isinstance(actual, Wikicode)
  132. length = len(expected.nodes)
  133. assert length == len(actual.nodes)
  134. for i in range(length):
  135. _assert_node_equal(expected.get(i), actual.get(i))