refactor: Move formatting logic to formatters

Formatters (previously templates) were pure data containers before,
continating the 'template' for how things should be formatted using
mustache. The formatting would be done a) in the exporters and b) in the
annotations.

This spread of formatting has now been consolidated into the Formatter,
which fixes the overall spread of formatting code and now can coherently
format a whole output instead of just individual annotations.

A formatter contains references to all documents and contained
annotations and will format everything at once by default, but the
formatting function can be invoked with reference to a specific
annotated document to only format that.

This commit should put more separation into the concerns of exporter and
formatter and made formatting a concern purely of the formatters and
annotation objects.
This commit is contained in:
Marty Oehme 2023-09-19 21:43:19 +02:00
parent 66f937e2a8
commit 5a6d672c76
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
6 changed files with 138 additions and 101 deletions

View file

@ -8,8 +8,7 @@ import papis.strings
from papis.document import Document
from papis_extract import extractor, exporter
from papis_extract.annotation_data import AnnotatedDocument
from papis_extract.templating import Csv, Markdown, Templating
from papis_extract.formatter import MarkdownFormatter, Formatter
logger = papis.logging.get_logger(__name__)
@ -39,8 +38,7 @@ papis.config.register_default_settings(DEFAULT_OPTIONS)
@click.option(
"--manual/--no-manual",
"-m",
help=
"Open each note in editor for manual editing after extracting its annotations.",
help="Open note in editor for manual editing after annotation extraction.",
)
@click.option(
"--template",
@ -82,23 +80,19 @@ def main(
return
if template == "csv":
template_type = Csv()
else:
template_type = Markdown()
run(documents, edit=manual, write=write, git=git, template=template_type)
raise NotImplementedError
run(documents, edit=manual, write=write, git=git, template=MarkdownFormatter())
def run(
documents: list[Document],
template: Formatter,
edit: bool = False,
write: bool = False,
git: bool = False,
template: Templating = Markdown(),
) -> None:
doc_annotations: list[AnnotatedDocument] = extractor.start(documents)
template.annotated_docs = extractor.start(documents)
if write:
exporter.to_notes(doc_annotations, template, edit=edit, git=git)
exporter.to_notes(template, edit=edit, git=git)
else:
exporter.to_stdout(doc_annotations, template)
exporter.to_stdout(template)