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

@ -8,7 +8,7 @@ import papis.strings
from papis.document import Document from papis.document import Document
from papis_extract import extractor, exporter from papis_extract import extractor, exporter
from papis_extract.formatter import MarkdownFormatter, Formatter from papis_extract.formatter import CountFormatter, MarkdownFormatter, Formatter
logger = papis.logging.get_logger(__name__) logger = papis.logging.get_logger(__name__)
@ -43,7 +43,7 @@ papis.config.register_default_settings(DEFAULT_OPTIONS)
@click.option( @click.option(
"--template", "--template",
"-t", "-t",
type=click.Choice(["markdown", "csv"], case_sensitive=False), type=click.Choice(["markdown", "count", "csv"], case_sensitive=False),
help="Choose an output template to format annotations with.", help="Choose an output template to format annotations with.",
) )
def main( def main(
@ -81,7 +81,12 @@ def main(
if template == "csv": if template == "csv":
raise NotImplementedError raise NotImplementedError
run(documents, edit=manual, write=write, git=git, template=MarkdownFormatter()) elif template == "count":
formatter = CountFormatter()
else:
formatter = MarkdownFormatter()
run(documents, edit=manual, write=write, git=git, template=formatter)
def run( def run(

View file

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