A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

86 rindas
3.5 KiB

  1. # Copyright (C) 2012-2016 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 import parser
  22. from mwparserfromhell.nodes import Tag, Template, Text, Wikilink
  23. from mwparserfromhell.nodes.extras import Parameter
  24. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  25. class TestParser(TreeEqualityTestCase):
  26. """Tests for the Parser class itself, which tokenizes and builds nodes."""
  27. def test_use_c(self):
  28. """make sure the correct tokenizer is used"""
  29. restore = parser.use_c
  30. if parser.use_c:
  31. self.assertTrue(parser.Parser()._tokenizer.USES_C)
  32. parser.use_c = False
  33. self.assertFalse(parser.Parser()._tokenizer.USES_C)
  34. parser.use_c = restore
  35. def test_parsing(self):
  36. """integration test for parsing overall"""
  37. text = "this is text; {{this|is=a|template={{with|[[links]]|in}}it}}"
  38. expected = wrap([
  39. Text("this is text; "),
  40. Template(wraptext("this"), [
  41. Parameter(wraptext("is"), wraptext("a")),
  42. Parameter(wraptext("template"), wrap([
  43. Template(wraptext("with"), [
  44. Parameter(wraptext("1"),
  45. wrap([Wikilink(wraptext("links"))]),
  46. showkey=False),
  47. Parameter(wraptext("2"),
  48. wraptext("in"), showkey=False)
  49. ]),
  50. Text("it")
  51. ]))
  52. ])
  53. ])
  54. actual = parser.Parser().parse(text)
  55. self.assertWikicodeEqual(expected, actual)
  56. def test_skip_style_tags(self):
  57. """test Parser.parse(skip_style_tags=True)"""
  58. def test():
  59. with_style = parser.Parser().parse(text, skip_style_tags=False)
  60. without_style = parser.Parser().parse(text, skip_style_tags=True)
  61. self.assertWikicodeEqual(a, with_style)
  62. self.assertWikicodeEqual(b, without_style)
  63. text = "This is an example with ''italics''!"
  64. a = wrap([Text("This is an example with "),
  65. Tag(wraptext("i"), wraptext("italics"), wiki_markup="''"),
  66. Text("!")])
  67. b = wraptext("This is an example with ''italics''!")
  68. restore = parser.use_c
  69. if parser.use_c:
  70. test()
  71. parser.use_c = False
  72. test()
  73. parser.use_c = restore
  74. if __name__ == "__main__":
  75. unittest.main(verbosity=2)