42 lines
1.4 KiB
Python
Executable File
42 lines
1.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# pdfref:// URL handler
|
|
|
|
from urllib.parse import urlparse, parse_qs
|
|
from sys import argv
|
|
from datatypes import *
|
|
from typing import cast, Any
|
|
from util import notify
|
|
import subprocess
|
|
import fitz
|
|
|
|
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", [])
|
|
|
|
if section_list != []:
|
|
section: SectionTitle = SectionTitle(section_list[0])
|
|
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
|
|
|
|
|
|
subprocess.run(["zathura", "--page", str(page), url.path], text=True)
|