-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlFct.py
356 lines (343 loc) · 12 KB
/
htmlFct.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
# attention, l'ordre des sous-fonctions est important
from os import sep
from PIL import Image, ImageOps
from io import BytesIO
import base64
from textFct import *
from fileLocal import pathRoot
import loggerFct as log
tagHtml =(
('\n<h1>', '\n====== '), ('</h1>\n', ' ======\n'), ('\n<h2>', '\n****** '), ('</h2>\n', ' ******\n'), ('\n<h3>', '\n------ '), ('</h3>\n', ' ------\n'), ('\n<h4>', '\n______ '), ('</h4>\n', ' ______\n'),
("\n<hr class='h1'/>\n", '\n\n======\n\n'), ("\n<hr class='h2'/>\n", '\n\n******\n\n'), ("\n<hr class='h3'/>\n", '\n\n------\n\n'),
("\n<hr>\n", '\n\n******\n\n'), ("\n<hr/>\n", '\n\n******\n\n'),
("\n<img src='", '\nImg\t'), ('\n<figure>', '\nFig\n'), ('</figure>', '\n/fig\n'), ('\n<xmp>', '\ncode\n'), ('</xmp>', '\n/code\n'),
('\n<li>', '\n\t')
)
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 toTable (text):
if '\t' in 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 d==-1 and c==-1 and '\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>\n' + textList [d]
textList [i-1] = textList [i-1] +'\n</table>'
i+=1
text = '\n'.join (textList)
# 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 toImage (text):
imgExtension =( 'jpg', 'jpeg', 'bmp', 'gif', 'png')
imgCharStart = '>\n\t\'",;!()[]{}:'
for ext in imgExtension:
if '.'+ ext in text:
textList = text.split ('.'+ ext)
textRange = range (len (textList) -1)
for i in textRange:
d= textList[i].rfind (':')
f= textList[i][:d].rfind (' ')
for char in imgCharStart:
e= textList[i][:d].rfind (char)
if e>f: f=e
f=f+1
title = textList[i][f+1:].replace ('-'," ")
if textList[i+1][:2] == ' (':
e= textList[i+1].find (')')
title = textList[i+1][2:e]
textList[i+1] = textList[i+1][e+1:]
title = title.replace ('-'," ")
else:
if '/' in title:
d=1+ title.rfind ('/')
title = title[d:]
if '\\' in title:
d=1+ title.rfind ('\\')
title = title[d:]
if '.' in title:
d=1+ title.rfind ('.')
title = title[d:]
title = cleanText (title)
title = title.replace ('_'," ")
title = title.replace ('.'," ")
textList[i] = textList[i][:f] + "<img src='" + textList[i][f:].replace ('http', 'ht/tp') +"."+ ext +"' alt='" + title +"'/>"
text = "".join (textList)
return text
def imgToB64One (imageName):
imageOriginal = Image.open (imageName)
imageOriginal = imageOriginal.convert ('RGB')
buff = BytesIO()
if imageName[-3:] == 'jpg': imageOriginal.save (buff, format='jpeg')
else: imageOriginal.save (buff, format=imageName[-3:])
imgStr = base64.b64encode (buff.getvalue())
imgStr = 'data:image/' + imageName[-3:] + ';base64,' + str (imgStr)[2:-1]
return imgStr
def imgToB64 (text):
if 'src=' in text:
text = text.replace ("src='http", "scr='http")
text = text.replace ('src="http', 'scr="http')
if 'src=' in text:
textList = text.split ('src=')
textRange = range (1, len (textList))
for t in textRange:
# if textList[t][1:5] == 'http': continue
f= textList[t].find (textList[t][0], 2)
if textList[t][f-4:f] not in '.bmp .png .gif .jpg': continue
imageName = textList[t][1:f].replace ('/', sep)
if pathRoot not in imageName: continue
imgStr = imgToB64One (imageName)
textList[t] = textList[t][0] + imgStr + textList[t][f:]
text = 'src='.join (textList)
text = text.replace ('scr=', 'src=')
return text
def toLink (text):
endingChars = '<;, !\t\n'
textList = text.split ('http')
paragraphRange = range (1, len (textList))
for p in paragraphRange:
paragraphTmp = textList[p]
e=-1; f=-1; d=-1
for char in endingChars:
if char in paragraphTmp:
f= paragraphTmp.find (char)
paragraphTmp = paragraphTmp [:f]
paragraphTmp = paragraphTmp.strip ('/')
d= paragraphTmp.rfind ('/') +1
e= len (paragraphTmp)
if '.' in paragraphTmp[d:]: e= paragraphTmp.rfind ('.')
title =""
if textList[p][f:f+2] == ' (':
e= textList[p].find (')')
title = textList[p][f+2:e]
textList[p] = textList[p][e+1:]
else:
textList[p] = textList[p][f:]
title = paragraphTmp [d:e].replace ('-',' ')
title = title.replace ('_',' ')
title = title.replace ('.'," ")
textList[p] = paragraphTmp +"'>"+ title +'</a> '+ textList[p]
text = " <a href='http".join (textList)
text = text.replace ('> <a ', '><a ')
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), 2)
for i in paragraphRange:
# nettoyer le texte pour faciliter sa transformation
textList[i] = textList[i].strip ('\n')
textList[i] = textList[i].split ('\n')
paragraphRange = range (len (textList[i]) -1)
for j in paragraphRange:
# les images ont deja ete modifiees precedement
if textList[i][j][:4] != '<img':
textList[i][j] = '<figcaption>' + textList[i][j] + '</figcaption>'
textList[i] = "".join (textList[i])
text = 'figure>'.join (textList)
return text
def toCode (text):
if '<xmp>' in text:
textList = text.split ('xmp>')
paragraphRange = range (1, len (textList), 2)
for i in paragraphRange:
textList[i] = textList[i].strip()
textList[i] = textList[i].strip ('\n\t ')
textList[i] = textList[i].replace ('\n', '\a')
textList[i] = textList[i].replace ('\t', '\f')
text = 'xmp>'.join (textList)
text = text.replace ('\a</xmp>', '</xmp>')
return text
def toHtml (text):
text = shape (text)
for char in '=*-_':
while 7* char in text: text = text.replace (7* char, 6* char)
# transformer la mise en page en balises
for html, perso in tagHtml:
if perso in text: text = text.replace (perso, html)
# autres modifications
text = toMath (text)
text = toFigure (text)
text = toCode (text)
text = toList (text)
text = toTable (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>'
# autres modifications
text = toImage (text)
text = toLink (text)
# restaurer le texte, remplacer mes placeholders
text = text.replace ('ht/tp', 'http')
text = text.replace ('\a', '\n')
text = text.replace ('\f', '\t')
text = cleanHtml (text)
text = text.replace (' </', '</')
text = text.replace ('<p>.</p>', '<br/>')
return text
def fromHtml (text):
# les conteneurs
container = [ 'div', 'section', 'ol', 'ul', 'table', 'figure', 'math' ]
tagsBlank =( ('<hr/>', '\n************\n'), ('<hr>', '\n************\n'), ('<br>', '\n'), ('<br/>', '\n'))
tagsClosing =( 'li', 'tr', 'th', 'td')
for tag in container:
text = text.replace ('</'+ tag +'>', "")
text = text.replace ('<'+ tag +'>', "")
# les tableaux
text = text.replace ('</td>', "")
text = text.replace ('</th>', ':')
text = text.replace ('</tr>', "")
text = text.replace ('<tr><td>', '\n')
text = text.replace ('<tr><th>', '\n')
text = text.replace ('<td>', '\t')
text = text.replace ('<th>', '\t')
# les tags
for html, perso in tagHtml: text = text.replace (html.strip(), perso)
for html, perso in tagsBlank: text = text.replace (html, perso)
for tag in tagsClosing: text = text.replace ('</'+ tag +'>', "")
# les lignes
text = text.replace ('</p><p>', '\n')
lines = [ 'p', 'caption', 'figcaption' ]
for tag in lines:
text = text.replace ('</'+ tag +'>', '\n')
text = text.replace ('<'+ tag +'>', '\n')
# les phrases
inner = [ 'span', 'em', 'strong' ]
for tag in inner:
text = text.replace ('</'+ tag +'>', ' ')
text = text.replace ('<'+ tag +'>', ' ')
text = text.replace (' \n', '\n')
text = text.replace ('\n ', '\n')
# les liens
ltext = text.split ('</a>')
rtext = range (len (ltext) -1)
for t in rtext:
d= ltext [t].find ('href') +6
f= ltext [t].find ("'", d)
link = ltext [t][d:f]
f= ltext [t].find ('>', f) +1
title = ltext [t][f:]
d= ltext [t].find ('<a ')
ltext [t] = ltext [t][:d] +' '+ title +': '+ link
text = ' '.join (ltext)
text = shape (text)
return text
def test():
text = """nfznvvz
zef,z "ofk" v,sknvdzkl.fnz fk" g
à 50:30:60
adresse: http://www.fr
"""
model = """fznvvz
%ssknvdzkl.fnz %s
"""
text = toHtml (text)
print ('toHtml\t', text)
text = fromHtml (text)
print ('fromHtml\t', text)