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.
 
 
 
 

592 lines
27 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2017 Ben Kurtovic <ben.kurtovic@gmail.com>
  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 itertools import chain
  24. import re
  25. from .compat import bytes, py3k, range, str
  26. from .nodes import (Argument, Comment, ExternalLink, Heading, HTMLEntity,
  27. Node, Tag, Template, Text, Wikilink)
  28. from .string_mixin import StringMixIn
  29. from .utils import parse_anything
  30. __all__ = ["Wikicode"]
  31. FLAGS = re.IGNORECASE | re.DOTALL | re.UNICODE
  32. class Wikicode(StringMixIn):
  33. """A ``Wikicode`` is a container for nodes that operates like a string.
  34. Additionally, it contains methods that can be used to extract data from or
  35. modify the nodes, implemented in an interface similar to a list. For
  36. example, :meth:`index` can get the index of a node in the list, and
  37. :meth:`insert` can add a new node at that index. The :meth:`filter()
  38. <ifilter>` series of functions is very useful for extracting and iterating
  39. over, for example, all of the templates in the object.
  40. """
  41. RECURSE_OTHERS = 2
  42. def __init__(self, nodes):
  43. super(Wikicode, self).__init__()
  44. self._nodes = nodes
  45. def __unicode__(self):
  46. return "".join([str(node) for node in self.nodes])
  47. @staticmethod
  48. def _get_children(node, contexts=False, restrict=None, parent=None):
  49. """Iterate over all child :class:`.Node`\ s of a given *node*."""
  50. yield (parent, node) if contexts else node
  51. if restrict and isinstance(node, restrict):
  52. return
  53. for code in node.__children__():
  54. for child in code.nodes:
  55. sub = Wikicode._get_children(child, contexts, restrict, code)
  56. for result in sub:
  57. yield result
  58. @staticmethod
  59. def _slice_replace(code, index, old, new):
  60. """Replace the string *old* with *new* across *index* in *code*."""
  61. nodes = [str(node) for node in code.get(index)]
  62. substring = "".join(nodes).replace(old, new)
  63. code.nodes[index] = parse_anything(substring).nodes
  64. @staticmethod
  65. def _build_matcher(matches, flags):
  66. """Helper for :meth:`_indexed_ifilter` and others.
  67. If *matches* is a function, return it. If it's a regex, return a
  68. wrapper around it that can be called with a node to do a search. If
  69. it's ``None``, return a function that always returns ``True``.
  70. """
  71. if matches:
  72. if callable(matches):
  73. return matches
  74. return lambda obj: re.search(matches, str(obj), flags)
  75. return lambda obj: True
  76. def _indexed_ifilter(self, recursive=True, matches=None, flags=FLAGS,
  77. forcetype=None):
  78. """Iterate over nodes and their corresponding indices in the node list.
  79. The arguments are interpreted as for :meth:`ifilter`. For each tuple
  80. ``(i, node)`` yielded by this method, ``self.index(node) == i``. Note
  81. that if *recursive* is ``True``, ``self.nodes[i]`` might not be the
  82. node itself, but will still contain it.
  83. """
  84. match = self._build_matcher(matches, flags)
  85. if recursive:
  86. restrict = forcetype if recursive == self.RECURSE_OTHERS else None
  87. def getter(i, node):
  88. for ch in self._get_children(node, restrict=restrict):
  89. yield (i, ch)
  90. inodes = chain(*(getter(i, n) for i, n in enumerate(self.nodes)))
  91. else:
  92. inodes = enumerate(self.nodes)
  93. for i, node in inodes:
  94. if (not forcetype or isinstance(node, forcetype)) and match(node):
  95. yield (i, node)
  96. def _do_strong_search(self, obj, recursive=True):
  97. """Search for the specific element *obj* within the node list.
  98. *obj* can be either a :class:`.Node` or a :class:`.Wikicode` object. If
  99. found, we return a tuple (*context*, *index*) where *context* is the
  100. :class:`.Wikicode` that contains *obj* and *index* is its index there,
  101. as a :class:`slice`. Note that if *recursive* is ``False``, *context*
  102. will always be ``self`` (since we only look for *obj* among immediate
  103. descendants), but if *recursive* is ``True``, then it could be any
  104. :class:`.Wikicode` contained by a node within ``self``. If *obj* is not
  105. found, :exc:`ValueError` is raised.
  106. """
  107. if isinstance(obj, Node):
  108. mkslice = lambda i: slice(i, i + 1)
  109. if not recursive:
  110. return self, mkslice(self.index(obj))
  111. for i, node in enumerate(self.nodes):
  112. for context, child in self._get_children(node, contexts=True):
  113. if obj is child:
  114. if not context:
  115. context = self
  116. return context, mkslice(context.index(child))
  117. raise ValueError(obj)
  118. context, ind = self._do_strong_search(obj.get(0), recursive)
  119. for i in range(1, len(obj.nodes)):
  120. if obj.get(i) is not context.get(ind.start + i):
  121. raise ValueError(obj)
  122. return context, slice(ind.start, ind.start + len(obj.nodes))
  123. def _do_weak_search(self, obj, recursive):
  124. """Search for an element that looks like *obj* within the node list.
  125. This follows the same rules as :meth:`_do_strong_search` with some
  126. differences. *obj* is treated as a string that might represent any
  127. :class:`.Node`, :class:`.Wikicode`, or combination of the two present
  128. in the node list. Thus, matching is weak (using string comparisons)
  129. rather than strong (using ``is``). Because multiple nodes can match
  130. *obj*, the result is a list of tuples instead of just one (however,
  131. :exc:`ValueError` is still raised if nothing is found). Individual
  132. matches will never overlap.
  133. The tuples contain a new first element, *exact*, which is ``True`` if
  134. we were able to match *obj* exactly to one or more adjacent nodes, or
  135. ``False`` if we found *obj* inside a node or incompletely spanning
  136. multiple nodes.
  137. """
  138. obj = parse_anything(obj)
  139. if not obj or obj not in self:
  140. raise ValueError(obj)
  141. results = []
  142. contexts = [self]
  143. while contexts:
  144. context = contexts.pop()
  145. i = len(context.nodes) - 1
  146. while i >= 0:
  147. node = context.get(i)
  148. if obj.get(-1) == node:
  149. for j in range(-len(obj.nodes), -1):
  150. if obj.get(j) != context.get(i + j + 1):
  151. break
  152. else:
  153. i -= len(obj.nodes) - 1
  154. index = slice(i, i + len(obj.nodes))
  155. results.append((True, context, index))
  156. elif recursive and obj in node:
  157. contexts.extend(node.__children__())
  158. i -= 1
  159. if not results:
  160. if not recursive:
  161. raise ValueError(obj)
  162. results.append((False, self, slice(0, len(self.nodes))))
  163. return results
  164. def _get_tree(self, code, lines, marker, indent):
  165. """Build a tree to illustrate the way the Wikicode object was parsed.
  166. The method that builds the actual tree is ``__showtree__`` of ``Node``
  167. objects. *code* is the ``Wikicode`` object to build a tree for. *lines*
  168. is the list to append the tree to, which is returned at the end of the
  169. method. *marker* is some object to be used to indicate that the builder
  170. should continue on from the last line instead of starting a new one; it
  171. should be any object that can be tested for with ``is``. *indent* is
  172. the starting indentation.
  173. """
  174. def write(*args):
  175. """Write a new line following the proper indentation rules."""
  176. if lines and lines[-1] is marker: # Continue from the last line
  177. lines.pop() # Remove the marker
  178. last = lines.pop()
  179. lines.append(last + " ".join(args))
  180. else:
  181. lines.append(" " * 6 * indent + " ".join(args))
  182. get = lambda code: self._get_tree(code, lines, marker, indent + 1)
  183. mark = lambda: lines.append(marker)
  184. for node in code.nodes:
  185. node.__showtree__(write, get, mark)
  186. return lines
  187. @classmethod
  188. def _build_filter_methods(cls, **meths):
  189. """Given Node types, build the corresponding i?filter shortcuts.
  190. The should be given as keys storing the method's base name paired with
  191. values storing the corresponding :class:`.Node` type. For example, the
  192. dict may contain the pair ``("templates", Template)``, which will
  193. produce the methods :meth:`ifilter_templates` and
  194. :meth:`filter_templates`, which are shortcuts for
  195. :meth:`ifilter(forcetype=Template) <ifilter>` and
  196. :meth:`filter(forcetype=Template) <filter>`, respectively. These
  197. shortcuts are added to the class itself, with an appropriate docstring.
  198. """
  199. doc = """Iterate over {0}.
  200. This is equivalent to :meth:`{1}` with *forcetype* set to
  201. :class:`~{2.__module__}.{2.__name__}`.
  202. """
  203. make_ifilter = lambda ftype: (lambda self, *a, **kw:
  204. self.ifilter(forcetype=ftype, *a, **kw))
  205. make_filter = lambda ftype: (lambda self, *a, **kw:
  206. self.filter(forcetype=ftype, *a, **kw))
  207. for name, ftype in (meths.items() if py3k else meths.iteritems()):
  208. ifilter = make_ifilter(ftype)
  209. filter = make_filter(ftype)
  210. ifilter.__doc__ = doc.format(name, "ifilter", ftype)
  211. filter.__doc__ = doc.format(name, "filter", ftype)
  212. setattr(cls, "ifilter_" + name, ifilter)
  213. setattr(cls, "filter_" + name, filter)
  214. @property
  215. def nodes(self):
  216. """A list of :class:`.Node` objects.
  217. This is the internal data actually stored within a :class:`.Wikicode`
  218. object.
  219. """
  220. return self._nodes
  221. @nodes.setter
  222. def nodes(self, value):
  223. if not isinstance(value, list):
  224. value = parse_anything(value).nodes
  225. self._nodes = value
  226. def get(self, index):
  227. """Return the *index*\ th node within the list of nodes."""
  228. return self.nodes[index]
  229. def set(self, index, value):
  230. """Set the ``Node`` at *index* to *value*.
  231. Raises :exc:`IndexError` if *index* is out of range, or
  232. :exc:`ValueError` if *value* cannot be coerced into one :class:`.Node`.
  233. To insert multiple nodes at an index, use :meth:`get` with either
  234. :meth:`remove` and :meth:`insert` or :meth:`replace`.
  235. """
  236. nodes = parse_anything(value).nodes
  237. if len(nodes) > 1:
  238. raise ValueError("Cannot coerce multiple nodes into one index")
  239. if index >= len(self.nodes) or -1 * index > len(self.nodes):
  240. raise IndexError("List assignment index out of range")
  241. if nodes:
  242. self.nodes[index] = nodes[0]
  243. else:
  244. self.nodes.pop(index)
  245. def index(self, obj, recursive=False):
  246. """Return the index of *obj* in the list of nodes.
  247. Raises :exc:`ValueError` if *obj* is not found. If *recursive* is
  248. ``True``, we will look in all nodes of ours and their descendants, and
  249. return the index of our direct descendant node within *our* list of
  250. nodes. Otherwise, the lookup is done only on direct descendants.
  251. """
  252. strict = isinstance(obj, Node)
  253. equivalent = (lambda o, n: o is n) if strict else (lambda o, n: o == n)
  254. for i, node in enumerate(self.nodes):
  255. if recursive:
  256. for child in self._get_children(node):
  257. if equivalent(obj, child):
  258. return i
  259. elif equivalent(obj, node):
  260. return i
  261. raise ValueError(obj)
  262. def insert(self, index, value):
  263. """Insert *value* at *index* in the list of nodes.
  264. *value* can be anything parsable by :func:`.parse_anything`, which
  265. includes strings or other :class:`.Wikicode` or :class:`.Node` objects.
  266. """
  267. nodes = parse_anything(value).nodes
  268. for node in reversed(nodes):
  269. self.nodes.insert(index, node)
  270. def insert_before(self, obj, value, recursive=True):
  271. """Insert *value* immediately before *obj*.
  272. *obj* can be either a string, a :class:`.Node`, or another
  273. :class:`.Wikicode` object (as created by :meth:`get_sections`, for
  274. example). If *obj* is a string, we will operate on all instances of
  275. that string within the code, otherwise only on the specific instance
  276. given. *value* can be anything parsable by :func:`.parse_anything`. If
  277. *recursive* is ``True``, we will try to find *obj* within our child
  278. nodes even if it is not a direct descendant of this :class:`.Wikicode`
  279. object. If *obj* is not found, :exc:`ValueError` is raised.
  280. """
  281. if isinstance(obj, (Node, Wikicode)):
  282. context, index = self._do_strong_search(obj, recursive)
  283. context.insert(index.start, value)
  284. else:
  285. for exact, context, index in self._do_weak_search(obj, recursive):
  286. if exact:
  287. context.insert(index.start, value)
  288. else:
  289. obj = str(obj)
  290. self._slice_replace(context, index, obj, str(value) + obj)
  291. def insert_after(self, obj, value, recursive=True):
  292. """Insert *value* immediately after *obj*.
  293. *obj* can be either a string, a :class:`.Node`, or another
  294. :class:`.Wikicode` object (as created by :meth:`get_sections`, for
  295. example). If *obj* is a string, we will operate on all instances of
  296. that string within the code, otherwise only on the specific instance
  297. given. *value* can be anything parsable by :func:`.parse_anything`. If
  298. *recursive* is ``True``, we will try to find *obj* within our child
  299. nodes even if it is not a direct descendant of this :class:`.Wikicode`
  300. object. If *obj* is not found, :exc:`ValueError` is raised.
  301. """
  302. if isinstance(obj, (Node, Wikicode)):
  303. context, index = self._do_strong_search(obj, recursive)
  304. context.insert(index.stop, value)
  305. else:
  306. for exact, context, index in self._do_weak_search(obj, recursive):
  307. if exact:
  308. context.insert(index.stop, value)
  309. else:
  310. obj = str(obj)
  311. self._slice_replace(context, index, obj, obj + str(value))
  312. def replace(self, obj, value, recursive=True):
  313. """Replace *obj* with *value*.
  314. *obj* can be either a string, a :class:`.Node`, or another
  315. :class:`.Wikicode` object (as created by :meth:`get_sections`, for
  316. example). If *obj* is a string, we will operate on all instances of
  317. that string within the code, otherwise only on the specific instance
  318. given. *value* can be anything parsable by :func:`.parse_anything`.
  319. If *recursive* is ``True``, we will try to find *obj* within our child
  320. nodes even if it is not a direct descendant of this :class:`.Wikicode`
  321. object. If *obj* is not found, :exc:`ValueError` is raised.
  322. """
  323. if isinstance(obj, (Node, Wikicode)):
  324. context, index = self._do_strong_search(obj, recursive)
  325. for i in range(index.start, index.stop):
  326. context.nodes.pop(index.start)
  327. context.insert(index.start, value)
  328. else:
  329. for exact, context, index in self._do_weak_search(obj, recursive):
  330. if exact:
  331. for i in range(index.start, index.stop):
  332. context.nodes.pop(index.start)
  333. context.insert(index.start, value)
  334. else:
  335. self._slice_replace(context, index, str(obj), str(value))
  336. def append(self, value):
  337. """Insert *value* at the end of the list of nodes.
  338. *value* can be anything parsable by :func:`.parse_anything`.
  339. """
  340. nodes = parse_anything(value).nodes
  341. for node in nodes:
  342. self.nodes.append(node)
  343. def remove(self, obj, recursive=True):
  344. """Remove *obj* from the list of nodes.
  345. *obj* can be either a string, a :class:`.Node`, or another
  346. :class:`.Wikicode` object (as created by :meth:`get_sections`, for
  347. example). If *obj* is a string, we will operate on all instances of
  348. that string within the code, otherwise only on the specific instance
  349. given. If *recursive* is ``True``, we will try to find *obj* within our
  350. child nodes even if it is not a direct descendant of this
  351. :class:`.Wikicode` object. If *obj* is not found, :exc:`ValueError` is
  352. raised.
  353. """
  354. if isinstance(obj, (Node, Wikicode)):
  355. context, index = self._do_strong_search(obj, recursive)
  356. for i in range(index.start, index.stop):
  357. context.nodes.pop(index.start)
  358. else:
  359. for exact, context, index in self._do_weak_search(obj, recursive):
  360. if exact:
  361. for i in range(index.start, index.stop):
  362. context.nodes.pop(index.start)
  363. else:
  364. self._slice_replace(context, index, str(obj), "")
  365. def matches(self, other):
  366. """Do a loose equivalency test suitable for comparing page names.
  367. *other* can be any string-like object, including :class:`.Wikicode`, or
  368. an iterable of these. This operation is symmetric; both sides are
  369. adjusted. Specifically, whitespace and markup is stripped and the first
  370. letter's case is normalized. Typical usage is
  371. ``if template.name.matches("stub"): ...``.
  372. """
  373. cmp = lambda a, b: (a[0].upper() + a[1:] == b[0].upper() + b[1:]
  374. if a and b else a == b)
  375. this = self.strip_code().strip()
  376. if isinstance(other, (str, bytes, Wikicode, Node)):
  377. that = parse_anything(other).strip_code().strip()
  378. return cmp(this, that)
  379. for obj in other:
  380. that = parse_anything(obj).strip_code().strip()
  381. if cmp(this, that):
  382. return True
  383. return False
  384. def ifilter(self, recursive=True, matches=None, flags=FLAGS,
  385. forcetype=None):
  386. """Iterate over nodes in our list matching certain conditions.
  387. If *forcetype* is given, only nodes that are instances of this type (or
  388. tuple of types) are yielded. Setting *recursive* to ``True`` will
  389. iterate over all children and their descendants. ``RECURSE_OTHERS``
  390. will only iterate over children that are not the instances of
  391. *forcetype*. ``False`` will only iterate over immediate children.
  392. ``RECURSE_OTHERS`` can be used to iterate over all un-nested templates,
  393. even if they are inside of HTML tags, like so:
  394. >>> code = mwparserfromhell.parse("{{foo}}<b>{{foo|{{bar}}}}</b>")
  395. >>> code.filter_templates(code.RECURSE_OTHERS)
  396. ["{{foo}}", "{{foo|{{bar}}}}"]
  397. *matches* can be used to further restrict the nodes, either as a
  398. function (taking a single :class:`.Node` and returning a boolean) or a
  399. regular expression (matched against the node's string representation
  400. with :func:`re.search`). If *matches* is a regex, the flags passed to
  401. :func:`re.search` are :const:`re.IGNORECASE`, :const:`re.DOTALL`, and
  402. :const:`re.UNICODE`, but custom flags can be specified by passing
  403. *flags*.
  404. """
  405. gen = self._indexed_ifilter(recursive, matches, flags, forcetype)
  406. return (node for i, node in gen)
  407. def filter(self, *args, **kwargs):
  408. """Return a list of nodes within our list matching certain conditions.
  409. This is equivalent to calling :func:`list` on :meth:`ifilter`.
  410. """
  411. return list(self.ifilter(*args, **kwargs))
  412. def get_sections(self, levels=None, matches=None, flags=FLAGS, flat=False,
  413. include_lead=None, include_headings=True):
  414. """Return a list of sections within the page.
  415. Sections are returned as :class:`.Wikicode` objects with a shared node
  416. list (implemented using :class:`.SmartList`) so that changes to
  417. sections are reflected in the parent Wikicode object.
  418. Each section contains all of its subsections, unless *flat* is
  419. ``True``. If *levels* is given, it should be a iterable of integers;
  420. only sections whose heading levels are within it will be returned. If
  421. *matches* is given, it should be either a function or a regex; only
  422. sections whose headings match it (without the surrounding equal signs)
  423. will be included. *flags* can be used to override the default regex
  424. flags (see :meth:`ifilter`) if a regex *matches* is used.
  425. If *include_lead* is ``True``, the first, lead section (without a
  426. heading) will be included in the list; ``False`` will not include it;
  427. the default will include it only if no specific *levels* were given. If
  428. *include_headings* is ``True``, the section's beginning
  429. :class:`.Heading` object will be included; otherwise, this is skipped.
  430. """
  431. title_matcher = self._build_matcher(matches, flags)
  432. matcher = lambda heading: (title_matcher(heading.title) and
  433. (not levels or heading.level in levels))
  434. iheadings = self._indexed_ifilter(recursive=False, forcetype=Heading)
  435. sections = [] # Tuples of (index_of_first_node, section)
  436. open_headings = [] # Tuples of (index, heading), where index and
  437. # heading.level are both monotonically increasing
  438. # Add the lead section if appropriate:
  439. if include_lead or not (include_lead is not None or matches or levels):
  440. itr = self._indexed_ifilter(recursive=False, forcetype=Heading)
  441. try:
  442. first = next(itr)[0]
  443. sections.append((0, Wikicode(self.nodes[:first])))
  444. except StopIteration: # No headings in page
  445. sections.append((0, Wikicode(self.nodes[:])))
  446. # Iterate over headings, adding sections to the list as they end:
  447. for i, heading in iheadings:
  448. if flat: # With flat, all sections close at the next heading
  449. newly_closed, open_headings = open_headings, []
  450. else: # Otherwise, figure out which sections have closed, if any
  451. closed_start_index = len(open_headings)
  452. for j, (start, last_heading) in enumerate(open_headings):
  453. if heading.level <= last_heading.level:
  454. closed_start_index = j
  455. break
  456. newly_closed = open_headings[closed_start_index:]
  457. del open_headings[closed_start_index:]
  458. for start, closed_heading in newly_closed:
  459. if matcher(closed_heading):
  460. sections.append((start, Wikicode(self.nodes[start:i])))
  461. start = i if include_headings else (i + 1)
  462. open_headings.append((start, heading))
  463. # Add any remaining open headings to the list of sections:
  464. for start, heading in open_headings:
  465. if matcher(heading):
  466. sections.append((start, Wikicode(self.nodes[start:])))
  467. # Ensure that earlier sections are earlier in the returned list:
  468. return [section for i, section in sorted(sections)]
  469. def strip_code(self, normalize=True, collapse=True):
  470. """Return a rendered string without unprintable code such as templates.
  471. The way a node is stripped is handled by the
  472. :meth:`~.Node.__strip__` method of :class:`.Node` objects, which
  473. generally return a subset of their nodes or ``None``. For example,
  474. templates and tags are removed completely, links are stripped to just
  475. their display part, headings are stripped to just their title. If
  476. *normalize* is ``True``, various things may be done to strip code
  477. further, such as converting HTML entities like ``&Sigma;``, ``&#931;``,
  478. and ``&#x3a3;`` to ``Σ``. If *collapse* is ``True``, we will try to
  479. remove excess whitespace as well (three or more newlines are converted
  480. to two, for example).
  481. """
  482. nodes = []
  483. for node in self.nodes:
  484. stripped = node.__strip__(normalize, collapse)
  485. if stripped:
  486. nodes.append(str(stripped))
  487. if collapse:
  488. stripped = "".join(nodes).strip("\n")
  489. while "\n\n\n" in stripped:
  490. stripped = stripped.replace("\n\n\n", "\n\n")
  491. return stripped
  492. else:
  493. return "".join(nodes)
  494. def get_tree(self):
  495. """Return a hierarchical tree representation of the object.
  496. The representation is a string makes the most sense printed. It is
  497. built by calling :meth:`_get_tree` on the :class:`.Wikicode` object and
  498. its children recursively. The end result may look something like the
  499. following::
  500. >>> text = "Lorem ipsum {{foo|bar|{{baz}}|spam=eggs}}"
  501. >>> print(mwparserfromhell.parse(text).get_tree())
  502. Lorem ipsum
  503. {{
  504. foo
  505. | 1
  506. = bar
  507. | 2
  508. = {{
  509. baz
  510. }}
  511. | spam
  512. = eggs
  513. }}
  514. """
  515. marker = object() # Random object we can find with certainty in a list
  516. return "\n".join(self._get_tree(self, [], marker, 0))
  517. Wikicode._build_filter_methods(
  518. arguments=Argument, comments=Comment, external_links=ExternalLink,
  519. headings=Heading, html_entities=HTMLEntity, tags=Tag, templates=Template,
  520. text=Text, wikilinks=Wikilink)