A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

102 line
3.7 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. """
  21. Tests for the Parser class itself, which tokenizes and builds nodes.
  22. """
  23. import pytest
  24. from mwparserfromhell import parser
  25. from mwparserfromhell.nodes import Tag, Template, Text, Wikilink
  26. from mwparserfromhell.nodes.extras import Parameter
  27. from .conftest import assert_wikicode_equal, wrap, wraptext
  28. @pytest.fixture()
  29. def pyparser():
  30. """make sure the correct tokenizer is used"""
  31. restore = parser.use_c
  32. if parser.use_c:
  33. parser.use_c = False
  34. yield
  35. parser.use_c = restore
  36. def test_use_c(pyparser):
  37. assert parser.Parser()._tokenizer.USES_C is False
  38. def test_parsing(pyparser):
  39. """integration test for parsing overall"""
  40. text = "this is text; {{this|is=a|template={{with|[[links]]|in}}it}}"
  41. expected = wrap(
  42. [
  43. Text("this is text; "),
  44. Template(
  45. wraptext("this"),
  46. [
  47. Parameter(wraptext("is"), wraptext("a")),
  48. Parameter(
  49. wraptext("template"),
  50. wrap(
  51. [
  52. Template(
  53. wraptext("with"),
  54. [
  55. Parameter(
  56. wraptext("1"),
  57. wrap([Wikilink(wraptext("links"))]),
  58. showkey=False,
  59. ),
  60. Parameter(
  61. wraptext("2"), wraptext("in"), showkey=False
  62. ),
  63. ],
  64. ),
  65. Text("it"),
  66. ]
  67. ),
  68. ),
  69. ],
  70. ),
  71. ]
  72. )
  73. actual = parser.Parser().parse(text)
  74. assert_wikicode_equal(expected, actual)
  75. def test_skip_style_tags(pyparser):
  76. """test Parser.parse(skip_style_tags=True)"""
  77. text = "This is an example with ''italics''!"
  78. a = wrap(
  79. [
  80. Text("This is an example with "),
  81. Tag(wraptext("i"), wraptext("italics"), wiki_markup="''"),
  82. Text("!"),
  83. ]
  84. )
  85. b = wraptext("This is an example with ''italics''!")
  86. with_style = parser.Parser().parse(text, skip_style_tags=False)
  87. without_style = parser.Parser().parse(text, skip_style_tags=True)
  88. assert_wikicode_equal(a, with_style)
  89. assert_wikicode_equal(b, without_style)