A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

70 linhas
3.1 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 module contains accessory functions for other parts of the library. Parser
  22. users generally won't need stuff from here.
  23. """
  24. __all__ = ["parse_anything"]
  25. def parse_anything(value, context=0, skip_style_tags=False):
  26. """Return a :class:`.Wikicode` for *value*, allowing multiple types.
  27. This differs from :meth:`.Parser.parse` in that we accept more than just a
  28. string to be parsed. Strings, bytes, integers (converted to strings),
  29. ``None``, existing :class:`.Node` or :class:`.Wikicode` objects, as well
  30. as an iterable of these types, are supported. This is used to parse input
  31. on-the-fly by various methods of :class:`.Wikicode` and others like
  32. :class:`.Template`, such as :meth:`wikicode.insert() <.Wikicode.insert>`
  33. or setting :meth:`template.name <.Template.name>`.
  34. Additional arguments are passed directly to :meth:`.Parser.parse`.
  35. """
  36. # pylint: disable=cyclic-import,import-outside-toplevel
  37. from .nodes import Node
  38. from .parser import Parser
  39. from .smart_list import SmartList
  40. from .wikicode import Wikicode
  41. if isinstance(value, Wikicode):
  42. return value
  43. if isinstance(value, Node):
  44. return Wikicode(SmartList([value]))
  45. if isinstance(value, str):
  46. return Parser().parse(value, context, skip_style_tags)
  47. if isinstance(value, bytes):
  48. return Parser().parse(value.decode("utf8"), context, skip_style_tags)
  49. if isinstance(value, int):
  50. return Parser().parse(str(value), context, skip_style_tags)
  51. if value is None:
  52. return Wikicode(SmartList())
  53. if hasattr(value, "read"):
  54. return parse_anything(value.read(), context, skip_style_tags)
  55. try:
  56. nodelist = SmartList()
  57. for item in value:
  58. nodelist += parse_anything(item, context, skip_style_tags).nodes
  59. return Wikicode(nodelist)
  60. except TypeError as exc:
  61. error = ("Needs string, Node, Wikicode, file, int, None, or "
  62. "iterable of these, but got {0}: {1}")
  63. raise ValueError(error.format(type(value).__name__, value)) from exc