Add extracting annotations on document import

This commit is contained in:
Marty Oehme 2022-12-22 20:24:20 +01:00
parent 2380772396
commit 8486864132
Signed by: Marty
GPG key ID: 73BA40D5AFAF49C9

View file

@ -8,7 +8,7 @@ import fitz
# from ... import uis # from ... import uis
from pubs.plugins import PapersPlugin from pubs.plugins import PapersPlugin
from pubs.events import PaperChangeEvent, PostCommandEvent from pubs.events import DocAddEvent, DocRemoveEvent
from pubs import repo from pubs import repo
from pubs.utils import resolve_citekey_list from pubs.utils import resolve_citekey_list
@ -30,13 +30,13 @@ class ExtractPlugin(PapersPlugin):
description = "Extract annotations from pubs documents" description = "Extract annotations from pubs documents"
def __init__(self, conf, ui): def __init__(self, conf, ui):
pass
self.ui = ui self.ui = ui
self.conf = conf
self.repository = repo.Repository(conf) self.repository = repo.Repository(conf)
self.pubsdir = os.path.expanduser(conf["main"]["pubsdir"]) self.pubsdir = os.path.expanduser(conf["main"]["pubsdir"])
self.broker = self.repository.databroker self.broker = self.repository.databroker
self.quiet = conf["plugins"].get("extract", {}).get("quiet", False) self.onimport = conf["plugins"].get("extract", {}).get("onimport", False)
# self.manual = conf['plugins'].get('git', {}).get('manual', False) # self.manual = conf['plugins'].get('git', {}).get('manual', False)
# self.force_color = conf['plugins'].get('git', {}).get('force_color', True) # self.force_color = conf['plugins'].get('git', {}).get('force_color', True)
# self.list_of_changes = [] # self.list_of_changes = []
@ -77,7 +77,7 @@ class ExtractPlugin(PapersPlugin):
return return
all_annotations = self.extract(citekeys) all_annotations = self.extract(citekeys)
if args.write: if args.write:
self._to_notes(conf, all_annotations, args.edit) self._to_notes(all_annotations, conf["main"]["note_extension"], args.edit)
else: else:
self._to_stdout(all_annotations) self._to_stdout(all_annotations)
self.repository.close() self.repository.close()
@ -129,13 +129,13 @@ class ExtractPlugin(PapersPlugin):
output+="\n" output+="\n"
print(output) print(output)
def _to_notes(self, conf, annotated_papers, edit=False): def _to_notes(self, annotated_papers, note_extension="txt", edit=False):
for contents in annotated_papers: for contents in annotated_papers:
paper = contents[0] paper = contents[0]
annotations = contents[1] annotations = contents[1]
if annotations: if annotations:
notepath = self.broker.real_notepath( notepath = self.broker.real_notepath(
paper.citekey, conf["main"]["note_extension"] paper.citekey, note_extension
) )
output = "# Annotations\n\n" output = "# Annotations\n\n"
for annotation in annotations: for annotation in annotations:
@ -146,31 +146,13 @@ class ExtractPlugin(PapersPlugin):
# TODO implement NoteEvent(citekey).send() # TODO implement NoteEvent(citekey).send()
@PaperChangeEvent.listen() @DocAddEvent.listen()
def paper_change_event(event): def modify_event(event):
"""When a paper is changed, commit the changes to the directory.""" if ExtractPlugin.is_loaded():
pass plg = ExtractPlugin.get_instance()
# if ExtractPlugin.is_loaded(): if plg.onimport:
# git = ExtractPlugin.get_instance() all_annotations = plg.extract([event.citekey])
# if not git.manual: if all_annotations[0][1]:
# event_desc = event.description plg._to_notes(all_annotations, plg.conf["main"]["note_extension"])
# for a, b in [('\\', '\\\\'), ('"', '\\"'), ('$', '\\$'), ('`', '\\`')]: plg.ui.info(f"Imported {event.citekey} annotations.")
# event_desc = event_desc.replace(a, b)
# git.list_of_changes.append(event_desc)
@PostCommandEvent.listen()
def git_commit(event):
pass
# if ExtractPlugin.is_loaded():
# try:
# extract = ExtractPlugin.get_instance()
# if len(extract.list_of_changes) > 0:
# if not extract.manual:
# title = ' '.join(sys.argv) + '\n'
# message = '\n'.join([title] + extract.list_of_changes)
#
# extract.shell('add .')
# extract.shell('commit -F-', message.encode('utf-8'))
# except RuntimeError as exc:
# uis.get_ui().warning(exc.args[0])