A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

68 lines
2.5 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_wikicode", "is_parsable", "is_visible"]
  25. PARSER_BLACKLIST = [
  26. # enwiki extensions @ 2013-06-28
  27. "categorytree", "gallery", "hiero", "imagemap", "inputbox", "math",
  28. "nowiki", "pre", "score", "section", "source", "syntaxhighlight",
  29. "templatedata", "timeline"
  30. ]
  31. INVISIBLE_TAGS = [
  32. # enwiki extensions @ 2013-06-28
  33. "categorytree", "gallery", "imagemap", "inputbox", "math", "score",
  34. "section", "templatedata", "timeline"
  35. ]
  36. # [mediawiki/core.git]/includes/Sanitizer.php @ 87a0aef762
  37. SINGLE_ONLY = ["br", "hr", "meta", "link", "img"]
  38. SINGLE = SINGLE_ONLY + ["li", "dt", "dd"]
  39. WIKICODE = {
  40. "i": {"open": "''", "close": "''"},
  41. "b": {"open": "'''", "close": "'''"},
  42. "ul": {"open": "*"},
  43. "ol": {"open": "#"},
  44. "dt": {"open": ";"},
  45. "dd": {"open": ":"},
  46. "hr": {"open": "----"},
  47. }
  48. def get_wikicode(tag):
  49. """Return the appropriate wikicode before and after the given *tag*."""
  50. data = WIKICODE[tag.lower()]
  51. return (data.get("open"), data.get("close"))
  52. def is_parsable(tag):
  53. """Return if the given *tag*'s contents should be passed to the parser."""
  54. return tag.lower() not in PARSER_BLACKLIST
  55. def is_visible(tag):
  56. """Return whether or not the given *tag* contains visible text."""
  57. return tag.lower() not in INVISIBLE_TAGS