-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertTXT_MD_ANKI.py
106 lines (100 loc) · 4.25 KB
/
ConvertTXT_MD_ANKI.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
# lines with 11 # or TOC in text are ignored
# overview / TOC is build with beginning #>
# headers are build with 6 # (must be identical to overview entries)
# singles lines are build with (left is code-content - right is description) - splitted with => or *>
# codeblock is build with "=> " as header and then the code between "" and ""
# textblock used with "=>" at every beginning line (makes bullet points)
import csv
files = ["GIT","JAVASCRIPT","HTML","CSS","PYTHON","SQL"]
for fn in files:
# read initial txt file
with open(fn+".txt","r") as f:
lines = [x.rstrip() for x in f.readlines()]
# convert file
myNewList = []
# myNewList.append ("---")
# jumpToTop = "[jump to top...](#" + fn.lower() +")<br><br>"
codeBlock = False
codeBlockCont = []
csvOutputAnki = []
csvDesc = ""
csvElem = ""
for idx,line in enumerate(lines):
if any(x in line for x in ["###########","### TOC","\n","///////////", "/// TOC"]):
pass
elif codeBlock == True and '""' not in line:
codeBlockCont.append(line.rstrip())
elif "#>" in line:
continue
# val = line.split("#> ",1)[1].strip()
# anchorLink = val.replace (", ", "-").replace (",", "-").replace (" ", "-").lower().strip()
# anchorLink = " [jump to...](#" + anchorLink + ")"
# myNewList.append("#### " + val + anchorLink)
elif "######" in line:
val = line.split("###### ",1)[1].strip()
myNewList.append("---")
myNewList.append("## " + val)
# myNewList.append(jumpToTop)
elif "*> " in line:
code = line.split("*>",1)[0].strip()
desc = line.split("*>",1)[1].strip()
myNewList.append (desc)
myNewList.append ("```markdown")
myNewList.append (code)
myNewList.append ("```")
elif "=> " in line:
code = line.split("=>",1)[0].strip()
desc = line.split("=>",1)[1].strip()
if desc != "" and code != "":
csvOutputAnki.append([desc,code,fn.lower()])
if idx > 0 and "=> " in lines[idx-1]:
myNewList.append(desc)
myNewList.append("```markdown")
myNewList.append(code)
myNewList.append("```")
else:
myNewList.append("<br>" + desc)
myNewList.append("```markdown")
myNewList.append(code)
myNewList.append("```")
elif desc != "" and code == "":
if (idx < len(lines) -1) and ("=>" in lines[idx+1]) and (lines[idx+1].split("=>",1)[0].strip() != ""):
myNewList.append (line.split ("=>", 1)[1].strip () + "<br>")
elif "######" in lines[idx-1]:
myNewList.append (line.split ("=>", 1)[1].strip ())
else:
myNewList.append ("<br>" + line.split("=>",1)[1].strip())
elif '""' in line and codeBlock == False:
codeBlock = True
for i in range(1,10,1):
if "=> " in lines[idx-i]:
csvDesc = lines[idx-i].replace("=> ","").replace("<br>","").strip()
break
elif '""' in line and codeBlock == True:
myNewList.append ("```markdown")
for elem in codeBlockCont:
myNewList.append(elem)
csvElem = csvElem + elem + "\n"
myNewList.append("```")
codeBlock = False
codeBlockCont = []
csvOutputAnki.append ([csvDesc, csvElem.strip(), fn.lower()])
csvElem = ""
else:
myNewList.append(line.strip())
# write final md file
with open(fn.lower() + '.md', 'w') as f:
for item in myNewList:
f.write("%s\n" % item)
print(f"File {fn.upper() + '.txt'} converted to {fn.lower() + '.md'}...")
# writing csv file for anki
while True:
try:
with open(fn.lower() + ".csv","w",newline="") as fp: # Open csv-file in writemode
a = csv.writer (fp, delimiter=";") # Define csv-writer with ","-delimiter
a.writerows (csvOutputAnki) # Writing individual rows
print(f"Anki-Cards inputfile wrote to {fn.lower() + '.csv'}...")
break
except Exception as e:
print ("Error: ", e)
input (f"File Open not possible - pls close and press <Enter> for {fn.lower() + '.csv'}..")