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.
 
 
 
 

70 lines
2.3 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 ._base import Node
  21. from ..utils import parse_anything
  22. __all__ = ["Heading"]
  23. class Heading(Node):
  24. """Represents a section heading in wikicode, like ``== Foo ==``."""
  25. def __init__(self, title, level):
  26. super().__init__()
  27. self.title = title
  28. self.level = level
  29. def __str__(self):
  30. return ("=" * self.level) + str(self.title) + ("=" * self.level)
  31. def __children__(self):
  32. yield self.title
  33. def __strip__(self, **kwargs):
  34. return self.title.strip_code(**kwargs)
  35. def __showtree__(self, write, get, mark):
  36. write("=" * self.level)
  37. get(self.title)
  38. write("=" * self.level)
  39. @property
  40. def title(self):
  41. """The title of the heading, as a :class:`.Wikicode` object."""
  42. return self._title
  43. @property
  44. def level(self):
  45. """The heading level, as an integer between 1 and 6, inclusive."""
  46. return self._level
  47. @title.setter
  48. def title(self, value):
  49. self._title = parse_anything(value)
  50. @level.setter
  51. def level(self, value):
  52. value = int(value)
  53. if value < 1 or value > 6:
  54. raise ValueError(value)
  55. self._level = value