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.
 
 
 
 

128 linhas
4.4 KiB

  1. # Copyright (C) 2012-2025 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. from __future__ import annotations
  24. import pytest
  25. from mwparserfromhell import parser
  26. from mwparserfromhell.nodes import Tag, Template, Text, Wikilink
  27. from mwparserfromhell.nodes.extras import Parameter
  28. from .conftest import assert_wikicode_equal, wrap, wraptext
  29. @pytest.fixture()
  30. def pyparser():
  31. """make sure the correct tokenizer is used"""
  32. restore = parser.use_c
  33. if parser.use_c:
  34. parser.use_c = False
  35. yield
  36. parser.use_c = restore
  37. def test_use_c(pyparser):
  38. assert parser.Parser()._tokenizer.USES_C is False
  39. def test_parsing(pyparser):
  40. """integration test for parsing overall"""
  41. text = "this is text; {{this|is=a|template={{with|[[links]]|in}}it}}"
  42. expected = wrap(
  43. [
  44. Text("this is text; "),
  45. Template(
  46. wraptext("this"),
  47. [
  48. Parameter(wraptext("is"), wraptext("a")),
  49. Parameter(
  50. wraptext("template"),
  51. wrap(
  52. [
  53. Template(
  54. wraptext("with"),
  55. [
  56. Parameter(
  57. wraptext("1"),
  58. wrap([Wikilink(wraptext("links"))]),
  59. showkey=False,
  60. ),
  61. Parameter(
  62. wraptext("2"), wraptext("in"), showkey=False
  63. ),
  64. ],
  65. ),
  66. Text("it"),
  67. ]
  68. ),
  69. ),
  70. ],
  71. ),
  72. ]
  73. )
  74. actual = parser.Parser().parse(text)
  75. assert_wikicode_equal(expected, actual)
  76. def test_skip_style_tags(pyparser):
  77. """test Parser.parse(skip_style_tags=True)"""
  78. text = "This is an example with ''italics''!"
  79. a = wrap(
  80. [
  81. Text("This is an example with "),
  82. Tag(wraptext("i"), wraptext("italics"), wiki_markup="''"),
  83. Text("!"),
  84. ]
  85. )
  86. b = wraptext("This is an example with ''italics''!")
  87. with_style = parser.Parser().parse(text, skip_style_tags=False)
  88. without_style = parser.Parser().parse(text, skip_style_tags=True)
  89. assert_wikicode_equal(a, with_style)
  90. assert_wikicode_equal(b, without_style)
  91. @pytest.mark.parametrize(
  92. "text",
  93. [
  94. "[[]]",
  95. "[[|]]",
  96. "[[|foo]]",
  97. "[[|||]]",
  98. ],
  99. )
  100. def test_empty_title_wikilink_filter_wikilinks(text):
  101. """[[]] and [[|...]] have an empty title and should not be links.
  102. MediaWiki does not render these as hyperlinks; filter_wikilinks() should
  103. not return false positives, while the parsed Wikilink node remains
  104. available to tools that want to detect and fix invalid wikilinks.
  105. Regression test for https://github.com/earwig/mwparserfromhell/issues/292.
  106. """
  107. parsed = parser.Parser().parse(text)
  108. assert parsed.filter_wikilinks() == []
  109. assert isinstance(parsed.get(0), Wikilink)
  110. assert str(parsed) == text