123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- #!/usr/bin/env python
- # __author__ = "Ronie Martinez"
- # __copyright__ = "Copyright 2016-2019, Ronie Martinez"
- # __credits__ = ["Ronie Martinez"]
- # __license__ = "MIT"
- # __maintainer__ = "Ronie Martinez"
- # __email__ = "ronmarti18@gmail.com"
- import re
- import xml.etree.cElementTree as eTree
- from xml.sax.saxutils import unescape
- from copy import deepcopy, copy
- from latex2maple.aggregator import aggregate
- from latex2maple.commands import MATRICES, COMMANDS
- from latex2maple.symbols_parser import convert_symbol
- def convert(latex):
- math = ''
- # 先加一个 mrow
- row = []
- latex = aggregate(latex)
- remove_excess(latex)
- print(latex)
- _classify_subgroup(latex, row)
- return row
- def _classify_subgroup(elements, row):
- # 对列表进行操作
- # elements:latex
- iterable = iter(range(len(elements)))
- for i in iterable:
- element = elements[i]
- if isinstance(element, list):
- # 遇到一个list 加一个 mrow
- _row = ''
- _classify_subgroup(element, _row)
- elif element in COMMANDS:
- _convert_command(element, elements, i, iterable, row)
- else:
- _classify(element, row)
- def _convert_command(element, elements, index, iterable, parent):
- # 对特殊符号进行操作
- _get_prefix_element(element, parent)
- # 先得到关键词信息
- params, tag, attributes = COMMANDS[element]
- # 得到一个主列表
- new_parent = [tag]
- for j in range(params): # 需要的元素数
- index += 1
- param = elements[index] #拿到一个元素
- print('else', param)
- if isinstance(param, list): # 如果是一个list
- '''
- 主元素后面添加对应属性
- '''
- print(new_parent, param, 'xxxx')
- _parent = parent
- # _parent.append(new_parent)
- _classify_subgroup(param, _parent)
- else:
- '这就要求只有特殊的是list 其余的都是str'
- _classify(param, new_parent)
- [next(iterable) for _ in range(params)]
- def _classify(_element, parent):
- # 对最后的str进行操作
- if isinstance(parent, list) and len(parent) > 0 and isinstance(parent[-1], list):
- parent[-1] += _element
- else:
- parent += _element
- def _remove_excess(iter):
- if isinstance(iter, list):
- if len(iter) == 1 and isinstance(iter[0], list):
- iter = iter[0]
- return _remove_excess(iter)
- return iter
- def remove_excess(iter):
- for i, j in enumerate(iter):
- x = _remove_excess(j)
- iter[i] = x
- if isinstance(j, list):
- remove_excess(j)
- def _convert(tree):
- xml_string = eTree.tostring(tree)
- try:
- return unescape(xml_string)
- except TypeError:
- return unescape(xml_string.decode('utf-8'))
- def _convert_matrix_content(param, parent, alignment=None):
- for row in param:
- mtr = eTree.SubElement(parent, 'mtr')
- iterable = iter(range(len(row)))
- for i in iterable:
- element = row[i]
- if alignment:
- column_align = {'r': 'right', 'l': 'left', 'c': 'center'}.get(alignment)
- mtd = eTree.SubElement(mtr, 'mtd', columnalign=column_align)
- else:
- mtd = eTree.SubElement(mtr, 'mtd')
- if isinstance(element, list):
- _classify_subgroup(element, mtd)
- elif element in COMMANDS:
- _convert_command(element, row, i, iterable, mtd)
- else:
- _classify(element, mtd)
- def _convert_array_content(param, parent, alignment=None):
- if '|' in alignment:
- _alignment, columnlines = [], []
- for i in alignment:
- if i == '|':
- columnlines.append('solid')
- else:
- _alignment.append(i)
- if len(_alignment) - len(columnlines) == 2:
- columnlines.append('none')
- parent.attrib['columnlines'] = ' '.join(columnlines)
- else:
- _alignment = list(alignment)
- rowlines = []
- row_count = 0
- for row in param:
- row_count += 1
- mtr = eTree.SubElement(parent, 'mtr')
- iterable = iter(range(len(row)))
- index = 0
- has_rowline = False
- for i in iterable:
- element = row[i]
- if element == r'\hline' and row_count > 1:
- rowlines.append('solid')
- has_rowline = True
- continue
- try:
- align = _alignment[index]
- except IndexError:
- align = None
- if align:
- # noinspection PyTypeChecker
- column_align = {'r': 'right', 'l': 'left', 'c': 'center'}.get(align)
- mtd = eTree.SubElement(mtr, 'mtd', columnalign=column_align)
- else:
- mtd = eTree.SubElement(mtr, 'mtd')
- if isinstance(element, list):
- _classify_subgroup(element, mtd)
- elif element in COMMANDS:
- _convert_command(element, row, i, iterable, mtd)
- else:
- _classify(element, mtd)
- index += 1
- if not has_rowline and row_count > 1:
- rowlines.append('none')
- if 'solid' in rowlines:
- parent.set('rowlines', ' '.join(rowlines))
- def _get_prefix_element(element, row):
- if element in (r'\binom', r'\pmatrix'):
- _convert_and_append_operator(r'\lparen', row)
- elif element == r'\bmatrix':
- _convert_and_append_operator(r'\lbrack', row)
- elif element == r'\Bmatrix':
- _convert_and_append_operator(r'\lbrace', row)
- elif element == r'\vmatrix':
- _convert_and_append_operator(r'\vert', row)
- elif element == r'\Vmatrix':
- _convert_and_append_operator(r'\Vert', row)
- def _convert_and_append_operator(symbol, parent):
- symbol = convert_symbol(symbol)
- mo = eTree.SubElement(parent, 'mo')
- mo.text = '&#x{};'.format(symbol)
- def _get_postfix_element(element, row):
- if element in (r'\binom', r'\pmatrix'):
- _convert_and_append_operator(r'\rparen', row)
- elif element == r'\bmatrix':
- _convert_and_append_operator(r'\rbrack', row)
- elif element == r'\Bmatrix':
- _convert_and_append_operator(r'\rbrace', row)
- elif element == r'\vmatrix':
- _convert_and_append_operator(r'\vert', row)
- elif element == r'\Vmatrix':
- _convert_and_append_operator(r'\Vert', row)
|