A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

92 rader
3.0 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):
  26. super().__init__()
  27. self.url = url
  28. self.title = title
  29. self.brackets = brackets
  30. def __str__(self):
  31. if self.brackets:
  32. if self.title is not None:
  33. return "[" + str(self.url) + " " + str(self.title) + "]"
  34. return "[" + str(self.url) + "]"
  35. return str(self.url)
  36. def __children__(self):
  37. yield self.url
  38. if self.title is not None:
  39. yield self.title
  40. def __strip__(self, **kwargs):
  41. if self.brackets:
  42. if self.title:
  43. return self.title.strip_code(**kwargs)
  44. return None
  45. return self.url.strip_code(**kwargs)
  46. def __showtree__(self, write, get, mark):
  47. if self.brackets:
  48. write("[")
  49. get(self.url)
  50. if self.title is not None:
  51. get(self.title)
  52. if self.brackets:
  53. write("]")
  54. @property
  55. def url(self):
  56. """The URL of the link target, as a :class:`.Wikicode` object."""
  57. return self._url
  58. @property
  59. def title(self):
  60. """The link title (if given), as a :class:`.Wikicode` object."""
  61. return self._title
  62. @property
  63. def brackets(self):
  64. """Whether to enclose the URL in brackets or display it straight."""
  65. return self._brackets
  66. @url.setter
  67. def url(self, value):
  68. # pylint: disable=import-outside-toplevel
  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)