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.
 
 
 
 

283 lines
11 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 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, Comment, ExternalLink, Heading, HTMLEntity, Tag,
  26. Template, Text, Wikilink)
  27. from ..nodes.extras import Attribute, Parameter
  28. from ..smart_list import SmartList
  29. from ..wikicode import Wikicode
  30. __all__ = ["Builder"]
  31. class Builder(object):
  32. """Combines a sequence of tokens into a tree of ``Wikicode`` objects.
  33. To use, pass a list of :py:class:`~.Token`\ s to the :py:meth:`build`
  34. method. The list will be exhausted as it is parsed and a
  35. :py:class:`~.Wikicode` object will be returned.
  36. """
  37. def __init__(self):
  38. self._tokens = []
  39. self._stacks = []
  40. def _wrap(self, nodes):
  41. """Properly wrap a list of nodes in a ``Wikicode`` object."""
  42. return Wikicode(SmartList(nodes))
  43. def _push(self):
  44. """Push a new node list onto the stack."""
  45. self._stacks.append([])
  46. def _pop(self, wrap=True):
  47. """Pop the current node list off of the stack.
  48. If *wrap* is ``True``, we will call :py:meth:`_wrap` on the list.
  49. """
  50. if wrap:
  51. return self._wrap(self._stacks.pop())
  52. return self._stacks.pop()
  53. def _write(self, item):
  54. """Append a node to the current node list."""
  55. self._stacks[-1].append(item)
  56. def _handle_parameter(self, default):
  57. """Handle a case where a parameter is at the head of the tokens.
  58. *default* is the value to use if no parameter name is defined.
  59. """
  60. key = None
  61. showkey = False
  62. self._push()
  63. while self._tokens:
  64. token = self._tokens.pop()
  65. if isinstance(token, tokens.TemplateParamEquals):
  66. key = self._pop()
  67. showkey = True
  68. self._push()
  69. elif isinstance(token, (tokens.TemplateParamSeparator,
  70. tokens.TemplateClose)):
  71. self._tokens.append(token)
  72. value = self._pop()
  73. if key is None:
  74. key = self._wrap([Text(str(default))])
  75. return Parameter(key, value, showkey)
  76. else:
  77. self._write(self._handle_token(token))
  78. def _handle_template(self):
  79. """Handle a case where a template is at the head of the tokens."""
  80. params = []
  81. default = 1
  82. self._push()
  83. while self._tokens:
  84. token = self._tokens.pop()
  85. if isinstance(token, tokens.TemplateParamSeparator):
  86. if not params:
  87. name = self._pop()
  88. param = self._handle_parameter(default)
  89. params.append(param)
  90. if not param.showkey:
  91. default += 1
  92. elif isinstance(token, tokens.TemplateClose):
  93. if not params:
  94. name = self._pop()
  95. return Template(name, params)
  96. else:
  97. self._write(self._handle_token(token))
  98. def _handle_argument(self):
  99. """Handle a case where an argument is at the head of the tokens."""
  100. name = None
  101. self._push()
  102. while self._tokens:
  103. token = self._tokens.pop()
  104. if isinstance(token, tokens.ArgumentSeparator):
  105. name = self._pop()
  106. self._push()
  107. elif isinstance(token, tokens.ArgumentClose):
  108. if name is not None:
  109. return Argument(name, self._pop())
  110. return Argument(self._pop())
  111. else:
  112. self._write(self._handle_token(token))
  113. def _handle_wikilink(self):
  114. """Handle a case where a wikilink is at the head of the tokens."""
  115. title = None
  116. self._push()
  117. while self._tokens:
  118. token = self._tokens.pop()
  119. if isinstance(token, tokens.WikilinkSeparator):
  120. title = self._pop()
  121. self._push()
  122. elif isinstance(token, tokens.WikilinkClose):
  123. if title is not None:
  124. return Wikilink(title, self._pop())
  125. return Wikilink(self._pop())
  126. else:
  127. self._write(self._handle_token(token))
  128. def _handle_external_link(self, token):
  129. """Handle when an external link is at the head of the tokens."""
  130. brackets, url = token.brackets, None
  131. self._push()
  132. while self._tokens:
  133. token = self._tokens.pop()
  134. if isinstance(token, tokens.ExternalLinkSeparator):
  135. url = self._pop()
  136. self._push()
  137. elif isinstance(token, tokens.ExternalLinkClose):
  138. if url is not None:
  139. return ExternalLink(url, self._pop(), brackets)
  140. return ExternalLink(self._pop(), brackets=brackets)
  141. else:
  142. self._write(self._handle_token(token))
  143. def _handle_entity(self):
  144. """Handle a case where an HTML entity is at the head of the tokens."""
  145. token = self._tokens.pop()
  146. if isinstance(token, tokens.HTMLEntityNumeric):
  147. token = self._tokens.pop()
  148. if isinstance(token, tokens.HTMLEntityHex):
  149. text = self._tokens.pop()
  150. self._tokens.pop() # Remove HTMLEntityEnd
  151. return HTMLEntity(text.text, named=False, hexadecimal=True,
  152. hex_char=token.char)
  153. self._tokens.pop() # Remove HTMLEntityEnd
  154. return HTMLEntity(token.text, named=False, hexadecimal=False)
  155. self._tokens.pop() # Remove HTMLEntityEnd
  156. return HTMLEntity(token.text, named=True, hexadecimal=False)
  157. def _handle_heading(self, token):
  158. """Handle a case where a heading is at the head of the tokens."""
  159. level = token.level
  160. self._push()
  161. while self._tokens:
  162. token = self._tokens.pop()
  163. if isinstance(token, tokens.HeadingEnd):
  164. title = self._pop()
  165. return Heading(title, level)
  166. else:
  167. self._write(self._handle_token(token))
  168. def _handle_comment(self):
  169. """Handle a case where an HTML comment is at the head of the tokens."""
  170. self._push()
  171. while self._tokens:
  172. token = self._tokens.pop()
  173. if isinstance(token, tokens.CommentEnd):
  174. contents = self._pop()
  175. return Comment(contents)
  176. else:
  177. self._write(self._handle_token(token))
  178. def _handle_attribute(self, start):
  179. """Handle a case where a tag attribute is at the head of the tokens."""
  180. name, quoted = None, False
  181. self._push()
  182. while self._tokens:
  183. token = self._tokens.pop()
  184. if isinstance(token, tokens.TagAttrEquals):
  185. name = self._pop()
  186. self._push()
  187. elif isinstance(token, tokens.TagAttrQuote):
  188. quoted = True
  189. elif isinstance(token, (tokens.TagAttrStart, tokens.TagCloseOpen,
  190. tokens.TagCloseSelfclose)):
  191. self._tokens.append(token)
  192. if name:
  193. value = self._pop()
  194. else:
  195. name, value = self._pop(), None
  196. return Attribute(name, value, quoted, start.pad_first,
  197. start.pad_before_eq, start.pad_after_eq)
  198. else:
  199. self._write(self._handle_token(token))
  200. def _handle_tag(self, token):
  201. """Handle a case where a tag is at the head of the tokens."""
  202. close_tokens = (tokens.TagCloseSelfclose, tokens.TagCloseClose)
  203. implicit, attrs, contents, closing_tag = False, [], None, None
  204. wiki_markup, invalid = token.wiki_markup, token.invalid or False
  205. self._push()
  206. while self._tokens:
  207. token = self._tokens.pop()
  208. if isinstance(token, tokens.TagAttrStart):
  209. attrs.append(self._handle_attribute(token))
  210. elif isinstance(token, tokens.TagCloseOpen):
  211. padding = token.padding or ""
  212. tag = self._pop()
  213. self._push()
  214. elif isinstance(token, tokens.TagOpenClose):
  215. contents = self._pop()
  216. self._push()
  217. elif isinstance(token, close_tokens):
  218. if isinstance(token, tokens.TagCloseSelfclose):
  219. tag = self._pop()
  220. self_closing = True
  221. padding = token.padding or ""
  222. implicit = token.implicit or False
  223. else:
  224. self_closing = False
  225. closing_tag = self._pop()
  226. return Tag(tag, contents, attrs, wiki_markup, self_closing,
  227. invalid, implicit, padding, closing_tag)
  228. else:
  229. self._write(self._handle_token(token))
  230. def _handle_token(self, token):
  231. """Handle a single token."""
  232. if isinstance(token, tokens.Text):
  233. return Text(token.text)
  234. elif isinstance(token, tokens.TemplateOpen):
  235. return self._handle_template()
  236. elif isinstance(token, tokens.ArgumentOpen):
  237. return self._handle_argument()
  238. elif isinstance(token, tokens.WikilinkOpen):
  239. return self._handle_wikilink()
  240. elif isinstance(token, tokens.ExternalLinkOpen):
  241. return self._handle_external_link(token)
  242. elif isinstance(token, tokens.HTMLEntityStart):
  243. return self._handle_entity()
  244. elif isinstance(token, tokens.HeadingStart):
  245. return self._handle_heading(token)
  246. elif isinstance(token, tokens.CommentStart):
  247. return self._handle_comment()
  248. elif isinstance(token, tokens.TagOpenOpen):
  249. return self._handle_tag(token)
  250. def build(self, tokenlist):
  251. """Build a Wikicode object from a list tokens and return it."""
  252. self._tokens = tokenlist
  253. self._tokens.reverse()
  254. self._push()
  255. while self._tokens:
  256. node = self._handle_token(self._tokens.pop())
  257. self._write(node)
  258. return self._pop()