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.
 
 
 
 

95 lines
3.9 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2014 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. This package contains the actual wikicode parser, split up into two main
  24. modules: the :py:mod:`~.tokenizer` and the :py:mod:`~.builder`. This module
  25. joins them together under one interface.
  26. """
  27. class ParserError(Exception):
  28. """Exception raised when an internal error occurs while parsing.
  29. This does not mean that the wikicode was invalid, because invalid markup
  30. should still be parsed correctly. This means that the parser caught itself
  31. with an impossible internal state and is bailing out before other problems
  32. can happen. Its appearance indicates a bug.
  33. """
  34. def __init__(self, extra):
  35. msg = "This is a bug and should be reported. Info: {0}.".format(extra)
  36. super(ParserError, self).__init__(msg)
  37. from .builder import Builder
  38. from .tokenizer import Tokenizer
  39. try:
  40. from ._tokenizer import CTokenizer
  41. use_c = True
  42. except ImportError:
  43. CTokenizer = None
  44. use_c = False
  45. __all__ = ["use_c", "Parser", "ParserError"]
  46. class Parser(object):
  47. """Represents a parser for wikicode.
  48. Actual parsing is a two-step process: first, the text is split up into a
  49. series of tokens by the :py:class:`.Tokenizer`, and then the tokens are
  50. converted into trees of :py:class:`.Wikicode` objects and
  51. :py:class:`.Node`\ s by the :py:class:`.Builder`.
  52. Instances of this class or its dependents (:py:class:`.Tokenizer` and
  53. :py:class:`.Builder`) should not be shared between threads.
  54. :py:meth:`parse` can be called multiple times as long as it is not done
  55. concurrently. In general, there is no need to do this because parsing
  56. should be done through :py:func:`mwparserfromhell.parse`, which creates a
  57. new :py:class:`.Parser` object as necessary.
  58. """
  59. def __init__(self):
  60. if use_c and CTokenizer:
  61. self._tokenizer = CTokenizer()
  62. else:
  63. self._tokenizer = Tokenizer()
  64. self._builder = Builder()
  65. def parse(self, text, context=0, skip_style_tags=False):
  66. """Parse *text*, returning a :py:class:`~.Wikicode` object tree.
  67. If given, *context* will be passed as a starting context to the parser.
  68. This is helpful when this function is used inside node attribute
  69. setters. For example, :py:class:`~.ExternalLink`\ 's
  70. :py:attr:`~.ExternalLink.url` setter sets *context* to
  71. :py:mod:`contexts.EXT_LINK_URI <.contexts>` to prevent the URL itself
  72. from becoming an :py:class:`~.ExternalLink`.
  73. If *skip_style_tags* is ``True``, then ``''`` and ``'''`` will not be
  74. parsed, but instead will be treated as plain text.
  75. If there is an internal error while parsing, :py:exc:`.ParserError`
  76. will be raised.
  77. """
  78. tokens = self._tokenizer.tokenize(text, context, skip_style_tags)
  79. code = self._builder.build(tokens)
  80. return code