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.
 
 
 
 

87 lines
3.4 KiB

  1. # Copyright (C) 2012-2020 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. """
  21. This package contains the actual wikicode parser, split up into two main
  22. modules: the :mod:`.tokenizer` and the :mod:`.builder`. This module joins them
  23. together into one interface.
  24. """
  25. from .builder import Builder
  26. from .errors import ParserError
  27. try:
  28. from ._tokenizer import CTokenizer
  29. use_c = True
  30. except ImportError:
  31. from .tokenizer import Tokenizer
  32. CTokenizer = None
  33. use_c = False
  34. __all__ = ["use_c", "Parser", "ParserError"]
  35. class Parser:
  36. """Represents a parser for wikicode.
  37. Actual parsing is a two-step process: first, the text is split up into a
  38. series of tokens by the :class:`.Tokenizer`, and then the tokens are
  39. converted into trees of :class:`.Wikicode` objects and :class:`.Node`\\ s
  40. by the :class:`.Builder`.
  41. Instances of this class or its dependents (:class:`.Tokenizer` and
  42. :class:`.Builder`) should not be shared between threads. :meth:`parse` can
  43. be called multiple times as long as it is not done concurrently. In
  44. general, there is no need to do this because parsing should be done through
  45. :func:`mwparserfromhell.parse`, which creates a new :class:`.Parser` object
  46. as necessary.
  47. """
  48. def __init__(self):
  49. if use_c and CTokenizer:
  50. self._tokenizer = CTokenizer()
  51. else:
  52. from .tokenizer import Tokenizer
  53. self._tokenizer = Tokenizer()
  54. self._builder = Builder()
  55. def parse(self, text, context=0, skip_style_tags=False):
  56. """Parse *text*, returning a :class:`.Wikicode` object tree.
  57. If given, *context* will be passed as a starting context to the parser.
  58. This is helpful when this function is used inside node attribute
  59. setters. For example, :class:`.ExternalLink`\\ 's
  60. :attr:`~.ExternalLink.url` setter sets *context* to
  61. :mod:`contexts.EXT_LINK_URI <.contexts>` to prevent the URL itself
  62. from becoming an :class:`.ExternalLink`.
  63. If *skip_style_tags* is ``True``, then ``''`` and ``'''`` will not be
  64. parsed, but instead will be treated as plain text.
  65. If there is an internal error while parsing, :exc:`.ParserError` will
  66. be raised.
  67. """
  68. tokens = self._tokenizer.tokenize(text, context, skip_style_tags)
  69. code = self._builder.build(tokens)
  70. return code