Add tags to formatted annotations

This commit is contained in:
Marty Oehme 2022-12-24 18:37:36 +01:00
parent 6bb7ed9c9f
commit 5bddf97e58
Signed by: Marty
GPG key ID: 73BA40D5AFAF49C9

View file

@ -40,6 +40,7 @@ class Annotation:
content: str = "" content: str = ""
page: int = 1 page: int = 1
colors: Tuple = (0.0, 0.0, 0.0) colors: Tuple = (0.0, 0.0, 0.0)
tag: str = None
def formatted(self, formatting): def formatted(self, formatting):
output = formatting output = formatting
@ -48,6 +49,7 @@ class Annotation:
r"{note}": self.content, r"{note}": self.content,
r"{page}": str(self.page), r"{page}": str(self.page),
r"{newline}": "\n", r"{newline}": "\n",
r"{tag}": self.tag,
} }
if self.text == "": if self.text == "":
output = re.sub(r"{quote_begin}.*{quote_end}", "", output) output = re.sub(r"{quote_begin}.*{quote_end}", "", output)
@ -77,6 +79,7 @@ class Annotation:
nearest = name nearest = name
return nearest return nearest
class ExtractPlugin(PapersPlugin): class ExtractPlugin(PapersPlugin):
"""Extract annotations from any pdf document. """Extract annotations from any pdf document.
@ -105,7 +108,7 @@ class ExtractPlugin(PapersPlugin):
self.minimum_similarity = float(settings.get("minimum_similarity", 0.75)) self.minimum_similarity = float(settings.get("minimum_similarity", 0.75))
self.formatting = settings.get( self.formatting = settings.get(
"formatting", "formatting",
"{newline}{quote_begin}> {quote} {quote_end}[{page}]{note_begin}{newline}Note: {note}{note_end}", "{newline}{quote_begin}> {quote} {quote_end}[{page}]{note_begin}{newline}Note: {note} {note_end} #{tag}",
) )
self.color_mapping = settings.get("color_mapping", {}) self.color_mapping = settings.get("color_mapping", {})
@ -189,7 +192,7 @@ class ExtractPlugin(PapersPlugin):
self.ui.error(f"Document {file} is broken: {e}") self.ui.error(f"Document {file} is broken: {e}")
return papers_annotated return papers_annotated
def mapped_tag(self, colorname): def tag_from_colorname(self, colorname):
return self.color_mapping.get(colorname) return self.color_mapping.get(colorname)
def _gather_papers(self, conf, args): def _gather_papers(self, conf, args):
@ -256,17 +259,17 @@ class ExtractPlugin(PapersPlugin):
for page in doc: for page in doc:
for annot in page.annots(): for annot in page.annots():
quote, note = self._retrieve_annotation_content(page, annot) quote, note = self._retrieve_annotation_content(page, annot)
annotations.append( a = Annotation(
Annotation( file=filename,
file=filename, paper=paper,
paper=paper, text=quote,
text=quote, content=note,
content=note, colors=annot.colors,
colors=annot.colors, type=annot.type[1],
type=annot.type, page=(page.number or 0) + 1,
page=(page.number or 0) + 1,
)
) )
a.tag = self.tag_from_colorname(a.colorname)
annotations.append(a)
return annotations return annotations
def _retrieve_annotation_content(self, page, annotation): def _retrieve_annotation_content(self, page, annotation):