pyinstantref/pdfref_handler

32 lines
946 B
Plaintext
Raw 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]))
2023-07-23 19:57:42 -04:00
section: SectionTitle = SectionTitle(query.get("section", [])[0])
if section != []:
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
2023-07-10 17:03:30 -04:00
subprocess.run(["zathura", "--page", str(page), url.path], text=True)