A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

91 行
3.0 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 tokens
  23. __all__ = ["Tokenizer"]
  24. class BadRoute(Exception):
  25. pass
  26. class Tokenizer(object):
  27. START = object()
  28. END = object()
  29. def __init__(self):
  30. self._text = None
  31. self._head = 0
  32. self._stacks = []
  33. self._context = []
  34. def _push(self):
  35. self._stacks.append([])
  36. def _pop(self):
  37. return self._stacks.pop()
  38. def _write(self, token, stack=None):
  39. if stack is None:
  40. stack = self._stacks[-1]
  41. if not stack:
  42. stack.append(token)
  43. return
  44. last = stack[-1]
  45. if isinstance(token, tokens.Text) and isinstance(last, tokens.Text):
  46. last.text += token.text
  47. else:
  48. stack.append(token)
  49. def _read(self, delta=0, wrap=False):
  50. index = self._head + delta
  51. if index < 0 and (not wrap or abs(index) > len(self._text)):
  52. return self.START
  53. if index >= len(self._text):
  54. return self.END
  55. return self._text[index]
  56. def _parse_until(self, stop):
  57. self._push()
  58. while True:
  59. if self._read() in (stop, self.END):
  60. return self._pop()
  61. elif self._read(0) == "{" and self._read(1) == "{":
  62. reset = self._head
  63. self._head += 2
  64. try:
  65. template = self._parse_until("}")
  66. except BadRoute:
  67. self._head = reset
  68. self._write(tokens.Text(text=self._read()))
  69. else:
  70. self._write(tokens.TemplateOpen())
  71. self._stacks[-1] += template
  72. self._write(tokens.TemplateClose())
  73. else:
  74. self._write(tokens.Text(text=self._read()))
  75. self._head += 1
  76. def tokenize(self, text):
  77. self._text = list(text)
  78. return self._parse_until(stop=self.END)