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

435 行
21 KiB

  1. #
  2. # Copyright (C) 2012-2019 Ben Kurtovic <ben.kurtovic@gmail.com>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. import unittest
  22. from mwparserfromhell.nodes import (Argument, Comment, ExternalLink, Heading,
  23. HTMLEntity, Tag, Template, Text, Wikilink)
  24. from mwparserfromhell.nodes.extras import Attribute, Parameter
  25. from mwparserfromhell.parser import tokens, ParserError
  26. from mwparserfromhell.parser.builder import Builder
  27. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  28. class TestBuilder(TreeEqualityTestCase):
  29. """Tests for the builder, which turns tokens into Wikicode objects."""
  30. def setUp(self):
  31. self.builder = Builder()
  32. def test_text(self):
  33. """tests for building Text nodes"""
  34. tests = [
  35. ([tokens.Text(text="foobar")], wraptext("foobar")),
  36. ([tokens.Text(text="fóóbar")], wraptext("fóóbar")),
  37. ([tokens.Text(text="spam"), tokens.Text(text="eggs")],
  38. wraptext("spam", "eggs")),
  39. ]
  40. for test, valid in tests:
  41. self.assertWikicodeEqual(valid, self.builder.build(test))
  42. def test_template(self):
  43. """tests for building Template nodes"""
  44. tests = [
  45. ([tokens.TemplateOpen(), tokens.Text(text="foobar"),
  46. tokens.TemplateClose()],
  47. wrap([Template(wraptext("foobar"))])),
  48. ([tokens.TemplateOpen(), tokens.Text(text="spam"),
  49. tokens.Text(text="eggs"), tokens.TemplateClose()],
  50. wrap([Template(wraptext("spam", "eggs"))])),
  51. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  52. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  53. tokens.TemplateClose()],
  54. wrap([Template(wraptext("foo"), params=[
  55. Parameter(wraptext("1"), wraptext("bar"), showkey=False)])])),
  56. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  57. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  58. tokens.TemplateParamEquals(), tokens.Text(text="baz"),
  59. tokens.TemplateClose()],
  60. wrap([Template(wraptext("foo"), params=[
  61. Parameter(wraptext("bar"), wraptext("baz"))])])),
  62. ([tokens.TemplateOpen(), tokens.TemplateParamSeparator(),
  63. tokens.TemplateParamSeparator(), tokens.TemplateParamEquals(),
  64. tokens.TemplateParamSeparator(), tokens.TemplateClose()],
  65. wrap([Template(wrap([]), params=[
  66. Parameter(wraptext("1"), wrap([]), showkey=False),
  67. Parameter(wrap([]), wrap([]), showkey=True),
  68. Parameter(wraptext("2"), wrap([]), showkey=False)])])),
  69. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  70. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  71. tokens.TemplateParamEquals(), tokens.Text(text="baz"),
  72. tokens.TemplateParamSeparator(), tokens.Text(text="biz"),
  73. tokens.TemplateParamSeparator(), tokens.Text(text="buzz"),
  74. tokens.TemplateParamSeparator(), tokens.Text(text="3"),
  75. tokens.TemplateParamEquals(), tokens.Text(text="buff"),
  76. tokens.TemplateParamSeparator(), tokens.Text(text="baff"),
  77. tokens.TemplateClose()],
  78. wrap([Template(wraptext("foo"), params=[
  79. Parameter(wraptext("bar"), wraptext("baz")),
  80. Parameter(wraptext("1"), wraptext("biz"), showkey=False),
  81. Parameter(wraptext("2"), wraptext("buzz"), showkey=False),
  82. Parameter(wraptext("3"), wraptext("buff")),
  83. Parameter(wraptext("3"), wraptext("baff"),
  84. showkey=False)])])),
  85. ]
  86. for test, valid in tests:
  87. self.assertWikicodeEqual(valid, self.builder.build(test))
  88. def test_argument(self):
  89. """tests for building Argument nodes"""
  90. tests = [
  91. ([tokens.ArgumentOpen(), tokens.Text(text="foobar"),
  92. tokens.ArgumentClose()],
  93. wrap([Argument(wraptext("foobar"))])),
  94. ([tokens.ArgumentOpen(), tokens.Text(text="spam"),
  95. tokens.Text(text="eggs"), tokens.ArgumentClose()],
  96. wrap([Argument(wraptext("spam", "eggs"))])),
  97. ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
  98. tokens.ArgumentSeparator(), tokens.Text(text="bar"),
  99. tokens.ArgumentClose()],
  100. wrap([Argument(wraptext("foo"), wraptext("bar"))])),
  101. ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
  102. tokens.Text(text="bar"), tokens.ArgumentSeparator(),
  103. tokens.Text(text="baz"), tokens.Text(text="biz"),
  104. tokens.ArgumentClose()],
  105. wrap([Argument(wraptext("foo", "bar"), wraptext("baz", "biz"))])),
  106. ]
  107. for test, valid in tests:
  108. self.assertWikicodeEqual(valid, self.builder.build(test))
  109. def test_wikilink(self):
  110. """tests for building Wikilink nodes"""
  111. tests = [
  112. ([tokens.WikilinkOpen(), tokens.Text(text="foobar"),
  113. tokens.WikilinkClose()],
  114. wrap([Wikilink(wraptext("foobar"))])),
  115. ([tokens.WikilinkOpen(), tokens.Text(text="spam"),
  116. tokens.Text(text="eggs"), tokens.WikilinkClose()],
  117. wrap([Wikilink(wraptext("spam", "eggs"))])),
  118. ([tokens.WikilinkOpen(), tokens.Text(text="foo"),
  119. tokens.WikilinkSeparator(), tokens.Text(text="bar"),
  120. tokens.WikilinkClose()],
  121. wrap([Wikilink(wraptext("foo"), wraptext("bar"))])),
  122. ([tokens.WikilinkOpen(), tokens.Text(text="foo"),
  123. tokens.Text(text="bar"), tokens.WikilinkSeparator(),
  124. tokens.Text(text="baz"), tokens.Text(text="biz"),
  125. tokens.WikilinkClose()],
  126. wrap([Wikilink(wraptext("foo", "bar"), wraptext("baz", "biz"))])),
  127. ]
  128. for test, valid in tests:
  129. self.assertWikicodeEqual(valid, self.builder.build(test))
  130. def test_external_link(self):
  131. """tests for building ExternalLink nodes"""
  132. tests = [
  133. ([tokens.ExternalLinkOpen(brackets=False),
  134. tokens.Text(text="http://example.com/"),
  135. tokens.ExternalLinkClose()],
  136. wrap([ExternalLink(wraptext("http://example.com/"),
  137. brackets=False)])),
  138. ([tokens.ExternalLinkOpen(brackets=True),
  139. tokens.Text(text="http://example.com/"),
  140. tokens.ExternalLinkClose()],
  141. wrap([ExternalLink(wraptext("http://example.com/"))])),
  142. ([tokens.ExternalLinkOpen(brackets=True),
  143. tokens.Text(text="http://example.com/"),
  144. tokens.ExternalLinkSeparator(), tokens.ExternalLinkClose()],
  145. wrap([ExternalLink(wraptext("http://example.com/"), wrap([]))])),
  146. ([tokens.ExternalLinkOpen(brackets=True),
  147. tokens.Text(text="http://example.com/"),
  148. tokens.ExternalLinkSeparator(), tokens.Text(text="Example"),
  149. tokens.ExternalLinkClose()],
  150. wrap([ExternalLink(wraptext("http://example.com/"),
  151. wraptext("Example"))])),
  152. ([tokens.ExternalLinkOpen(brackets=False),
  153. tokens.Text(text="http://example"), tokens.Text(text=".com/foo"),
  154. tokens.ExternalLinkClose()],
  155. wrap([ExternalLink(wraptext("http://example", ".com/foo"),
  156. brackets=False)])),
  157. ([tokens.ExternalLinkOpen(brackets=True),
  158. tokens.Text(text="http://example"), tokens.Text(text=".com/foo"),
  159. tokens.ExternalLinkSeparator(), tokens.Text(text="Example"),
  160. tokens.Text(text=" Web Page"), tokens.ExternalLinkClose()],
  161. wrap([ExternalLink(wraptext("http://example", ".com/foo"),
  162. wraptext("Example", " Web Page"))])),
  163. ]
  164. for test, valid in tests:
  165. self.assertWikicodeEqual(valid, self.builder.build(test))
  166. def test_html_entity(self):
  167. """tests for building HTMLEntity nodes"""
  168. tests = [
  169. ([tokens.HTMLEntityStart(), tokens.Text(text="nbsp"),
  170. tokens.HTMLEntityEnd()],
  171. wrap([HTMLEntity("nbsp", named=True, hexadecimal=False)])),
  172. ([tokens.HTMLEntityStart(), tokens.HTMLEntityNumeric(),
  173. tokens.Text(text="107"), tokens.HTMLEntityEnd()],
  174. wrap([HTMLEntity("107", named=False, hexadecimal=False)])),
  175. ([tokens.HTMLEntityStart(), tokens.HTMLEntityNumeric(),
  176. tokens.HTMLEntityHex(char="X"), tokens.Text(text="6B"),
  177. tokens.HTMLEntityEnd()],
  178. wrap([HTMLEntity("6B", named=False, hexadecimal=True,
  179. hex_char="X")])),
  180. ]
  181. for test, valid in tests:
  182. self.assertWikicodeEqual(valid, self.builder.build(test))
  183. def test_heading(self):
  184. """tests for building Heading nodes"""
  185. tests = [
  186. ([tokens.HeadingStart(level=2), tokens.Text(text="foobar"),
  187. tokens.HeadingEnd()],
  188. wrap([Heading(wraptext("foobar"), 2)])),
  189. ([tokens.HeadingStart(level=4), tokens.Text(text="spam"),
  190. tokens.Text(text="eggs"), tokens.HeadingEnd()],
  191. wrap([Heading(wraptext("spam", "eggs"), 4)])),
  192. ]
  193. for test, valid in tests:
  194. self.assertWikicodeEqual(valid, self.builder.build(test))
  195. def test_comment(self):
  196. """tests for building Comment nodes"""
  197. tests = [
  198. ([tokens.CommentStart(), tokens.Text(text="foobar"),
  199. tokens.CommentEnd()],
  200. wrap([Comment("foobar")])),
  201. ([tokens.CommentStart(), tokens.Text(text="spam"),
  202. tokens.Text(text="eggs"), tokens.CommentEnd()],
  203. wrap([Comment("spameggs")])),
  204. ]
  205. for test, valid in tests:
  206. self.assertWikicodeEqual(valid, self.builder.build(test))
  207. def test_tag(self):
  208. """tests for building Tag nodes"""
  209. tests = [
  210. # <ref></ref>
  211. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  212. tokens.TagCloseOpen(padding=""), tokens.TagOpenClose(),
  213. tokens.Text(text="ref"), tokens.TagCloseClose()],
  214. wrap([Tag(wraptext("ref"), wrap([]),
  215. closing_tag=wraptext("ref"))])),
  216. # <ref name></ref>
  217. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  218. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  219. pad_after_eq=""),
  220. tokens.Text(text="name"), tokens.TagCloseOpen(padding=""),
  221. tokens.TagOpenClose(), tokens.Text(text="ref"),
  222. tokens.TagCloseClose()],
  223. wrap([Tag(wraptext("ref"), wrap([]),
  224. attrs=[Attribute(wraptext("name"))])])),
  225. # <ref name="abc" />
  226. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  227. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  228. pad_after_eq=""),
  229. tokens.Text(text="name"), tokens.TagAttrEquals(),
  230. tokens.TagAttrQuote(char='"'), tokens.Text(text="abc"),
  231. tokens.TagCloseSelfclose(padding=" ")],
  232. wrap([Tag(wraptext("ref"),
  233. attrs=[Attribute(wraptext("name"), wraptext("abc"))],
  234. self_closing=True, padding=" ")])),
  235. # <br/>
  236. ([tokens.TagOpenOpen(), tokens.Text(text="br"),
  237. tokens.TagCloseSelfclose(padding="")],
  238. wrap([Tag(wraptext("br"), self_closing=True)])),
  239. # <li>
  240. ([tokens.TagOpenOpen(), tokens.Text(text="li"),
  241. tokens.TagCloseSelfclose(padding="", implicit=True)],
  242. wrap([Tag(wraptext("li"), self_closing=True, implicit=True)])),
  243. # </br>
  244. ([tokens.TagOpenOpen(invalid=True), tokens.Text(text="br"),
  245. tokens.TagCloseSelfclose(padding="", implicit=True)],
  246. wrap([Tag(wraptext("br"), self_closing=True, invalid=True,
  247. implicit=True)])),
  248. # </br/>
  249. ([tokens.TagOpenOpen(invalid=True), tokens.Text(text="br"),
  250. tokens.TagCloseSelfclose(padding="")],
  251. wrap([Tag(wraptext("br"), self_closing=True, invalid=True)])),
  252. # <ref name={{abc}} foo="bar {{baz}}" abc={{de}}f ghi=j{{k}}{{l}}
  253. # mno = '{{p}} [[q]] {{r}}'>[[Source]]</ref>
  254. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  255. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  256. pad_after_eq=""),
  257. tokens.Text(text="name"), tokens.TagAttrEquals(),
  258. tokens.TemplateOpen(), tokens.Text(text="abc"),
  259. tokens.TemplateClose(),
  260. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  261. pad_after_eq=""),
  262. tokens.Text(text="foo"), tokens.TagAttrEquals(),
  263. tokens.TagAttrQuote(char='"'), tokens.Text(text="bar "),
  264. tokens.TemplateOpen(), tokens.Text(text="baz"),
  265. tokens.TemplateClose(),
  266. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  267. pad_after_eq=""),
  268. tokens.Text(text="abc"), tokens.TagAttrEquals(),
  269. tokens.TemplateOpen(), tokens.Text(text="de"),
  270. tokens.TemplateClose(), tokens.Text(text="f"),
  271. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  272. pad_after_eq=""),
  273. tokens.Text(text="ghi"), tokens.TagAttrEquals(),
  274. tokens.Text(text="j"), tokens.TemplateOpen(),
  275. tokens.Text(text="k"), tokens.TemplateClose(),
  276. tokens.TemplateOpen(), tokens.Text(text="l"),
  277. tokens.TemplateClose(),
  278. tokens.TagAttrStart(pad_first=" \n ", pad_before_eq=" ",
  279. pad_after_eq=" "),
  280. tokens.Text(text="mno"), tokens.TagAttrEquals(),
  281. tokens.TagAttrQuote(char="'"), tokens.TemplateOpen(),
  282. tokens.Text(text="p"), tokens.TemplateClose(),
  283. tokens.Text(text=" "), tokens.WikilinkOpen(),
  284. tokens.Text(text="q"), tokens.WikilinkClose(),
  285. tokens.Text(text=" "), tokens.TemplateOpen(),
  286. tokens.Text(text="r"), tokens.TemplateClose(),
  287. tokens.TagCloseOpen(padding=""), tokens.WikilinkOpen(),
  288. tokens.Text(text="Source"), tokens.WikilinkClose(),
  289. tokens.TagOpenClose(), tokens.Text(text="ref"),
  290. tokens.TagCloseClose()],
  291. wrap([Tag(wraptext("ref"), wrap([Wikilink(wraptext("Source"))]), [
  292. Attribute(wraptext("name"),
  293. wrap([Template(wraptext("abc"))]), None),
  294. Attribute(wraptext("foo"), wrap([Text("bar "),
  295. Template(wraptext("baz"))]), pad_first=" "),
  296. Attribute(wraptext("abc"), wrap([Template(wraptext("de")),
  297. Text("f")]), None),
  298. Attribute(wraptext("ghi"), wrap([Text("j"),
  299. Template(wraptext("k")),
  300. Template(wraptext("l"))]), None),
  301. Attribute(wraptext("mno"), wrap([Template(wraptext("p")),
  302. Text(" "), Wikilink(wraptext("q")), Text(" "),
  303. Template(wraptext("r"))]), "'", " \n ", " ",
  304. " ")])])),
  305. # "''italic text''"
  306. ([tokens.TagOpenOpen(wiki_markup="''"), tokens.Text(text="i"),
  307. tokens.TagCloseOpen(), tokens.Text(text="italic text"),
  308. tokens.TagOpenClose(), tokens.Text(text="i"),
  309. tokens.TagCloseClose()],
  310. wrap([Tag(wraptext("i"), wraptext("italic text"),
  311. wiki_markup="''")])),
  312. # * bullet
  313. ([tokens.TagOpenOpen(wiki_markup="*"), tokens.Text(text="li"),
  314. tokens.TagCloseSelfclose(), tokens.Text(text=" bullet")],
  315. wrap([Tag(wraptext("li"), wiki_markup="*", self_closing=True),
  316. Text(" bullet")])),
  317. ]
  318. for test, valid in tests:
  319. self.assertWikicodeEqual(valid, self.builder.build(test))
  320. def test_integration(self):
  321. """a test for building a combination of templates together"""
  322. # {{{{{{{{foo}}bar|baz=biz}}buzz}}usr|{{bin}}}}
  323. test = [tokens.TemplateOpen(), tokens.TemplateOpen(),
  324. tokens.TemplateOpen(), tokens.TemplateOpen(),
  325. tokens.Text(text="foo"), tokens.TemplateClose(),
  326. tokens.Text(text="bar"), tokens.TemplateParamSeparator(),
  327. tokens.Text(text="baz"), tokens.TemplateParamEquals(),
  328. tokens.Text(text="biz"), tokens.TemplateClose(),
  329. tokens.Text(text="buzz"), tokens.TemplateClose(),
  330. tokens.Text(text="usr"), tokens.TemplateParamSeparator(),
  331. tokens.TemplateOpen(), tokens.Text(text="bin"),
  332. tokens.TemplateClose(), tokens.TemplateClose()]
  333. valid = wrap(
  334. [Template(wrap([Template(wrap([Template(wrap([Template(wraptext(
  335. "foo")), Text("bar")]), params=[Parameter(wraptext("baz"),
  336. wraptext("biz"))]), Text("buzz")])), Text("usr")]), params=[
  337. Parameter(wraptext("1"), wrap([Template(wraptext("bin"))]),
  338. showkey=False)])])
  339. self.assertWikicodeEqual(valid, self.builder.build(test))
  340. def test_integration2(self):
  341. """an even more audacious test for building a horrible wikicode mess"""
  342. # {{a|b|{{c|[[d]]{{{e}}}}}}}[[f|{{{g}}}<!--h-->]]{{i|j=&nbsp;}}
  343. test = [tokens.TemplateOpen(), tokens.Text(text="a"),
  344. tokens.TemplateParamSeparator(), tokens.Text(text="b"),
  345. tokens.TemplateParamSeparator(), tokens.TemplateOpen(),
  346. tokens.Text(text="c"), tokens.TemplateParamSeparator(),
  347. tokens.WikilinkOpen(), tokens.Text(text="d"),
  348. tokens.WikilinkClose(), tokens.ArgumentOpen(),
  349. tokens.Text(text="e"), tokens.ArgumentClose(),
  350. tokens.TemplateClose(), tokens.TemplateClose(),
  351. tokens.WikilinkOpen(), tokens.Text(text="f"),
  352. tokens.WikilinkSeparator(), tokens.ArgumentOpen(),
  353. tokens.Text(text="g"), tokens.ArgumentClose(),
  354. tokens.CommentStart(), tokens.Text(text="h"),
  355. tokens.CommentEnd(), tokens.WikilinkClose(),
  356. tokens.TemplateOpen(), tokens.Text(text="i"),
  357. tokens.TemplateParamSeparator(), tokens.Text(text="j"),
  358. tokens.TemplateParamEquals(), tokens.HTMLEntityStart(),
  359. tokens.Text(text="nbsp"), tokens.HTMLEntityEnd(),
  360. tokens.TemplateClose()]
  361. valid = wrap(
  362. [Template(wraptext("a"), params=[Parameter(wraptext("1"), wraptext(
  363. "b"), showkey=False), Parameter(wraptext("2"), wrap([Template(
  364. wraptext("c"), params=[Parameter(wraptext("1"), wrap([Wikilink(
  365. wraptext("d")), Argument(wraptext("e"))]), showkey=False)])]),
  366. showkey=False)]), Wikilink(wraptext("f"), wrap([Argument(wraptext(
  367. "g")), Comment("h")])), Template(wraptext("i"), params=[
  368. Parameter(wraptext("j"), wrap([HTMLEntity("nbsp",
  369. named=True)]))])])
  370. self.assertWikicodeEqual(valid, self.builder.build(test))
  371. def test_parser_errors(self):
  372. """test whether ParserError gets thrown for bad input"""
  373. missing_closes = [
  374. [tokens.TemplateOpen(), tokens.TemplateParamSeparator()],
  375. [tokens.TemplateOpen()], [tokens.ArgumentOpen()],
  376. [tokens.WikilinkOpen()], [tokens.ExternalLinkOpen()],
  377. [tokens.HeadingStart()], [tokens.CommentStart()],
  378. [tokens.TagOpenOpen(), tokens.TagAttrStart()],
  379. [tokens.TagOpenOpen()]
  380. ]
  381. msg = r"_handle_token\(\) got unexpected TemplateClose"
  382. self.assertRaisesRegex(ParserError, msg, self.builder.build, [tokens.TemplateClose()])
  383. for test in missing_closes:
  384. self.assertRaises(ParserError, self.builder.build, test)
  385. if __name__ == "__main__":
  386. unittest.main(verbosity=2)