A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Você não pode selecionar mais de 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.
 
 
 
 

87 linhas
3.5 KiB

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