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.
 
 
 
 

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