feat: Add count formatter

Added formatter which counts and outputs the number of
annotations in each document.
This commit is contained in:
Marty Oehme 2023-09-20 08:38:06 +02:00
parent 5a6d672c76
commit 5f0bc2ffad
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
2 changed files with 21 additions and 20 deletions

View file

@ -47,11 +47,11 @@ class MarkdownFormatter:
output += a.format(self.string)
if entry != last:
print(f"entry: {entry}, last: {last}")
output += "\n\n\n"
return output
@dataclass
class CountFormatter:
annotated_docs: list[AnnotatedDocument] = field(default_factory=lambda: list())
@ -60,31 +60,27 @@ class CountFormatter:
footer: str = ""
def execute(self, doc: AnnotatedDocument | None = None) -> str:
output = ""
documents = self.annotated_docs if doc is None else [doc]
last = documents[-1]
output = ""
for entry in documents:
if not entry.annotations:
continue
title_decoration = (
f"{'=' * len(entry.document.get('title', ''))} "
f"{'-' * len(entry.document.get('author', ''))}"
)
output += (
f"{title_decoration}\n"
f"{entry.document['title']} - {entry.document['author']}\n"
f"{title_decoration}\n\n"
)
for a in entry.annotations:
output += a.format(self.string)
count = 0
for _ in entry.annotations:
count += 1
if entry != last:
print(f"entry: {entry}, last: {last}")
output += "\n\n\n"
d = entry.document
output += (
f"{d['author'] if 'author' in d else ''}"
f"{' - ' if 'author' in d else ''}" # only put separator if author
f"{entry.document['title'] if 'title' in d else ''}: "
f"{count}\n"
)
return output
@dataclass
class CsvFormatter:
header: str = "type, tag, page, quote, note, file"