-
Notifications
You must be signed in to change notification settings - Fork 46
/
html_parser.py
454 lines (387 loc) · 18.5 KB
/
html_parser.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import html
import re
from bs4 import BeautifulSoup
import markdown
from flask import request
import os
# Convert Markdown to HTML using the markdown library
# Parameters:
# - md_content: String containing Markdown content
# Returns:
# - String containing HTML content
def convert_md_to_html(md_content):
html_content = markdown.markdown(md_content, tab_length=2)
return html_content
# Convert custom code block shortcodes to HTML
# Parameters:
# - md_content: String containing Markdown content with custom code block shortcodes
# Returns:
# - String with code block shortcodes converted to HTML
def convert_code_blocks_to_html(md_content):
# Regular expression to match code block shortcodes
codeblock_pattern = re.compile(r'{{%\s+codeblock\s+%}}(.*?){{%\s+/codeblock\s+%}}', re.DOTALL)
# Function to replace code block shortcodes with HTML
def replace_codeblock(match):
code_content = match.group(1).strip()
# Regular expression to match '[R-link]' followed directly by content within parentheses
pattern = r'\[R-link\]\(([^)]+)\)'
# Search for the pattern in the text
link_match = re.search(pattern, code_content)
code_content_to_open = None
if link_match:
# Extract the content inside the parentheses
code_content_to_open = link_match.group(1).strip()
language_matches = re.finditer(r'```(\w+)(.*?)```', code_content, re.DOTALL)
code_blocks = []
tab_nav = []
languages_processed = []
idx = 0
for language_match in language_matches:
language = language_match.group(1)
code = language_match.group(2).strip()
code = re.sub(r'```', '', code)
code = code.replace('<', '<').replace('>', '>')
is_first_codeblock = idx == 0
active_class = ' active' if is_first_codeblock else ''
highlight_class = 'highlight highlight-active' if is_first_codeblock else 'highlight highlight-inactive'
code_blocks.append(f'<div class="{highlight_class}" data-language="{language}">\n'
f'<pre><code class="language-{language}">\n{code}\n</code></pre>\n'
f'</div>\n')
if language not in languages_processed:
tab_nav.append(f'<li class="nav-item" role="presentation">'
f'<a class="nav-link nav-language{active_class}" aria-selected="true" data-language="{language}">{language}</a>'
f'</li>')
languages_processed.append(language)
idx += 1
code_content = ''.join(code_blocks)
tab_nav_html = f'<ul class="nav nav-tabs mb-3" id="pills-tab" role="tablist">\n' \
f' {" ".join(tab_nav)}\n' \
f'</ul>'
## Only display download button when there is code content for that
download_button_html = f'<a class="downloadCodeBtn" style="margin-right: 20px" href="../{code_content_to_open}"><img src="/static/img/download.svg"></a>' if code_content_to_open else ''
return f'<div class="codeblock">\n' \
f'<div class="d-flex justify-content-between">\n' \
f' {tab_nav_html}\n' \
f' <div class="float-right d-flex">\n' \
f' {download_button_html}\n' \
f' <a class="copyCodeBtn" href="#0"><img src="/static/img/copy-code.svg"></a>\n' \
f' </div>\n' \
f'</div>\n' \
f' <div class="inner">\n' \
f' {code_content}\n' \
f' </div>\n' \
f'</div>'
md_content = codeblock_pattern.sub(replace_codeblock, md_content)
return md_content
# Convert custom tip shortcodes to HTML
# Parameters:
# - md_content: String containing Markdown content with custom tip shortcodes
# Returns:
# - String with tip shortcodes converted to HTML
def convert_tips_to_html(md_content):
tipblock_shortcode_pattern = re.compile(r'{{%\s+tip\s+%}}(.*?){{%\s+/tip\s+%}}', re.DOTALL)
def replace_tip_shortcode(match):
tip_content = match.group(1)
html_content = convert_md_to_html(tip_content)
return f' <div class="admonition tip">' \
f' <div class="font-weight-bold mb-3"><img class="align-bottom mr-2" src="/static/img/tip.svg"> Tip</div>\n' \
f' <div class="admonition-content"><p>{html_content}</p></div>' \
f' </div>\n'
html_output = tipblock_shortcode_pattern.sub(replace_tip_shortcode, md_content)
return html_output
# Convert custom summary shortcodes to HTML
# Parameters:
# - md_content: String containing Markdown content with custom summary shortcodes
# Returns:
# - String with summary shortcodes converted to HTML
def convert_summary_to_html(md_content):
summaryblock_pattern = re.compile(r'{{%\s+summary\s+%}}(.*?){{%\s+/summary\s+%}}', re.DOTALL)
def replace_summary_shortcode(match):
summary_content = match.group(1)
html_content = convert_md_to_html(summary_content)
return f' <div class="admonition summary">' \
f' <div class="font-weight-bold mb-3"><img class="align-bottom mr-2" src="/static/img/summary.svg"> Summary</div>\n' \
f' <div class="admonition-content"><p>{html_content}</p></div>' \
f' </div>\n'
html_output = summaryblock_pattern.sub(replace_summary_shortcode, md_content)
return html_output
# Convert custom warning shortcodes to HTML
# Parameters:
# - md_content: String containing Markdown content with custom warning shortcodes
# Returns:
# - String with warning shortcodes converted to HTML
def convert_warning_to_html(md_content):
warningblock_pattern = re.compile(r'{{%\s+warning\s+%}}(.*?){{%\s+/warning\s+%}}', re.DOTALL)
def replace_warning_shortcode(match):
warning_content = match.group(1)
html_content = convert_md_to_html(warning_content)
return f' <div class="admonition warning">' \
f' <div class="font-weight-bold mb-3"><img class="align-bottom mr-2" src="/img/warning.svg"> Warning</div>\n' \
f' <div class="admonition-content"><p>{html_content}</p></div>' \
f' </div>\n'
html_output = warningblock_pattern.sub(replace_warning_shortcode, md_content)
return html_output
# Convert custom example shortcodes to HTML
# Parameters:
# - md_content: String containing Markdown content with custom example shortcodes
# Returns:
# - String with example shortcodes converted to HTML
def convert_example_shortcode_to_html(md_content):
example_shortcode_pattern = re.compile(r'{{%\s+example\s+%}}(.*?){{%\s+/example\s+%}}', re.DOTALL)
def replace_example_shortcode(match):
example_content = match.group(1)
html_content = convert_md_to_html(example_content)
return f'<div class="admonition example">\n' \
f' <div class="font-weight-bold mb-3"><img class="align-bottom mr-2" src="/img/example.svg" width="20px"> Example</div>\n' \
f' <div class="admonition-content">{html_content}</div>\n' \
f'</div>'
html_output = example_shortcode_pattern.sub(replace_example_shortcode, md_content)
return html_output
# Convert custom YouTube shortcodes to HTML
# Parameters:
# - md_content: String containing Markdown content with custom YouTube shortcodes
# Returns:
# - String with YouTube shortcodes converted to HTML
def convert_youtube_shortcode_to_html(md_content):
youtube_shortcode_pattern = re.compile(r'{{<\s*youtube\s+(.*?)\s+iframe-video-margins\s*>}}')
def replace_youtube_shortcode(match):
youtube_content = match.group(1)
return f'<div class="iframe-video-margins">\n' \
f' <iframe src="https://www.youtube.com/embed/{youtube_content}" ' \
f'allowfullscreen="" title="YouTube Video"></iframe>\n' \
f'</div>\n'
html_output = youtube_shortcode_pattern.sub(replace_youtube_shortcode, md_content)
return html_output
# Convert custom primary call-to-action shortcodes to HTML
# Parameters:
# - md_content: String containing Markdown content with custom CTA primary shortcodes
# Returns:
# - String with CTA primary shortcodes converted to HTML
def convert_cta_center_shortcode_to_html(md_content):
cta_shortcode_pattern = re.compile(r'{{%\s+cta-primary-center\s+"([^"]+)"\s+"([^"]+)"\s+%}}')
def replace_cta_shortcode(match):
button_text = match.group(1).strip()
button_url = match.group(2).strip()
return f'<div class="cta-primary" style="text-align:center;">' \
f' <a href="{button_url}">' \
f' <button class="btn btn-primary btn-md mt-2">{button_text}</button>' \
f' </a>' \
f'</div><br>'
html_output = cta_shortcode_pattern.sub(replace_cta_shortcode, md_content)
return html_output
# Convert custom secondary call-to-action shortcodes to HTML
# Parameters:
# - md_content: String containing Markdown content with custom CTA secondary shortcodes
# Returns:
# - String with CTA secondary shortcodes converted to HTML
def convert_cta_secondary_center_shortcode_to_html(md_content):
cta_shortcode_pattern = re.compile(r'{{%\s+cta-secondary-center\s+"([^"]+)"\s+"([^"]+)"\s+%}}')
def replace_cta_secondary_shortcode(match):
button_text = match.group(1).strip()
button_url = match.group(2).strip()
return f'<div class="cta-secondary" style="text-align:center;">' \
f' <a href="{button_url}">' \
f' <button class="btn btn-secondary btn-md mt-2">{button_text}</button>' \
f' </a>' \
f'</div><br>'
html_output = cta_shortcode_pattern.sub(replace_cta_secondary_shortcode, md_content)
return html_output
# Convert custom primary call-to-action shortcodes to HTML
# Parameters:
# - md_content: String containing Markdown content with custom CTA primary shortcodes
# Returns:
# - String with CTA primary shortcodes converted to HTML
def convert_cta_shortcode_to_html(md_content):
cta_shortcode_pattern = re.compile(r'{{%\s+cta-primary\s+"([^"]+)"\s+"([^"]+)"\s+%}}')
def replace_cta_shortcode(match):
button_text = match.group(1).strip()
button_url = match.group(2).strip()
return f'<div class="cta-primary">' \
f' <a href="{button_url}">' \
f' <button class="btn btn-primary btn-md mt-2">{button_text}</button>' \
f' </a>' \
f'</div><br>'
html_output = cta_shortcode_pattern.sub(replace_cta_shortcode, md_content)
return html_output
# Convert custom KaTeX shortcodes to HTML
# Parameters:
# - input_text: String containing text with custom KaTeX shortcodes
# Returns:
# - String with KaTeX shortcodes converted to HTML
def convert_katex_shortcode_to_html(input_text):
katex_shortcode_pattern = re.compile(r'{{<katex>}}(.*?){{<\/katex>}}', re.DOTALL)
def replace_katex_shortcode(match):
katex_content = match.group(1).replace('"','').strip()
return f'<span class="katex">{katex_content}</span>'
html_output = katex_shortcode_pattern.sub(replace_katex_shortcode, input_text)
return html_output
# Convert fallback code blocks to HTML
# Parameters:
# - md_content: String containing Markdown content with fallback code blocks
# Returns:
# - String with fallback code blocks converted to HTML
def convert_fallback_block_to_html(md_content):
code_block_pattern = re.compile(r'```(.*?)```', re.DOTALL)
def replace_code_blocks(match):
code_content = match.group(1).strip()
return f'<div class="highlight"><pre class="chroma"><code class="language-fallback" data-lang="fallback">{code_content}</code></pre></div>'
html_output = code_block_pattern.sub(replace_code_blocks, md_content)
return html_output
# Convert Markdown tables to HTML
# Parameters:
# - table_md: String containing Markdown table content
# Returns:
# - String with table content converted to HTML
def convert_md_table_to_html(table_md):
rows = table_md.strip().split('\n')
html_table = '<table>\n'
headers = rows[0].split('|')[1:-1]
html_table += '<tr>' + ''.join(f'<th>{header.strip()}</th>' for header in headers) + '</tr>\n'
for row in rows[2:]:
columns = row.split('|')[1:-1]
html_table += '<tr>' + ''.join(f'<td>{column.strip()}</td>' for column in columns) + '</tr>\n'
html_table += '</table>'
return html_table
# Convert custom table shortcodes to HTML
# Parameters:
# - md_content: String containing Markdown content with custom table shortcodes
# Returns:
# - String with table shortcodes converted to HTML
def convert_tables_to_html(md_content):
table_pattern = re.compile(r'{{%\s*table\s*%}}(.*?){{%\s*/table\s*%}}', re.DOTALL)
html_content = re.sub(table_pattern, lambda match: convert_md_table_to_html(match.group(1)), md_content)
return html_content
# Convert Markdown titles to HTML with IDs
# Parameters:
# - md_content: String containing Markdown content with titles
# Returns:
# - String with titles converted to HTML with IDs
def convert_md_titles_to_html(md_content):
def replace_title_h1(match):
title = match.group(1)
title_id = title.lower().replace(' ', '-')
return f'<h1 id="{title_id}">{title}</h1>'
def replace_title_h2(match):
title = match.group(1)
title_id = title.lower().replace(' ', '-')
return f'<h2 id="{title_id}">{title}</h2>'
def replace_title_h3(match):
title = match.group(1)
title_id = title.lower().replace(' ', '-')
return f'<h3 id="{title_id}">{title}</h3>'
title_pattern_h1 = re.compile(r'<h1>(.*?)<\/h1>', re.DOTALL)
title_pattern_h2 = re.compile(r'<h2>(.*?)<\/h2>', re.DOTALL)
title_pattern_h3 = re.compile(r'<h3>(.*?)<\/h3>', re.DOTALL)
md_content = title_pattern_h3.sub(replace_title_h3, md_content)
md_content = title_pattern_h2.sub(replace_title_h2, md_content)
md_content = title_pattern_h1.sub(replace_title_h1, md_content)
return md_content
# Replace image source paths in HTML content
# Parameters:
# - md_content: String containing HTML content with image tags
# Returns:
# - String with updated image source paths
def replace_img_src(md_content):
soup = BeautifulSoup(md_content, 'html.parser')
img_tags = soup.find_all('img')
# Define valid image extensions
image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.mov']
for img_tag in img_tags:
src = img_tag.get('src')
if src:
# Get the filename and extension
filename, ext = os.path.splitext(src.split('/')[-1])
# Check if the extension is one of the defined extensions
if ext.lower() in image_extensions:
# Replace the extension with .webp
new_filename = filename + '.webp'
new_src = '/static/img/' + new_filename
img_tag['src'] = new_src
# Convert the modified soup back to a string
md_content_with_new_img_src = str(soup)
return md_content_with_new_img_src
# Replace video source paths in HTML content
# Parameters:
# - html_content: String containing HTML content with video tags
# Returns:
# - String with updated video source paths
def replace_video_src(html_content):
soup = BeautifulSoup(html_content, 'html.parser')
video_tags = soup.find_all('video')
for video_tag in video_tags:
sources = video_tag.find_all('source')
for source_tag in sources:
src = source_tag.get('src')
if src:
filename = src.split('/')[-1]
new_src = '/static/img/' + filename
source_tag['src'] = new_src
html_content_with_new_video_src = str(soup)
return html_content_with_new_video_src
# Replace Markdown links with HTML links
# Parameters:
# - md_content: String containing Markdown content with links
# Returns:
# - String with links converted to HTML
def replace_links(md_content):
root_url = request.host_url.rstrip('/')
file_extensions = ['.xlsx', '.sh', '.zip', '.bib', '.tex', '.docx', '.bat', '.csv', '.txt', '.rda', '.rdata', '.pptx', '.rmd', '.py', '.r', '.history', '.pdf', '.dta']
link_pattern = re.compile(r'\[([^\]]+)\]\(([^)]+)\)')
def replace_link(match):
link_text = match.group(1)
link_url = match.group(2)
if link_url.startswith('files/'):
link_url = f'{root_url}/static/{link_url}'
new_link = f'<a href="{link_url}" alt="{link_text}">{link_text}</a>'
return new_link
md_content_with_new_links = re.sub(link_pattern, replace_link, md_content)
return md_content_with_new_links
# Remove empty <pre><code></code></pre> tags from HTML content
# Parameters:
# - html_content: String containing HTML content
# Returns:
# - String with empty <pre><code></code></pre> tags removed
def remove_empty_pre_code_tags(html_content):
empty_pre_code_pattern = re.compile(r'<pre><code>\s*</code></pre>')
cleaned_content = re.sub(empty_pre_code_pattern, '', html_content)
return cleaned_content
# Parameters:
# - r_content: String R code content
# Returns:
# - String with R code enclosed in <pre><code></code></pre> tags
def r_to_html_plaintext(r_content):
# Escape HTML special characters to display as plain text
escaped_content = html.escape(r_content)
# Wrap the content in <pre> and <code> tags for HTML
html_content = f"<pre><code>{escaped_content}</code></pre>"
return html_content
# Main function to convert Markdown file content to HTML
# Parameters:
# - md_file_content: String containing Markdown file content
# Returns:
# - String with all shortcodes and Markdown converted to HTML
def htmlize(md_file_content):
transformations = [
convert_code_blocks_to_html,
convert_tips_to_html,
convert_summary_to_html,
convert_warning_to_html,
convert_cta_shortcode_to_html,
convert_cta_center_shortcode_to_html,
convert_cta_secondary_center_shortcode_to_html,
convert_example_shortcode_to_html,
convert_youtube_shortcode_to_html,
convert_fallback_block_to_html,
replace_links,
convert_md_to_html,
convert_tables_to_html,
convert_katex_shortcode_to_html,
convert_md_titles_to_html,
replace_img_src,
replace_video_src,
remove_empty_pre_code_tags
]
html_content = md_file_content
for transform_func in transformations:
html_content = transform_func(html_content)
return html_content