-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path00_markdown_simple_example.py
More file actions
71 lines (50 loc) · 2.29 KB
/
00_markdown_simple_example.py
File metadata and controls
71 lines (50 loc) · 2.29 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
import time
from bs4 import BeautifulSoup
from markdown_content import markdown_content
import markdown
def convert_and_save_markdown_with_toc_markdown_lib(markdown_content, file_name):
"""
Convert Markdown to HTML with TOC using markdown library, format the HTML, and save to a file.
"""
print(f"Markdown library:")
# 1. Simple convert without extra (Do not output)
html_content = markdown.markdown(markdown_content)
# 2. Conversion with TOC extension
html_content = markdown.markdown(markdown_content, extensions=['toc'])
# Use BeautifulSoup to format HTML code
soup = BeautifulSoup(html_content, 'html.parser')
formatted_html = soup.prettify()
print(f"{formatted_html}")
# Save HTML result to file
with open(file_name, 'w', encoding='utf-8') as file:
file.write(html_content)
print(f"HTML content with TOC successfully saved to {file_name} using markdown library.")
import markdown2
def convert_and_save_markdown_with_toc_markdown2_lib(markdown_content, file_name):
"""
Convert Markdown to HTML with TOC using markdown2 library, format the HTML, and save to a file.
"""
print(f"Markdown2 library:")
# 1. Simple convert without extra (Do not output)
html_content = markdown2.markdown(markdown_content)
# 2. Conversion with TOC extra
html_content = markdown2.markdown(markdown_content, extras=["toc"])
# Use BeautifulSoup to format HTML code
soup = BeautifulSoup(html_content, 'html.parser')
formatted_html = soup.prettify()
print(f"{formatted_html}")
# Save HTML result to file
with open(file_name, 'w', encoding='utf-8') as file:
file.write(html_content)
print(f"HTML content with TOC successfully saved to {file_name} using markdown2 library.")
# Display the differences between the two libraries
print("""Comparing "markdown" library and "markdown2" library:\n""")
# Display the differences between the two libraries
print("="*40)
print("Using markdown library:")
print("="*40)
convert_and_save_markdown_with_toc_markdown_lib(markdown_content, 'converted_html/markdown_example_with_toc.html')
print("\n" + "="*40)
print("Using markdown2 library:")
print("="*40)
convert_and_save_markdown_with_toc_markdown2_lib(markdown_content, 'converted_html/markdown2_example_with_toc.html')