-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code-to-md.py
executable file
·177 lines (151 loc) · 6.72 KB
/
code-to-md.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/python3
# os for paths, sys for command line args
import os, sys, argparse
''' Read files and generate md file in the format
'1. <first comment in file> as title
The rest as code block'
and do for all code files in that directory
'''
def return_path(arg, default, prompt):
if arg and os.path.exists(arg):
return arg
else:
path=input(f'''{prompt} {default}
Enter here > ''')
if not os.path.exists(path):
print(f'''Input skipped or entered path is invalid!
Choosing {default} as the path...''')
return default
else: return path
def generate_md(d=False):
default = output_path = os.getcwd()
default_filename=output_filename="programs.md"
ext= default_extension=".py"
input_path=os.path.join(default, "programs")
if not os.path.exists(input_path):
print(f'''{input_path} does not exist!
Please make sure you have an input directory''')
exit()
if not d:
input_prompt=('''Enter directory path where files exist
Skipping would choose ''')
input_path=return_path(args.input_dir, input_path, input_prompt) # hope this works :D
ext=args.extension
if not ext or ext not in ['.py', '.c', '.cpp']:
ext=input(f'''Enter file extension to be considered (.c .cpp or .py)
Default is {default_extension}
Enter here > ''')
if ext=="":
print(f'''Input skipped!
Choosing {default_extension} as the extension...''')
ext=default_extension
output_prompt=('''Enter folder path to save md file
Skip to choose the default path as ''')
output_path=return_path(args.output, default, output_prompt)
if args.filename:
output_filename=args.filename
else:
output_filename=input(f'''Enter filename to be saved (e.g. programs.md)
Skip to choose the default filename as {default_filename}
Enter here > ''')
if output_filename=="":
print(f'''Input skipped!
Choosing {default_filename} as the filename...''')
output_filename=default_filename
files=[]
for i in sorted(os.listdir(input_path)):
if i.endswith(ext):
files.append(i)
#create md file
md_file=open(os.path.join(output_path,output_filename), "w"); sno=1
for i in files:
path=os.path.join(input_path, i)
file=open(path, 'r')
data= file.readlines();j=0; flag=False; first=True;
try:
if ext=='.py':
while True:
title=f"### {data[j]}"
if data[0][0]=='#' and j==0:
line_1=f"### {sno}. {data[j][1:]}"
md_file.write(line_1)
sno+=1
elif data[0][0:3] in ['"""', "'''" ] and j==0:
################################### Check for ending quotes here too, also in C's case
line_1=f"### {sno}. {data[j][3:]}"
md_file.write(line_1)
flag=True
sno+=1
if data[j][-4:-1] in ['"""', "'''" ]:
flag=False
# if there is no comment in the first line, use the file name as the title
elif j==0:
md_file.write(f"### {sno}. {i}\n")
sno+=1
elif flag==True:
if data[j][-4:-1] in ['"""', "'''" ]:
md_file.write(title[0:-4]+"\n")
flag=False
else:
md_file.write(title)
else:
if (first!=True) and (j!=(len(data)-1)): md_file.write(f"{data[j]}")
elif first==True:
md_file.write(f"```python\n{data[j]}")
first=False
else:
md_file.write(f"{data[j]}\n```")
j+=1
elif ext=='.c' or ext=='.cpp':
while True:
title=f"### {data[j]}"; line_1=f"### {sno}. {data[j][2:]}"
if data[0][0:2]=='//' and j==0:
md_file.write(line_1); sno+=1
elif data[0][0:2]=="/*" and j==0:
md_file.write(line_1)
flag=True
sno+=1
if data[j][-3:-1]=="*/":
flag=False
# if there is no comment in the first line, use the file name as the title
elif j==0:
md_file.write(f"### {sno}. {i}\n")
sno+=1
elif flag==True:
if data[j][-3:-1]=="*/":
md_file.write(title[0:-3]+"\n")
flag=False
else:
md_file.write(title)
else:
if (first!=True) and (j!=(len(data)-1)): md_file.write(f"{data[j]}")
elif first==True:
md_file.write(f"```c\n{data[j]}")
first=False
else:
md_file.write(f"{data[j]}\n```")
j+=1
except:
md_file.write("\n\n")
continue
return f"{output_filename} saved successfully at {output_path} !"
print("<<< code-to-md by realkarthiknair >>>")
parser=argparse.ArgumentParser(description="Create a single md file from a directory full of code files!")
parser.add_argument("-idir","--input_dir", help="Directory path where the code files exist")
parser.add_argument("-e","--extension", help="File extension to be considered (.c .cpp or .py)")
parser.add_argument("-odir","--output", help="Directory path to save the md file")
parser.add_argument("-o","--filename", help="Filename to be saved (e.g. programs.md)")
parser.add_argument("-d","--default", help="Use default values", action="store_true")
parser.add_argument("-v","--version", help="Show version", action="store_true")
args=parser.parse_args()
if args.version:
print("code-to-md v1.1.2")
exit()
if args.default:
if len({i:j for i,j in vars(args).items() if j==True})>1:
print('''The program doesn't accept any other arguments with --default
Any other options will be ignored''')
stats=generate_md(d=True)
else:
stats=generate_md()
print(stats)