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.
 
 
 
 

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