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.
 
 
 
 

188 lines
5.5 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, attrs=None, showtag=True,
  63. self_closing=False, open_padding=0, close_padding=0):
  64. self._type = type_
  65. self._tag = tag
  66. self._contents = contents
  67. if attrs:
  68. self._attrs = attrs
  69. else:
  70. self._attrs = []
  71. self._showtag = showtag
  72. self._self_closing = self_closing
  73. self._open_padding = open_padding
  74. self._close_padding = close_padding
  75. def __unicode__(self):
  76. if not self.showtag:
  77. open_, close = self._translate()
  78. if self.self_closing:
  79. return open_
  80. else:
  81. return open_ + unicode(self.contents) + close
  82. result = "<" + unicode(self.tag)
  83. if self.attrs:
  84. result += " " + u" ".join([unicode(attr) for attr in self.attrs])
  85. if self.self_closing:
  86. result += " " * self.open_padding + "/>"
  87. else:
  88. result += " " * self.open_padding + ">" + unicode(self.contents)
  89. result += "</" + unicode(self.tag) + " " * self.close_padding + ">"
  90. return result
  91. def __iternodes__(self, getter):
  92. yield None, self
  93. if self.showtag:
  94. for child in getter(self.tag):
  95. yield self.tag, child
  96. for attr in self.attrs:
  97. for child in getter(attr.name):
  98. yield attr.name, child
  99. if attr.value:
  100. for child in getter(attr.value):
  101. yield attr.value, child
  102. for child in getter(self.contents):
  103. yield self.contents, child
  104. def __strip__(self, normalize, collapse):
  105. if self.type in self.TAGS_VISIBLE:
  106. return self.contents.strip_code(normalize, collapse)
  107. return None
  108. def __showtree__(self, write, get, mark):
  109. tagnodes = self.tag.nodes
  110. if (not self.attrs and len(tagnodes) == 1 and
  111. isinstance(tagnodes[0], Text)):
  112. write("<" + unicode(tagnodes[0]) + ">")
  113. else:
  114. write("<")
  115. get(self.tag)
  116. for attr in self.attrs:
  117. get(attr.name)
  118. if not attr.value:
  119. continue
  120. write(" = ")
  121. mark()
  122. get(attr.value)
  123. write(">")
  124. get(self.contents)
  125. if len(tagnodes) == 1 and isinstance(tagnodes[0], Text):
  126. write("</" + unicode(tagnodes[0]) + ">")
  127. else:
  128. write("</")
  129. get(self.tag)
  130. write(">")
  131. def _translate(self):
  132. translations = {
  133. self.TAG_ITALIC: ("''", "''"),
  134. self.TAG_BOLD: ("'''", "'''"),
  135. self.TAG_UNORDERED_LIST: ("*", ""),
  136. self.TAG_ORDERED_LIST: ("#", ""),
  137. self.TAG_DEF_TERM: (";", ""),
  138. self.TAG_DEF_ITEM: (":", ""),
  139. self.TAG_RULE: ("----", ""),
  140. }
  141. return translations[self.type]
  142. @property
  143. def type(self):
  144. return self._type
  145. @property
  146. def tag(self):
  147. return self._tag
  148. @property
  149. def contents(self):
  150. return self._contents
  151. @property
  152. def attrs(self):
  153. return self._attrs
  154. @property
  155. def showtag(self):
  156. return self._showtag
  157. @property
  158. def self_closing(self):
  159. return self._self_closing
  160. @property
  161. def open_padding(self):
  162. return self._open_padding
  163. @property
  164. def close_padding(self):
  165. return self._close_padding