Created
November 4, 2024 08:05
-
-
Save francescopapaleo/c801f8a9865deed10e713ced13af8ce2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| def convert_bookmarks(json_file, html_file): | |
| with open(json_file, 'r', encoding='utf-8') as f: | |
| bookmarks = json.load(f) | |
| def parse_node(node): | |
| if 'children' in node: | |
| if node['type'] == 'folder': | |
| folder_html = f"<DT><H3>{node['name']}</H3>\n<DL><p>\n" | |
| for child in node['children']: | |
| folder_html += parse_node(child) | |
| folder_html += "</DL><p>\n" | |
| return folder_html | |
| else: | |
| return '' | |
| else: | |
| return f'<DT><A HREF="{node["url"]}">{node["name"]}</A>\n' | |
| bookmark_html = ( | |
| "<!DOCTYPE NETSCAPE-Bookmark-file-1>\n" | |
| "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">\n" | |
| "<TITLE>Bookmarks</TITLE>\n" | |
| "<H1>Bookmarks</H1>\n<DL><p>\n" | |
| ) | |
| for item in bookmarks['roots'].values(): | |
| bookmark_html += parse_node(item) | |
| bookmark_html += "</DL><p>\n" | |
| with open(html_file, 'w', encoding='utf-8') as f: | |
| f.write(bookmark_html) | |
| # Example usage: | |
| convert_bookmarks('bookmarks.json', 'bookmarks.html') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment