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.
 
 
 
 

94 lines
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. """
  22. Contains data about certain markup, like HTML tags and external links.
  23. When updating this file, please also update the the C tokenizer version:
  24. - mwparserfromhell/parser/ctokenizer/definitions.c
  25. - mwparserfromhell/parser/ctokenizer/definitions.h
  26. """
  27. __all__ = ["get_html_tag", "is_parsable", "is_visible", "is_single",
  28. "is_single_only", "is_scheme"]
  29. URI_SCHEMES = {
  30. # [mediawiki/core.git]/includes/DefaultSettings.php @ 374a0ad943
  31. "http": True, "https": True, "ftp": True, "ftps": True, "ssh": True,
  32. "sftp": True, "irc": True, "ircs": True, "xmpp": False, "sip": False,
  33. "sips": False, "gopher": True, "telnet": True, "nntp": True,
  34. "worldwind": True, "mailto": False, "tel": False, "sms": False,
  35. "news": False, "svn": True, "git": True, "mms": True, "bitcoin": False,
  36. "magnet": False, "urn": False, "geo": False
  37. }
  38. PARSER_BLACKLIST = [
  39. # enwiki extensions @ 2013-06-28
  40. "categorytree", "gallery", "hiero", "imagemap", "inputbox", "math",
  41. "nowiki", "pre", "score", "section", "source", "syntaxhighlight",
  42. "templatedata", "timeline"
  43. ]
  44. INVISIBLE_TAGS = [
  45. # enwiki extensions @ 2013-06-28
  46. "categorytree", "gallery", "imagemap", "inputbox", "math", "score",
  47. "section", "templatedata", "timeline"
  48. ]
  49. # [mediawiki/core.git]/includes/Sanitizer.php @ 065bec63ea
  50. SINGLE_ONLY = ["br", "hr", "meta", "link", "img", "wbr"]
  51. SINGLE = SINGLE_ONLY + ["li", "dt", "dd", "th", "td", "tr"]
  52. MARKUP_TO_HTML = {
  53. "#": "li",
  54. "*": "li",
  55. ";": "dt",
  56. ":": "dd"
  57. }
  58. def get_html_tag(markup):
  59. """Return the HTML tag associated with the given wiki-markup."""
  60. return MARKUP_TO_HTML[markup]
  61. def is_parsable(tag):
  62. """Return if the given *tag*'s contents should be passed to the parser."""
  63. return tag.lower() not in PARSER_BLACKLIST
  64. def is_visible(tag):
  65. """Return whether or not the given *tag* contains visible text."""
  66. return tag.lower() not in INVISIBLE_TAGS
  67. def is_single(tag):
  68. """Return whether or not the given *tag* can exist without a close tag."""
  69. return tag.lower() in SINGLE
  70. def is_single_only(tag):
  71. """Return whether or not the given *tag* must exist without a close tag."""
  72. return tag.lower() in SINGLE_ONLY
  73. def is_scheme(scheme, slashes=True):
  74. """Return whether *scheme* is valid for external links."""
  75. scheme = scheme.lower()
  76. if slashes:
  77. return scheme in URI_SCHEMES
  78. return scheme in URI_SCHEMES and not URI_SCHEMES[scheme]