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.
 
 
 
 

78 lines
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. """
  23. This package contains :py:class:`~.Wikicode` "nodes", which represent a single
  24. unit of wikitext, such as a Template, an HTML tag, a Heading, or plain text.
  25. The node "tree" is far from flat, as most types can contain additional
  26. :py:class:`~.Wikicode` types within them - and with that, more nodes. For
  27. example, the name of a :py:class:`~.Template` is a :py:class:`~.Wikicode`
  28. object that can contain text or more templates.
  29. """
  30. from __future__ import unicode_literals
  31. from ..compat import str
  32. from ..string_mixin import StringMixIn
  33. __all__ = ["Node", "Text", "Argument", "Heading", "HTMLEntity", "Tag",
  34. "Template"]
  35. class Node(StringMixIn):
  36. """Represents the base Node type, demonstrating the methods to override.
  37. :py:meth:`__unicode__` must be overridden. It should return a ``unicode``
  38. or (``str`` in py3k) representation of the node. If the node contains
  39. :py:class:`~.Wikicode` objects inside of it, :py:meth:`__children__`
  40. should be a generator that iterates over them. If the node is printable
  41. (shown when the page is rendered), :py:meth:`__strip__` should return its
  42. printable version, stripping out any formatting marks. It does not have to
  43. return a string, but something that can be converted to a string with
  44. ``str()``. Finally, :py:meth:`__showtree__` can be overridden to build a
  45. nice tree representation of the node, if desired, for
  46. :py:meth:`~.Wikicode.get_tree`.
  47. """
  48. def __unicode__(self):
  49. raise NotImplementedError()
  50. def __children__(self):
  51. return
  52. yield # pragma: no cover (this is a generator that yields nothing)
  53. def __strip__(self, normalize, collapse):
  54. return None
  55. def __showtree__(self, write, get, mark):
  56. write(str(self))
  57. from . import extras
  58. from .text import Text
  59. from .argument import Argument
  60. from .comment import Comment
  61. from .external_link import ExternalLink
  62. from .heading import Heading
  63. from .html_entity import HTMLEntity
  64. from .tag import Tag
  65. from .template import Template
  66. from .wikilink import Wikilink