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.
 
 
 
 

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