1
1
"""Convert Chrome bookmarks to markdown format."""
2
2
3
- from bs4 import BeautifulSoup
3
+ import argparse
4
+ import sys
4
5
from pathlib import Path
5
6
from typing import Optional
6
7
8
+ from bs4 import BeautifulSoup
9
+
10
+
7
11
class BookmarkError (Exception ):
8
12
"""Base exception for bookmark conversion errors."""
9
- pass
10
13
11
- def convert_bookmarks (input_file : Path , output_file : Path , folder_name : Optional [str ] = "EXPORT_FOLDER" ) -> None :
14
+
15
+ def convert_bookmarks (
16
+ input_file : Path , output_file : Path , folder_name : Optional [str ] = "EXPORT_FOLDER"
17
+ ) -> None :
12
18
"""Convert Chrome bookmarks HTML file to markdown format."""
13
19
# Validate inputs
14
20
if not isinstance (input_file , Path ):
@@ -27,13 +33,13 @@ def convert_bookmarks(input_file: Path, output_file: Path, folder_name: Optional
27
33
raise BookmarkError (f"Output directory does not exist: { output_file .parent } " )
28
34
29
35
try :
30
- with open (input_file , 'r' , encoding = ' utf-8' ) as f :
31
- soup = BeautifulSoup (f , ' html.parser' )
36
+ with open (input_file , "r" , encoding = " utf-8" ) as f :
37
+ soup = BeautifulSoup (f , " html.parser" )
32
38
except Exception as e :
33
39
raise BookmarkError (f"Failed to parse HTML file: { e } " )
34
40
35
41
# Find target folder
36
- folders = soup .find_all ('h3' )
42
+ folders = soup .find_all ("h3" )
37
43
target_folder = None
38
44
for folder in folders :
39
45
if folder .string == folder_name :
@@ -44,39 +50,29 @@ def convert_bookmarks(input_file: Path, output_file: Path, folder_name: Optional
44
50
raise BookmarkError (f"Folder '{ folder_name } ' not found!" )
45
51
46
52
try :
47
- bookmarks_dl = target_folder .find_next ('dl' )
53
+ bookmarks_dl = target_folder .find_next ("dl" )
48
54
if not bookmarks_dl :
49
55
raise BookmarkError (f"No bookmarks found in folder '{ folder_name } '" )
50
- bookmarks = bookmarks_dl .find_all ('a' )
56
+ bookmarks = bookmarks_dl .find_all ("a" )
51
57
52
- with open (output_file , 'w' , encoding = ' utf-8' ) as f :
58
+ with open (output_file , "w" , encoding = " utf-8" ) as f :
53
59
for bookmark in bookmarks :
54
60
title = bookmark .string or "Untitled"
55
- url = bookmark .get (' href' , '' )
61
+ url = bookmark .get (" href" , "" )
56
62
f .write (f"- [{ title } ]({ url } )\n " )
57
63
except Exception as e :
58
64
raise BookmarkError (f"Failed to process bookmarks: { e } " )
59
65
66
+
60
67
def main ():
61
68
"""Command line interface."""
62
- import argparse
63
- import sys
64
-
65
- parser = argparse .ArgumentParser (
66
- description = "Convert Chrome bookmarks to markdown format"
67
- )
68
- parser .add_argument (
69
- 'input_file' ,
70
- help = "Chrome bookmarks HTML file"
71
- )
69
+ parser = argparse .ArgumentParser (description = "Convert Chrome bookmarks to markdown format" )
70
+ parser .add_argument ("input_file" , help = "Chrome bookmarks HTML file" )
71
+ parser .add_argument ("output_file" , help = "Output markdown file" )
72
72
parser .add_argument (
73
- 'output_file' ,
74
- help = "Output markdown file"
75
- )
76
- parser .add_argument (
77
- '--folder' ,
73
+ "--folder" ,
78
74
default = "EXPORT_FOLDER" ,
79
- help = "Folder name to extract (default: EXPORT_FOLDER)"
75
+ help = "Folder name to extract (default: EXPORT_FOLDER)" ,
80
76
)
81
77
82
78
args = parser .parse_args ()
@@ -90,5 +86,6 @@ def main():
90
86
print (f"Unexpected error: { e } " , file = sys .stderr )
91
87
sys .exit (1 )
92
88
93
- if __name__ == '__main__' :
89
+
90
+ if __name__ == "__main__" :
94
91
main ()
0 commit comments