A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

94 líneas
3.7 KiB

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