2023-09-21 19:54:24 +00:00
|
|
|
from collections.abc import Callable
|
2023-09-19 19:43:19 +00:00
|
|
|
|
2023-09-20 15:22:29 +00:00
|
|
|
from papis_extract.annotation import AnnotatedDocument
|
2023-09-19 19:43:19 +00:00
|
|
|
|
2023-09-21 19:54:24 +00:00
|
|
|
Formatter = Callable[[list[AnnotatedDocument]], str]
|
2023-09-19 19:43:19 +00:00
|
|
|
|
|
|
|
|
2023-09-21 19:54:24 +00:00
|
|
|
def format_markdown(docs: list[AnnotatedDocument] = []) -> str:
|
|
|
|
template = (
|
2023-09-19 19:43:19 +00:00
|
|
|
"{{#tag}}#{{tag}}\n{{/tag}}"
|
2023-09-21 19:54:24 +00:00
|
|
|
"{{#quote}}> {{quote}}{{/quote}} {{#page}}[p. {{page}}]{{/page}}"
|
|
|
|
"\n{{#note}} NOTE: {{note}}{{/note}}"
|
2023-09-19 19:43:19 +00:00
|
|
|
)
|
2023-09-21 19:54:24 +00:00
|
|
|
output = ""
|
|
|
|
for entry in docs:
|
|
|
|
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(template)
|
|
|
|
output += "\n"
|
|
|
|
|
|
|
|
output += "\n\n\n"
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
def format_count(docs: list[AnnotatedDocument] = []) -> str:
|
|
|
|
output = ""
|
|
|
|
for entry in docs:
|
|
|
|
if not entry.annotations:
|
|
|
|
continue
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
for _ in entry.annotations:
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def format_csv(docs: list[AnnotatedDocument] = []) -> str:
|
2023-09-20 06:49:55 +00:00
|
|
|
header: str = "type,tag,page,quote,note,author,title,ref,file"
|
2023-09-21 19:54:24 +00:00
|
|
|
template: str = (
|
2023-09-20 06:49:55 +00:00
|
|
|
'{{type}},{{tag}},{{page}},"{{quote}}","{{note}}",'
|
|
|
|
'"{{doc.author}}","{{doc.title}}","{{doc.ref}}","{{file}}"'
|
|
|
|
)
|
2023-09-21 19:54:24 +00:00
|
|
|
output = f"{header}\n"
|
|
|
|
for entry in docs:
|
|
|
|
if not entry.annotations:
|
|
|
|
continue
|
2023-09-20 06:49:55 +00:00
|
|
|
|
2023-09-21 19:54:24 +00:00
|
|
|
d = entry.document
|
|
|
|
for a in entry.annotations:
|
|
|
|
output += a.format(template, doc=d)
|
|
|
|
output += "\n"
|
2023-09-19 19:43:19 +00:00
|
|
|
|
2023-09-21 19:54:24 +00:00
|
|
|
return output
|