A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

95 行
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2016 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from __future__ import unicode_literals
  23. from . import Node
  24. from ..compat import str
  25. from ..utils import parse_anything
  26. __all__ = ["ExternalLink"]
  27. class ExternalLink(Node):
  28. """Represents an external link, like ``[http://example.com/ Example]``."""
  29. def __init__(self, url, title=None, brackets=True):
  30. super(ExternalLink, self).__init__()
  31. self._url = url
  32. self._title = title
  33. self._brackets = brackets
  34. def __unicode__(self):
  35. if self.brackets:
  36. if self.title is not None:
  37. return "[" + str(self.url) + " " + str(self.title) + "]"
  38. return "[" + str(self.url) + "]"
  39. return str(self.url)
  40. def __children__(self):
  41. yield self.url
  42. if self.title is not None:
  43. yield self.title
  44. def __strip__(self, **kwargs):
  45. if self.brackets:
  46. if self.title:
  47. return self.title.strip_code(**kwargs)
  48. return None
  49. return self.url.strip_code(**kwargs)
  50. def __showtree__(self, write, get, mark):
  51. if self.brackets:
  52. write("[")
  53. get(self.url)
  54. if self.title is not None:
  55. get(self.title)
  56. if self.brackets:
  57. write("]")
  58. @property
  59. def url(self):
  60. """The URL of the link target, as a :class:`.Wikicode` object."""
  61. return self._url
  62. @property
  63. def title(self):
  64. """The link title (if given), as a :class:`.Wikicode` object."""
  65. return self._title
  66. @property
  67. def brackets(self):
  68. """Whether to enclose the URL in brackets or display it straight."""
  69. return self._brackets
  70. @url.setter
  71. def url(self, value):
  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)