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.
 
 
 
 

72 lines
3.1 KiB

  1. #
  2. # Copyright (C) 2012-2019 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. from .nodes import Node
  26. from .smart_list import SmartList
  27. __all__ = ["parse_anything"]
  28. def parse_anything(value, context=0, skip_style_tags=False):
  29. """Return a :class:`.Wikicode` for *value*, allowing multiple types.
  30. This differs from :meth:`.Parser.parse` in that we accept more than just a
  31. string to be parsed. Unicode objects (strings in py3k), strings (bytes in
  32. py3k), integers (converted to strings), ``None``, existing :class:`.Node`
  33. or :class:`.Wikicode` objects, as well as an iterable of these types, are
  34. supported. This is used to parse input on-the-fly by various methods of
  35. :class:`.Wikicode` and others like :class:`.Template`, such as
  36. :meth:`wikicode.insert() <.Wikicode.insert>` or setting
  37. :meth:`template.name <.Template.name>`.
  38. Additional arguments are passed directly to :meth:`.Parser.parse`.
  39. """
  40. from .parser import Parser
  41. from .wikicode import Wikicode
  42. if isinstance(value, Wikicode):
  43. return value
  44. elif isinstance(value, Node):
  45. return Wikicode(SmartList([value]))
  46. elif isinstance(value, str):
  47. return Parser().parse(value, context, skip_style_tags)
  48. elif isinstance(value, bytes):
  49. return Parser().parse(value.decode("utf8"), context, skip_style_tags)
  50. elif isinstance(value, int):
  51. return Parser().parse(str(value), context, skip_style_tags)
  52. elif value is None:
  53. return Wikicode(SmartList())
  54. elif 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:
  62. error = "Needs string, Node, Wikicode, file, int, None, or iterable of these, but got {0}: {1}"
  63. raise ValueError(error.format(type(value).__name__, value))