forked from BVShashankKhatri/file-conversion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_to_md_folder.py
More file actions
63 lines (45 loc) · 1.66 KB
/
html_to_md_folder.py
File metadata and controls
63 lines (45 loc) · 1.66 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
"""Install markdownify by running pip3 install markdownify"""
import markdownify
import os
import logging
from functools import wraps
logger = logging.getLogger(__name__)
def error_handler(func):
"""A decorator that handles errors in the decorated function."""
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
logger.error("Error in function: %s", func.__name__, e)
return wrapper
@error_handler
def convert_gitrepo_html_to_md(html_file_path, md_file_path):
"""Converts an HTML file to an MD file.
Args:
html_file_path: The path to the HTML file.
md_file_path: The path to the MD file.
"""
with open(html_file_path, "r") as html_file:
html_text = html_file.read()
markdown_text = markdownify.markdownify(html_text)
with open(md_file_path, "w") as md_file:
md_file.write(markdown_text)
print(md_file_path+' is successfully created.')
@error_handler
def convert_gitrepo_html_files(folder_path):
"""Converts all of the HTML files in the specified folder and all of its subfolders to MD files.
Args:
folder_path: The path to the folder containing the HTML files.
"""
for root, dirs, files in os.walk(folder_path):
for filename in files:
if filename.endswith(".html"):
html_file_path = os.path.join(root, filename)
md_file_path = os.path.join(root, filename[:-5]) + ".md"
convert_gitrepo_html_to_md(html_file_path, md_file_path)
for subdir in dirs:
convert_gitrepo_html_files(os.path.join(root, subdir))
if __name__ == "__main__":
folder_path = "path of the folder of html files."
convert_gitrepo_html_files(folder_path)