-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtexExporter.py
250 lines (221 loc) · 9.06 KB
/
texExporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import os
def getAllInside(first,last,content):
# returns a dict of keys first+value+last and the values
valuesDict = {}
for i in range(content.count(first)):
try:
f = content.index(first)
l = content[f+len(first):].index(last)
key = content[f:f+len(first)+l+len(last)]
value = key.replace(first,'').replace(last,'')
except ValueError:
print('Error')
continue
valuesDict[key] = value
content = content.replace(key,'')
return valuesDict
def getInside(first,last,content):
f = content.index(first)
l = content[f+len(first):].index(last)
key = content[f:f+len(first)+l+len(last)]
value = key.replace(first,'').replace(last,'')
return value
class TexExporter():
def __init__(self,filename,docID,docType):
self.docID = docID
self.filename = filename
self.document = open(os.getcwd()+'/Archieves/'+docID+'/'+filename+'.tex','w', encoding='utf8')
self.docType = docType
if docType == 'abntepusp':
from shutil import copyfile
copyfile(os.getcwd()+'/Modelos/abntepusp.cls',os.getcwd()+'/Archieves/'+docID+'/abntepusp.cls')
self.options = 'a4paper,capchap,espacoduplo,normaltoc'
self.addPackages = ['\\usepackage[alf,abnt-etal-list=0,abnt-repeated-author-omit=no]{abntex2cite}',]
else:
self.options = ''
self.addPackages = []
self.inBullet = False
self.writePreamble()
def writePreamble(self):
self.document.write('''\\documentclass['''+self.options+''']{'''+self.docType+'''}
\\usepackage[T1]{fontenc} %font encoding setup
\\usepackage[utf8]{inputenc} %input encoding setup
\\usepackage{graphicx} %for displaying figures
\\usepackage{mathtools} %for displaying math
\\usepackage{listings} %for displaying code
\\usepackage[font=small]{caption}
''')
for package in self.addPackages:
self.document.write(package+'\n')
self.document.write('''
\\setcounter{secnumdepth}{5} %for displaying numbers up to 5 levels
\\setcounter{tocdepth}{5} %for displaying numbers up to 5 levels in TOC
\\begin{document}
''')
def formatText(self,text):
'''handle bold, italic, etc '''
content = text.replace('\\sec{','\\ref{').replace('\\fig{','\\ref{').replace('\\eq{','\\ref{').replace('\\tab{','\\ref{')
if '<b>' in text:
toBold = getAllInside('<b>','</b>',text)
for bold in toBold:
text = text.replace(bold,'\\textbf{'+toBold[bold]+'}')
content = text
if '<i>' in text:
toItalicize = getAllInside('<i>','</i>',text)
for italic in toItalicize:
text = text.replace(italic,'\\textit{'+toItalicize[italic]+'}')
content = text
if '<code><pre>' in text: #multiline code
codes = getAllInside('<code><pre>','</pre></code>',text)
for code in codes:
text = text.replace(code,'\\begin{lstlisting}\n'+codes[code]+'\n\end{lstlisting}')
content = text
if '<code>' in text: #inline code
codes = getAllInside('<code>','</code>',text)
for code in codes:
text = text.replace(code,'\lstinline{'+codes[code]+'}')
content = text
return content
def addText(self,text,output):
if '!eq' in text:
text = output
formatedText = self.formatText(text)
self.document.write(formatedText+'\n\n')
def addHeading(self,title,level,label):
section = {1:'\chapter{',2:'\section{',3:'\subsection{',4:'\subsubsection{',5:'\paragraph{',6:'\subparagraph'}
formatedTitle = self.formatText(title)
if self.docType == 'article':
level += 1
if label:
end = '}\label{'+label+'}'
else:
end = '}'
tex = section[level]+formatedTitle+end
self.document.write(tex+'\n\n')
def addFigure(self,img,caption,source='',label='',width='0.5'):
citations = getAllInside('\cite{','}',source)
for cite in citations:
source = source.replace(cite,'\\protect'+cite)
if source:
figure = '''\\begin{figure}[!h]
\centering
\caption{'''+caption+'''}
\includegraphics[width='''+width+'''\\textwidth]{Images/'''+img+'''}
\label{'''+label+'''}
\caption*{Source: '''+source+'''}
\end{figure}'''
else:
figure = '''\\begin{figure}[!h]
\centering
\caption{'''+caption+'''}
\includegraphics[width='''+width+'''\\textwidth]{Images/'''+img+'''}
\label{'''+label+'''}
\end{figure}'''
self.document.write(figure+'\n\n')
def addTable(self,table,caption,label='',source=''):
cols = 0
for row in table.split('\n'):
rowCols = 0
if '||' in row:
rowCols = len(row.split('||'))
elif '|' in row:
rowCols = len(row.split('|'))
if rowCols > cols:
cols = rowCols
tableCode = '''\\begin{table}
\caption{'''+caption+'''}
\label{'''+label+'''}
\centering
\\begin{tabular}{'''+' c |'*(cols-1)+''' c }
'''
for row in table.split('\n'):
if '||' in row:
headings = row.split('||')
for heading in headings:
tableCode += heading + ' & '
tableCode = tableCode[:-3]
tableCode += '\\\\\n\hline\n'
elif '|' in row:
colText = row.split('|')
for text in colText:
tableCode += text + ' & '
tableCode = tableCode[:-3]
tableCode += '\\\\\n'
tableCode += '''\hline
\end{tabular}'''
if source:
tableCode += '''
\caption*{Source: '''+source+'''}
'''
tableCode += '\end{table}'
self.document.write(tableCode+'\n\n')
def addTitle(self,title):
if self.docType == 'abntepusp':
titleCode = '\\titulo{'+title+'}\n'
else:
titleCode = '\\title{'+title+'}\n\\maketitle\n\n'
self.document.write(titleCode)
def addAuthor(self,author):
if self.docType == 'abntepusp':
names = author.split(' ')
firstNames = ' '.join(names[:-1])
lastName = names[-1]
self.document.write('\\autorPoli{'+firstNames+'}{}{}{}{'+lastName+'}\n')
else:
self.document.write('\\author{'+author+'}\n')
def addAdvisor(self,advisor):
if self.docType == 'abntepusp':
self.document.write('\\orientador{'+advisor+'}\n')
def addConcentrationArea(self,area):
if self.docType == 'abntepusp':
self.document.write('\\areaConcentracao{'+area+'}\n')
def addDepartment(self,department):
if self.docType == 'abntepusp':
self.document.write('\\departamento{'+department+'}\n')
def addModelType(self,modelType,area):
if self.docType == 'abntepusp':
self.document.write('\\'+modelType+'{'+area+'}\n')
def addLocal(self,local):
if self.docType == 'abntepusp':
self.document.write('\\local{'+local+'}\n')
def addDate(self,date):
if self.docType == 'abntepusp':
self.document.write('\\data{'+date+'}\n')
def makeCover(self):
if self.docType == 'abntepusp':
self.document.write('\\capa{}\n\\folhaderosto{}\n\n')
self.document.write('\\tableofcontents\n')
self.document.write('\\listoffigures\n')
self.document.write('\\listoftables\n\n')
def addBullet(self,topic):
topic = self.formatText(topic)
self.document.close()
self.document = open(os.getcwd()+'/Archieves/'+self.docID+'/'+self.filename+'.tex','r', encoding='utf8')
backUp = self.document.readlines()
self.document.close()
self.document = open(os.getcwd()+'/Archieves/'+self.docID+'/'+self.filename+'.tex','w', encoding='utf8')
if '\\end{itemize}' in backUp[-2]:
del backUp[-1]
backUp[-1] = '\\item '+topic+'\n'
self.document.write(''.join(backUp))
self.document.write('\\end{itemize}\n\n')
else:
self.document.write(''.join(backUp))
self.document.write('\\begin{itemize}\n')
self.document.write('\\item '+topic+'\n')
self.document.write('\\end{itemize}\n\n')
def addReferences(self,refs):
self.document.write('\\newpage\n')
if not self.docType == 'abntepusp':
self.document.write('\\bibliographystyle{plain}\n\n')
self.document.write('\\bibliography{database}\n\n')
def addAbstracts(self,title,content):
content = content.replace('<br>','\n')
content = content.replace('<h1 class="abstract">'+title+'</h1>','')
title = title.lower()
self.document.write('\\begin{'+title+'}\n\n')
self.document.write(self.formatText(content.replace('!'+title,'')))
self.document.write('\\end{'+title+'}\n\n')
def close(self):
self.document.write('\\end{document}')
self.document.close()