-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_html_jupyter.py
305 lines (206 loc) · 8.78 KB
/
convert_html_jupyter.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
from bs4 import BeautifulSoup
#from beautifulsoup4 import BeautifulSoup
import json
import urllib
import re
def parse_cell_output(div):
cell = {}
cell['output_type'] = "stream"
cell['name'] = "stdout"
cell['text'] = []
#print('find level 1')
for div2 in div.find_all('pre'):
#line = {}
ligneavecbalises = div2.get_text()
ligne = ligneavecbalises
#ligne = re.sub('<.*?>', '', ligneavecbalises)
ligne = ligne.replace(u'\u200b', '\n')
ligne = ligne.replace(u'\u0d0a', '\n')
if ligne != '' :
#lignehex = ":".join("{:02x}".format(ord(c)) for c in ligne)
#print(ligne, lignehex)
#cell['source'].append(json.dumps(ligne))
cell['text'].append(ligne)
return cell
def parse_cell_outputPng(div):
cell = {}
cell['data'] = { }
#text_html = []
for item in div.contents:
txt = str(item)
txt = txt.replace('\n', '')
paterns = re.findall('base64,.*?\"', txt)
for pat in paterns:
#print("Found 1 img")
ligne = pat[7:-1]
if ligne != '' :
#lignehex = ":".join("{:02x}".format(ord(c)) for c in ligne)
#print(ligne)
#cell['source'].append(json.dumps(ligne))
text_html = ligne
#print(ligne)
cell['data']["image/png"] = text_html
return cell
def parse_cell_outputHtml(div):
cell = {}
cell['output_type'] = "execute_result"
cell['data'] = { }
text_html = []
for item in div.contents:
txt = str(item)
#print('ligneavecbalises:', txt)
ligne = txt
if ligne != '' :
#lignehex = ":".join("{:02x}".format(ord(c)) for c in ligne)
#print(ligne)
#cell['source'].append(json.dumps(ligne))
text_html.append(ligne)
cell['data']["text/html"] = text_html
return cell
def parse_cell_Exec(div):
txt = str(div)
try:
patern = re.findall('\[.*?\]', txt)
num = int(patern[0][1:-1])
return num
except ValueError:
return -1
def parse_cell_code(div, cell_back_ground_wellow):
source = []
if cell_back_ground_wellow:
source.append('#SOL:\n')
#print('find level 1')
for div2 in div.find_all('pre', attrs={'class': 'CodeMirror-line'}):
#line = {}
ligneavecbalises = div2.get_text()
ligne = re.sub('<.*?>', '', ligneavecbalises)
ligne = ligne.replace(u'\u200b', '\n')
ligne = ligne.replace(u'\u0d0a', '\n')
if ligne != '' :
#lignehex = ":".join("{:02x}".format(ord(c)) for c in ligne)
#print(ligne, lignehex)
#cell['source'].append(json.dumps(ligne))
ligne = ligne + '\n'
source.append(ligne)
return source
def parse_cell_inout(div, DownloadCode, DownloadOutput):
cell = {}
cell['metadata'] = {}
cell['cell_type'] = "code"
cell['execution_count'] = None
cell['outputs'] = []
cell['source'] = []
cell_back_ground_wellow = False
for div2 in div.find_all('div', attrs={'class': ['input_area', 'output_subarea', 'input_prompt' ]}):
#print(div['aria-label'])
#if 'input_area' in div2['class'] and div2['aria-label']=='Edit code here':
if 'input_area' in div2['class'] and DownloadCode==True:
#print('In Out detected')
try:
if div2['style']=="background-color: rgb(255, 254, 240);":
cell_back_ground_wellow= True
cell['metadata']['function'] = "Yellow"
except KeyError:
pass
onecell = parse_cell_code(div2, cell_back_ground_wellow)
#cell['source'].append(onecell)
cell['source'] = onecell
else:
if 'output_subarea' in div2['class'] and 'output_text' in div2['class'] and DownloadOutput==True:
#print('Text Ouput detected')
onecellout = parse_cell_output(div2)
cell['outputs'].append(onecellout)
if 'output_subarea' in div2['class'] and 'output_html' in div2['class'] and DownloadOutput==True:
#print('Html Ouput detected')
onecellout = parse_cell_outputHtml(div2)
cell['outputs'].append(onecellout)
if 'output_subarea' in div2['class'] and 'output_png' in div2['class'] and DownloadOutput==True:
#print('Png Ouput detected')
onecellout = parse_cell_outputPng(div2)
cell['outputs'].append(onecellout)
if 'input_prompt' in div2['class'] and DownloadOutput==True:
#print('Excecution count detected')
onecellout = parse_cell_Exec(div2)
if onecellout != -1:
cell['execution_count'] = onecellout
return cell
def parse_cell_markup(div):
cell = {}
cell['metadata'] = {}
cell['cell_type'] = "markdown"
cell['source'] = []
for item in div.contents:
txt = str(item)
#print('ligneavecbalises:', txt)
ligne = re.sub('<blockquote.*?>', '> ', txt)
ligne = re.sub('</blockquote>', '\n', ligne)
ligne = re.sub('#', '\#', ligne)
ligne = re.sub('<li>', '* ', ligne)
ligne = re.sub('</?strong>', '**', ligne)
ligne = re.sub('<h[1-9].*?>', '# ', ligne)
ligne = re.sub('<.*?>', '', ligne)
ligne = ligne.replace(u'\u200b', '\n')
if ligne != '' :
#lignehex = ":".join("{:02x}".format(ord(c)) for c in ligne)
#print(ligne)
#cell['source'].append(json.dumps(ligne))
cell['source'].append(ligne)
return cell
def get_data(soup, DownloadMarkup, DownloadCode, DownloadOutput):
create_nb = {}
create_nb['nbformat'] = 4
create_nb['nbformat_minor'] = 2
create_nb['cells'] = []
create_nb['metadata'] = {"kernelspec":
{"display_name": "Python 3",
"language": "python", "name": "python3" }}
# Loop on cells (input only)
for div in soup.find_all('div', attrs={'class': ['code_cell', 'text_cell_render' ]}):
#print(div['aria-label'])
if 'code_cell' in div['class']:
#print('In Out detected')
cell = parse_cell_inout(div, DownloadCode, DownloadOutput)
create_nb['cells'].append(cell)
else:
if 'text_cell_render' in div['class'] and DownloadMarkup==True:
#print('Markup detected')
cell = parse_cell_markup(div)
create_nb['cells'].append(cell)
print('.', end='')
print('.')
return create_nb
import sys
DownloadMarkup=True
DownloadCode=True
DownloadOutput=True
filelist=[]
for i, arg in enumerate(sys.argv):
if i>0:
if len(arg) >=2 and arg[0] == '-':
param=True
if arg[2] == '0' or arg[2] == '0':
param=False
if arg[1] == 'm' or arg[1] == 'M':
DownloadMarkup=param
print('Switching markup to:', DownloadMarkup)
elif arg[1] == 'c' or arg[1] == 'C':
DownloadCode=param
print('Switching code to:', DownloadCode)
elif arg[1] == 'o' or arg[1] == 'O':
DownloadOutput=param
print('Switching output to:', DownloadOutput)
else:
filelist.append(arg)
for filenamearg in filelist:
with open(filenamearg) as filenames:
#print(filenames.name)
print("managing ", filenames.name)
fhand = open(filenames.name, mode="r", encoding="utf-8")
text = fhand.read()
#print('Please wait')
soup = BeautifulSoup(text, 'lxml')
#get_data(soup, 'post-content')
create_nb = get_data(soup, DownloadMarkup, DownloadCode, DownloadOutput)
#print('Saving outcome')
with open(filenames.name + '.ipynb', 'w') as jynotebook:
jynotebook.write(json.dumps(create_nb))