-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarkdown-html-converter.py
63 lines (48 loc) · 1.85 KB
/
markdown-html-converter.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
import os
import glob
import argparse
import markdown_it
from bs4 import BeautifulSoup
def main(args):
"""
Function to convert all Markdown files in the specified directory to HTML files.
"""
# Turn the specified path into a system path
system_path = os.path.abspath(args.path)
# delete all files in the specified directory that end with .html
for f in glob.glob(f"{system_path}/*.html"):
os.remove(f)
# Get a list of all Markdown files in the specified directory
markdown_files = glob.glob(f"{system_path}/*.md")
# Loop for each Markdown file
for path in markdown_files:
# Ignore the README.md file
if "README" in path:
continue
# Read the Markdown file
with open(path, "r") as file:
markdown_text = file.read()
# Initialize the parser
translator = markdown_it.MarkdownIt()
# Parse the Markdown text
parsed_html = translator.render(markdown_text)
# Initialize the HTML parser
soup = BeautifulSoup(parsed_html, 'html.parser')
# Find all anchor elements and add the target="_blank" attribute
for a in soup.find_all('a'):
a['target'] = '_blank'
# Rename the file to -modified.md
path_list = path.split(".")
path_list[-1] = "html"
new_path = ".".join(path_list).replace(".-","-")
# Save the modified Markdown file
with open(new_path, 'w') as file:
file.write(str(soup))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Convert Markdown files to HTML files.')
parser.add_argument('-p', '--path', type=str, help='Path to the directory containing the Markdown files.')
args = parser.parse_args()
main(args)
print("Done!")
# How to use:
# python markdown-html-converter.py -p ./EPS/reliability