-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbookmarker.py
314 lines (257 loc) · 12.1 KB
/
bookmarker.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import sys
import os
import re
def add_link_to_md(link, foldername=None, description=None, tags=None):
# Define the path to your documents folder
documents_folder = os.path.expanduser("~/Documents/Bookmarker/")
# Define the path to the Markdown file
md_file_path = os.path.join(documents_folder, "bookmarks.md")
# Check if the Markdown file exists, and create it if it doesn't
if not os.path.exists(md_file_path):
with open(md_file_path, "w") as file:
file.write("# Bookmarks\n\n")
# If a foldername is provided, check if it exists in the Markdown file
if foldername:
with open(md_file_path, "r") as file:
lines = file.readlines()
# Remove trailing slash if present
foldername = foldername.rstrip("/")
folder_parts = foldername.split("/")
with open(md_file_path, "a") as file:
for i, part in enumerate(folder_parts):
indent = " " * i
if i == 0:
file.write(f"- {'#' * 2} {part}\n")
else:
file.write(f"{indent}- {'#' * 3} {part}\n")
link_line = f"{' ' * len(folder_parts)}- [{description or link}]({link})"
if tags:
link_line += f" - Tags: {tags}\n"
else:
link_line += "\n"
with open(md_file_path, "a") as file:
file.write(link_line)
else:
# Append the link to the Markdown file without a folder
with open(md_file_path, "a") as file:
link_line = f"- [ {description or link}]({link})"
if tags:
link_line += f" - Tags: {tags}\n"
else:
link_line += "\n"
file.write(link_line)
def import_data_from_html(html_file_path):
# Check if the HTML file exists
if not os.path.exists(html_file_path):
print(f"HTML file '{html_file_path}' not found.")
return
# Open the HTML file and read its contents
with open(html_file_path, "r") as html_file:
html_content = html_file.read()
# Split the HTML content by lines
lines = html_content.splitlines()
# Initialize variables to track folder and tags
current_folder = None
current_tags = ""
# Iterate through the lines and process the content
for line in lines:
line = line.strip() # Remove leading/trailing whitespace
# Check if the line starts with '<DT><H3>' indicating a folder
if line.startswith("<DT><H3>"):
current_folder = re.search(r'<DT><H3>(.*?)</H3>', line).group(1)
# Check if the line contains a link
elif line.startswith("<DT><A HREF="):
link_match = re.search(r'<A HREF="(.*?)".*?TAGS="(.*?)">(.*?)</A>', line)
if link_match:
link, tags, description = link_match.groups()
# Check if a folder is defined, and if so, add it to the Markdown
if current_folder:
add_link_to_md(link, foldername=current_folder, description=description, tags=tags)
else:
add_link_to_md(link, description=description, tags=tags)
print(f"Data imported from '{html_file_path}' to Markdown file.")
## Function to create Netscape bookmarks in HTML format from the Markdown file
def create_netscape_bookmarks():
# Define the path to the Netscape bookmark file (HTML format)
documents_folder = os.path.expanduser("~/Documents/Bookmarker/")
html_file_path = os.path.join(documents_folder, "bookmarks.html")
md_file_path = os.path.join(documents_folder, "bookmarks.md")
# Read the Markdown file and generate the Netscape bookmark file
with open(md_file_path, "r") as md_file:
lines = md_file.readlines()
with open(html_file_path, "w") as html_file:
html_file.write("<!DOCTYPE NETSCAPE-Bookmark-file-1>\n")
html_file.write("<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">\n")
html_file.write("<TITLE>Bookmarks</TITLE>\n")
html_file.write("<H1>Bookmarks</H1>\n")
# Initialize a stack to keep track of folder hierarchy
folder_stack = []
for line in lines:
# Use regular expression to match valid links and folders
match_link = re.search(r'- \[([^]]+)\]\(([^)]+)\)', line)
match_folder = re.search(r'- (.+)', line)
match_tags = re.search(r'- Tags: (.+)', line)
if match_link:
description = match_link.group(1)
link = match_link.group(2)
tags = ""
if match_tags:
tags = match_tags.group(1)
# Write the link under the current folder hierarchy
html_file.write(f'<DT><A HREF="{link}" TAGS="{tags}">{description}</A>\n')
elif match_folder:
folder_name = match_folder.group(1).strip()
# Adjust folder hierarchy based on indentation
while len(folder_stack) > 0 and folder_stack[-1][0] >= len(line) - len(line.lstrip()):
folder_stack.pop()
# Write the folder information to HTML
html_file.write(f'<DT><H3>{folder_name}</H3>\n')
html_file.write("<DL>\n")
folder_stack.append((len(line) - len(line.lstrip()), folder_name))
# Close any remaining folders
while len(folder_stack) > 0:
html_file.write("</DL>\n")
folder_stack.pop()
# Function to list all links in the Markdown file
def list_all_links():
# Define the path to the Markdown file
documents_folder = os.path.expanduser("~/Documents/Bookmarker/")
md_file_path = os.path.join(documents_folder, "bookmarks.md")
# Read the Markdown file and print all links
with open(md_file_path, "r") as md_file:
lines = md_file.readlines()
for line in lines:
# Use regular expression to match valid links
match_link = re.search(r'- \[([^]]+)\]\(([^)]+)\)', line)
if match_link:
description = match_link.group(1)
link = match_link.group(2)
print(f"[{description}]({link})")
def list_all_folders():
# Define the path to the Markdown file
documents_folder = os.path.expanduser("~/Documents/Bookmarker/")
md_file_path = os.path.join(documents_folder, "bookmarks.md")
# Read the Markdown file and print folders
with open(md_file_path, "r") as md_file:
lines = md_file.readlines()
folder_stack = [] # To keep track of the folder hierarchy
for line in lines:
# Check if the line starts with "- ##" or "- ###" followed by a word or group of words
match_folder = re.match(r'^(\s*)- (##|###) (.+)', line)
if match_folder:
folder_name = match_folder.group(3).strip()
folder_depth = len(match_folder.group(1)) // 4 # Four spaces represent one level of depth
# Pop folders from the stack until the correct depth is reached
while len(folder_stack) > folder_depth:
folder_stack.pop()
# Append the current folder to the stack
folder_stack.append(folder_name)
# Print the folder hierarchy with proper formatting
folder_structure = ""
for i in range(len(folder_stack)):
folder_structure += "| - " * i + folder_stack[i] + "\n"
print(folder_structure)
def list_all_links_from_folder(foldername):
# Define the path to the Markdown file
documents_folder = os.path.expanduser("~/Documents/Bookmarker/")
md_file_path = os.path.join(documents_folder, "bookmarks.md")
# Read the Markdown file and print all links
with open(md_file_path, "r") as md_file:
lines = md_file.readlines()
foldername = foldername.rstrip("/")
folder_parts = foldername.split("/")
last_folder_part = folder_parts[-1]
folder_depth = len(folder_parts)
folder_found = False
for line in lines:
# Check if the line starts with "- ##" or "- ###" followed by a word or group of words
match_folder = re.match(r'^(\s*)- (##|###) ' + last_folder_part, line)
if match_folder:
current_folder_depth = len(match_folder.group(1)) // 4
if current_folder_depth == folder_depth - 1:
folder_found = True
continue
# Start matching links only after the folder line is found
if folder_found:
# Check if the line is a subfolder line
match_subfolder = re.match(r'^(\s*)- (##|###) ', line)
if match_subfolder:
current_subfolder_depth = len(match_subfolder.group(1)) // 4
if current_subfolder_depth >= folder_depth:
break # Stop matching links if a subfolder line is found
# Use regular expression to match valid links
match_link = re.search(r'- \[([^]]+)\]\(([^)]+)\)', line)
if match_link:
description = match_link.group(1)
link = match_link.group(2)
print(f"[{description}]({link})")
def list_all_links_with_tag(tag):
# Define the path to the Markdown file
documents_folder = os.path.expanduser("~/Documents/Bookmarker/")
md_file_path = os.path.join(documents_folder, "bookmarks.md")
# Read the Markdown file and print all links
with open(md_file_path, "r") as md_file:
lines = md_file.readlines()
for line in lines:
# Use regular expression to match valid links with tags
match_link_with_tags = re.search(r'- \[([^]]+)\]\(([^)]+)\) - Tags: ([^\n]+)', line)
if match_link_with_tags:
description = match_link_with_tags.group(1)
link = match_link_with_tags.group(2)
tags = match_link_with_tags.group(3).split(", ")
if tag in tags:
print(f"[{description}]({link})")
def main():
# Check if the command-line argument is provided
if len(sys.argv) < 2:
print("Usage: bookmarker (bk) [--export] [--import <html_file_path>] <link> [-f <foldername>] [-d <description>] [-t <tags>] [--list --all] [--folders] [--list --f <foldername>] [--list --t <tag>]")
sys.exit(1)
if sys.argv[1] == "--list" and sys.argv[2] == "--all":
list_all_links()
elif sys.argv[1] == "--list" and sys.argv[2] == "--f" and len(sys.argv) >= 4:
foldername = sys.argv[3]
list_all_links_from_folder(foldername)
elif sys.argv[1] == "--list" and sys.argv[2] == "--t" and len(sys.argv) >= 4:
tag = sys.argv[3]
list_all_links_with_tag(tag)
elif sys.argv[1] == "--folders":
list_all_folders()
elif sys.argv[1] == "--export":
create_netscape_bookmarks()
print("Bookmarks exported to HTML file.")
elif sys.argv[1] == "--import" and len(sys.argv) >= 3:
html_file_path = sys.argv[2]
import_data_from_html(html_file_path)
else:
# Check if the --export argument is present in the arguments
export_requested = "--export" in sys.argv
link_index = 1 if not export_requested else 2 # Adjust the index based on --export
link = sys.argv[link_index]
foldername = None
description = None
tags = None
# Parse command-line arguments starting from index link_index + 1
i = link_index + 1
while i < len(sys.argv):
arg = sys.argv[i]
if arg == "-f" and i + 1 < len(sys.argv):
foldername = sys.argv[i + 1]
i += 1
elif arg == "-d" and i + 1 < len(sys.argv):
description = sys.argv[i + 1]
i += 1
elif arg == "-t" and i + 1 < len(sys.argv):
tags = sys.argv[i + 1]
i += 1
i += 1
add_link_to_md(link, foldername, description, tags)
# Only export if --export was requested
if export_requested:
create_netscape_bookmarks()
print("Bookmarks exported to HTML file.")
if foldername:
print(f"Added {link} to the '{foldername}' folder in your bookmarks.")
else:
print(f"Added {link} to your bookmarks.")
if __name__ == "__main__":
main()