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.
 
 
 
 

53 lines
2.2 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. from ..string_mixin import StringMixIn
  21. __all__ = ["Node"]
  22. class Node(StringMixIn):
  23. """Represents the base Node type, demonstrating the methods to override.
  24. :meth:`__str__` must be overridden. It should return a ``str``
  25. representation of the node. If the node contains :class:`.Wikicode`
  26. objects inside of it, :meth:`__children__` should be a generator that
  27. iterates over them. If the node is printable (shown when the page is
  28. rendered), :meth:`__strip__` should return its printable version,
  29. stripping out any formatting marks. It does not have to return a string,
  30. but something that can be converted to a string with ``str()``. Finally,
  31. :meth:`__showtree__` can be overridden to build a nice tree representation
  32. of the node, if desired, for :meth:`~.Wikicode.get_tree`.
  33. """
  34. def __str__(self):
  35. raise NotImplementedError()
  36. def __children__(self):
  37. return
  38. # pylint: disable=unreachable
  39. yield # pragma: no cover (this is a generator that yields nothing)
  40. def __strip__(self, **kwargs):
  41. return None
  42. def __showtree__(self, write, get, mark):
  43. write(str(self))