A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

365 行
18 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_tag(self):
  174. """tests for building Tag nodes"""
  175. tests = [
  176. # <ref></ref>
  177. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  178. tokens.TagCloseOpen(padding=""), tokens.TagOpenClose(),
  179. tokens.Text(text="ref"), tokens.TagCloseClose()],
  180. wrap([Tag(wraptext("ref"), wrap([]),
  181. closing_tag=wraptext("ref"))])),
  182. # <ref name></ref>
  183. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  184. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  185. pad_after_eq=""),
  186. tokens.Text(text="name"), tokens.TagCloseOpen(padding=""),
  187. tokens.TagOpenClose(), tokens.Text(text="ref"),
  188. tokens.TagCloseClose()],
  189. wrap([Tag(wraptext("ref"), wrap([]),
  190. attrs=[Attribute(wraptext("name"))])])),
  191. # <ref name="abc" />
  192. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  193. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  194. pad_after_eq=""),
  195. tokens.Text(text="name"), tokens.TagAttrEquals(),
  196. tokens.TagAttrQuote(), tokens.Text(text="abc"),
  197. tokens.TagCloseSelfclose(padding=" ")],
  198. wrap([Tag(wraptext("ref"),
  199. attrs=[Attribute(wraptext("name"), wraptext("abc"))],
  200. self_closing=True, padding=" ")])),
  201. # <br/>
  202. ([tokens.TagOpenOpen(), tokens.Text(text="br"),
  203. tokens.TagCloseSelfclose(padding="")],
  204. wrap([Tag(wraptext("br"), self_closing=True)])),
  205. # <li>
  206. ([tokens.TagOpenOpen(), tokens.Text(text="li"),
  207. tokens.TagCloseSelfclose(padding="", implicit=True)],
  208. wrap([Tag(wraptext("li"), self_closing=True, implicit=True)])),
  209. # </br>
  210. ([tokens.TagOpenOpen(invalid=True), tokens.Text(text="br"),
  211. tokens.TagCloseSelfclose(padding="", implicit=True)],
  212. wrap([Tag(wraptext("br"), self_closing=True, invalid=True,
  213. implicit=True)])),
  214. # </br/>
  215. ([tokens.TagOpenOpen(invalid=True), tokens.Text(text="br"),
  216. tokens.TagCloseSelfclose(padding="")],
  217. wrap([Tag(wraptext("br"), self_closing=True, invalid=True)])),
  218. # <ref name={{abc}} foo="bar {{baz}}" abc={{de}}f ghi=j{{k}}{{l}}
  219. # mno = "{{p}} [[q]] {{r}}">[[Source]]</ref>
  220. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  221. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  222. pad_after_eq=""),
  223. tokens.Text(text="name"), tokens.TagAttrEquals(),
  224. tokens.TemplateOpen(), tokens.Text(text="abc"),
  225. tokens.TemplateClose(),
  226. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  227. pad_after_eq=""),
  228. tokens.Text(text="foo"), tokens.TagAttrEquals(),
  229. tokens.TagAttrQuote(), tokens.Text(text="bar "),
  230. tokens.TemplateOpen(), tokens.Text(text="baz"),
  231. tokens.TemplateClose(),
  232. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  233. pad_after_eq=""),
  234. tokens.Text(text="abc"), tokens.TagAttrEquals(),
  235. tokens.TemplateOpen(), tokens.Text(text="de"),
  236. tokens.TemplateClose(), tokens.Text(text="f"),
  237. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  238. pad_after_eq=""),
  239. tokens.Text(text="ghi"), tokens.TagAttrEquals(),
  240. tokens.Text(text="j"), tokens.TemplateOpen(),
  241. tokens.Text(text="k"), tokens.TemplateClose(),
  242. tokens.TemplateOpen(), tokens.Text(text="l"),
  243. tokens.TemplateClose(),
  244. tokens.TagAttrStart(pad_first=" \n ", pad_before_eq=" ",
  245. pad_after_eq=" "),
  246. tokens.Text(text="mno"), tokens.TagAttrEquals(),
  247. tokens.TagAttrQuote(), tokens.TemplateOpen(),
  248. tokens.Text(text="p"), tokens.TemplateClose(),
  249. tokens.Text(text=" "), tokens.WikilinkOpen(),
  250. tokens.Text(text="q"), tokens.WikilinkClose(),
  251. tokens.Text(text=" "), tokens.TemplateOpen(),
  252. tokens.Text(text="r"), tokens.TemplateClose(),
  253. tokens.TagCloseOpen(padding=""), tokens.WikilinkOpen(),
  254. tokens.Text(text="Source"), tokens.WikilinkClose(),
  255. tokens.TagOpenClose(), tokens.Text(text="ref"),
  256. tokens.TagCloseClose()],
  257. wrap([Tag(wraptext("ref"), wrap([Wikilink(wraptext("Source"))]), [
  258. Attribute(wraptext("name"),
  259. wrap([Template(wraptext("abc"))]), False),
  260. Attribute(wraptext("foo"), wrap([Text("bar "),
  261. Template(wraptext("baz"))]), pad_first=" "),
  262. Attribute(wraptext("abc"), wrap([Template(wraptext("de")),
  263. Text("f")]), False),
  264. Attribute(wraptext("ghi"), wrap([Text("j"),
  265. Template(wraptext("k")),
  266. Template(wraptext("l"))]), False),
  267. Attribute(wraptext("mno"), wrap([Template(wraptext("p")),
  268. Text(" "), Wikilink(wraptext("q")), Text(" "),
  269. Template(wraptext("r"))]), True, " \n ", " ",
  270. " ")])])),
  271. ]
  272. for test, valid in tests:
  273. self.assertWikicodeEqual(valid, self.builder.build(test))
  274. def test_integration(self):
  275. """a test for building a combination of templates together"""
  276. # {{{{{{{{foo}}bar|baz=biz}}buzz}}usr|{{bin}}}}
  277. test = [tokens.TemplateOpen(), tokens.TemplateOpen(),
  278. tokens.TemplateOpen(), tokens.TemplateOpen(),
  279. tokens.Text(text="foo"), tokens.TemplateClose(),
  280. tokens.Text(text="bar"), tokens.TemplateParamSeparator(),
  281. tokens.Text(text="baz"), tokens.TemplateParamEquals(),
  282. tokens.Text(text="biz"), tokens.TemplateClose(),
  283. tokens.Text(text="buzz"), tokens.TemplateClose(),
  284. tokens.Text(text="usr"), tokens.TemplateParamSeparator(),
  285. tokens.TemplateOpen(), tokens.Text(text="bin"),
  286. tokens.TemplateClose(), tokens.TemplateClose()]
  287. valid = wrap(
  288. [Template(wrap([Template(wrap([Template(wrap([Template(wraptext(
  289. "foo")), Text("bar")]), params=[Parameter(wraptext("baz"),
  290. wraptext("biz"))]), Text("buzz")])), Text("usr")]), params=[
  291. Parameter(wraptext("1"), wrap([Template(wraptext("bin"))]),
  292. showkey=False)])])
  293. self.assertWikicodeEqual(valid, self.builder.build(test))
  294. def test_integration2(self):
  295. """an even more audacious test for building a horrible wikicode mess"""
  296. # {{a|b|{{c|[[d]]{{{e}}}}}}}[[f|{{{g}}}<!--h-->]]{{i|j=&nbsp;}}
  297. test = [tokens.TemplateOpen(), tokens.Text(text="a"),
  298. tokens.TemplateParamSeparator(), tokens.Text(text="b"),
  299. tokens.TemplateParamSeparator(), tokens.TemplateOpen(),
  300. tokens.Text(text="c"), tokens.TemplateParamSeparator(),
  301. tokens.WikilinkOpen(), tokens.Text(text="d"),
  302. tokens.WikilinkClose(), tokens.ArgumentOpen(),
  303. tokens.Text(text="e"), tokens.ArgumentClose(),
  304. tokens.TemplateClose(), tokens.TemplateClose(),
  305. tokens.WikilinkOpen(), tokens.Text(text="f"),
  306. tokens.WikilinkSeparator(), tokens.ArgumentOpen(),
  307. tokens.Text(text="g"), tokens.ArgumentClose(),
  308. tokens.CommentStart(), tokens.Text(text="h"),
  309. tokens.CommentEnd(), tokens.WikilinkClose(),
  310. tokens.TemplateOpen(), tokens.Text(text="i"),
  311. tokens.TemplateParamSeparator(), tokens.Text(text="j"),
  312. tokens.TemplateParamEquals(), tokens.HTMLEntityStart(),
  313. tokens.Text(text="nbsp"), tokens.HTMLEntityEnd(),
  314. tokens.TemplateClose()]
  315. valid = wrap(
  316. [Template(wraptext("a"), params=[Parameter(wraptext("1"), wraptext(
  317. "b"), showkey=False), Parameter(wraptext("2"), wrap([Template(
  318. wraptext("c"), params=[Parameter(wraptext("1"), wrap([Wikilink(
  319. wraptext("d")), Argument(wraptext("e"))]), showkey=False)])]),
  320. showkey=False)]), Wikilink(wraptext("f"), wrap([Argument(wraptext(
  321. "g")), Comment(wraptext("h"))])), Template(wraptext("i"), params=[
  322. Parameter(wraptext("j"), wrap([HTMLEntity("nbsp",
  323. named=True)]))])])
  324. self.assertWikicodeEqual(valid, self.builder.build(test))
  325. if __name__ == "__main__":
  326. unittest.main(verbosity=2)