refactor: Move templating to separate file

This commit is contained in:
Marty Oehme 2023-09-19 17:35:39 +02:00
parent e633c0335e
commit 4eb983d9e3
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
7 changed files with 97 additions and 41 deletions

View file

@ -9,7 +9,7 @@ from papis.document import Document
from papis_extract import extractor, exporter
from papis_extract.annotation_data import AnnotatedDocument
from papis_extract.model.templating import Markdown
from papis_extract.templating import Markdown
logger = papis.logging.get_logger(__name__)

View file

@ -1,11 +1,12 @@
import math
from dataclasses import dataclass, field
from typing import Protocol
import papis.config
from papis.document import Document
import chevron
from papis_extract.templating import Templating
TEXT_SIMILARITY_MINIMUM = 0.75
COLOR_SIMILARITY_MINIMUM = 0.833
@ -32,7 +33,7 @@ class Annotation:
type: str = "Highlight"
minimum_similarity_color: float = 1.0
def format(self, formatting):
def format(self, template: Templating):
"""Return a formatted string of the annotation.
Given a provided formatting pattern, this method returns the annotation
@ -47,7 +48,7 @@ class Annotation:
"tag": self.tag,
"type": self.type,
}
return chevron.render(formatting, data)
return chevron.render(template.string, data)
@property
def colorname(self):

View file

@ -8,7 +8,7 @@ import papis.config
import Levenshtein
from papis_extract.annotation_data import AnnotatedDocument
from papis_extract.model.templating import Templating
from papis_extract.templating import Templating
logger = papis.logging.get_logger(__name__)
@ -36,7 +36,7 @@ def to_stdout(annots: list[AnnotatedDocument], template: Templating) -> None:
f"{title_decoration}\n{papis.document.describe(entry.document)}\n{title_decoration}\n"
)
for a in entry.annotations:
print(a.format(template.string))
print(a.format(template))
if entry != last:
print("\n")
@ -60,7 +60,7 @@ def to_notes(
formatted_annotations: list[str] = []
for a in entry.annotations:
formatted_annotations.append(a.format(template.string))
formatted_annotations.append(a.format(template))
_add_annots_to_note(entry.document, formatted_annotations)

View file

@ -1,16 +0,0 @@
from dataclasses import dataclass
from typing import Protocol
@dataclass
class Templating(Protocol):
string: str
@dataclass
class Markdown:
string: str = (
"{{#tag}}#{{tag}}\n{{/tag}}"
"{{#quote}}> {{quote}}{{/quote}} {{#page}}[p. {{page}}]{{/page}}\n"
"{{#note}} NOTE: {{note}}{{/note}}"
)

View file

@ -0,0 +1,35 @@
from dataclasses import dataclass
from typing import Protocol
@dataclass
class Templating(Protocol):
header: str
string: str
footer: str
@dataclass
class Markdown:
header: str = ""
string: str = (
"{{#tag}}#{{tag}}\n{{/tag}}"
"{{#quote}}> {{quote}}{{/quote}} {{#page}}[p. {{page}}]{{/page}}\n"
"{{#note}} NOTE: {{note}}{{/note}}"
)
footer: str = ""
@dataclass
class Csv:
header: str = "type, tag, page, quote, note, file"
string: str = "{{type}}, {{tag}}, {{page}}, {{quote}}, {{note}}, {{file}}"
footer: str = ""
@dataclass
class Custom:
def __init__(self, header: str = "", string: str = "", footer: str = "") -> None:
self.header = header
self.string = string
self.footer = footer