A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

261 righe
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_tag(self):
  167. """tests for building Tag nodes"""
  168. tests = [
  169. ([tokens.TagOpenOpen(showtag=True, type=101),
  170. tokens.Text(text="ref"), tokens.TagCloseOpen(padding=""),
  171. tokens.TagOpenClose(), tokens.Text(text="ref"),
  172. tokens.TagCloseClose()],
  173. wrap([Tag(101, wraptext("ref"), wrap([]), [], True, False, "",
  174. wraptext("ref"))])),
  175. ]
  176. for test, valid in tests:
  177. self.assertWikicodeEqual(valid, self.builder.build(test))
  178. def test_integration(self):
  179. """a test for building a combination of templates together"""
  180. # {{{{{{{{foo}}bar|baz=biz}}buzz}}usr|{{bin}}}}
  181. test = [tokens.TemplateOpen(), tokens.TemplateOpen(),
  182. tokens.TemplateOpen(), tokens.TemplateOpen(),
  183. tokens.Text(text="foo"), tokens.TemplateClose(),
  184. tokens.Text(text="bar"), tokens.TemplateParamSeparator(),
  185. tokens.Text(text="baz"), tokens.TemplateParamEquals(),
  186. tokens.Text(text="biz"), tokens.TemplateClose(),
  187. tokens.Text(text="buzz"), tokens.TemplateClose(),
  188. tokens.Text(text="usr"), tokens.TemplateParamSeparator(),
  189. tokens.TemplateOpen(), tokens.Text(text="bin"),
  190. tokens.TemplateClose(), tokens.TemplateClose()]
  191. valid = wrap(
  192. [Template(wrap([Template(wrap([Template(wrap([Template(wraptext(
  193. "foo")), Text("bar")]), params=[Parameter(wraptext("baz"),
  194. wraptext("biz"))]), Text("buzz")])), Text("usr")]), params=[
  195. Parameter(wraptext("1"), wrap([Template(wraptext("bin"))]),
  196. showkey=False)])])
  197. self.assertWikicodeEqual(valid, self.builder.build(test))
  198. def test_integration2(self):
  199. """an even more audacious test for building a horrible wikicode mess"""
  200. # {{a|b|{{c|[[d]]{{{e}}}}}}}[[f|{{{g}}}<!--h-->]]{{i|j=&nbsp;}}
  201. test = [tokens.TemplateOpen(), tokens.Text(text="a"),
  202. tokens.TemplateParamSeparator(), tokens.Text(text="b"),
  203. tokens.TemplateParamSeparator(), tokens.TemplateOpen(),
  204. tokens.Text(text="c"), tokens.TemplateParamSeparator(),
  205. tokens.WikilinkOpen(), tokens.Text(text="d"),
  206. tokens.WikilinkClose(), tokens.ArgumentOpen(),
  207. tokens.Text(text="e"), tokens.ArgumentClose(),
  208. tokens.TemplateClose(), tokens.TemplateClose(),
  209. tokens.WikilinkOpen(), tokens.Text(text="f"),
  210. tokens.WikilinkSeparator(), tokens.ArgumentOpen(),
  211. tokens.Text(text="g"), tokens.ArgumentClose(),
  212. tokens.CommentStart(), tokens.Text(text="h"),
  213. tokens.CommentEnd(), tokens.WikilinkClose(),
  214. tokens.TemplateOpen(), tokens.Text(text="i"),
  215. tokens.TemplateParamSeparator(), tokens.Text(text="j"),
  216. tokens.TemplateParamEquals(), tokens.HTMLEntityStart(),
  217. tokens.Text(text="nbsp"), tokens.HTMLEntityEnd(),
  218. tokens.TemplateClose()]
  219. valid = wrap(
  220. [Template(wraptext("a"), params=[Parameter(wraptext("1"), wraptext(
  221. "b"), showkey=False), Parameter(wraptext("2"), wrap([Template(
  222. wraptext("c"), params=[Parameter(wraptext("1"), wrap([Wikilink(
  223. wraptext("d")), Argument(wraptext("e"))]), showkey=False)])]),
  224. showkey=False)]), Wikilink(wraptext("f"), wrap([Argument(wraptext(
  225. "g")), Comment(wraptext("h"))])), Template(wraptext("i"), params=[
  226. Parameter(wraptext("j"), wrap([HTMLEntity("nbsp",
  227. named=True)]))])])
  228. self.assertWikicodeEqual(valid, self.builder.build(test))
  229. if __name__ == "__main__":
  230. unittest.main(verbosity=2)