2023-09-19 19:43:19 +00:00
|
|
|
from dataclasses import dataclass, field
|
|
|
|
from typing import Protocol
|
|
|
|
|
2023-09-20 15:22:29 +00:00
|
|
|
from papis_extract.annotation import AnnotatedDocument
|
2023-09-19 19:43:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Formatter(Protocol):
|
|
|
|
annotated_docs: list[AnnotatedDocument]
|
|
|
|
header: str
|
|
|
|
string: str
|
|
|
|
footer: str
|
|
|
|
|
|
|
|
def execute(self, doc: AnnotatedDocument | None = None) -> str:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class MarkdownFormatter:
|
|
|
|
annotated_docs: list[AnnotatedDocument] = field(default_factory=lambda: list())
|
|
|
|
header: str = ""
|
|
|
|
string: str = (
|
|
|
|
"{{#tag}}#{{tag}}\n{{/tag}}"
|
|
|
|
"{{#quote}}> {{quote}}{{/quote}} {{#page}}[p. {{page}}]{{/page}}\n"
|
|
|
|
"{{#note}} NOTE: {{note}}{{/note}}"
|
|
|
|
)
|
|
|
|
footer: str = ""
|
|
|
|
|
|
|
|
def execute(self, doc: AnnotatedDocument | None = None) -> str:
|
|
|
|
output = ""
|
|
|
|
documents = self.annotated_docs if doc is None else [doc]
|
|
|
|
last = documents[-1]
|
|
|
|
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)
|
|
|
|
|
|
|
|
if entry != last:
|
|
|
|
output += "\n\n\n"
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
2023-09-20 06:38:06 +00:00
|
|
|
|
2023-09-19 19:43:19 +00:00
|
|
|
@dataclass
|
|
|
|
class CountFormatter:
|
|
|
|
annotated_docs: list[AnnotatedDocument] = field(default_factory=lambda: list())
|
|
|
|
header: str = ""
|
|
|
|
string: str = ""
|
|
|
|
footer: str = ""
|
|
|
|
|
|
|
|
def execute(self, doc: AnnotatedDocument | None = None) -> str:
|
|
|
|
documents = self.annotated_docs if doc is None else [doc]
|
2023-09-20 06:38:06 +00:00
|
|
|
output = ""
|
2023-09-19 19:43:19 +00:00
|
|
|
for entry in documents:
|
|
|
|
if not entry.annotations:
|
|
|
|
continue
|
|
|
|
|
2023-09-20 06:38:06 +00:00
|
|
|
count = 0
|
|
|
|
for _ in entry.annotations:
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
d = entry.document
|
2023-09-19 19:43:19 +00:00
|
|
|
output += (
|
2023-09-20 06:38:06 +00:00
|
|
|
f"{d['author'] if 'author' in d else ''}"
|
2023-09-20 06:49:55 +00:00
|
|
|
f"{' - ' if 'author' in d else ''}" # only put separator if author
|
2023-09-20 06:38:06 +00:00
|
|
|
f"{entry.document['title'] if 'title' in d else ''}: "
|
|
|
|
f"{count}\n"
|
2023-09-19 19:43:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
2023-09-20 06:38:06 +00:00
|
|
|
|
2023-09-19 19:43:19 +00:00
|
|
|
@dataclass
|
|
|
|
class CsvFormatter:
|
2023-09-20 06:49:55 +00:00
|
|
|
annotated_docs: list[AnnotatedDocument] = field(default_factory=lambda: list())
|
|
|
|
header: str = "type,tag,page,quote,note,author,title,ref,file"
|
|
|
|
string: str = (
|
|
|
|
'{{type}},{{tag}},{{page}},"{{quote}}","{{note}}",'
|
|
|
|
'"{{doc.author}}","{{doc.title}}","{{doc.ref}}","{{file}}"'
|
|
|
|
)
|
2023-09-19 19:43:19 +00:00
|
|
|
footer: str = ""
|
|
|
|
|
2023-09-20 06:49:55 +00:00
|
|
|
def execute(self, doc: AnnotatedDocument | None = None) -> str:
|
|
|
|
documents = self.annotated_docs if doc is None else [doc]
|
|
|
|
output = f"{self.header}\n"
|
|
|
|
for entry in documents:
|
|
|
|
if not entry.annotations:
|
|
|
|
continue
|
|
|
|
|
|
|
|
d = entry.document
|
|
|
|
for a in entry.annotations:
|
|
|
|
output += a.format(self.string, doc=d)
|
|
|
|
output += "\n"
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
2023-09-19 19:43:19 +00:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class CustomFormatter:
|
|
|
|
def __init__(self, header: str = "", string: str = "", footer: str = "") -> None:
|
|
|
|
self.header = header
|
|
|
|
self.string = string
|
|
|
|
self.footer = footer
|