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.
 
 
 
 

83 lines
3.4 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 package contains the actual wikicode parser, split up into two main
  23. modules: the :mod:`.tokenizer` and the :mod:`.builder`. This module joins them
  24. together into one interface.
  25. """
  26. from .builder import Builder
  27. from .errors import ParserError
  28. try:
  29. from ._tokenizer import CTokenizer
  30. use_c = True
  31. except ImportError:
  32. from .tokenizer import Tokenizer
  33. CTokenizer = None
  34. use_c = False
  35. __all__ = ["use_c", "Parser", "ParserError"]
  36. class Parser:
  37. """Represents a parser for wikicode.
  38. Actual parsing is a two-step process: first, the text is split up into a
  39. series of tokens by the :class:`.Tokenizer`, and then the tokens are
  40. converted into trees of :class:`.Wikicode` objects and :class:`.Node`\\ s
  41. by the :class:`.Builder`.
  42. Instances of this class or its dependents (:class:`.Tokenizer` and
  43. :class:`.Builder`) should not be shared between threads. :meth:`parse` can
  44. be called multiple times as long as it is not done concurrently. In
  45. general, there is no need to do this because parsing should be done through
  46. :func:`mwparserfromhell.parse`, which creates a new :class:`.Parser` object
  47. as necessary.
  48. """
  49. def __init__(self):
  50. if use_c and CTokenizer:
  51. self._tokenizer = CTokenizer()
  52. else:
  53. from .tokenizer import Tokenizer
  54. self._tokenizer = Tokenizer()
  55. self._builder = Builder()
  56. def parse(self, text, context=0, skip_style_tags=False):
  57. """Parse *text*, returning a :class:`.Wikicode` object tree.
  58. If given, *context* will be passed as a starting context to the parser.
  59. This is helpful when this function is used inside node attribute
  60. setters. For example, :class:`.ExternalLink`\\ 's
  61. :attr:`~.ExternalLink.url` setter sets *context* to
  62. :mod:`contexts.EXT_LINK_URI <.contexts>` to prevent the URL itself
  63. from becoming an :class:`.ExternalLink`.
  64. If *skip_style_tags* is ``True``, then ``''`` and ``'''`` will not be
  65. parsed, but instead will be treated as plain text.
  66. If there is an internal error while parsing, :exc:`.ParserError` will
  67. be raised.
  68. """
  69. tokens = self._tokenizer.tokenize(text, context, skip_style_tags)
  70. code = self._builder.build(tokens)
  71. return code