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.
 
 
 
 

77 line
2.8 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 regarding certain HTML tags."""
  23. from __future__ import unicode_literals
  24. __all__ = ["get_wiki_markup", "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. WIKI_MARKUP = {
  41. "i": {"open": "''", "close": "''"},
  42. "b": {"open": "'''", "close": "'''"},
  43. "ul": {"open": "*"},
  44. "ol": {"open": "#"},
  45. "dt": {"open": ";"},
  46. "dd": {"open": ":"},
  47. "hr": {"open": "----"},
  48. }
  49. def get_wiki_markup(tag):
  50. """Return the appropriate wiki markup before and after the given *tag*."""
  51. data = WIKI_MARKUP[tag.lower()]
  52. return (data.get("open"), data.get("close"))
  53. def is_parsable(tag):
  54. """Return if the given *tag*'s contents should be passed to the parser."""
  55. return tag.lower() not in PARSER_BLACKLIST
  56. def is_visible(tag):
  57. """Return whether or not the given *tag* contains visible text."""
  58. return tag.lower() not in INVISIBLE_TAGS
  59. def is_single(tag):
  60. """Return whether or not the given *tag* can exist without a close tag."""
  61. return tag.lower() in SINGLE
  62. def is_single_only(tag):
  63. """Return whether or not the given *tag* must exist without a close tag."""
  64. return tag.lower() in SINGLE_ONLY