File size: 920 Bytes
6b8b1ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
import markdown
from pathlib import Path
import sys

def convert_md_to_html(filepath):
   input_path = Path(filepath)
   output_path = input_path.with_suffix('.html')
   
   try:
       with open(input_path, 'r', encoding='utf-8') as md_file:
           text = md_file.read()
           html = markdown.markdown(text)
       
       with open(output_path, 'w', encoding='utf-8', errors='xmlcharrefreplace') as html_file:
           html_file.write(html)
           
       print(f"Converted {input_path} -> {output_path}")
           
   except FileNotFoundError:
       print(f"Error: Could not find file {input_path}")
       sys.exit(1)
   except Exception as e:
       print(f"Error converting file: {e}")
       sys.exit(1)

if __name__ == '__main__':
   if len(sys.argv) != 2:
       print("Usage: python convert.py FILEPATH.md")
       sys.exit(1)
       
   convert_md_to_html(sys.argv[1])