converter.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/usr/bin/env python
  2. # __author__ = "Ronie Martinez"
  3. # __copyright__ = "Copyright 2016-2019, Ronie Martinez"
  4. # __credits__ = ["Ronie Martinez"]
  5. # __license__ = "MIT"
  6. # __maintainer__ = "Ronie Martinez"
  7. # __email__ = "ronmarti18@gmail.com"
  8. import re
  9. import xml.etree.cElementTree as eTree
  10. from xml.sax.saxutils import unescape
  11. from copy import deepcopy, copy
  12. from latex2maple.aggregator import aggregate
  13. from latex2maple.commands import MATRICES, COMMANDS
  14. from latex2maple.symbols_parser import convert_symbol
  15. def convert(latex):
  16. math = ''
  17. # 先加一个 mrow
  18. row = []
  19. latex = aggregate(latex)
  20. remove_excess(latex)
  21. print(latex)
  22. _classify_subgroup(latex, row)
  23. return row
  24. def _classify_subgroup(elements, row):
  25. # 对列表进行操作
  26. # elements:latex
  27. iterable = iter(range(len(elements)))
  28. for i in iterable:
  29. element = elements[i]
  30. if isinstance(element, list):
  31. # 遇到一个list 加一个 mrow
  32. _row = ''
  33. _classify_subgroup(element, _row)
  34. elif element in COMMANDS:
  35. _convert_command(element, elements, i, iterable, row)
  36. else:
  37. _classify(element, row)
  38. def _convert_command(element, elements, index, iterable, parent):
  39. # 对特殊符号进行操作
  40. _get_prefix_element(element, parent)
  41. # 先得到关键词信息
  42. params, tag, attributes = COMMANDS[element]
  43. # 得到一个主列表
  44. new_parent = [tag]
  45. for j in range(params): # 需要的元素数
  46. index += 1
  47. param = elements[index] #拿到一个元素
  48. print('else', param)
  49. if isinstance(param, list): # 如果是一个list
  50. '''
  51. 主元素后面添加对应属性
  52. '''
  53. print(new_parent, param, 'xxxx')
  54. _parent = parent
  55. # _parent.append(new_parent)
  56. _classify_subgroup(param, _parent)
  57. else:
  58. '这就要求只有特殊的是list 其余的都是str'
  59. _classify(param, new_parent)
  60. [next(iterable) for _ in range(params)]
  61. def _classify(_element, parent):
  62. # 对最后的str进行操作
  63. if isinstance(parent, list) and len(parent) > 0 and isinstance(parent[-1], list):
  64. parent[-1] += _element
  65. else:
  66. parent += _element
  67. def _remove_excess(iter):
  68. if isinstance(iter, list):
  69. if len(iter) == 1 and isinstance(iter[0], list):
  70. iter = iter[0]
  71. return _remove_excess(iter)
  72. return iter
  73. def remove_excess(iter):
  74. for i, j in enumerate(iter):
  75. x = _remove_excess(j)
  76. iter[i] = x
  77. if isinstance(j, list):
  78. remove_excess(j)
  79. def _convert(tree):
  80. xml_string = eTree.tostring(tree)
  81. try:
  82. return unescape(xml_string)
  83. except TypeError:
  84. return unescape(xml_string.decode('utf-8'))
  85. def _convert_matrix_content(param, parent, alignment=None):
  86. for row in param:
  87. mtr = eTree.SubElement(parent, 'mtr')
  88. iterable = iter(range(len(row)))
  89. for i in iterable:
  90. element = row[i]
  91. if alignment:
  92. column_align = {'r': 'right', 'l': 'left', 'c': 'center'}.get(alignment)
  93. mtd = eTree.SubElement(mtr, 'mtd', columnalign=column_align)
  94. else:
  95. mtd = eTree.SubElement(mtr, 'mtd')
  96. if isinstance(element, list):
  97. _classify_subgroup(element, mtd)
  98. elif element in COMMANDS:
  99. _convert_command(element, row, i, iterable, mtd)
  100. else:
  101. _classify(element, mtd)
  102. def _convert_array_content(param, parent, alignment=None):
  103. if '|' in alignment:
  104. _alignment, columnlines = [], []
  105. for i in alignment:
  106. if i == '|':
  107. columnlines.append('solid')
  108. else:
  109. _alignment.append(i)
  110. if len(_alignment) - len(columnlines) == 2:
  111. columnlines.append('none')
  112. parent.attrib['columnlines'] = ' '.join(columnlines)
  113. else:
  114. _alignment = list(alignment)
  115. rowlines = []
  116. row_count = 0
  117. for row in param:
  118. row_count += 1
  119. mtr = eTree.SubElement(parent, 'mtr')
  120. iterable = iter(range(len(row)))
  121. index = 0
  122. has_rowline = False
  123. for i in iterable:
  124. element = row[i]
  125. if element == r'\hline' and row_count > 1:
  126. rowlines.append('solid')
  127. has_rowline = True
  128. continue
  129. try:
  130. align = _alignment[index]
  131. except IndexError:
  132. align = None
  133. if align:
  134. # noinspection PyTypeChecker
  135. column_align = {'r': 'right', 'l': 'left', 'c': 'center'}.get(align)
  136. mtd = eTree.SubElement(mtr, 'mtd', columnalign=column_align)
  137. else:
  138. mtd = eTree.SubElement(mtr, 'mtd')
  139. if isinstance(element, list):
  140. _classify_subgroup(element, mtd)
  141. elif element in COMMANDS:
  142. _convert_command(element, row, i, iterable, mtd)
  143. else:
  144. _classify(element, mtd)
  145. index += 1
  146. if not has_rowline and row_count > 1:
  147. rowlines.append('none')
  148. if 'solid' in rowlines:
  149. parent.set('rowlines', ' '.join(rowlines))
  150. def _get_prefix_element(element, row):
  151. if element in (r'\binom', r'\pmatrix'):
  152. _convert_and_append_operator(r'\lparen', row)
  153. elif element == r'\bmatrix':
  154. _convert_and_append_operator(r'\lbrack', row)
  155. elif element == r'\Bmatrix':
  156. _convert_and_append_operator(r'\lbrace', row)
  157. elif element == r'\vmatrix':
  158. _convert_and_append_operator(r'\vert', row)
  159. elif element == r'\Vmatrix':
  160. _convert_and_append_operator(r'\Vert', row)
  161. def _convert_and_append_operator(symbol, parent):
  162. symbol = convert_symbol(symbol)
  163. mo = eTree.SubElement(parent, 'mo')
  164. mo.text = '&#x{};'.format(symbol)
  165. def _get_postfix_element(element, row):
  166. if element in (r'\binom', r'\pmatrix'):
  167. _convert_and_append_operator(r'\rparen', row)
  168. elif element == r'\bmatrix':
  169. _convert_and_append_operator(r'\rbrack', row)
  170. elif element == r'\Bmatrix':
  171. _convert_and_append_operator(r'\rbrace', row)
  172. elif element == r'\vmatrix':
  173. _convert_and_append_operator(r'\vert', row)
  174. elif element == r'\Vmatrix':
  175. _convert_and_append_operator(r'\Vert', row)