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

425 行
20 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  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. try:
  24. import unittest2 as unittest
  25. except ImportError:
  26. import unittest
  27. from mwparserfromhell.nodes import (Argument, Comment, ExternalLink, Heading,
  28. HTMLEntity, Tag, Template, Text, Wikilink)
  29. from mwparserfromhell.nodes.extras import Attribute, Parameter
  30. from mwparserfromhell.parser import tokens
  31. from mwparserfromhell.parser.builder import Builder
  32. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  33. class TestBuilder(TreeEqualityTestCase):
  34. """Tests for the builder, which turns tokens into Wikicode objects."""
  35. def setUp(self):
  36. self.builder = Builder()
  37. def test_text(self):
  38. """tests for building Text nodes"""
  39. tests = [
  40. ([tokens.Text(text="foobar")], wraptext("foobar")),
  41. ([tokens.Text(text="fóóbar")], wraptext("fóóbar")),
  42. ([tokens.Text(text="spam"), tokens.Text(text="eggs")],
  43. wraptext("spam", "eggs")),
  44. ]
  45. for test, valid in tests:
  46. self.assertWikicodeEqual(valid, self.builder.build(test))
  47. def test_template(self):
  48. """tests for building Template nodes"""
  49. tests = [
  50. ([tokens.TemplateOpen(), tokens.Text(text="foobar"),
  51. tokens.TemplateClose()],
  52. wrap([Template(wraptext("foobar"))])),
  53. ([tokens.TemplateOpen(), tokens.Text(text="spam"),
  54. tokens.Text(text="eggs"), tokens.TemplateClose()],
  55. wrap([Template(wraptext("spam", "eggs"))])),
  56. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  57. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  58. tokens.TemplateClose()],
  59. wrap([Template(wraptext("foo"), params=[
  60. Parameter(wraptext("1"), wraptext("bar"), showkey=False)])])),
  61. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  62. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  63. tokens.TemplateParamEquals(), tokens.Text(text="baz"),
  64. tokens.TemplateClose()],
  65. wrap([Template(wraptext("foo"), params=[
  66. Parameter(wraptext("bar"), wraptext("baz"))])])),
  67. ([tokens.TemplateOpen(), tokens.TemplateParamSeparator(),
  68. tokens.TemplateParamSeparator(), tokens.TemplateParamEquals(),
  69. tokens.TemplateParamSeparator(), tokens.TemplateClose()],
  70. wrap([Template(wrap([]), params=[
  71. Parameter(wraptext("1"), wrap([]), showkey=False),
  72. Parameter(wrap([]), wrap([]), showkey=True),
  73. Parameter(wraptext("2"), wrap([]), showkey=False)])])),
  74. ([tokens.TemplateOpen(), tokens.Text(text="foo"),
  75. tokens.TemplateParamSeparator(), tokens.Text(text="bar"),
  76. tokens.TemplateParamEquals(), tokens.Text(text="baz"),
  77. tokens.TemplateParamSeparator(), tokens.Text(text="biz"),
  78. tokens.TemplateParamSeparator(), tokens.Text(text="buzz"),
  79. tokens.TemplateParamSeparator(), tokens.Text(text="3"),
  80. tokens.TemplateParamEquals(), tokens.Text(text="buff"),
  81. tokens.TemplateParamSeparator(), tokens.Text(text="baff"),
  82. tokens.TemplateClose()],
  83. wrap([Template(wraptext("foo"), params=[
  84. Parameter(wraptext("bar"), wraptext("baz")),
  85. Parameter(wraptext("1"), wraptext("biz"), showkey=False),
  86. Parameter(wraptext("2"), wraptext("buzz"), showkey=False),
  87. Parameter(wraptext("3"), wraptext("buff")),
  88. Parameter(wraptext("3"), wraptext("baff"),
  89. showkey=False)])])),
  90. ]
  91. for test, valid in tests:
  92. self.assertWikicodeEqual(valid, self.builder.build(test))
  93. def test_argument(self):
  94. """tests for building Argument nodes"""
  95. tests = [
  96. ([tokens.ArgumentOpen(), tokens.Text(text="foobar"),
  97. tokens.ArgumentClose()],
  98. wrap([Argument(wraptext("foobar"))])),
  99. ([tokens.ArgumentOpen(), tokens.Text(text="spam"),
  100. tokens.Text(text="eggs"), tokens.ArgumentClose()],
  101. wrap([Argument(wraptext("spam", "eggs"))])),
  102. ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
  103. tokens.ArgumentSeparator(), tokens.Text(text="bar"),
  104. tokens.ArgumentClose()],
  105. wrap([Argument(wraptext("foo"), wraptext("bar"))])),
  106. ([tokens.ArgumentOpen(), tokens.Text(text="foo"),
  107. tokens.Text(text="bar"), tokens.ArgumentSeparator(),
  108. tokens.Text(text="baz"), tokens.Text(text="biz"),
  109. tokens.ArgumentClose()],
  110. wrap([Argument(wraptext("foo", "bar"), wraptext("baz", "biz"))])),
  111. ]
  112. for test, valid in tests:
  113. self.assertWikicodeEqual(valid, self.builder.build(test))
  114. def test_wikilink(self):
  115. """tests for building Wikilink nodes"""
  116. tests = [
  117. ([tokens.WikilinkOpen(), tokens.Text(text="foobar"),
  118. tokens.WikilinkClose()],
  119. wrap([Wikilink(wraptext("foobar"))])),
  120. ([tokens.WikilinkOpen(), tokens.Text(text="spam"),
  121. tokens.Text(text="eggs"), tokens.WikilinkClose()],
  122. wrap([Wikilink(wraptext("spam", "eggs"))])),
  123. ([tokens.WikilinkOpen(), tokens.Text(text="foo"),
  124. tokens.WikilinkSeparator(), tokens.Text(text="bar"),
  125. tokens.WikilinkClose()],
  126. wrap([Wikilink(wraptext("foo"), wraptext("bar"))])),
  127. ([tokens.WikilinkOpen(), tokens.Text(text="foo"),
  128. tokens.Text(text="bar"), tokens.WikilinkSeparator(),
  129. tokens.Text(text="baz"), tokens.Text(text="biz"),
  130. tokens.WikilinkClose()],
  131. wrap([Wikilink(wraptext("foo", "bar"), wraptext("baz", "biz"))])),
  132. ]
  133. for test, valid in tests:
  134. self.assertWikicodeEqual(valid, self.builder.build(test))
  135. def test_external_link(self):
  136. """tests for building ExternalLink nodes"""
  137. tests = [
  138. ([tokens.ExternalLinkOpen(brackets=False),
  139. tokens.Text(text="http://example.com/"),
  140. tokens.ExternalLinkClose()],
  141. wrap([ExternalLink(wraptext("http://example.com/"),
  142. brackets=False)])),
  143. ([tokens.ExternalLinkOpen(brackets=True),
  144. tokens.Text(text="http://example.com/"),
  145. tokens.ExternalLinkClose()],
  146. wrap([ExternalLink(wraptext("http://example.com/"))])),
  147. ([tokens.ExternalLinkOpen(brackets=True),
  148. tokens.Text(text="http://example.com/"),
  149. tokens.ExternalLinkSeparator(), tokens.ExternalLinkClose()],
  150. wrap([ExternalLink(wraptext("http://example.com/"), wrap([]))])),
  151. ([tokens.ExternalLinkOpen(brackets=True),
  152. tokens.Text(text="http://example.com/"),
  153. tokens.ExternalLinkSeparator(), tokens.Text(text="Example"),
  154. tokens.ExternalLinkClose()],
  155. wrap([ExternalLink(wraptext("http://example.com/"),
  156. wraptext("Example"))])),
  157. ([tokens.ExternalLinkOpen(brackets=False),
  158. tokens.Text(text="http://example"), tokens.Text(text=".com/foo"),
  159. tokens.ExternalLinkClose()],
  160. wrap([ExternalLink(wraptext("http://example", ".com/foo"),
  161. brackets=False)])),
  162. ([tokens.ExternalLinkOpen(brackets=True),
  163. tokens.Text(text="http://example"), tokens.Text(text=".com/foo"),
  164. tokens.ExternalLinkSeparator(), tokens.Text(text="Example"),
  165. tokens.Text(text=" Web Page"), tokens.ExternalLinkClose()],
  166. wrap([ExternalLink(wraptext("http://example", ".com/foo"),
  167. wraptext("Example", " Web Page"))])),
  168. ]
  169. for test, valid in tests:
  170. self.assertWikicodeEqual(valid, self.builder.build(test))
  171. def test_html_entity(self):
  172. """tests for building HTMLEntity nodes"""
  173. tests = [
  174. ([tokens.HTMLEntityStart(), tokens.Text(text="nbsp"),
  175. tokens.HTMLEntityEnd()],
  176. wrap([HTMLEntity("nbsp", named=True, hexadecimal=False)])),
  177. ([tokens.HTMLEntityStart(), tokens.HTMLEntityNumeric(),
  178. tokens.Text(text="107"), tokens.HTMLEntityEnd()],
  179. wrap([HTMLEntity("107", named=False, hexadecimal=False)])),
  180. ([tokens.HTMLEntityStart(), tokens.HTMLEntityNumeric(),
  181. tokens.HTMLEntityHex(char="X"), tokens.Text(text="6B"),
  182. tokens.HTMLEntityEnd()],
  183. wrap([HTMLEntity("6B", named=False, hexadecimal=True,
  184. hex_char="X")])),
  185. ]
  186. for test, valid in tests:
  187. self.assertWikicodeEqual(valid, self.builder.build(test))
  188. def test_heading(self):
  189. """tests for building Heading nodes"""
  190. tests = [
  191. ([tokens.HeadingStart(level=2), tokens.Text(text="foobar"),
  192. tokens.HeadingEnd()],
  193. wrap([Heading(wraptext("foobar"), 2)])),
  194. ([tokens.HeadingStart(level=4), tokens.Text(text="spam"),
  195. tokens.Text(text="eggs"), tokens.HeadingEnd()],
  196. wrap([Heading(wraptext("spam", "eggs"), 4)])),
  197. ]
  198. for test, valid in tests:
  199. self.assertWikicodeEqual(valid, self.builder.build(test))
  200. def test_comment(self):
  201. """tests for building Comment nodes"""
  202. tests = [
  203. ([tokens.CommentStart(), tokens.Text(text="foobar"),
  204. tokens.CommentEnd()],
  205. wrap([Comment(wraptext("foobar"))])),
  206. ([tokens.CommentStart(), tokens.Text(text="spam"),
  207. tokens.Text(text="eggs"), tokens.CommentEnd()],
  208. wrap([Comment(wraptext("spam", "eggs"))])),
  209. ]
  210. for test, valid in tests:
  211. self.assertWikicodeEqual(valid, self.builder.build(test))
  212. def test_tag(self):
  213. """tests for building Tag nodes"""
  214. tests = [
  215. # <ref></ref>
  216. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  217. tokens.TagCloseOpen(padding=""), tokens.TagOpenClose(),
  218. tokens.Text(text="ref"), tokens.TagCloseClose()],
  219. wrap([Tag(wraptext("ref"), wrap([]),
  220. closing_tag=wraptext("ref"))])),
  221. # <ref name></ref>
  222. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  223. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  224. pad_after_eq=""),
  225. tokens.Text(text="name"), tokens.TagCloseOpen(padding=""),
  226. tokens.TagOpenClose(), tokens.Text(text="ref"),
  227. tokens.TagCloseClose()],
  228. wrap([Tag(wraptext("ref"), wrap([]),
  229. attrs=[Attribute(wraptext("name"))])])),
  230. # <ref name="abc" />
  231. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  232. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  233. pad_after_eq=""),
  234. tokens.Text(text="name"), tokens.TagAttrEquals(),
  235. tokens.TagAttrQuote(), tokens.Text(text="abc"),
  236. tokens.TagCloseSelfclose(padding=" ")],
  237. wrap([Tag(wraptext("ref"),
  238. attrs=[Attribute(wraptext("name"), wraptext("abc"))],
  239. self_closing=True, padding=" ")])),
  240. # <br/>
  241. ([tokens.TagOpenOpen(), tokens.Text(text="br"),
  242. tokens.TagCloseSelfclose(padding="")],
  243. wrap([Tag(wraptext("br"), self_closing=True)])),
  244. # <li>
  245. ([tokens.TagOpenOpen(), tokens.Text(text="li"),
  246. tokens.TagCloseSelfclose(padding="", implicit=True)],
  247. wrap([Tag(wraptext("li"), self_closing=True, implicit=True)])),
  248. # </br>
  249. ([tokens.TagOpenOpen(invalid=True), tokens.Text(text="br"),
  250. tokens.TagCloseSelfclose(padding="", implicit=True)],
  251. wrap([Tag(wraptext("br"), self_closing=True, invalid=True,
  252. implicit=True)])),
  253. # </br/>
  254. ([tokens.TagOpenOpen(invalid=True), tokens.Text(text="br"),
  255. tokens.TagCloseSelfclose(padding="")],
  256. wrap([Tag(wraptext("br"), self_closing=True, invalid=True)])),
  257. # <ref name={{abc}} foo="bar {{baz}}" abc={{de}}f ghi=j{{k}}{{l}}
  258. # mno = "{{p}} [[q]] {{r}}">[[Source]]</ref>
  259. ([tokens.TagOpenOpen(), tokens.Text(text="ref"),
  260. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  261. pad_after_eq=""),
  262. tokens.Text(text="name"), tokens.TagAttrEquals(),
  263. tokens.TemplateOpen(), tokens.Text(text="abc"),
  264. tokens.TemplateClose(),
  265. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  266. pad_after_eq=""),
  267. tokens.Text(text="foo"), tokens.TagAttrEquals(),
  268. tokens.TagAttrQuote(), tokens.Text(text="bar "),
  269. tokens.TemplateOpen(), tokens.Text(text="baz"),
  270. tokens.TemplateClose(),
  271. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  272. pad_after_eq=""),
  273. tokens.Text(text="abc"), tokens.TagAttrEquals(),
  274. tokens.TemplateOpen(), tokens.Text(text="de"),
  275. tokens.TemplateClose(), tokens.Text(text="f"),
  276. tokens.TagAttrStart(pad_first=" ", pad_before_eq="",
  277. pad_after_eq=""),
  278. tokens.Text(text="ghi"), tokens.TagAttrEquals(),
  279. tokens.Text(text="j"), tokens.TemplateOpen(),
  280. tokens.Text(text="k"), tokens.TemplateClose(),
  281. tokens.TemplateOpen(), tokens.Text(text="l"),
  282. tokens.TemplateClose(),
  283. tokens.TagAttrStart(pad_first=" \n ", pad_before_eq=" ",
  284. pad_after_eq=" "),
  285. tokens.Text(text="mno"), tokens.TagAttrEquals(),
  286. tokens.TagAttrQuote(), tokens.TemplateOpen(),
  287. tokens.Text(text="p"), tokens.TemplateClose(),
  288. tokens.Text(text=" "), tokens.WikilinkOpen(),
  289. tokens.Text(text="q"), tokens.WikilinkClose(),
  290. tokens.Text(text=" "), tokens.TemplateOpen(),
  291. tokens.Text(text="r"), tokens.TemplateClose(),
  292. tokens.TagCloseOpen(padding=""), tokens.WikilinkOpen(),
  293. tokens.Text(text="Source"), tokens.WikilinkClose(),
  294. tokens.TagOpenClose(), tokens.Text(text="ref"),
  295. tokens.TagCloseClose()],
  296. wrap([Tag(wraptext("ref"), wrap([Wikilink(wraptext("Source"))]), [
  297. Attribute(wraptext("name"),
  298. wrap([Template(wraptext("abc"))]), False),
  299. Attribute(wraptext("foo"), wrap([Text("bar "),
  300. Template(wraptext("baz"))]), pad_first=" "),
  301. Attribute(wraptext("abc"), wrap([Template(wraptext("de")),
  302. Text("f")]), False),
  303. Attribute(wraptext("ghi"), wrap([Text("j"),
  304. Template(wraptext("k")),
  305. Template(wraptext("l"))]), False),
  306. Attribute(wraptext("mno"), wrap([Template(wraptext("p")),
  307. Text(" "), Wikilink(wraptext("q")), Text(" "),
  308. Template(wraptext("r"))]), True, " \n ", " ",
  309. " ")])])),
  310. # "''italic text''"
  311. ([tokens.TagOpenOpen(wiki_markup="''"), tokens.Text(text="i"),
  312. tokens.TagCloseOpen(), tokens.Text(text="italic text"),
  313. tokens.TagOpenClose(), tokens.Text(text="i"),
  314. tokens.TagCloseClose()],
  315. wrap([Tag(wraptext("i"), wraptext("italic text"),
  316. wiki_markup="''")])),
  317. # * bullet
  318. ([tokens.TagOpenOpen(wiki_markup="*"), tokens.Text(text="li"),
  319. tokens.TagCloseSelfclose(), tokens.Text(text=" bullet")],
  320. wrap([Tag(wraptext("li"), wiki_markup="*", self_closing=True),
  321. Text(" bullet")])),
  322. ]
  323. for test, valid in tests:
  324. self.assertWikicodeEqual(valid, self.builder.build(test))
  325. def test_integration(self):
  326. """a test for building a combination of templates together"""
  327. # {{{{{{{{foo}}bar|baz=biz}}buzz}}usr|{{bin}}}}
  328. test = [tokens.TemplateOpen(), tokens.TemplateOpen(),
  329. tokens.TemplateOpen(), tokens.TemplateOpen(),
  330. tokens.Text(text="foo"), tokens.TemplateClose(),
  331. tokens.Text(text="bar"), tokens.TemplateParamSeparator(),
  332. tokens.Text(text="baz"), tokens.TemplateParamEquals(),
  333. tokens.Text(text="biz"), tokens.TemplateClose(),
  334. tokens.Text(text="buzz"), tokens.TemplateClose(),
  335. tokens.Text(text="usr"), tokens.TemplateParamSeparator(),
  336. tokens.TemplateOpen(), tokens.Text(text="bin"),
  337. tokens.TemplateClose(), tokens.TemplateClose()]
  338. valid = wrap(
  339. [Template(wrap([Template(wrap([Template(wrap([Template(wraptext(
  340. "foo")), Text("bar")]), params=[Parameter(wraptext("baz"),
  341. wraptext("biz"))]), Text("buzz")])), Text("usr")]), params=[
  342. Parameter(wraptext("1"), wrap([Template(wraptext("bin"))]),
  343. showkey=False)])])
  344. self.assertWikicodeEqual(valid, self.builder.build(test))
  345. def test_integration2(self):
  346. """an even more audacious test for building a horrible wikicode mess"""
  347. # {{a|b|{{c|[[d]]{{{e}}}}}}}[[f|{{{g}}}<!--h-->]]{{i|j=&nbsp;}}
  348. test = [tokens.TemplateOpen(), tokens.Text(text="a"),
  349. tokens.TemplateParamSeparator(), tokens.Text(text="b"),
  350. tokens.TemplateParamSeparator(), tokens.TemplateOpen(),
  351. tokens.Text(text="c"), tokens.TemplateParamSeparator(),
  352. tokens.WikilinkOpen(), tokens.Text(text="d"),
  353. tokens.WikilinkClose(), tokens.ArgumentOpen(),
  354. tokens.Text(text="e"), tokens.ArgumentClose(),
  355. tokens.TemplateClose(), tokens.TemplateClose(),
  356. tokens.WikilinkOpen(), tokens.Text(text="f"),
  357. tokens.WikilinkSeparator(), tokens.ArgumentOpen(),
  358. tokens.Text(text="g"), tokens.ArgumentClose(),
  359. tokens.CommentStart(), tokens.Text(text="h"),
  360. tokens.CommentEnd(), tokens.WikilinkClose(),
  361. tokens.TemplateOpen(), tokens.Text(text="i"),
  362. tokens.TemplateParamSeparator(), tokens.Text(text="j"),
  363. tokens.TemplateParamEquals(), tokens.HTMLEntityStart(),
  364. tokens.Text(text="nbsp"), tokens.HTMLEntityEnd(),
  365. tokens.TemplateClose()]
  366. valid = wrap(
  367. [Template(wraptext("a"), params=[Parameter(wraptext("1"), wraptext(
  368. "b"), showkey=False), Parameter(wraptext("2"), wrap([Template(
  369. wraptext("c"), params=[Parameter(wraptext("1"), wrap([Wikilink(
  370. wraptext("d")), Argument(wraptext("e"))]), showkey=False)])]),
  371. showkey=False)]), Wikilink(wraptext("f"), wrap([Argument(wraptext(
  372. "g")), Comment(wraptext("h"))])), Template(wraptext("i"), params=[
  373. Parameter(wraptext("j"), wrap([HTMLEntity("nbsp",
  374. named=True)]))])])
  375. self.assertWikicodeEqual(valid, self.builder.build(test))
  376. if __name__ == "__main__":
  377. unittest.main(verbosity=2)