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.
 
 
 
 

73 lines
2.6 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"]
  26. PARSER_BLACKLIST = [
  27. # enwiki extensions @ 2013-06-28
  28. "categorytree", "gallery", "hiero", "imagemap", "inputbox", "math",
  29. "nowiki", "pre", "score", "section", "source", "syntaxhighlight",
  30. "templatedata", "timeline"
  31. ]
  32. INVISIBLE_TAGS = [
  33. # enwiki extensions @ 2013-06-28
  34. "categorytree", "gallery", "imagemap", "inputbox", "math", "score",
  35. "section", "templatedata", "timeline"
  36. ]
  37. # [mediawiki/core.git]/includes/Sanitizer.php @ 87a0aef762
  38. SINGLE_ONLY = ["br", "hr", "meta", "link", "img"]
  39. SINGLE = SINGLE_ONLY + ["li", "dt", "dd"]
  40. MARKUP_TO_HTML = {
  41. "#": "li",
  42. "*": "li",
  43. ";": "dt",
  44. ":": "dd"
  45. }
  46. def get_html_tag(markup):
  47. """Return the HTML tag associated with the given wiki-markup."""
  48. return MARKUP_TO_HTML[markup]
  49. def is_parsable(tag):
  50. """Return if the given *tag*'s contents should be passed to the parser."""
  51. return tag.lower() not in PARSER_BLACKLIST
  52. def is_visible(tag):
  53. """Return whether or not the given *tag* contains visible text."""
  54. return tag.lower() not in INVISIBLE_TAGS
  55. def is_single(tag):
  56. """Return whether or not the given *tag* can exist without a close tag."""
  57. return tag.lower() in SINGLE
  58. def is_single_only(tag):
  59. """Return whether or not the given *tag* must exist without a close tag."""
  60. return tag.lower() in SINGLE_ONLY