2023-07-10 11:20:55 -04:00
|
|
|
#!/usr/bin/env python3
|
2023-07-10 17:03:30 -04:00
|
|
|
|
|
|
|
from pdf_data import get_metadata_pdf
|
|
|
|
from datatypes import *
|
|
|
|
from enum import Enum, auto
|
|
|
|
from os import environ
|
|
|
|
from urllib.parse import urlencode
|
|
|
|
import subprocess
|
2023-07-11 14:01:49 -04:00
|
|
|
import pydbus
|
2023-07-10 17:03:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
class LinkFormat(Enum):
|
|
|
|
TYPST = auto()
|
|
|
|
|
|
|
|
|
|
|
|
def clip_copy(txt: str):
|
|
|
|
try:
|
|
|
|
subprocess.run(["xsel", "-ib"], text=True, input=txt)
|
|
|
|
except OSError as e:
|
|
|
|
raise Exception("Please install `xsel`.") from e
|
|
|
|
|
|
|
|
|
|
|
|
def format_typst(ref: Reference) -> str:
|
|
|
|
path_str = environ.get("TYPST_ROOT", None)
|
|
|
|
if path_str is None:
|
|
|
|
raise KeyError("Please set TYPST_ROOT to format links with Typst.")
|
|
|
|
typst_root = Path(path_str)
|
|
|
|
|
|
|
|
relative: bool = ref.filepath.is_relative_to(typst_root)
|
|
|
|
format_path: str
|
|
|
|
|
|
|
|
if relative:
|
|
|
|
format_path = "/" + str(ref.filepath.relative_to(typst_root))
|
|
|
|
else:
|
|
|
|
format_path = str(ref.filepath.absolute())
|
|
|
|
|
|
|
|
params = dict(page=ref.page)
|
|
|
|
|
|
|
|
if relative:
|
|
|
|
return f'#lref("{format_path}?{urlencode(params)}", pdfref: true)[]'
|
|
|
|
else:
|
|
|
|
return f'#link("pdfref://{format_path}?{urlencode(params)}")[]'
|
|
|
|
|
|
|
|
|
|
|
|
def copy_ref(ref: Reference, format: LinkFormat) -> None:
|
|
|
|
"""Formats Reference and copies it to clipboard."""
|
|
|
|
match format:
|
|
|
|
case LinkFormat.TYPST:
|
|
|
|
link_txt = format_typst(ref)
|
|
|
|
|
|
|
|
clip_copy(link_txt)
|
|
|
|
|
|
|
|
|
2023-07-11 14:01:49 -04:00
|
|
|
def notify(title:str, txt: str) -> None:
|
|
|
|
"""Send a text notification."""
|
|
|
|
bus = pydbus.SessionBus()
|
|
|
|
notifs = bus.get(".Notifications")
|
|
|
|
notifs.Notify("instantref", 0, "dialog-information", title, txt, [], {}, 5000)
|
|
|
|
|
|
|
|
|
2023-07-10 17:03:30 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
ref = get_metadata_pdf()
|
|
|
|
|
|
|
|
format = LinkFormat.TYPST
|
|
|
|
copy_ref(ref, format)
|
2023-07-11 14:01:49 -04:00
|
|
|
notify("Copied ref", f"{ref.filepath.name} p. {ref.page}")
|