pyinstantref/pdfref_handler

42 lines
1.4 KiB
Plaintext
Raw Permalink Normal View History

2023-07-10 17:03:30 -04:00
#!/usr/bin/env python3
# pdfref:// URL handler
from urllib.parse import urlparse, parse_qs
from sys import argv
2023-07-23 19:57:42 -04:00
from datatypes import *
from typing import cast, Any
from util import notify
import subprocess
import fitz
2023-07-10 17:03:30 -04:00
url = urlparse(argv[1])
query = parse_qs(url.query)
page: PageNumber = PageNumber(int(query.get("page", ["0"])[0]))
section_list = query.get("section", [])
destination_list = query.get("destination", [])
2023-07-23 19:57:42 -04:00
if section_list != []:
section: SectionTitle = SectionTitle(section_list[0])
2023-07-23 19:57:42 -04:00
with fitz.Document(url.path) as doc:
toc = [FitzBookmark(*x) for x in cast(Any, doc).get_toc()]
headers = [x for x in toc if x.title == section]
if headers == []:
notify("", f"Failed to find section '{section}': did the title change?")
else:
if len(headers) > 1:
notify("", f"Multiple sections '{section}' found: page might be incorrect")
page = headers[0].page
elif destination_list != []:
destination_name = destination_list[0]
with fitz.Document(url.path) as doc:
destinations = FitzDestinations(cast(Any, doc).resolve_names())
destination = destinations.get(destination_name)
if not destination:
notify("", f"Failed to find named destination '{destination_name}': did the document change?")
else:
page = destination["page"]+1
2023-07-23 19:57:42 -04:00
2023-07-10 17:03:30 -04:00
subprocess.run(["zathura", "--page", str(page), url.path], text=True)