A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

434 Zeilen
21 KiB

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