1+ import os
2+ import subprocess
13import time
24import webbrowser
35from typing import Dict , List , Callable , Any , Tuple , Iterable , Union
46from urllib .parse import urlencode , quote
57import datetime
8+ from calibre_plugins .highlights_to_obsidian .config import prefs
69
7- # avoid importing anything from calibre or the highlights_to_obsidian plugin here
8-
9-
10- # might be better to move these into resource files
11- library_default_name = "Calibre Library"
12- vault_default_name = "My Vault"
13- title_default_format = "Books/{title} by {authors}"
14- body_default_format = "\n [Highlighted]({url}) on {date} at {time} UTC:\n {blockquote}\n \n {notes}\n \n ---\n "
15- no_notes_default_format = "\n [Highlighted]({url}) on {date} at {time} UTC:\n {blockquote}\n \n ---\n "
16- header_default_format = "\n {booksent} highlights from \" {title}\" sent on {datenow} at {timenow} UTC.\n \n ---\n "
17-
18- sort_key_default = "location"
10+ # avoid importing anything else from calibre or the highlights_to_obsidian plugin here.
11+ # this is to avoid having references to the config or the calibre database scattered
12+ # throughout HighlightSender. those references are in HighlightSender.__init__() and
13+ # in make_sender() in button_actions.py.
1914
2015
2116def send_item_to_obsidian (obsidian_data : Dict [str , str ]) -> None :
@@ -28,7 +23,18 @@ def send_item_to_obsidian(obsidian_data: Dict[str, str]) -> None:
2823 encoded_data = urlencode (obsidian_data , quote_via = quote )
2924 uri = "obsidian://new?" + encoded_data
3025 try :
31- webbrowser .open (uri )
26+ if prefs ['use_xdg_open' ]:
27+ # this is to avoid a bug on linux, where the uri is opened in web browser instead of Obsidian
28+ # on windows, this only gives a good error message if shell=True. i need to know what it
29+ # does on Linux and Mac.
30+ subprocess .run (['xdg-open' , uri ], check = True , shell = True )
31+
32+ # can't use os.system, since it doesn't give an error message when you try to xdg-open on windows
33+ # os.system(f'xdg-open \"{uri}\"')
34+ else :
35+ # it might actually be better to do away with webbrowser.open() and only use os.system(). that might fix
36+ # the problem of needing to limit the max file size. would need to add support for each operating system.
37+ webbrowser .open (uri )
3238 except ValueError as e :
3339 raise ValueError (f" send_item_to_obsidian: '{ e } ' in note '{ obsidian_data ['file' ]} '.\n \n "
3440 f"If this error says that the filepath is too long, try reducing the max file size in "
@@ -506,17 +512,17 @@ class HighlightSender:
506512
507513 def __init__ (self ):
508514 # set defaults
509- self .library_name = library_default_name
510- self .vault_name = vault_default_name
511- self .title_format = title_default_format
512- self .body_format = body_default_format
513- self .no_notes_format = no_notes_default_format
514- self .header_format = header_default_format
515+ self .library_name = prefs . defaults [ 'library_name' ]
516+ self .vault_name = prefs . defaults [ 'vault_name' ]
517+ self .title_format = prefs . defaults [ 'title_format' ]
518+ self .body_format = prefs . defaults [ 'body_format' ]
519+ self .no_notes_format = prefs . defaults [ 'no_notes_format' ]
520+ self .header_format = prefs . defaults [ 'header_format' ]
515521 self .book_titles_authors = {}
516522 self .annotations_list = []
517523 self .max_file_size = - 1 # -1 = unlimited
518524 self .copy_header = False
519- self .sort_key = sort_key_default
525+ self .sort_key = prefs . defaults [ 'sort_key' ]
520526 self .sleep_time = 0
521527
522528 def set_library (self , library_name : str ):
0 commit comments