linkding/bookmarks/services/exporter.py

54 lines
1.5 KiB
Python
Raw Normal View History

2023-02-18 17:25:54 +00:00
import html
2019-12-26 12:45:12 +00:00
from typing import List
from bookmarks.models import Bookmark
BookmarkDocument = List[str]
def export_netscape_html(bookmarks: List[Bookmark]):
doc = []
append_header(doc)
append_list_start(doc)
[append_bookmark(doc, bookmark) for bookmark in bookmarks]
append_list_end(doc)
2024-01-27 10:29:16 +00:00
return "\n\r".join(doc)
2019-12-26 12:45:12 +00:00
def append_header(doc: BookmarkDocument):
2024-01-27 10:29:16 +00:00
doc.append("<!DOCTYPE NETSCAPE-Bookmark-file-1>")
2019-12-26 12:45:12 +00:00
doc.append('<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">')
2024-01-27 10:29:16 +00:00
doc.append("<TITLE>Bookmarks</TITLE>")
doc.append("<H1>Bookmarks</H1>")
2019-12-26 12:45:12 +00:00
def append_list_start(doc: BookmarkDocument):
2024-01-27 10:29:16 +00:00
doc.append("<DL><p>")
2019-12-26 12:45:12 +00:00
def append_bookmark(doc: BookmarkDocument, bookmark: Bookmark):
url = bookmark.url
2024-01-27 10:29:16 +00:00
title = html.escape(bookmark.resolved_title or "")
desc = html.escape(bookmark.resolved_description or "")
if bookmark.notes:
2024-01-27 10:29:16 +00:00
desc += f"[linkding-notes]{html.escape(bookmark.notes)}[/linkding-notes]"
tag_names = bookmark.tag_names
if bookmark.is_archived:
2024-01-27 10:29:16 +00:00
tag_names.append("linkding:archived")
tags = ",".join(tag_names)
toread = "1" if bookmark.unread else "0"
private = "0" if bookmark.shared else "1"
2019-12-26 12:45:12 +00:00
added = int(bookmark.date_added.timestamp())
doc.append(
2024-01-27 10:29:16 +00:00
f'<DT><A HREF="{url}" ADD_DATE="{added}" PRIVATE="{private}" TOREAD="{toread}" TAGS="{tags}">{title}</A>'
)
2019-12-26 12:45:12 +00:00
if desc:
2024-01-27 10:29:16 +00:00
doc.append(f"<DD>{desc}")
2019-12-26 12:45:12 +00:00
def append_list_end(doc: BookmarkDocument):
2024-01-27 10:29:16 +00:00
doc.append("</DL><p>")