Skip to content

Commit 3bf9e65

Browse files
committed
add linux xdg-open option
replaced webbrowser.open(uri) with subprocess.run(["xdg-open", uri]) when xdg-open option is enabled added note to readme and help menu for xdg-open option moved some defaults from highlight_sender.py to config.py, to avoid a circular import
1 parent 2d9e3fc commit 3bf9e65

File tree

5 files changed

+51
-27
lines changed

5 files changed

+51
-27
lines changed

h2o/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from calibre.customize import InterfaceActionBase
55

6-
_version = (1, 4, 0)
6+
_version = (1, 4, 1)
77
version = ".".join([str(x) for x in _version])
88

99

h2o/button_actions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ def help_menu(parent):
2020
"be sent. If this happens, you can use the \"Resend Previously Sent Highlights\" function.\n\n" + \
2121
"You can set keyboard shortcuts in calibre's Preferences -> Shortcuts -> H2O.\n\n" + \
2222
"Due to URI length limits, H2O can only send a few thousand words to a single note at once. Extra text " \
23-
"will be sent to different notes with increasing numbers added to the end of the title."
23+
"will be sent to different notes with increasing numbers added to the end of the title.\n\n" + \
24+
"If you're using Linux and H2O opens your web browser instead of Obsidian, see the xdg-open setting " + \
25+
"at the bottom of the config's Other Options."
2426
info_dialog(parent, title, body, show=True)
2527

2628

h2o/config.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
QPushButton, QDialog, QDialogButtonBox, QCheckBox)
55
from calibre.gui2 import warning_dialog
66
from calibre.utils.config import JSONConfig
7-
from calibre_plugins.highlights_to_obsidian.highlight_sender import (title_default_format, body_default_format,
8-
vault_default_name, no_notes_default_format,
9-
header_default_format, sort_key_default)
107
from calibre_plugins.highlights_to_obsidian.__init__ import version
118

129
# This is where all preferences for this plugin will be stored
@@ -20,6 +17,17 @@
2017
# set time to 2 days after unix epoch start. hopefully prevents platform-dependent invalid default
2118
# last_send_time when using time.mktime()
2219

20+
sort_key_default = "location"
21+
22+
# might be better to move these into resource files
23+
library_default_name = "Calibre Library"
24+
vault_default_name = "My Vault"
25+
title_default_format = "Books/{title} by {authors}"
26+
body_default_format = "\n[Highlighted]({url}) on {date} at {time} UTC:\n{blockquote}\n\n{notes}\n\n---\n"
27+
no_notes_default_format = "\n[Highlighted]({url}) on {date} at {time} UTC:\n{blockquote}\n\n---\n"
28+
header_default_format = "\n{booksent} highlights from \"{title}\" sent on {datenow} at {timenow} UTC.\n\n---\n"
29+
30+
prefs.defaults['library_name'] = library_default_name
2331
prefs.defaults['vault_name'] = vault_default_name
2432
prefs.defaults['title_format'] = title_default_format
2533
prefs.defaults['body_format'] = body_default_format
@@ -36,11 +44,13 @@
3644
prefs.defaults['max_note_size'] = "20000"
3745
prefs.defaults['use_max_note_size'] = True # make max_note_size easy to toggle
3846
prefs.defaults['copy_header'] = False # whether to copy header when splitting a too-big note
39-
prefs.defaults['web_user'] = False # whether we should send web user or local user's highlights
4047
prefs.defaults['web_user_name'] = "*"
48+
prefs.defaults['web_user'] = False # whether we should send web user or local user's highlights
49+
prefs.defaults['use_xdg_open'] = False
4150
prefs.defaults['sleep_secs'] = 0.1
4251

4352

53+
4454
class ConfigWidget(QWidget):
4555

4656
def __init__(self):
@@ -347,6 +357,11 @@ def __init__(self):
347357
self.web_user_checkbox.setChecked(prefs['web_user'])
348358
self.l.addWidget(self.web_user_checkbox)
349359

360+
# checkbox for linux xdg-open
361+
self.linux_xdg_checkbox = QCheckBox("Use Linux xdg-open command instead of Python webbrowser.open()")
362+
self.linux_xdg_checkbox.setChecked(prefs['use_xdg_open'])
363+
self.l.addWidget(self.linux_xdg_checkbox)
364+
350365
self.l.addSpacing(self.spacing)
351366

352367
# ok and cancel buttons
@@ -369,9 +384,10 @@ def save_settings(self):
369384
prefs['copy_header'] = self.copy_header_checkbox.isChecked()
370385
prefs['confirm_send_all'] = self.show_confirmation_checkbox.isChecked()
371386
prefs['highlights_sent_dialog'] = self.show_count_checkbox.isChecked()
372-
prefs['web_user'] = self.web_user_checkbox.isChecked()
373387
username = self.web_user_name_input.text()
374388
prefs['web_user_name'] = "*" if username == "" else username
389+
prefs['web_user'] = self.web_user_checkbox.isChecked()
390+
prefs['use_xdg_open'] = self.linux_xdg_checkbox.isChecked()
375391

376392
sleep_time = self.sleep_time_input.text()
377393
try:

h2o/highlight_sender.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1+
import os
2+
import subprocess
13
import time
24
import webbrowser
35
from typing import Dict, List, Callable, Any, Tuple, Iterable, Union
46
from urllib.parse import urlencode, quote
57
import 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

2116
def 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):

zip/h2o-1.4.1-wip.zip

34.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)