pyinstantref/copy_ref

57 lines
1.6 KiB
Plaintext
Raw Permalink Normal View History

#!/usr/bin/env python3
2023-07-10 17:03:30 -04:00
from pdf_data import get_destination_pdf, get_page_pdf, get_section_pdf
2023-07-10 17:03:30 -04:00
from datatypes import *
from enum import Enum, auto
2023-07-23 19:57:42 -04:00
from util import notify
2023-07-10 17:03:30 -04:00
import subprocess
import argparse
import formatter.typst as typst_fmt
parser = argparse.ArgumentParser()
dest_group = parser.add_mutually_exclusive_group()
dest_group.add_argument("--section", help="Copy reference to the section title instead of the page number.", action="store_true")
dest_group.add_argument("--destination", help="Copy reference to a named destination instead of the page number.", action="store_true")
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 copy_ref(ref: Reference, format: LinkFormat) -> None:
"""Formats Reference and copies it to clipboard."""
match format:
case LinkFormat.TYPST:
link_txt = typst_fmt.ref(ref)
2023-07-10 17:03:30 -04:00
clip_copy(link_txt)
if __name__ == "__main__":
args = parser.parse_args()
if args.section:
2023-07-23 19:57:42 -04:00
ref = get_section_pdf()
elif args.destination:
ref = get_destination_pdf()
2023-07-23 19:57:42 -04:00
else:
ref = get_page_pdf()
2023-07-10 17:03:30 -04:00
format = LinkFormat.TYPST
copy_ref(ref, format)
2023-07-23 19:57:42 -04:00
match ref:
case PDFPage():
notify("Copied ref", f"{ref.filepath.name} p. {ref.page}")
case PDFSection():
notify("Copied ref", f"{ref.filepath.name} sec. {ref.title}")
case PDFDestination():
notify("Copied ref", f"{ref.filepath.name} {ref.name}")