1+ #!/usr/bin/env python3
2+ import os
3+ import sys
4+ import re
5+
6+
7+ def convert_to_markdown (text ):
8+ """Convert plain text to markdown format."""
9+ lines = text .split ('\n ' )
10+ markdown_lines = []
11+
12+ for line in lines :
13+ line = line .strip ()
14+ if not line :
15+ markdown_lines .append ('' )
16+ continue
17+
18+ # Convert headers (lines ending with :)
19+ if line .endswith (':' ):
20+ markdown_lines .append (f"## { line [:- 1 ]} " )
21+ # Convert bullet points (lines starting with -, *, or •)
22+ elif line .startswith (('-' , '*' , '•' )):
23+ content = line [1 :].strip ()
24+ markdown_lines .append (f"- { content } " )
25+ # Convert numbered lists
26+ elif re .match (r'^\d+\.?\s+' , line ):
27+ content = re .sub (r'^\d+\.?\s+' , '' , line )
28+ markdown_lines .append (f"1. { content } " )
29+ # Regular text
30+ else :
31+ markdown_lines .append (line )
32+
33+ return '\n ' .join (markdown_lines )
34+
35+
36+ def process_file (input_file , output_file = None ):
37+ """Process input file and convert to markdown."""
38+ try :
39+ with open (input_file , 'r' , encoding = 'utf-8' ) as f :
40+ content = f .read ()
41+
42+ markdown_content = convert_to_markdown (content )
43+
44+ if not output_file :
45+ output_file = os .path .splitext (input_file )[0 ] + '.md'
46+
47+ with open (output_file , 'w' , encoding = 'utf-8' ) as f :
48+ f .write (markdown_content )
49+
50+ print (f"Converted: { input_file } -> { output_file } " )
51+ return True
52+
53+ except FileNotFoundError :
54+ print (f"Error: File not found: { input_file } " )
55+ return False
56+ except Exception as e :
57+ print (f"Error: { e } " )
58+ return False
59+
60+
61+ def create_sample_file ():
62+ """Create a sample text file for demonstration."""
63+ sample_content = """Introduction:
64+ This is a sample document.
65+
66+ Features:
67+ - Easy to use
68+ - Fast conversion
69+ - Supports headers
70+ * Multiple bullet styles
71+ • Unicode bullets too
72+
73+ Steps to follow:
74+ 1. Open the file
75+ 2. Run the converter
76+ 3. Check the output
77+
78+ Conclusion:
79+ The conversion is complete."""
80+
81+ with open ('sample.txt' , 'w' , encoding = 'utf-8' ) as f :
82+ f .write (sample_content )
83+ print ("Created sample.txt" )
84+
85+
86+ def main ():
87+ """Main function."""
88+ print ("Simple Markdown Converter" )
89+ print ("=" * 30 )
90+
91+ if len (sys .argv ) > 1 :
92+ input_file = sys .argv [1 ]
93+ output_file = sys .argv [2 ] if len (sys .argv ) > 2 else None
94+ else :
95+ input_file = input ("Enter input file path (or press Enter for sample): " ).strip ()
96+
97+ if not input_file :
98+ create_sample_file ()
99+ input_file = 'sample.txt'
100+
101+ output_file = input ("Enter output file path (optional): " ).strip () or None
102+
103+ if not os .path .exists (input_file ):
104+ print (f"File not found: { input_file } " )
105+ return
106+
107+ if process_file (input_file , output_file ):
108+ print ("Conversion completed successfully!" )
109+
110+
111+ if __name__ == "__main__" :
112+ main ()
0 commit comments