-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlFromText.py
More file actions
327 lines (314 loc) · 11.1 KB
/
htmlFromText.py
File metadata and controls
327 lines (314 loc) · 11.1 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
# attention, l'ordre des sous-fonctions est important
from os import sep
from io import BytesIO
import base64
from textFct import *
from htmlFct import *
from fileLocal import pathRoot
import loggerFct as log
protocols =( 'https://', 'http://', 'file:///' )
def toList (text):
if '<li>' in text:
text = '\n'+ text +'\n'
textList = text.split ('\n')
lc= range (len (textList))
# rajouter les balises fermantes
for l in lc:
if '<li>' in textList[l]: textList[l] = textList[l] +'</li>'
lc= range (1, len (textList) -1)
# rajouter les balises ouvrantes et fermantes delimitant la liste, <ul/>. reperer les listes imbriquees.
for l in lc:
if '<li>' in textList[l]:
# compter le niveau d'imbrication (n) de l'element textList[l]
n=0
while '<li>'+n*'\t' in textList[l]: n+=1
n-=1
if '<li>'+n*'\t' in textList[l]:
# debut de la liste (ou sous-liste), mettre le <ul>
if '<li>'+n*'\t' not in textList [l-1]: textList[l] = '<ul>'+ textList[l]
# fin de la liste (ou sous-liste), mettre le </ul>
if '<li>'+n*'\t' not in textList [l+1]:
while n >-1:
if '<li>'+n*'\t' not in textList [l+1]: textList[l] = textList[l] + '</ul>'
n-=1
# mettre le texte au propre
text = '\n'.join (textList)
text = text.strip ('\n')
while '<li>\t' in text: text = text.replace ('<li>\t', '<li>')
while '<ul>\t' in text: text = text.replace ('<ul>\t', '<ul>')
# liste ordonnée
while '<li># ' in text:
d= text.find ('<li># ')
d= text[:d].rfind ('<ul>')
text = text[:d] + '<ol>' + text[d+4:]
f= text.find ('</ul>', d)
while text[d:f].count ('<ul>') != text[d:f].count ('</ul>'): f= text.find ('</ul>', f+4)
text = text[:f] + '</ol>' + text[f+5:]
text = text.replace ('<li># ', '<li>', 1)
return text
def toDefList (text):
if ": " not in text: return text
textList = text.split ('\n')
textListLen = len (textList)
d=-1; t=0
while t< textListLen:
if ": " in textList[t] and textList[t].count (": ") ==1 and d==-1: d=t
elif ": " not in textList[t] and d>=0:
if t-d >1:
listRange = range (d,t)
for l in listRange: textList[l] = '<dt>' + textList[l].replace (": ", '</dt><dd>') + '</dd>'
textList[d] = textList[d].replace ('<dt>', '<dl><dt>')
textList[t-1] = textList[t-1].replace ('</dd>', '</dd></dl>')
d=-1
t+=1
text = '\n'.join (textList)
text = text.replace ('\n<dt>', '<dt>')
text = text.replace ('</dd>\n', '<dd>')
return text
def toTable (text):
if '\t' not in text: return text
# les cases vides sont représentées par un point. les doubles tabulations ont été rajoutées pour une meilleure lisibilité au format txt
while '\t\t' in text: text = text.replace ('\t\t', '\t')
textList = text.split ('\n')
len_chn = len (textList)
d=-1; c=-1; i=0
while i< len_chn:
# rechercher une table
d=-1; c=-1
if '\t' in textList[i]:
c= textList[i].count ('\t')
d=i; i+=1
while i< len_chn and textList[i].count ('\t') ==c: i+=1
c=i-d
# une table a ete trouve
if c>1 and d>0:
rtable = range (d, i)
for j in rtable:
# entre les cases
textList [j] = textList [j].replace ('\t', '</td><td>')
# bordure des cases
textList [j] = '<tr><td>' + textList [j] +'</td></tr>'
# les limites de la table
textList [d] = '<table>' + textList [d]
textList [i-1] = textList [i-1] +'</table>'
i+=1
text = '\n'.join (textList)
text = text.replace ('\n<tr>', '<tr>')
# les titres de colonnes ou de lignes
if ':</td></tr>' in text:
textList = text.split (':</td></tr>')
paragraphRange = range (len (textList) -1)
for p in paragraphRange:
d= textList[p].rfind ('<tr><td>')
textList[p] = textList[p][:d] +'<tr><th>'+ textList[p][d+8:].replace ('td>', 'th>')
text = '</th></tr>'.join (textList)
if ':</td>' in text:
textList = text.split (':</td>')
paragraphRange = range (len (textList) -1)
for p in paragraphRange:
d= textList[p].rfind ('<td>')
textList[p] = textList[p][:d] +'<th>'+ textList[p][d+4:]
text = '</th>'.join (textList)
text = text.replace ('\t', "")
text = text.replace ('<td>.</td>', '<td></td>')
return text
def toImageProtocolExtension (text, protocol, extension):
extension = '.'+ extension
if protocol not in text or extension not in text: return text
textList = text.split (extension);
textRange = range (len (textList) -1)
for i in textRange:
if protocol not in textList[i]: continue
# trouver l'url
d= textList[i].rfind (protocol)
if '\n' in textList[i][d:] or '\t' in textList[i][d:]: continue
url = textList[i][d:] + extension
textList[i] = textList[i][:d]
title =""
# trouver la description
if textList[i+1][:2] == " (":
d= textList[i+1].find (')')
title = textList[i+1][2:d]
textList[i+1] = textList[i+1][d+1:].strip()
elif textList[i][-2:] == ": ":
d=0
if '\n' in textList[i]: d=1+ textList[i].rfind ('\n')
title = textList[i][d:-2]
# if '>' in title or '<' in title: title = findTitleFromUrl (url)
textList[i] = textList[i][:d]
else:
if '\\' in url: d=1+ url.rfind ('\\')
else: d=1+ url.rfind ('/')
title = url[d:]
d= title.rfind ('.')
title = title[:d]
url = "\n<img src='" + url + "' alt='" + title +"' />\n"
url = url.replace ('http', 'ht/tp');
url = url.replace ('file', 'fi/le');
textList[i] = textList[i] + url
text = extension.join (textList)
text = text.replace ('/>\n' + extension, ' />\n')
return text
def toImage (text):
imgExtension =( 'jpg', 'jpeg', 'bmp', 'gif', 'png')
text = text.replace ('file:///', "")
text = text.replace ('C:/', 'file:///C:/')
text = text.replace ('C:\\', 'file:///C:\\')
for protocol in protocols:
for extension in imgExtension: text = toImageProtocolExtension (text, protocol, extension)
return text
def toLinkProtocol (text, protocol):
if protocol not in text: return text
endingChars = '<;, !\t\n'
text = text +'\n'
textList = text.split (protocol)
paragraphRange = range (1, len (textList))
for p in paragraphRange:
# récupérer l'url
url = textList[p]
e=-1; f=-1; d=-1
for char in endingChars:
if char in url:
f= url.find (char)
if f>=0: url = url[:f]
d= len (url)
textList[p] = textList[p][d:]
if url[-1] == '/': url = url[:-1]
# récupérer le tître
title =""
if textList[p][:2] == ' (':
d= textList[p].find (')')
title = textList[p][2:d]
textList[p] = textList[p][d+1:]
elif ': '== textList[p-1][-2:]:
d=1+ textList[p-1].rfind ('\n')
title = textList[p-1][d:-2]
textList[p-1] = textList[p-1][:d]
else: title = findTitleFromUrl (url)
textList[p] = url +"'>"+ title +'</a>\n'+ textList[p]
text = ("\n<a href='" + protocol).join (textList)
while '\n\n' in text: text = text.replace ('\n\n', '\n')
return text
def toLink (text):
text = text.replace ('c:/', 'file:///c:/')
text = text.replace ('file:///file:///', 'file:///')
for protocol in protocols: text = toLinkProtocol (text, protocol)
return text
def toEmphasis (text):
if '\n* ' in text:
textList = text.split ('\n* ')
lc= range (len (textList))
# rajouter les balises fermantes
for l in lc:
if ': ' in textList[l][1:100]:
textList[l] = textList[l].replace (': ',':</strong> ',1)
textList[l] = '<strong>' + textList[l]
text = '\n'.join (textList)
return text
def toMath (text):
if '\nM\t' in text:
textList = text.split ('\n')
paragraphRange = range (1, len (textList))
for i in paragraphRange:
if textList[i][:2] == 'M\t':
if 'f/' in textList[i]:
textList[i] = textList[i].replace (' f/ ', '<mfrac><mrow>')
textList[i] = textList[i].replace ('\tf/ ', '<mfrac><mrow>')
textList[i] = textList[i].replace (' /f', '</mrow></mfrac>')
if '</mfrac>' not in textList[i]: textList[i] = textList[i] + '</mrow></mfrac>'
textList[i] = textList[i].replace (' / ', '</mrow><mrow>')
textList[i] = '<math>' + textList[i][2:] + '</math>'
text = '\n'.join (textList)
return text
def toFigure (text):
if '<figure>' in text:
# mettre en forme le contenu des figures
textList = text.split ('<figure>')
paragraphRange= range (1, len (textList))
for i in paragraphRange:
f= textList[i].find ('<p>/</p>')
fin = '</figure>' + textList[i][f+8:]
textList[i] = textList[i][:f].strip()
textList[i] = textList[i].strip ('\n\t ')
# nettoyer le texte
textList[i] = textList[i].replace ('/></p>', '/>')
textList[i] = textList[i].replace ('<p><img', '<img')
textList[i] = textList[i].replace ('<p>', '<figcaption>', 1)
textList[i] = textList[i].replace ('</p>', '</figcaption>', 1)
textList[i] = textList[i] + fin
text = '<figure>'.join (textList)
return text
def toCode (text):
if '<xmp>' not in text: return text
elif '<xmp>:' in text:
textList = text.split ('\n<xmp>:')
paragraphRange = range (1, len (textList))
for t in paragraphRange:
textList[t] = textList[t].replace ('\n', '</xmp>\n', 1)
text = '\n<xmp>'.join (textList)
if '<xmp>!' in text:
textList = text.split ('<xmp>!')
paragraphRange = range (1, len (textList))
for i in paragraphRange:
f= textList[i].find ('\n/\n')
fin = '</xmp>\n' + textList[i][f+3:]
textList[i] = textList[i][:f].strip()
textList[i] = textList[i].strip ('\n\t ')
textList[i] = textList[i].replace ('\n', '\a')
textList[i] = textList[i].replace ('\t', '\f')
textList[i] = textList[i] + fin
text = '\n<xmp>'.join (textList)
text = text.replace ('\a</xmp>', '</xmp>')
text = text.replace ('< ','<') # reformer les balises html du code
return text
def toHtml (text):
text = shape (text)
# transformer la mise en page en balises
text = '\n'+ text +'\n'
for html, perso in tagHtml:
if perso in text: text = text.replace (perso, html)
while '\n\n' in text: text = text.replace ('\n\n', '\n')
# compléter les tîtres
textList = text.split ('\n')
textRange = range (len (textList))
for l in textRange:
if textList[l][:2] != '<h' or textList[l][3] != '>' or textList[l][2] not in '123456': continue
textList[l] = textList[l] +'</h'+ textList[l][2] +'>'
text = '\n'.join (textList)
# autres modifications
text = toImage (text)
text = toMath (text)
text = toCode (text)
text = toList (text)
text = toTable (text)
text = toLink (text)
text = toDefList (text)
text = cleanText (text)
text = toEmphasis (text)
# rajouter les <p/>
text = text.replace ('\n', '</p><p>')
text = text.replace ('></p><p><', '><')
text = text.replace ('></p><p>', '><p>')
text = text.replace ('</p><p><', '</p><')
# rajouter d'eventuel <p/> s'il n'y a pas de balise en debut ou fin de text
if '<' not in text [0:3]: text = '<p>'+ text
if '>' not in text [-3:]: text = text +'</p>'
# restaurer le texte, remplacer mes placeholders
text = text.replace ('ht/tp', 'http')
text = text.replace ('fi/le', 'file')
# mettre les liens dans des paragraphes
text = text.replace ('</a>', '</a></p>')
text = text.replace ('<a ', '<p><a ')
text = text.replace ('</p></p>', '</p>')
text = text.replace ('<p><p>', '<p>')
while 'ile:///file:///' in text: text = text.replace ('ile:///file:///', 'ile:///')
text = text.replace ('\a', '\n')
text = text.replace ('\f', '\t')
text = cleanBasic (text)
text = text.replace (' </', '</')
text = text.replace ('<p>.</p>', '<br/>')
text = toFigure (text)
text = text.replace ('<p>-->', "<p class='arrow'>")
return text