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.
 
 
 
 

248 regels
12 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 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 __future__ import unicode_literals
  23. import unittest
  24. from mwparserfromhell.nodes import (Argument, Comment, Heading, HTMLEntity,
  25. Tag, Template, Text, Wikilink)
  26. from mwparserfromhell.nodes.extras import Attribute, Parameter
  27. from mwparserfromhell.parser import tokens
  28. from mwparserfromhell.parser.builder import Builder
  29. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  30. class TestBuilder(TreeEqualityTestCase):
  31. """Tests for the builder, which turns tokens into Wikicode objects."""
  32. def setUp(self):
  33. self.builder = Builder()
  34. def test_text(self):
  35. """tests for building Text nodes"""
  36. tests = [
  37. ([tokens.Text(text="foobar")], wraptext("foobar")),
  38. ([tokens.Text(text="fóóbar")], wraptext("fóóbar")),
  39. ([tokens.Text(text="spam"), tokens.Text(text="eggs")],
  40. wraptext("spam", "eggs")),
  41. ]
  42. for test, valid in tests:
  43. self.assertWikicodeEqual(valid, self.builder.build(test))
  44. def test_template(self):
  45. """tests for building Template nodes"""
  46. tests = [
  47. ([tokens.TemplateOpen(), tokens.Text(text="foobar"),
  48. tokens.TemplateClose()],
  49. wrap([Template(wraptext("foobar"))])),
  50. ([tokens.TemplateOpen(), tokens.Text(text="spam"),
  51. tokens.Text(text="eggs"), tokens.TemplateClose()],
  52. wrap([Template(wraptext("spam", "eggs"))])),
  53. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  54. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  55. tokens.TemplateClose()],
  56. wrap([Template(wraptext("foo"), params=[
  57. Parameter(wraptext("1"), wraptext("bar"), showkey=False)])])),
  58. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  59. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  60. tokens.TemplateParamEquals(), tokens.Text(text="baz"),
  61. tokens.TemplateClose()],
  62. wrap([Template(wraptext("foo"), params=[
  63. Parameter(wraptext("bar"), wraptext("baz"))])])),
  64. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  65. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  66. tokens.TemplateParamEquals(), tokens.Text(text="baz"),
  67. tokens.TemplateParamSeparator(), tokens.Text(text="biz"),
  68. tokens.TemplateParamSeparator(), tokens.Text(text="buzz"),
  69. tokens.TemplateParamSeparator(), tokens.Text(text="3"),
  70. tokens.TemplateParamEquals(), tokens.Text(text="buff"),
  71. tokens.TemplateParamSeparator(), tokens.Text(text="baff"),
  72. tokens.TemplateClose()],
  73. wrap([Template(wraptext("foo"), params=[
  74. Parameter(wraptext("bar"), wraptext("baz")),
  75. Parameter(wraptext("1"), wraptext("biz"), showkey=False),
  76. Parameter(wraptext("2"), wraptext("buzz"), showkey=False),
  77. Parameter(wraptext("3"), wraptext("buff")),
  78. Parameter(wraptext("3"), wraptext("baff"),
  79. showkey=False)])])),
  80. ]
  81. for test, valid in tests:
  82. self.assertWikicodeEqual(valid, self.builder.build(test))
  83. def test_argument(self):
  84. """tests for building Argument nodes"""
  85. tests = [
  86. ([tokens.ArgumentOpen(), tokens.Text(text="foobar"),
  87. tokens.ArgumentClose()],
  88. wrap([Argument(wraptext("foobar"))])),
  89. ([tokens.ArgumentOpen(), tokens.Text(text="spam"),
  90. tokens.Text(text="eggs"), tokens.ArgumentClose()],
  91. wrap([Argument(wraptext("spam", "eggs"))])),
  92. ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
  93. tokens.ArgumentSeparator(), tokens.Text(text="bar"),
  94. tokens.ArgumentClose()],
  95. wrap([Argument(wraptext("foo"), wraptext("bar"))])),
  96. ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
  97. tokens.Text(text="bar"), tokens.ArgumentSeparator(),
  98. tokens.Text(text="baz"), tokens.Text(text="biz"),
  99. tokens.ArgumentClose()],
  100. wrap([Argument(wraptext("foo", "bar"), wraptext("baz", "biz"))])),
  101. ]
  102. for test, valid in tests:
  103. self.assertWikicodeEqual(valid, self.builder.build(test))
  104. def test_wikilink(self):
  105. """tests for building Wikilink nodes"""
  106. tests = [
  107. ([tokens.WikilinkOpen(), tokens.Text(text="foobar"),
  108. tokens.WikilinkClose()],
  109. wrap([Wikilink(wraptext("foobar"))])),
  110. ([tokens.WikilinkOpen(), tokens.Text(text="spam"),
  111. tokens.Text(text="eggs"), tokens.WikilinkClose()],
  112. wrap([Wikilink(wraptext("spam", "eggs"))])),
  113. ([tokens.WikilinkOpen(), tokens.Text(text="foo"),
  114. tokens.WikilinkSeparator(), tokens.Text(text="bar"),
  115. tokens.WikilinkClose()],
  116. wrap([Wikilink(wraptext("foo"), wraptext("bar"))])),
  117. ([tokens.WikilinkOpen(), tokens.Text(text="foo"),
  118. tokens.Text(text="bar"), tokens.WikilinkSeparator(),
  119. tokens.Text(text="baz"), tokens.Text(text="biz"),
  120. tokens.WikilinkClose()],
  121. wrap([Wikilink(wraptext("foo", "bar"), wraptext("baz", "biz"))])),
  122. ]
  123. for test, valid in tests:
  124. self.assertWikicodeEqual(valid, self.builder.build(test))
  125. def test_html_entity(self):
  126. """tests for building HTMLEntity nodes"""
  127. tests = [
  128. ([tokens.HTMLEntityStart(), tokens.Text(text="nbsp"),
  129. tokens.HTMLEntityEnd()],
  130. wrap([HTMLEntity("nbsp", named=True, hexadecimal=False)])),
  131. ([tokens.HTMLEntityStart(), tokens.HTMLEntityNumeric(),
  132. tokens.Text(text="107"), tokens.HTMLEntityEnd()],
  133. wrap([HTMLEntity("107", named=False, hexadecimal=False)])),
  134. ([tokens.HTMLEntityStart(), tokens.HTMLEntityNumeric(),
  135. tokens.HTMLEntityHex(char="X"), tokens.Text(text="6B"),
  136. tokens.HTMLEntityEnd()],
  137. wrap([HTMLEntity("6B", named=False, hexadecimal=True,
  138. hex_char="X")])),
  139. ]
  140. for test, valid in tests:
  141. self.assertWikicodeEqual(valid, self.builder.build(test))
  142. def test_heading(self):
  143. """tests for building Heading nodes"""
  144. tests = [
  145. ([tokens.HeadingStart(level=2), tokens.Text(text="foobar"),
  146. tokens.HeadingEnd()],
  147. wrap([Heading(wraptext("foobar"), 2)])),
  148. ([tokens.HeadingStart(level=4), tokens.Text(text="spam"),
  149. tokens.Text(text="eggs"), tokens.HeadingEnd()],
  150. wrap([Heading(wraptext("spam", "eggs"), 4)])),
  151. ]
  152. for test, valid in tests:
  153. self.assertWikicodeEqual(valid, self.builder.build(test))
  154. def test_comment(self):
  155. """tests for building Comment nodes"""
  156. tests = [
  157. ([tokens.CommentStart(), tokens.Text(text="foobar"),
  158. tokens.CommentEnd()],
  159. wrap([Comment(wraptext("foobar"))])),
  160. ([tokens.CommentStart(), tokens.Text(text="spam"),
  161. tokens.Text(text="eggs"), tokens.CommentEnd()],
  162. wrap([Comment(wraptext("spam", "eggs"))])),
  163. ]
  164. for test, valid in tests:
  165. self.assertWikicodeEqual(valid, self.builder.build(test))
  166. def test_integration(self):
  167. """a test for building a combination of templates together"""
  168. # {{{{{{{{foo}}bar|baz=biz}}buzz}}usr|{{bin}}}}
  169. test = [tokens.TemplateOpen(), tokens.TemplateOpen(),
  170. tokens.TemplateOpen(), tokens.TemplateOpen(),
  171. tokens.Text(text="foo"), tokens.TemplateClose(),
  172. tokens.Text(text="bar"), tokens.TemplateParamSeparator(),
  173. tokens.Text(text="baz"), tokens.TemplateParamEquals(),
  174. tokens.Text(text="biz"), tokens.TemplateClose(),
  175. tokens.Text(text="buzz"), tokens.TemplateClose(),
  176. tokens.Text(text="usr"), tokens.TemplateParamSeparator(),
  177. tokens.TemplateOpen(), tokens.Text(text="bin"),
  178. tokens.TemplateClose(), tokens.TemplateClose()]
  179. valid = wrap(
  180. [Template(wrap([Template(wrap([Template(wrap([Template(wraptext(
  181. "foo")), Text("bar")]), params=[Parameter(wraptext("baz"),
  182. wraptext("biz"))]), Text("buzz")])), Text("usr")]), params=[
  183. Parameter(wraptext("1"), wrap([Template(wraptext("bin"))]),
  184. showkey=False)])])
  185. self.assertWikicodeEqual(valid, self.builder.build(test))
  186. def test_integration2(self):
  187. """an even more audacious test for building a horrible wikicode mess"""
  188. # {{a|b|{{c|[[d]]{{{e}}}}}}}[[f|{{{g}}}<!--h-->]]{{i|j=&nbsp;}}
  189. test = [tokens.TemplateOpen(), tokens.Text(text="a"),
  190. tokens.TemplateParamSeparator(), tokens.Text(text="b"),
  191. tokens.TemplateParamSeparator(), tokens.TemplateOpen(),
  192. tokens.Text(text="c"), tokens.TemplateParamSeparator(),
  193. tokens.WikilinkOpen(), tokens.Text(text="d"),
  194. tokens.WikilinkClose(), tokens.ArgumentOpen(),
  195. tokens.Text(text="e"), tokens.ArgumentClose(),
  196. tokens.TemplateClose(), tokens.TemplateClose(),
  197. tokens.WikilinkOpen(), tokens.Text(text="f"),
  198. tokens.WikilinkSeparator(), tokens.ArgumentOpen(),
  199. tokens.Text(text="g"), tokens.ArgumentClose(),
  200. tokens.CommentStart(), tokens.Text(text="h"),
  201. tokens.CommentEnd(), tokens.WikilinkClose(),
  202. tokens.TemplateOpen(), tokens.Text(text="i"),
  203. tokens.TemplateParamSeparator(), tokens.Text(text="j"),
  204. tokens.TemplateParamEquals(), tokens.HTMLEntityStart(),
  205. tokens.Text(text="nbsp"), tokens.HTMLEntityEnd(),
  206. tokens.TemplateClose()]
  207. valid = wrap(
  208. [Template(wraptext("a"), params=[Parameter(wraptext("1"), wraptext(
  209. "b"), showkey=False), Parameter(wraptext("2"), wrap([Template(
  210. wraptext("c"), params=[Parameter(wraptext("1"), wrap([Wikilink(
  211. wraptext("d")), Argument(wraptext("e"))]), showkey=False)])]),
  212. showkey=False)]), Wikilink(wraptext("f"), wrap([Argument(wraptext(
  213. "g")), Comment(wraptext("h"))])), Template(wraptext("i"), params=[
  214. Parameter(wraptext("j"), wrap([HTMLEntity("nbsp",
  215. named=True)]))])])
  216. self.assertWikicodeEqual(valid, self.builder.build(test))
  217. if __name__ == "__main__":
  218. unittest.main(verbosity=2)