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.

external_link.py 3.0 KiB

пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
пре 10 година
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. from . 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 __unicode__(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. from ..parser import contexts
  70. self._url = parse_anything(value, contexts.EXT_LINK_URI)
  71. @title.setter
  72. def title(self, value):
  73. self._title = None if value is None else parse_anything(value)
  74. @brackets.setter
  75. def brackets(self, value):
  76. self._brackets = bool(value)