A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

229 linhas
10 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.compat import str
  25. from mwparserfromhell.nodes import Tag, Template, Text
  26. from mwparserfromhell.nodes.extras import Attribute
  27. from ._test_tree_equality import TreeEqualityTestCase, getnodes, wrap, wraptext
  28. agen = lambda name, value: Attribute(wraptext(name), wraptext(value))
  29. agennq = lambda name, value: Attribute(wraptext(name), wraptext(value), False)
  30. agenpnv = lambda name, a, b, c: Attribute(wraptext(name), None, True, a, b, c)
  31. class TestTag(TreeEqualityTestCase):
  32. """Test cases for the Tag node."""
  33. def test_unicode(self):
  34. """test Tag.__unicode__()"""
  35. node1 = Tag(wraptext("ref"))
  36. node2 = Tag(wraptext("span"), wraptext("foo"),
  37. [agen("style", "color: red;")])
  38. node3 = Tag(wraptext("ref"),
  39. attrs=[agennq("name", "foo"),
  40. agenpnv("some_attr", " ", "", "")],
  41. self_closing=True)
  42. node4 = Tag(wraptext("br"), self_closing=True, padding=" ")
  43. node5 = Tag(wraptext("br"), self_closing=True, implicit=True)
  44. node6 = Tag(wraptext("br"), self_closing=True, invalid=True,
  45. implicit=True)
  46. node7 = Tag(wraptext("br"), self_closing=True, invalid=True,
  47. padding=" ")
  48. node8 = Tag(wraptext("hr"), wiki_markup="----", self_closing=True)
  49. node9 = Tag(wraptext("i"), wraptext("italics!"), wiki_markup="''")
  50. self.assertEqual("<ref></ref>", str(node1))
  51. self.assertEqual('<span style="color: red;">foo</span>', str(node2))
  52. self.assertEqual("<ref name=foo some_attr/>", str(node3))
  53. self.assertEqual("<br />", str(node4))
  54. self.assertEqual("<br>", str(node5))
  55. self.assertEqual("</br>", str(node6))
  56. self.assertEqual("</br />", str(node7))
  57. self.assertEqual("----", str(node8))
  58. self.assertEqual("''italics!''", str(node9))
  59. def test_iternodes(self):
  60. """test Tag.__iternodes__()"""
  61. node1n1, node1n2 = Text("ref"), Text("foobar")
  62. node2n1, node3n1, node3n2 = Text("bold text"), Text("img"), Text("id")
  63. node3n3, node3n4, node3n5 = Text("foo"), Text("class"), Text("bar")
  64. # <ref>foobar</ref>
  65. node1 = Tag(wrap([node1n1]), wrap([node1n2]))
  66. # '''bold text'''
  67. node2 = Tag(wraptext("b"), wrap([node2n1]), wiki_markup="'''")
  68. # <img id="foo" class="bar" />
  69. node3 = Tag(wrap([node3n1]),
  70. attrs=[Attribute(wrap([node3n2]), wrap([node3n3])),
  71. Attribute(wrap([node3n4]), wrap([node3n5]))],
  72. self_closing=True, padding=" ")
  73. gen1 = node1.__iternodes__(getnodes)
  74. gen2 = node2.__iternodes__(getnodes)
  75. gen3 = node3.__iternodes__(getnodes)
  76. self.assertEqual((None, node1), next(gen1))
  77. self.assertEqual((None, node2), next(gen2))
  78. self.assertEqual((None, node3), next(gen3))
  79. self.assertEqual((node1.tag, node1n1), next(gen1))
  80. self.assertEqual((node3.tag, node3n1), next(gen3))
  81. self.assertEqual((node3.attributes[0].name, node3n2), next(gen3))
  82. self.assertEqual((node3.attributes[0].value, node3n3), next(gen3))
  83. self.assertEqual((node3.attributes[1].name, node3n4), next(gen3))
  84. self.assertEqual((node3.attributes[1].value, node3n5), next(gen3))
  85. self.assertEqual((node1.contents, node1n2), next(gen1))
  86. self.assertEqual((node2.contents, node2n1), next(gen2))
  87. self.assertEqual((node1.closing_tag, node1n1), next(gen1))
  88. self.assertRaises(StopIteration, next, gen1)
  89. self.assertRaises(StopIteration, next, gen2)
  90. self.assertRaises(StopIteration, next, gen3)
  91. def test_strip(self):
  92. """test Tag.__strip__()"""
  93. node1 = Tag(wraptext("i"), wraptext("foobar"))
  94. node2 = Tag(wraptext("math"), wraptext("foobar"))
  95. node3 = Tag(wraptext("br"), self_closing=True)
  96. for a in (True, False):
  97. for b in (True, False):
  98. self.assertEqual("foobar", node1.__strip__(a, b))
  99. self.assertEqual(None, node2.__strip__(a, b))
  100. self.assertEqual(None, node3.__strip__(a, b))
  101. def test_showtree(self):
  102. """test Tag.__showtree__()"""
  103. output = []
  104. getter, marker = object(), object()
  105. get = lambda code: output.append((getter, code))
  106. mark = lambda: output.append(marker)
  107. node1 = Tag(wraptext("ref"), wraptext("text"), [agen("name", "foo")])
  108. node2 = Tag(wraptext("br"), self_closing=True, padding=" ")
  109. node3 = Tag(wraptext("br"), self_closing=True, invalid=True,
  110. implicit=True, padding=" ")
  111. node1.__showtree__(output.append, get, mark)
  112. node2.__showtree__(output.append, get, mark)
  113. node3.__showtree__(output.append, get, mark)
  114. valid = [
  115. "<", (getter, node1.tag), (getter, node1.attributes[0].name),
  116. " = ", marker, (getter, node1.attributes[0].value), ">",
  117. (getter, node1.contents), "</", (getter, node1.closing_tag), ">",
  118. "<", (getter, node2.tag), "/>", "</", (getter, node3.tag), ">"]
  119. self.assertEqual(valid, output)
  120. def test_tag(self):
  121. """test getter/setter for the tag attribute"""
  122. tag = wraptext("ref")
  123. node = Tag(tag, wraptext("text"))
  124. self.assertIs(tag, node.tag)
  125. self.assertIs(tag, node.closing_tag)
  126. node.tag = "span"
  127. self.assertWikicodeEqual(wraptext("span"), node.tag)
  128. self.assertWikicodeEqual(wraptext("span"), node.closing_tag)
  129. self.assertEqual("<span>text</span>", node)
  130. def test_contents(self):
  131. """test getter/setter for the contents attribute"""
  132. contents = wraptext("text")
  133. node = Tag(wraptext("ref"), contents)
  134. self.assertIs(contents, node.contents)
  135. node.contents = "text and a {{template}}"
  136. parsed = wrap([Text("text and a "), Template(wraptext("template"))])
  137. self.assertWikicodeEqual(parsed, node.contents)
  138. self.assertEqual("<ref>text and a {{template}}</ref>", node)
  139. def test_attributes(self):
  140. """test getter for the attributes attribute"""
  141. attrs = [agen("name", "bar")]
  142. node1 = Tag(wraptext("ref"), wraptext("foo"))
  143. node2 = Tag(wraptext("ref"), wraptext("foo"), attrs)
  144. self.assertEqual([], node1.attributes)
  145. self.assertIs(attrs, node2.attributes)
  146. def test_wiki_markup(self):
  147. """test getter/setter for the wiki_markup attribute"""
  148. node = Tag(wraptext("i"), wraptext("italic text"))
  149. self.assertIs(None, node.wiki_markup)
  150. node.wiki_markup = "''"
  151. self.assertEqual("''", node.wiki_markup)
  152. self.assertEqual("''italic text''", node)
  153. node.wiki_markup = False
  154. self.assertFalse(node.wiki_markup)
  155. self.assertEqual("<i>italic text</i>", node)
  156. def test_self_closing(self):
  157. """test getter/setter for the self_closing attribute"""
  158. node = Tag(wraptext("ref"), wraptext("foobar"))
  159. self.assertFalse(node.self_closing)
  160. node.self_closing = True
  161. self.assertTrue(node.self_closing)
  162. self.assertEqual("<ref/>", node)
  163. node.self_closing = 0
  164. self.assertFalse(node.self_closing)
  165. self.assertEqual("<ref>foobar</ref>", node)
  166. def test_invalid(self):
  167. """test getter/setter for the invalid attribute"""
  168. node = Tag(wraptext("br"), self_closing=True, implicit=True)
  169. self.assertFalse(node.invalid)
  170. node.invalid = True
  171. self.assertTrue(node.invalid)
  172. self.assertEqual("</br>", node)
  173. node.invalid = 0
  174. self.assertFalse(node.invalid)
  175. self.assertEqual("<br>", node)
  176. def test_implicit(self):
  177. """test getter/setter for the implicit attribute"""
  178. node = Tag(wraptext("br"), self_closing=True)
  179. self.assertFalse(node.implicit)
  180. node.implicit = True
  181. self.assertTrue(node.implicit)
  182. self.assertEqual("<br>", node)
  183. node.implicit = 0
  184. self.assertFalse(node.implicit)
  185. self.assertEqual("<br/>", node)
  186. def test_padding(self):
  187. """test getter/setter for the padding attribute"""
  188. node = Tag(wraptext("ref"), wraptext("foobar"))
  189. self.assertEqual("", node.padding)
  190. node.padding = " "
  191. self.assertEqual(" ", node.padding)
  192. self.assertEqual("<ref >foobar</ref>", node)
  193. node.padding = None
  194. self.assertEqual("", node.padding)
  195. self.assertEqual("<ref>foobar</ref>", node)
  196. self.assertRaises(ValueError, setattr, node, "padding", True)
  197. def test_closing_tag(self):
  198. """test getter/setter for the closing_tag attribute"""
  199. tag = wraptext("ref")
  200. node = Tag(tag, wraptext("foobar"))
  201. self.assertIs(tag, node.closing_tag)
  202. node.closing_tag = "ref {{ignore me}}"
  203. parsed = wrap([Text("ref "), Template(wraptext("ignore me"))])
  204. self.assertWikicodeEqual(parsed, node.closing_tag)
  205. self.assertEqual("<ref>foobar</ref {{ignore me}}>", node)
  206. if __name__ == "__main__":
  207. unittest.main(verbosity=2)