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.
 
 
 
 

172 lines
6.4 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012 by 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. import re
  23. import mwparserfromhell
  24. from mwparserfromhell.node import Node
  25. from mwparserfromhell.string_mixin import StringMixin
  26. from mwparserfromhell.template import Template
  27. from mwparserfromhell.text import Text
  28. __all__ = ["Wikicode"]
  29. FLAGS = re.I | re.S | re.U
  30. class Wikicode(StringMixin):
  31. def __init__(self, nodes):
  32. self._nodes = nodes
  33. def __unicode__(self):
  34. return "".join([unicode(node) for node in self.nodes])
  35. def _nodify(self, value):
  36. if isinstance(value, Wikicode):
  37. return value.nodes
  38. if isinstance(value, Node):
  39. return [value]
  40. if isinstance(value, str) or isinstance(value, unicode):
  41. return mwparserfromhell.parse(value).nodes
  42. error = "Needs string, Node, or Wikicode object, but got {0}: {1}"
  43. raise ValueError(error.format(type(value), value))
  44. def _get_all_nodes(self, code):
  45. for node in code.nodes:
  46. yield node
  47. if isinstance(node, Template):
  48. for child in self._get_all_nodes(node.name):
  49. yield child
  50. for param in node.params:
  51. if param.showkey:
  52. for child in self._get_all_nodes(param.name):
  53. yield child
  54. for child in self._get_all_nodes(param.value):
  55. yield child
  56. def _show_tree(self, code, lines, marker=None, indent=0):
  57. def write(*args):
  58. if lines and lines[-1] is marker: # Continue from the last line
  59. lines.pop() # Remove the marker
  60. last = lines.pop()
  61. lines.append(last + " ".join(args))
  62. else:
  63. lines.append(" " * indent + " ".join(args))
  64. for node in code.nodes:
  65. if isinstance(node, Template):
  66. write("{{", )
  67. self._show_tree(node.name, lines, marker, indent + 1)
  68. for param in node.params:
  69. write(" | ")
  70. lines.append(marker) # Continue from this line
  71. self._show_tree(param.name, lines, marker, indent + 1)
  72. write(" = ")
  73. lines.append(marker) # Continue from this line
  74. self._show_tree(param.value, lines, marker, indent + 1)
  75. write("}}")
  76. elif isinstance(node, Text):
  77. write(unicode(node))
  78. else:
  79. raise NotImplementedError(node)
  80. return lines
  81. @property
  82. def nodes(self):
  83. return self._nodes
  84. def get(self, index):
  85. return self.nodes[index]
  86. def set(self, index, value):
  87. nodes = self._nodify(value)
  88. if len(nodes) > 1:
  89. raise ValueError("Cannot coerce multiple nodes into one index")
  90. if index >= len(self.nodes) or -1 * index > len(self.nodes):
  91. raise IndexError("List assignment index out of range")
  92. self.nodex.pop(index)
  93. if nodes:
  94. self.nodes[index] = nodes[0]
  95. def index(self, obj):
  96. if obj not in self.nodes:
  97. raise ValueError(obj)
  98. if isinstance(obj, Node):
  99. for i, node in enumerate(self.nodes):
  100. if node is obj:
  101. return i
  102. raise ValueError(obj)
  103. return self.nodes.index(obj)
  104. def insert(self, index, value, recursive=True):
  105. nodes = self._nodify(value)
  106. for node in reversed(nodes):
  107. self.nodes.insert(index, node)
  108. def insert_before(self, obj, value):
  109. if obj not in self.nodes:
  110. raise KeyError(obj)
  111. self.insert(self.index(obj), value)
  112. def insert_after(self, obj, value):
  113. if obj not in self.nodes:
  114. raise KeyError(obj)
  115. self.insert(self.index(obj) + 1, value)
  116. def append(self, value):
  117. nodes = self._nodify(value)
  118. for node in nodes:
  119. self.nodes.append(node)
  120. def remove(self, node):
  121. self.nodes.pop(self.index(node))
  122. def ifilter(self, recursive=False, matches=None, flags=FLAGS,
  123. forcetype=None):
  124. if recursive:
  125. nodes = self._get_all_nodes(self)
  126. else:
  127. nodes = self.nodes
  128. for node in nodes:
  129. if not forcetype or isinstance(node, forcetype):
  130. if not matches or re.search(matches, unicode(node), flags):
  131. yield node
  132. def ifilter_templates(self, recursive=False, matches=None, flags=FLAGS):
  133. return self.filter(recursive, matches, flags, forcetype=Template)
  134. def ifilter_text(self, recursive=False, matches=None, flags=FLAGS):
  135. return self.filter(recursive, matches, flags, forcetype=Text)
  136. def filter(self, recursive=False, matches=None, flags=FLAGS,
  137. forcetype=None):
  138. return list(self.ifilter(recursive, matches, flags, forcetype))
  139. def filter_templates(self, recursive=False, matches=None, flags=FLAGS):
  140. return list(self.ifilter_templates(recursive, matches, flags))
  141. def filter_text(self, recursive=False, matches=None, flags=FLAGS):
  142. return list(self.ifilter_text(recursive, matches, flags))
  143. def show_tree(self):
  144. marker = object() # Random object we can find with certainty in a list
  145. print "\n".join(self._show_tree(self, [], marker))