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.
 
 
 
 

192 lines
5.6 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 __future__ import unicode_literals
  23. from . import Node, Text
  24. from ..compat import str, bytes
  25. __all__ = ["Tag"]
  26. class Tag(Node):
  27. TAG_UNKNOWN = 0
  28. # Basic HTML:
  29. TAG_ITALIC = 1
  30. TAG_BOLD = 2
  31. TAG_UNDERLINE = 3
  32. TAG_STRIKETHROUGH = 4
  33. TAG_UNORDERED_LIST = 5
  34. TAG_ORDERED_LIST = 6
  35. TAG_DEF_TERM = 7
  36. TAG_DEF_ITEM = 8
  37. TAG_BLOCKQUOTE = 9
  38. TAG_RULE = 10
  39. TAG_BREAK = 11
  40. TAG_ABBR = 12
  41. TAG_PRE = 13
  42. TAG_MONOSPACE = 14
  43. TAG_CODE = 15
  44. TAG_SPAN = 16
  45. TAG_DIV = 17
  46. TAG_FONT = 18
  47. TAG_SMALL = 19
  48. TAG_BIG = 20
  49. TAG_CENTER = 21
  50. # MediaWiki parser hooks:
  51. TAG_REF = 101
  52. TAG_GALLERY = 102
  53. TAG_MATH = 103
  54. TAG_NOWIKI = 104
  55. TAG_NOINCLUDE = 105
  56. TAG_INCLUDEONLY = 106
  57. TAG_ONLYINCLUDE = 107
  58. # Additional parser hooks:
  59. TAG_SYNTAXHIGHLIGHT = 201
  60. TAG_POEM = 202
  61. # Lists of tags:
  62. TAGS_INVISIBLE = set((TAG_REF, TAG_GALLERY, TAG_MATH, TAG_NOINCLUDE))
  63. TAGS_VISIBLE = set(range(300)) - TAGS_INVISIBLE
  64. def __init__(self, type_, tag, contents=None, attrs=None, showtag=True,
  65. self_closing=False, open_padding=0, close_padding=0):
  66. super(Tag, self).__init__()
  67. self._type = type_
  68. self._tag = tag
  69. self._contents = contents
  70. if attrs:
  71. self._attrs = attrs
  72. else:
  73. self._attrs = []
  74. self._showtag = showtag
  75. self._self_closing = self_closing
  76. self._open_padding = open_padding
  77. self._close_padding = close_padding
  78. def __unicode__(self):
  79. if not self.showtag:
  80. open_, close = self._translate()
  81. if self.self_closing:
  82. return open_
  83. else:
  84. return open_ + unicode(self.contents) + close
  85. result = "<" + unicode(self.tag)
  86. if self.attrs:
  87. result += " " + " ".join([unicode(attr) for attr in self.attrs])
  88. if self.self_closing:
  89. result += " " * self.open_padding + "/>"
  90. else:
  91. result += " " * self.open_padding + ">" + unicode(self.contents)
  92. result += "</" + unicode(self.tag) + " " * self.close_padding + ">"
  93. return result
  94. def __iternodes__(self, getter):
  95. yield None, self
  96. if self.showtag:
  97. for child in getter(self.tag):
  98. yield self.tag, child
  99. for attr in self.attrs:
  100. for child in getter(attr.name):
  101. yield attr.name, child
  102. if attr.value:
  103. for child in getter(attr.value):
  104. yield attr.value, child
  105. for child in getter(self.contents):
  106. yield self.contents, child
  107. def __strip__(self, normalize, collapse):
  108. if self.type in self.TAGS_VISIBLE:
  109. return self.contents.strip_code(normalize, collapse)
  110. return None
  111. def __showtree__(self, write, get, mark):
  112. tagnodes = self.tag.nodes
  113. if (not self.attrs and len(tagnodes) == 1 and
  114. isinstance(tagnodes[0], Text)):
  115. write("<" + unicode(tagnodes[0]) + ">")
  116. else:
  117. write("<")
  118. get(self.tag)
  119. for attr in self.attrs:
  120. get(attr.name)
  121. if not attr.value:
  122. continue
  123. write(" = ")
  124. mark()
  125. get(attr.value)
  126. write(">")
  127. get(self.contents)
  128. if len(tagnodes) == 1 and isinstance(tagnodes[0], Text):
  129. write("</" + unicode(tagnodes[0]) + ">")
  130. else:
  131. write("</")
  132. get(self.tag)
  133. write(">")
  134. def _translate(self):
  135. translations = {
  136. self.TAG_ITALIC: ("''", "''"),
  137. self.TAG_BOLD: ("'''", "'''"),
  138. self.TAG_UNORDERED_LIST: ("*", ""),
  139. self.TAG_ORDERED_LIST: ("#", ""),
  140. self.TAG_DEF_TERM: (";", ""),
  141. self.TAG_DEF_ITEM: (":", ""),
  142. self.TAG_RULE: ("----", ""),
  143. }
  144. return translations[self.type]
  145. @property
  146. def type(self):
  147. return self._type
  148. @property
  149. def tag(self):
  150. return self._tag
  151. @property
  152. def contents(self):
  153. return self._contents
  154. @property
  155. def attrs(self):
  156. return self._attrs
  157. @property
  158. def showtag(self):
  159. return self._showtag
  160. @property
  161. def self_closing(self):
  162. return self._self_closing
  163. @property
  164. def open_padding(self):
  165. return self._open_padding
  166. @property
  167. def close_padding(self):
  168. return self._close_padding