10
0
mirror of https://github.com/LCPQ/QUESTDB_website.git synced 2024-11-05 05:33:55 +01:00
QUESTDB_website/tools/lib/LaTeX.py

140 lines
4.0 KiB
Python
Raw Normal View History

import re
from TexSoup import TexSoup,TexCmd,TexNode
from .utils import *
import itertools
from enum import Enum
from abc import ABCMeta
class commandBase(metaclass=ABCMeta):
def __init__(self,source,supportedTexType):
if not (source.name==supportedTexType):
raise ValueError(source+" is not a "+supportedTexType)
self.source=source
def __str__(self):
return self.source.__str__
def __repr__(self):
return self.source.__repr__()
class newCommand(commandBase):
def __init__(self,source):
super(newCommand, self).__init__(source,"newcommand")
@property
def commandName(self):
return self.source.args[0].value[1:]
@property
def argNum(self):
return 0 if self.source.args[1].type!='optional' else self.source.args[1].value
@property
def result(self):
return nodify(list(self.source.args[len(self.source.args)-1].contents))
def exist(self,tex):
exist=tex.find(self.commandName)!=None
return exist
def run(self,tex):
cmds=list(tex.find_all(self.commandName))
if len(cmds)==0:
raise ValueError("Command not found in tex")
else:
for cmd in cmds:
if self.argNum==0:
2019-12-12 15:18:48 +01:00
fres=self.result
else:
resultstr=str(self.result)
2020-02-06 15:42:49 +01:00
res=TexSoup(re.sub(f'\#([1-{self.argNum}])',lambda m: cmd.args[int(m.group(1))-1].value,resultstr))
2019-12-12 15:18:48 +01:00
fres=TexSoup(res)
2020-02-06 15:42:49 +01:00
if str(tex)==str(cmd):
tex.expr=fres.expr
else:
2020-02-09 14:04:03 +01:00
try:
tex.replace(cmd,fres)
except:
return False
return True
2020-02-07 11:08:19 +01:00
def tryRun(self,tex):
cmds=list(tex.find_all(self.commandName))
if len(cmds)!=0:
for cmd in cmds:
if self.argNum==0:
res=self.result
else:
res=re.sub('#[1-{}]'.format(self.argNum),lambda m: cmd.args[int(m.group(1))-1],self.result)
soup=TexSoup(res)
tex.replace(cmd,soup)
@staticmethod
def runAll(tex,collection):
cmds=[cmd for cmd in collection if cmd.exist(tex)]
if(len(cmds)>0):
for cmd in cmds:
2020-02-09 14:04:03 +01:00
if not cmd.run(tex):
cmds.remove(cmd)
collection.remove(cmd)
newCommand.runAll(tex,collection)
class columnAlignment(Enum):
Left = "l"
Right = "r"
Center = "c"
class multiColumn(commandBase):
def __init__(self,source):
super(multiColumn,self).__init__(source,"multicolumn")
@property
def cols(self):
return int(self.source.args[0].value)
@property
def align(self):
return columnAlignment(self.source.args[1].value)
@property
def contents(self):
return nodify(list(self.source.args[2].contents))
def tabularToData(table,commands=None):
if table.name=="tabular":
ctable=str(table)
ctable=ctable.split("\n")
ctable=ctable[1:len(ctable)-1]
rows=[x.strip() for x in ''.join(ctable).split(r'\\') if x.strip()!='']
ltable=[]
ltable=[[c.strip() for c in r.split("&")] for r in rows]
lnewtable=[]
for row in ltable:
r=[]
for item in row:
texitem=TexSoup(item)
child=list(texitem.children)
if(len(child)==1 and child[0].name=="multicolumn"):
mcolel=child[0]
mcol=multiColumn(mcolel)
el=mcol.contents
2020-02-07 11:08:19 +01:00
el=desarg(el)
if commands!=None:
newCommand.runAll(el,commands)
for i in range(int(mcol.cols)):
r.append(el)
else:
if type(item) is str:
el=TexSoup(item)
else:
el=item
el=desarg(el)
if commands!=None:
newCommand.runAll(el,commands)
r.append(el)
lnewtable.append(r)
lens=[len(x) for x in lnewtable]
#Check if all rows have the same dimension
slens=set(lens)
if(len(slens)!=1):
raise ValueError("This tabular is not supported because lines have not the same column numbers for each row the coulumns numbers are {}".format(lens))
import numpy as np
table=np.array(lnewtable,TexNode)
return table
else:
raise ValueError("Only tabular LaTeX environment is supported")