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.
 
 
 
 

99 lines
2.9 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012 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 mwparserfromhell.nodes import Node
  23. from mwparserfromhell.nodes.extras import Attribute
  24. __all__ = ["Tag"]
  25. class Tag(Node):
  26. TAG_UNKNOWN = 0
  27. TAG_BOLD = 1
  28. TAG_ITALIC = 2
  29. TAG_REF
  30. TAG_MISC_HTML = 99
  31. TAGS_VISIBLE = []
  32. TAGS_INVISIBLE = []
  33. def __init__(self, type_, tag, contents, attrs=None, showtag=True,
  34. self_closing=False, open_padding=0, close_padding=0):
  35. self._type = type_
  36. self._tag = tag
  37. self._contents = contents
  38. if attrs:
  39. self._attrs = attrs
  40. else:
  41. self._attrs = []
  42. self._showtag = showtag
  43. self._self_closing = self_closing
  44. self._open_padding = open_padding
  45. self._close_padding = close_padding
  46. def __unicode__(self):
  47. if not self.showtag:
  48. raise NotImplementedError()
  49. result = "<" + unicode(self.tag)
  50. if self.attrs:
  51. result += " " + u" ".join([unicode(attr) for attr in self.attrs])
  52. if self.self_closing:
  53. result += " " * self.open_padding + "/>"
  54. else:
  55. result += " " * self.open_padding + ">" + unicode(self.contents)
  56. result += "</" + unicode(self.tag) + " " * self.close_padding + ">"
  57. return result
  58. @property
  59. def type(self):
  60. return self._type
  61. @property
  62. def tag(self):
  63. return self._tag
  64. @property
  65. def contents(self):
  66. return self._contents
  67. @property
  68. def attrs(self):
  69. return self._attrs
  70. @property
  71. def showtag(self):
  72. return self._showtag
  73. @property
  74. def self_closing(self):
  75. return self._self_closing
  76. @property
  77. def open_padding(self):
  78. return self._open_padding
  79. @property
  80. def close_padding(self):
  81. return self._close_padding