feat: Add count formatter
Added formatter which counts and outputs the number of annotations in each document.
This commit is contained in:
parent
5a6d672c76
commit
5f0bc2ffad
2 changed files with 21 additions and 20 deletions
|
@ -8,7 +8,7 @@ import papis.strings
|
|||
from papis.document import Document
|
||||
|
||||
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__)
|
||||
|
||||
|
@ -43,7 +43,7 @@ papis.config.register_default_settings(DEFAULT_OPTIONS)
|
|||
@click.option(
|
||||
"--template",
|
||||
"-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.",
|
||||
)
|
||||
def main(
|
||||
|
@ -81,7 +81,12 @@ def main(
|
|||
|
||||
if template == "csv":
|
||||
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(
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue