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.
 
 
 
 

93 lines
3.0 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. from ._base import Node
  22. from ..utils import parse_anything
  23. __all__ = ["ExternalLink"]
  24. class ExternalLink(Node):
  25. """Represents an external link, like ``[http://example.com/ Example]``."""
  26. def __init__(self, url, title=None, brackets=True):
  27. super().__init__()
  28. self.url = url
  29. self.title = title
  30. self.brackets = brackets
  31. def __str__(self):
  32. if self.brackets:
  33. if self.title is not None:
  34. return "[" + str(self.url) + " " + str(self.title) + "]"
  35. return "[" + str(self.url) + "]"
  36. return str(self.url)
  37. def __children__(self):
  38. yield self.url
  39. if self.title is not None:
  40. yield self.title
  41. def __strip__(self, **kwargs):
  42. if self.brackets:
  43. if self.title:
  44. return self.title.strip_code(**kwargs)
  45. return None
  46. return self.url.strip_code(**kwargs)
  47. def __showtree__(self, write, get, mark):
  48. if self.brackets:
  49. write("[")
  50. get(self.url)
  51. if self.title is not None:
  52. get(self.title)
  53. if self.brackets:
  54. write("]")
  55. @property
  56. def url(self):
  57. """The URL of the link target, as a :class:`.Wikicode` object."""
  58. return self._url
  59. @property
  60. def title(self):
  61. """The link title (if given), as a :class:`.Wikicode` object."""
  62. return self._title
  63. @property
  64. def brackets(self):
  65. """Whether to enclose the URL in brackets or display it straight."""
  66. return self._brackets
  67. @url.setter
  68. def url(self, value):
  69. # pylint: disable=import-outside-toplevel
  70. from ..parser import contexts
  71. self._url = parse_anything(value, contexts.EXT_LINK_URI)
  72. @title.setter
  73. def title(self, value):
  74. self._title = None if value is None else parse_anything(value)
  75. @brackets.setter
  76. def brackets(self, value):
  77. self._brackets = bool(value)