-
Notifications
You must be signed in to change notification settings - Fork 1
/
PKM.py
executable file
·264 lines (233 loc) · 8.28 KB
/
PKM.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ------------------------------------------------
# Author: Konfido <[email protected]>
# Created Date: August 6th 2020
# ------------------------------------------------
import re
import os
import Config
from Items import Display, Items
from Search import File as F
from Search import Search as S
from Utils import Utils as U
C = Config.Config().configs
def varibles_checked():
all_set = True
# Check validity of Workflow env variables
for env in ["markdown_app", "files_path", "notes_path"]:
if not U.get_env(env):
Display.show(("ERROR: Find empty environt varibles!",
f"Please check: \"{env}\"."))
all_set = False
for path in U.get_env("files_path").split(","):
if not(U.path_exists(U.get_abspath(path))):
Display.show(("ERROR: Find invalid directory!",
f"Please check \"files_path\": {path}"))
all_set = False
if not U.get_env("notes_path"):
Display.show(("ERROR: Find invalid directory!",
"Please check \"notes_path\""))
all_set = False
# Check validity of config file
if not U.path_exists(Config.CONFIG_DIR):
U.mkdir(Config.CONFIG_DIR)
if not U.path_exists(Config.CONFIG_PATH):
Config.Config.reset_all()
else:
try:
Config.Config._load_all()
except Exception as e:
U.output(f'error|[search, {e}]')
#TODO: reset all config/go check?
all_set = False
return all_set
def get_parsed_arg():
"""Parse the input query into two groups of args
Returns:
mode: str, "And_Search"|"Or_Search"|"Title_Search"|"Exact_Search"
args_1: list
args_2: list
"""
# no string
if not query.strip():
mode, args_1, args_2 = "Recent", [], []
else:
commas = re.findall('[,,]', query)
if not commas:
a1string, a2string = query, ""
elif commas.__len__() == 1:
a1string, a2string = re.match(r'(.*)[,,](.*)', query).groups()
# include more that 2 commas
elif commas.__len__() >= 2:
Display.show(("Error!", "Having 2 (>=) commas is not allowed!"))
exit()
else:
Display.show("Error!","get_parsed_arg()")
exit()
# parse keywords
if '&' in a1string:
args_1 = [k for k in a1string.split("&") if k != ""]
mode = "And_Search"
elif '|' in a1string:
args_1 = [k for k in a1string.split("|") if k != ""]
mode = "Or_Search"
elif " " not in a1string and not commas: # 1 word with no space tail
args_1 = [a1string]
mode = "Title_Search"
else:
args_1 = [a1string.strip()]
mode = "Exact_Search"
# parse tags
a2string = a2string.translate(str.maketrans("&||", " "))
args_2 = [t for t in a2string.split(" ") if t != ""]
return mode, args_1, args_2
def show_notes():
if not varibles_checked():
return 0
# Get all sorted notes
if C['search_all_folders']:
sorted_note_list = S.get_sorted_files(Config.FILES_PATH)
else:
sorted_note_list = S.get_sorted_files(Config.NOTES_PATH)
# Parse input
mode, keywords, tags = get_parsed_arg()
if mode == "Recent":
result = sorted_note_list
elif mode == "Title_Search":
result = S.title_search(keywords, sorted_note_list)
elif mode == "And_Search" or mode == "Exact_Search":
result = S.and_search(keywords, sorted_note_list)
elif mode == "Or_Search":
result = S.or_search(keywords, sorted_note_list)
# Parse tags if needed
if tags:
result = S.metric_search("tag", tags, result)
# Generate ScriptFilter Output
if result:
# show matched results
num = int(C["result_nums"])
display_matched_result(query, result[:num])
else:
# show none matched info
Display.show({
"title": "Nothing found ...",
"subtitle": f'Presh "⌘" to create a new Note with title \"{query}\"',
"arg": '',
"mods": {
"cmd": {
"arg": f'new|[Note>{query}]',
"subtitle": "Press 'Enter' to confirm creating"}}})
return
def show_snippets():
"Input format: `key1 key2, language1 language2`"
if not varibles_checked():
return 0
# Get all sorted snippets
if not ['search_all_folders']:
sorted_note_list = S.get_sorted_files(C["path_to_new_Snippet"])
else:
all_files = S.get_sorted_files(Config.FILES_PATH)
# only search notes with code fences
sorted_note_list = list(filter(lambda x: "```" in x['content'], all_files))
# Parse input
mode, keywords, languages = get_parsed_arg()
if mode == "Recent":
result = sorted_note_list
elif mode == "Title_Search":
result = S.title_search(keywords, sorted_note_list)
elif mode == "And_Search" or mode == "Exact_Search":
result = S.and_search(keywords, sorted_note_list)
elif mode == "Or_Search":
result = S.or_search(keywords, sorted_note_list)
if languages:
result = S.metric_search("language", languages, sorted_note_list)
# Generate ScriptFilter Output
if result:
# show matched results
num = int(C["result_nums"])
display_matched_result(query, result[:num])
else:
# show none matched info
Display.show({
"title": "Nothing found ..",
"subtitle": f'Presh "⌘" to create a new Snippet with title \"{query}\"',
"arg": '',
"mods": {
"cmd": {
"arg": f'new|[Snippet>{query}]',
"subtitle": "Press <Enter> to confirm creating"}}})
return
def show_markdown_links():
"""Show MarkDown links contained in currently opened file"""
filename = U.get_typora_filename()
if filename:
link_list = S.markdown_links_search(filename, filename=True)
matched_list = []
for link in link_list:
path = U.get_abspath(link, query_dict=True)
matched_list.append(F.get_file_info(path))
if not matched_list:
Display.show({
"title": "No MarkDown Link is found in the current file.",
"subtitle": ""
})
else:
display_matched_result(filename, matched_list)
else:
Display.show({
"title": "Error!",
"subtitle": "No file is opened in Typora."
})
def show_backlinks():
filename = U.get_typora_filename()
if filename:
link_list = S.backlinks_search(filename)
matched_list = []
for link in link_list:
path = U.get_abspath(link, query_dict=True)
matched_list.append(F.get_file_info(path))
if not matched_list:
Display.show({
"title": "Not found related Backlinks",
"subtitle": "No other notes links to current file"
})
else:
display_matched_result(filename, matched_list)
else:
Display.show({
"title": "Error!",
"subtitle": "No file is opened in Typora."
})
def display_matched_result(query, matched_list):
items = []
for m in matched_list:
items.append({
"title": m['title'],
"subtitle": Config.WF_SHOW_SUBTITLE.format(
folder=m["folder"],
mdate=m["mdate"],
),
"type": 'file',
"arg": f'open|{m["path"]}',
"mods": {
"cmd": {
"arg": f'show_actions|[{m["path"]}, {query}]',
"subtitle": "Press <Enter> to select your next action"
}},
"quicklookurl": m["path"]
})
Display.show(items)
if __name__ == "__main__":
query = U.get_query(lower=True)
search_type = U.get_search_type()
if search_type == 'normal':
show_notes()
elif search_type == 'markdown_links':
show_markdown_links()
elif search_type == 'backlinks':
show_backlinks()
elif search_type == 'snippet':
show_snippets()
else:
Display.show("Error!")