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.
 
 
 
 

105 lines
4.5 KiB

  1. #
  2. # Copyright (C) 2012-2020 Ben Kurtovic <ben.kurtovic@gmail.com>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. """
  22. This module contains the token definitions that are used as an intermediate
  23. parsing data type - they are stored in a flat list, with each token being
  24. identified by its type and optional attributes. The token list is generated in
  25. a syntactically valid form by the :class:`.Tokenizer`, and then converted into
  26. the :class`.Wikicode` tree by the :class:`.Builder`.
  27. """
  28. __all__ = ["Token"]
  29. class Token(dict):
  30. """A token stores the semantic meaning of a unit of wikicode."""
  31. def __repr__(self):
  32. args = []
  33. for key, value in self.items():
  34. if isinstance(value, str) and len(value) > 100:
  35. args.append(key + "=" + repr(value[:97] + "..."))
  36. else:
  37. args.append(key + "=" + repr(value))
  38. return "{}({})".format(type(self).__name__, ", ".join(args))
  39. def __eq__(self, other):
  40. return isinstance(other, type(self)) and dict.__eq__(self, other)
  41. def __ne__(self, other):
  42. return not self.__eq__(other)
  43. def __getattr__(self, key):
  44. return self.get(key)
  45. def __setattr__(self, key, value):
  46. self[key] = value
  47. def __delattr__(self, key):
  48. del self[key]
  49. def make(name):
  50. """Create a new Token class using ``type()`` and add it to ``__all__``."""
  51. __all__.append(name)
  52. return type(name, (Token,), {})
  53. Text = make("Text")
  54. TemplateOpen = make("TemplateOpen") # {{
  55. TemplateParamSeparator = make("TemplateParamSeparator") # |
  56. TemplateParamEquals = make("TemplateParamEquals") # =
  57. TemplateClose = make("TemplateClose") # }}
  58. ArgumentOpen = make("ArgumentOpen") # {{{
  59. ArgumentSeparator = make("ArgumentSeparator") # |
  60. ArgumentClose = make("ArgumentClose") # }}}
  61. WikilinkOpen = make("WikilinkOpen") # [[
  62. WikilinkSeparator = make("WikilinkSeparator") # |
  63. WikilinkClose = make("WikilinkClose") # ]]
  64. ExternalLinkOpen = make("ExternalLinkOpen") # [
  65. ExternalLinkSeparator = make("ExternalLinkSeparator") #
  66. ExternalLinkClose = make("ExternalLinkClose") # ]
  67. HTMLEntityStart = make("HTMLEntityStart") # &
  68. HTMLEntityNumeric = make("HTMLEntityNumeric") # #
  69. HTMLEntityHex = make("HTMLEntityHex") # x
  70. HTMLEntityEnd = make("HTMLEntityEnd") # ;
  71. HeadingStart = make("HeadingStart") # =...
  72. HeadingEnd = make("HeadingEnd") # =...
  73. CommentStart = make("CommentStart") # <!--
  74. CommentEnd = make("CommentEnd") # -->
  75. TagOpenOpen = make("TagOpenOpen") # <
  76. TagAttrStart = make("TagAttrStart")
  77. TagAttrEquals = make("TagAttrEquals") # =
  78. TagAttrQuote = make("TagAttrQuote") # ", '
  79. TagCloseOpen = make("TagCloseOpen") # >
  80. TagCloseSelfclose = make("TagCloseSelfclose") # />
  81. TagOpenClose = make("TagOpenClose") # </
  82. TagCloseClose = make("TagCloseClose") # >
  83. del make