A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

98 linhas
3.3 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net>
  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 __iternodes__(self, getter):
  41. yield None, self
  42. for child in getter(self.url):
  43. yield self.url, child
  44. if self.title is not None:
  45. for child in getter(self.title):
  46. yield self.title, child
  47. def __strip__(self, normalize, collapse):
  48. if self.brackets:
  49. if self.title:
  50. return self.title.strip_code(normalize, collapse)
  51. return None
  52. return self.url.strip_code(normalize, collapse)
  53. def __showtree__(self, write, get, mark):
  54. if self.brackets:
  55. write("[")
  56. get(self.url)
  57. if self.title is not None:
  58. get(self.title)
  59. if self.brackets:
  60. write("]")
  61. @property
  62. def url(self):
  63. """The URL of the link target, as a :py:class:`~.Wikicode` object."""
  64. return self._url
  65. @property
  66. def title(self):
  67. """The link title (if given), as a :py:class:`~.Wikicode` object."""
  68. return self._title
  69. @property
  70. def brackets(self):
  71. """Whether to enclose the URL in brackets or display it straight."""
  72. return self._brackets
  73. @url.setter
  74. def url(self, value):
  75. from ..parser import contexts
  76. self._url = parse_anything(value, contexts.EXT_LINK_URI)
  77. @title.setter
  78. def title(self, value):
  79. self._title = None if value is None else parse_anything(value)
  80. @brackets.setter
  81. def brackets(self, value):
  82. self._brackets = bool(value)