A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

256 řádky
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.TemplateParamSeparator(),
  65. tokens.TemplateParamSeparator(), tokens.TemplateParamEquals(),
  66. tokens.TemplateParamSeparator(), tokens.TemplateClose()],
  67. wrap([Template(wrap([]), params=[
  68. Parameter(wraptext("1"), wrap([]), showkey=False),
  69. Parameter(wrap([]), wrap([]), showkey=True),
  70. Parameter(wraptext("2"), wrap([]), showkey=False)])])),
  71. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  72. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  73. tokens.TemplateParamEquals(), tokens.Text(text="baz"),
  74. tokens.TemplateParamSeparator(), tokens.Text(text="biz"),
  75. tokens.TemplateParamSeparator(), tokens.Text(text="buzz"),
  76. tokens.TemplateParamSeparator(), tokens.Text(text="3"),
  77. tokens.TemplateParamEquals(), tokens.Text(text="buff"),
  78. tokens.TemplateParamSeparator(), tokens.Text(text="baff"),
  79. tokens.TemplateClose()],
  80. wrap([Template(wraptext("foo"), params=[
  81. Parameter(wraptext("bar"), wraptext("baz")),
  82. Parameter(wraptext("1"), wraptext("biz"), showkey=False),
  83. Parameter(wraptext("2"), wraptext("buzz"), showkey=False),
  84. Parameter(wraptext("3"), wraptext("buff")),
  85. Parameter(wraptext("3"), wraptext("baff"),
  86. showkey=False)])])),
  87. ]
  88. for test, valid in tests:
  89. self.assertWikicodeEqual(valid, self.builder.build(test))
  90. def test_argument(self):
  91. """tests for building Argument nodes"""
  92. tests = [
  93. ([tokens.ArgumentOpen(), tokens.Text(text="foobar"),
  94. tokens.ArgumentClose()],
  95. wrap([Argument(wraptext("foobar"))])),
  96. ([tokens.ArgumentOpen(), tokens.Text(text="spam"),
  97. tokens.Text(text="eggs"), tokens.ArgumentClose()],
  98. wrap([Argument(wraptext("spam", "eggs"))])),
  99. ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
  100. tokens.ArgumentSeparator(), tokens.Text(text="bar"),
  101. tokens.ArgumentClose()],
  102. wrap([Argument(wraptext("foo"), wraptext("bar"))])),
  103. ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
  104. tokens.Text(text="bar"), tokens.ArgumentSeparator(),
  105. tokens.Text(text="baz"), tokens.Text(text="biz"),
  106. tokens.ArgumentClose()],
  107. wrap([Argument(wraptext("foo", "bar"), wraptext("baz", "biz"))])),
  108. ]
  109. for test, valid in tests:
  110. self.assertWikicodeEqual(valid, self.builder.build(test))
  111. def test_wikilink(self):
  112. """tests for building Wikilink nodes"""
  113. tests = [
  114. ([tokens.WikilinkOpen(), tokens.Text(text="foobar"),
  115. tokens.WikilinkClose()],
  116. wrap([Wikilink(wraptext("foobar"))])),
  117. ([tokens.WikilinkOpen(), tokens.Text(text="spam"),
  118. tokens.Text(text="eggs"), tokens.WikilinkClose()],
  119. wrap([Wikilink(wraptext("spam", "eggs"))])),
  120. ([tokens.WikilinkOpen(), tokens.Text(text="foo"),
  121. tokens.WikilinkSeparator(), tokens.Text(text="bar"),
  122. tokens.WikilinkClose()],
  123. wrap([Wikilink(wraptext("foo"), wraptext("bar"))])),
  124. ([tokens.WikilinkOpen(), tokens.Text(text="foo"),
  125. tokens.Text(text="bar"), tokens.WikilinkSeparator(),
  126. tokens.Text(text="baz"), tokens.Text(text="biz"),
  127. tokens.WikilinkClose()],
  128. wrap([Wikilink(wraptext("foo", "bar"), wraptext("baz", "biz"))])),
  129. ]
  130. for test, valid in tests:
  131. self.assertWikicodeEqual(valid, self.builder.build(test))
  132. def test_html_entity(self):
  133. """tests for building HTMLEntity nodes"""
  134. tests = [
  135. ([tokens.HTMLEntityStart(), tokens.Text(text="nbsp"),
  136. tokens.HTMLEntityEnd()],
  137. wrap([HTMLEntity("nbsp", named=True, hexadecimal=False)])),
  138. ([tokens.HTMLEntityStart(), tokens.HTMLEntityNumeric(),
  139. tokens.Text(text="107"), tokens.HTMLEntityEnd()],
  140. wrap([HTMLEntity("107", named=False, hexadecimal=False)])),
  141. ([tokens.HTMLEntityStart(), tokens.HTMLEntityNumeric(),
  142. tokens.HTMLEntityHex(char="X"), tokens.Text(text="6B"),
  143. tokens.HTMLEntityEnd()],
  144. wrap([HTMLEntity("6B", named=False, hexadecimal=True,
  145. hex_char="X")])),
  146. ]
  147. for test, valid in tests:
  148. self.assertWikicodeEqual(valid, self.builder.build(test))
  149. def test_heading(self):
  150. """tests for building Heading nodes"""
  151. tests = [
  152. ([tokens.HeadingStart(level=2), tokens.Text(text="foobar"),
  153. tokens.HeadingEnd()],
  154. wrap([Heading(wraptext("foobar"), 2)])),
  155. ([tokens.HeadingStart(level=4), tokens.Text(text="spam"),
  156. tokens.Text(text="eggs"), tokens.HeadingEnd()],
  157. wrap([Heading(wraptext("spam", "eggs"), 4)])),
  158. ]
  159. for test, valid in tests:
  160. self.assertWikicodeEqual(valid, self.builder.build(test))
  161. def test_comment(self):
  162. """tests for building Comment nodes"""
  163. tests = [
  164. ([tokens.CommentStart(), tokens.Text(text="foobar"),
  165. tokens.CommentEnd()],
  166. wrap([Comment(wraptext("foobar"))])),
  167. ([tokens.CommentStart(), tokens.Text(text="spam"),
  168. tokens.Text(text="eggs"), tokens.CommentEnd()],
  169. wrap([Comment(wraptext("spam", "eggs"))])),
  170. ]
  171. for test, valid in tests:
  172. self.assertWikicodeEqual(valid, self.builder.build(test))
  173. def test_integration(self):
  174. """a test for building a combination of templates together"""
  175. # {{{{{{{{foo}}bar|baz=biz}}buzz}}usr|{{bin}}}}
  176. test = [tokens.TemplateOpen(), tokens.TemplateOpen(),
  177. tokens.TemplateOpen(), tokens.TemplateOpen(),
  178. tokens.Text(text="foo"), tokens.TemplateClose(),
  179. tokens.Text(text="bar"), tokens.TemplateParamSeparator(),
  180. tokens.Text(text="baz"), tokens.TemplateParamEquals(),
  181. tokens.Text(text="biz"), tokens.TemplateClose(),
  182. tokens.Text(text="buzz"), tokens.TemplateClose(),
  183. tokens.Text(text="usr"), tokens.TemplateParamSeparator(),
  184. tokens.TemplateOpen(), tokens.Text(text="bin"),
  185. tokens.TemplateClose(), tokens.TemplateClose()]
  186. valid = wrap(
  187. [Template(wrap([Template(wrap([Template(wrap([Template(wraptext(
  188. "foo")), Text("bar")]), params=[Parameter(wraptext("baz"),
  189. wraptext("biz"))]), Text("buzz")])), Text("usr")]), params=[
  190. Parameter(wraptext("1"), wrap([Template(wraptext("bin"))]),
  191. showkey=False)])])
  192. self.assertWikicodeEqual(valid, self.builder.build(test))
  193. def test_integration2(self):
  194. """an even more audacious test for building a horrible wikicode mess"""
  195. # {{a|b|{{c|[[d]]{{{e}}}}}}}[[f|{{{g}}}<!--h-->]]{{i|j=&nbsp;}}
  196. test = [tokens.TemplateOpen(), tokens.Text(text="a"),
  197. tokens.TemplateParamSeparator(), tokens.Text(text="b"),
  198. tokens.TemplateParamSeparator(), tokens.TemplateOpen(),
  199. tokens.Text(text="c"), tokens.TemplateParamSeparator(),
  200. tokens.WikilinkOpen(), tokens.Text(text="d"),
  201. tokens.WikilinkClose(), tokens.ArgumentOpen(),
  202. tokens.Text(text="e"), tokens.ArgumentClose(),
  203. tokens.TemplateClose(), tokens.TemplateClose(),
  204. tokens.WikilinkOpen(), tokens.Text(text="f"),
  205. tokens.WikilinkSeparator(), tokens.ArgumentOpen(),
  206. tokens.Text(text="g"), tokens.ArgumentClose(),
  207. tokens.CommentStart(), tokens.Text(text="h"),
  208. tokens.CommentEnd(), tokens.WikilinkClose(),
  209. tokens.TemplateOpen(), tokens.Text(text="i"),
  210. tokens.TemplateParamSeparator(), tokens.Text(text="j"),
  211. tokens.TemplateParamEquals(), tokens.HTMLEntityStart(),
  212. tokens.Text(text="nbsp"), tokens.HTMLEntityEnd(),
  213. tokens.TemplateClose()]
  214. valid = wrap(
  215. [Template(wraptext("a"), params=[Parameter(wraptext("1"), wraptext(
  216. "b"), showkey=False), Parameter(wraptext("2"), wrap([Template(
  217. wraptext("c"), params=[Parameter(wraptext("1"), wrap([Wikilink(
  218. wraptext("d")), Argument(wraptext("e"))]), showkey=False)])]),
  219. showkey=False)]), Wikilink(wraptext("f"), wrap([Argument(wraptext(
  220. "g")), Comment(wraptext("h"))])), Template(wraptext("i"), params=[
  221. Parameter(wraptext("j"), wrap([HTMLEntity("nbsp",
  222. named=True)]))])])
  223. self.assertWikicodeEqual(valid, self.builder.build(test))
  224. if __name__ == "__main__":
  225. unittest.main(verbosity=2)