-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_md.py
229 lines (177 loc) · 6.77 KB
/
write_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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
'''
DOCSTRING
'''
import time
import os
import sys
from IO.template_reader import *
from IO.io_plan import *
from configs.config import *
# config
def add_link(link: str): # TODO move to CORE
"""
function add link to string of links in markdown file. If first character
is # then leave it for further processing
param1::: link - string represention of Zettelkasten link
return::: prepared link string [[ ]]
WARNING If link is tag like #python will be automatically put into
global variable TAGS.
"""
global TAGS
if link[0] == "#": # support for TAGS
TAGS += link + " "
return ""
else:
return "[[" + link + "]]"
def prepare_multiple_links(links: list): # TODO Move to CORE
"""
function takes list of links and prepares string to write into markdown file
param1::: links - list of links
return::: result which contains string with all links prepared for Markdown template
"""
result = str()
for i in links:
if i[-1] == "\n":
i = i[:-1]
result += " " + add_link(i)
print(result)
return result
def read_template(filename: str):
"""
Function reads template and stores ROWS into Dictionary. Then
Automatically finds predefined <tags> and put them separately.
param1::: filename of template file in templates folder # TODO Make It configurable
return::: Dictionary containing all rows of template
"""
result = dict()
# TODO Check if template exists
file = os.getcwd()+"/templates/"+filename
print(file)
obj = TemplateReader(filename=file)
obj.read_template()
print("ROWS:,",obj.rows)
print("TAGS:",obj.tags) # prints out founded tags (links, author, tags)
print("LENGTH OF TEMPLATE:",len(obj.rows))
obj.purge_last30blankrows() # get rid of empty space
result = obj.seek_tags_and_links()
print("FOUNDED TAGS:", result)
return result
def prepare_data(name: str, links: str, tags: str): # DEFAULT TEMPLATE
"""
function prepares data to default template of markdown file.
param1::: name - string represents name of Markdown Note
param2::: link - string represents link to another note
param3::: tags: Is overwritten by global variable TAGS
return::: prepared string
"""
# TODO In future there will data from plan should be parsed and put into specified template from templates directory.
global TAGS
tags = TAGS
result = "# "
result += name + "\n\n"
# TODO check if not tag example #topic
result += "## Links "+ links
result += "\n## Tags " + tags
result += "\n\nCreated by SCRIPT"
return result
def prepare_data_custom(name: str, links: str, tags: str):
"""
function prepares data for CUSTOM template saved in templates folder
param1::: name - string represents name of Markdown Note
param2::: link - string represents link to another note
param3::: tags: Is overwritten by global variable TAGS
return::: prepared string
"""
global TAGS, AUTHOR
tags = TAGS
result = str()
loaded_template=read_template(filename="template")
print(loaded_template)
for i in loaded_template:
# print("ROW CONTENT :", i, loaded_template[i]) # control print
if "<Name>" in loaded_template[i]:
result += "# "+ name + "\n"
continue
if "<Tags>" in loaded_template[i]:
result += "## TAGS :"+ tags + "\n"
continue
if "Links" in loaded_template[i]:
result += "## LINKS : "+ links + "\n"
continue
if "<Author>" in loaded_template[i]:
result += "### Was Created By : "+ AUTHOR + "\n"
continue
if "<Date>" in loaded_template[i]:
date = time.strftime("%d.%m.%Y %H:%M")
result += "### Date : "+ date + "\n"
continue
else:
result += loaded_template[i]
return result
def write_md(filename, data): # TODO Move to IO
"""
Simple function takes filename and prepared data and saves them to Markdown file.
param1::: filename - string with path and filename
param2::: data - String prepared
return::: Boolean status if data was saved
"""
with open(file=filename, mode="w", encoding="utf8") as f:
f.write(data)
print("SAVED")
return True
def prepare_data_2_save(plan, index): # TODO Move to CORE
"""
Function iterates thru plan and prepare values to write to files.
First value in plan is NAME of markdown file, other value is LINK
"""
global TAGS, SEPARATOR # get global variables from config
for row in plan: # loop thru plan by rows
splitted_row = row.split(SEPARATOR) # split row to list
print("ROW :",splitted_row) # control print
links = splitted_row[1:] # get rid of symbol from last link
if len(splitted_row) > 1: # if more then 1
data = prepare_data_custom(splitted_row[0], prepare_multiple_links(links), TAGS)
filename = splitted_row[0] + '.md'
print(filename) # control print created filename
# check if file already exists
if filename_exists(filename, index_of_files) is True:
print("FILE ALREADY EXISTS")
elif filename_exists(filename,index_of_files) is False:
write_md(filename=filename, data=data) # if file is new write him down
print("SAVED", filename) # control print
return True
if __name__ == "__main__":
"""
M A I N P R O G R A M M - U N D E R D E V E L O P M E N T
"""
print(SEPARATOR, TAGS, END_SYMBOL, AUTHOR) # Control print
# check if file was passed as argument via CLI
if len(sys.argv) == 1:
print("NO FILE ARGUMENT")
else:
filename = sys.argv[1]
# READ FILE CONTAINING PLAN
try:
plan = read_plan(filename=PLANFILE) # use function read plan from IO.io_plan
except FileNotFoundError:
plan = []
print("PLAN FILE DOESN'T EXIST")
# TODO Make a New Plan File
finally:
# TODO Log if plan file was successfully loaded
pass
# get index of existing Markdown files
index_of_files = get_index() # use function get_index from IO.io_plan
print(index_of_files) # control print all collected Markdown files
#check if doesnt contain duplicity
a = duplicity_check_plan(plan) # TODO Does'nt work
print(a)
# save prepared data to files
prepare_data_2_save(plan, index_of_files) # reads row from plan and generates Markdown file
a = prepare_multiple_links(["hallo", "hallo"])
print(a)
print(sys.argv) # control print all arguments passed via CLI
"""
a = prepare_data_custom(name="# Andrea", links= "[[Hello]]", tags="#python")
print(a)
"""