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.
 
 
 
 

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