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.
 
 
 
 

224 lines
8.7 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 tokens
  24. from ..compat import str
  25. from ..nodes import Argument, Heading, HTMLEntity, Tag, Template, Text
  26. from ..nodes.extras import Attribute, Parameter
  27. from ..smart_list import SmartList
  28. from ..wikicode import Wikicode
  29. __all__ = ["Builder"]
  30. class Builder(object):
  31. """Combines a sequence of tokens into a tree of ``Wikicode`` objects.
  32. To use, pass a list of :py:class:`~.Token`\ s to the :py:meth:`build`
  33. method. The list will be exhausted as it is parsed and a
  34. :py:class:`~.Wikicode` object will be returned.
  35. """
  36. def __init__(self):
  37. self._tokens = []
  38. self._stacks = []
  39. def _wrap(self, nodes):
  40. """Properly wrap a list of nodes in a ``Wikicode`` object."""
  41. return Wikicode(SmartList(nodes))
  42. def _push(self):
  43. """Push a new node list onto the stack."""
  44. self._stacks.append([])
  45. def _pop(self, wrap=True):
  46. """Pop the current node list off of the stack.
  47. If *wrap* is ``True``, we will call :py:meth:`_wrap` on the list.
  48. """
  49. if wrap:
  50. return self._wrap(self._stacks.pop())
  51. return self._stacks.pop()
  52. def _write(self, item):
  53. """Append a node to the current node list."""
  54. self._stacks[-1].append(item)
  55. def _handle_parameter(self, default):
  56. """Handle a case where a parameter is at the head of the tokens.
  57. *default* is the value to use if no parameter name is defined.
  58. """
  59. key = None
  60. showkey = False
  61. self._push()
  62. while self._tokens:
  63. token = self._tokens.pop()
  64. if isinstance(token, tokens.TemplateParamEquals):
  65. key = self._pop()
  66. showkey = True
  67. self._push()
  68. elif isinstance(token, (tokens.TemplateParamSeparator,
  69. tokens.TemplateClose)):
  70. self._tokens.append(token)
  71. value = self._pop()
  72. if not key:
  73. key = self._wrap([Text(str(default))])
  74. return Parameter(key, value, showkey)
  75. else:
  76. self._write(self._handle_token(token))
  77. def _handle_template(self):
  78. """Handle a case where a template is at the head of the tokens."""
  79. params = []
  80. default = 1
  81. self._push()
  82. while self._tokens:
  83. token = self._tokens.pop()
  84. if isinstance(token, tokens.TemplateParamSeparator):
  85. if not params:
  86. name = self._pop()
  87. param = self._handle_parameter(default)
  88. params.append(param)
  89. if not param.showkey:
  90. default += 1
  91. elif isinstance(token, tokens.TemplateClose):
  92. if not params:
  93. name = self._pop()
  94. return Template(name, params)
  95. else:
  96. self._write(self._handle_token(token))
  97. def _handle_argument(self):
  98. """Handle a case where an argument is at the head of the tokens."""
  99. name = None
  100. self._push()
  101. while self._tokens:
  102. token = self._tokens.pop()
  103. if isinstance(token, tokens.ArgumentSeparator):
  104. name = self._pop()
  105. self._push()
  106. elif isinstance(token, tokens.ArgumentClose):
  107. if name is not None:
  108. return Argument(name, self._pop())
  109. return Argument(self._pop())
  110. else:
  111. self._write(self._handle_token(token))
  112. def _handle_entity(self):
  113. """Handle a case where a HTML entity is at the head of the tokens."""
  114. token = self._tokens.pop()
  115. if isinstance(token, tokens.HTMLEntityNumeric):
  116. token = self._tokens.pop()
  117. if isinstance(token, tokens.HTMLEntityHex):
  118. text = self._tokens.pop()
  119. self._tokens.pop() # Remove HTMLEntityEnd
  120. return HTMLEntity(text.text, named=False, hexadecimal=True,
  121. hex_char=token.char)
  122. self._tokens.pop() # Remove HTMLEntityEnd
  123. return HTMLEntity(token.text, named=False, hexadecimal=False)
  124. self._tokens.pop() # Remove HTMLEntityEnd
  125. return HTMLEntity(token.text, named=True, hexadecimal=False)
  126. def _handle_heading(self, token):
  127. """Handle a case where a heading is at the head of the tokens."""
  128. level = token.level
  129. self._push()
  130. while self._tokens:
  131. token = self._tokens.pop()
  132. if isinstance(token, tokens.HeadingEnd):
  133. title = self._pop()
  134. return Heading(title, level)
  135. else:
  136. self._write(self._handle_token(token))
  137. def _handle_attribute(self):
  138. """Handle a case where a tag attribute is at the head of the tokens."""
  139. name, quoted = None, False
  140. self._push()
  141. while self._tokens:
  142. token = self._tokens.pop()
  143. if isinstance(token, tokens.TagAttrEquals):
  144. name = self._pop()
  145. self._push()
  146. elif isinstance(token, tokens.TagAttrQuote):
  147. quoted = True
  148. elif isinstance(token, (tokens.TagAttrStart,
  149. tokens.TagCloseOpen)):
  150. self._tokens.append(token)
  151. if name is not None:
  152. return Attribute(name, self._pop(), quoted)
  153. return Attribute(self._pop(), quoted=quoted)
  154. else:
  155. self._write(self._handle_token(token))
  156. def _handle_tag(self, token):
  157. """Handle a case where a tag is at the head of the tokens."""
  158. type_, showtag = token.type, token.showtag
  159. attrs = []
  160. self._push()
  161. while self._tokens:
  162. token = self._tokens.pop()
  163. if isinstance(token, tokens.TagAttrStart):
  164. attrs.append(self._handle_attribute())
  165. elif isinstance(token, tokens.TagCloseOpen):
  166. open_pad = token.padding
  167. tag = self._pop()
  168. self._push()
  169. elif isinstance(token, tokens.TagCloseSelfclose):
  170. tag = self._pop()
  171. return Tag(type_, tag, attrs=attrs, showtag=showtag,
  172. self_closing=True, open_padding=token.padding)
  173. elif isinstance(token, tokens.TagOpenClose):
  174. contents = self._pop()
  175. elif isinstance(token, tokens.TagCloseClose):
  176. return Tag(type_, tag, contents, attrs, showtag, False,
  177. open_pad, token.padding)
  178. else:
  179. self._write(self._handle_token(token))
  180. def _handle_token(self, token):
  181. """Handle a single token."""
  182. if isinstance(token, tokens.Text):
  183. return Text(token.text)
  184. elif isinstance(token, tokens.TemplateOpen):
  185. return self._handle_template()
  186. elif isinstance(token, tokens.ArgumentOpen):
  187. return self._handle_argument()
  188. elif isinstance(token, tokens.HTMLEntityStart):
  189. return self._handle_entity()
  190. elif isinstance(token, tokens.HeadingStart):
  191. return self._handle_heading(token)
  192. elif isinstance(token, tokens.TagOpenOpen):
  193. return self._handle_tag(token)
  194. def build(self, tokenlist):
  195. """Build a Wikicode object from a list tokens and return it."""
  196. self._tokens = tokenlist
  197. self._tokens.reverse()
  198. self._push()
  199. while self._tokens:
  200. node = self._handle_token(self._tokens.pop())
  201. self._write(node)
  202. return self._pop()