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.
 
 
 
 

92 lines
3.4 KiB

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