Add note annotation appending
This commit is contained in:
parent
223771f02d
commit
1bb3502709
1 changed files with 31 additions and 8 deletions
|
@ -11,7 +11,7 @@ from pubs.events import DocAddEvent, NoteEvent
|
||||||
|
|
||||||
from pubs import repo
|
from pubs import repo
|
||||||
from pubs.utils import resolve_citekey_list
|
from pubs.utils import resolve_citekey_list
|
||||||
from pubs.content import write_file
|
from pubs.content import check_file, read_text_file, write_file
|
||||||
|
|
||||||
|
|
||||||
class ExtractPlugin(PapersPlugin):
|
class ExtractPlugin(PapersPlugin):
|
||||||
|
@ -52,18 +52,17 @@ class ExtractPlugin(PapersPlugin):
|
||||||
nargs=argparse.REMAINDER,
|
nargs=argparse.REMAINDER,
|
||||||
help="citekey(s) of the documents to extract from",
|
help="citekey(s) of the documents to extract from",
|
||||||
)
|
)
|
||||||
# TODO option for writing to stdout or notes
|
|
||||||
extract_parser.add_argument(
|
extract_parser.add_argument(
|
||||||
"-w",
|
"-w",
|
||||||
"--write",
|
"--write",
|
||||||
help="write to individual notes instead of standard out. CAREFUL: OVERWRITES NOTES CURRENTLY",
|
help="Write to individual notes instead of standard out. Appends to existing notes.",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
default=None,
|
default=None,
|
||||||
)
|
)
|
||||||
extract_parser.add_argument(
|
extract_parser.add_argument(
|
||||||
"-e",
|
"-e",
|
||||||
"--edit",
|
"--edit",
|
||||||
help="open each note in editor for manual editing after extracting annotations to it",
|
help="Open each note in editor for manual editing after extracting annotations to it.",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
default=False,
|
default=False,
|
||||||
)
|
)
|
||||||
|
@ -136,14 +135,38 @@ class ExtractPlugin(PapersPlugin):
|
||||||
annotations = contents[1]
|
annotations = contents[1]
|
||||||
if annotations:
|
if annotations:
|
||||||
notepath = self.broker.real_notepath(paper.citekey, note_extension)
|
notepath = self.broker.real_notepath(paper.citekey, note_extension)
|
||||||
output = "# Annotations\n\n"
|
if check_file(notepath, fail=False):
|
||||||
for annotation in annotations:
|
self._append_to_note(notepath, annotations)
|
||||||
output += f"> {annotation}\n\n"
|
else:
|
||||||
write_file(notepath, output, "w")
|
self._write_new_note(notepath, annotations)
|
||||||
|
|
||||||
if edit is True:
|
if edit is True:
|
||||||
self.ui.edit_file(notepath, temporary=False)
|
self.ui.edit_file(notepath, temporary=False)
|
||||||
NoteEvent(paper.citekey).send()
|
NoteEvent(paper.citekey).send()
|
||||||
|
|
||||||
|
def _write_new_note(self, notepath, annotations):
|
||||||
|
output = "# Annotations\n\n"
|
||||||
|
for annotation in annotations:
|
||||||
|
output += f"> {annotation}\n\n"
|
||||||
|
write_file(notepath, output, "w")
|
||||||
|
|
||||||
|
def _append_to_note(self, notepath, annotations):
|
||||||
|
"""Append new annotations to the end of a note.
|
||||||
|
|
||||||
|
Looks through note to determine any new annotations which should be
|
||||||
|
added and adds them to the end of the note file.
|
||||||
|
"""
|
||||||
|
existing = read_text_file(notepath)
|
||||||
|
# removed annotations already found in the note
|
||||||
|
existing_dropped = [x for x in annotations if x not in existing]
|
||||||
|
if not existing_dropped:
|
||||||
|
return
|
||||||
|
|
||||||
|
output = ""
|
||||||
|
for annotation in existing_dropped:
|
||||||
|
output += f"> {annotation}\n\n"
|
||||||
|
write_file(notepath, output, "a")
|
||||||
|
|
||||||
|
|
||||||
@DocAddEvent.listen()
|
@DocAddEvent.listen()
|
||||||
def modify_event(event):
|
def modify_event(event):
|
||||||
|
|
Loading…
Reference in a new issue